Skip to content

Commit b4e19c2

Browse files
committed
feat(glob): allow passing a glob pattern
1 parent 93fe633 commit b4e19c2

7 files changed

+85
-34
lines changed

lib/cli.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import yargs from "yargs";
22

3-
import { parse, Aliases, NAME_FORMATS, NameFormat } from "./sass";
3+
import { Aliases, NAME_FORMATS, NameFormat } from "./sass";
4+
import { main } from "./main";
45

56
const nameFormatDefault: NameFormat = "camel";
67

7-
const { _: files, includePaths, aliases, nameFormat } = yargs
8+
const { _: patterns, includePaths, aliases, nameFormat } = yargs
89
.demandOption("_")
910
.option("aliases", { coerce: (obj): Aliases => obj, alias: "a" })
1011
.option("nameFormat", {
@@ -14,4 +15,4 @@ const { _: files, includePaths, aliases, nameFormat } = yargs
1415
})
1516
.option("includePaths", { array: true, string: true }).argv;
1617

17-
parse(files[0], { includePaths, aliases, nameFormat });
18+
main(patterns[0], { includePaths, aliases, nameFormat });

lib/main.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { SassError } from "node-sass";
2+
import fs from "fs";
3+
import path from "path";
4+
import glob from "glob";
5+
import chalk from "chalk";
6+
7+
import { Options, fileToClassNames } from "./sass";
8+
import { classNamesToTypeDefinitions } from "./typescript";
9+
10+
const error = (message: string) => console.log(chalk.red(`[ERROR] ${message}`));
11+
const notice = (message: string) => console.log(chalk.gray(`${message}`));
12+
const success = (message: string) => console.log(chalk.green(message));
13+
14+
export const main = (pattern: string, options: Options): void => {
15+
const files = glob.sync(pattern);
16+
17+
if (!files || !files.length) {
18+
error("No files found.");
19+
return;
20+
}
21+
22+
if (files.length === 1) {
23+
notice(
24+
'Only 1 file found. If using a glob pattern (eg: dir/**/*.scss) make sure to wrap in quotes (eg: "dir/**/*.scss").'
25+
);
26+
}
27+
28+
success(`Found ${files.length} files. Generating type defintions...`);
29+
30+
for (let index in files) {
31+
const file = files[index];
32+
33+
fileToClassNames(file, options)
34+
.then(classNames => {
35+
const typeDefinition = classNamesToTypeDefinitions(classNames);
36+
const path = `${file}.d.ts`;
37+
38+
if (!typeDefinition) {
39+
notice(`No types generated for ${file}`);
40+
return null;
41+
}
42+
43+
fs.writeFileSync(path, typeDefinition);
44+
success(`Generated type defintions: ${path}`);
45+
})
46+
.catch(({ message, file, line, column }: SassError) => {
47+
const location = file ? `(${file}[${line}:${column}])` : "";
48+
error(`${message} ${location}`);
49+
});
50+
}
51+
};

lib/sass/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
export { Aliases, NameFormat, NAME_FORMATS } from "./file-to-class-names";
2-
export { parse } from "./parse";
1+
export {
2+
Aliases,
3+
NameFormat,
4+
NAME_FORMATS,
5+
Options,
6+
fileToClassNames
7+
} from "./file-to-class-names";

lib/sass/parse.ts

-21
This file was deleted.

lib/typescript/__tests__/class-names-to-type-definitions.test.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ describe("classNamesToTypeDefinitions", () => {
55
const definition = classNamesToTypeDefinitions(["myClass", "yourClass"]);
66

77
expect(definition).toEqual(
8-
"export const myClass: string;\nexport const yourClass: string;"
8+
"export const myClass: string;\nexport const yourClass: string;\n"
99
);
1010
});
11+
12+
it("returns null if there are no class names", () => {
13+
const definition = classNamesToTypeDefinitions([]);
14+
15+
expect(definition).toBeNull;
16+
});
1117
});
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
import { ClassNames } from "lib/sass/file-to-class-names";
1+
import { ClassNames, ClassName } from "lib/sass/file-to-class-names";
22

3-
export const classNamesToTypeDefinitions = (classNames: ClassNames): string => {
4-
return (
5-
classNames
6-
.map(className => `export const ${className}: string;`)
7-
.join("\n") + "\n"
8-
);
3+
const classNameToTypeDefinition = (className: ClassName) =>
4+
`export const ${className}: string;`;
5+
6+
export const classNamesToTypeDefinitions = (
7+
classNames: ClassNames
8+
): string | null => {
9+
if (classNames.length) {
10+
const typeDefinitions = classNames.map(classNameToTypeDefinition);
11+
12+
// Sepearte all type definitions be a newline with a trailing newline.
13+
return typeDefinitions.join("\n") + "\n";
14+
} else {
15+
return null;
16+
}
917
};

lib/typescript/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { classNamesToTypeDefinitions } from "./class-names-to-type-definition"

0 commit comments

Comments
 (0)