diff --git a/lib/core/generate.ts b/lib/core/generate.ts index 33f5e4f..b02f7f1 100644 --- a/lib/core/generate.ts +++ b/lib/core/generate.ts @@ -1,6 +1,5 @@ -import glob from "glob"; - import { alerts } from "./alerts"; +import { listFilesAndPerformSanityChecks } from "./list-files-and-perform-sanity-checks"; import { ConfigOptions } from "./types"; import { writeFile } from "./write-file"; @@ -14,22 +13,12 @@ export const generate = async ( pattern: string, options: ConfigOptions ): Promise => { - // Find all the files that match the provided pattern. - const files = glob.sync(pattern, { ignore: options.ignore }); + const files = listFilesAndPerformSanityChecks(pattern, options); - if (!files || !files.length) { - alerts.error("No files found."); + if (files.length === 0) { return; } - // This case still works as expected but it's easy to do on accident so - // provide a (hopefully) helpful warning. - if (files.length === 1) { - alerts.warn( - `Only 1 file found for ${pattern}. If using a glob pattern (eg: dir/**/*.scss) make sure to wrap in quotes (eg: "dir/**/*.scss").` - ); - } - alerts.success( `Found ${files.length} file${ files.length === 1 ? `` : `s` diff --git a/lib/core/list-files-and-perform-sanity-checks.ts b/lib/core/list-files-and-perform-sanity-checks.ts new file mode 100644 index 0000000..24cfab0 --- /dev/null +++ b/lib/core/list-files-and-perform-sanity-checks.ts @@ -0,0 +1,33 @@ +import glob from "glob"; + +import { alerts } from "./alerts"; +import { ConfigOptions } from "./types"; + +/** + * Return the files matching the given pattern and alert the user if only 0 or 1 + * files matched. + * + * @param pattern the file pattern to generate type definitions for + * @param options the CLI options + */ +export function listFilesAndPerformSanityChecks( + pattern: string, + options: ConfigOptions +): string[] { + // Find all the files that match the provided pattern. + const files = glob.sync(pattern, { ignore: options.ignore }); + + if (!files || !files.length) { + alerts.error("No files found."); + } + + // This case still works as expected but it's easy to do on accident so + // provide a (hopefully) helpful warning. + if (files.length === 1) { + alerts.warn( + `Only 1 file found for ${pattern}. If using a glob pattern (eg: dir/**/*.scss) make sure to wrap in quotes (eg: "dir/**/*.scss").` + ); + } + + return files; +} diff --git a/lib/core/watch.ts b/lib/core/watch.ts index 106f6d4..eca5d4b 100644 --- a/lib/core/watch.ts +++ b/lib/core/watch.ts @@ -4,6 +4,7 @@ import { alerts } from "./alerts"; import { removeSCSSTypeDefinitionFile } from "./remove-file"; import { writeFile } from "./write-file"; import { ConfigOptions } from "./types"; +import { listFilesAndPerformSanityChecks } from "./list-files-and-perform-sanity-checks"; /** * Watch a file glob and generate the corresponding types. @@ -12,6 +13,10 @@ import { ConfigOptions } from "./types"; * @param options the CLI options */ export const watch = (pattern: string, options: ConfigOptions): void => { + // This is called so that we print a warning instead if no files matched the + // pattern + listFilesAndPerformSanityChecks(pattern, options); + alerts.success("Watching files..."); chokidar