Skip to content

Commit

Permalink
feat: perform sanity check in watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
srmagura authored and skovy committed Sep 14, 2022
1 parent 71992dd commit c773162
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
17 changes: 3 additions & 14 deletions lib/core/generate.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -14,22 +13,12 @@ export const generate = async (
pattern: string,
options: ConfigOptions
): Promise<void> => {
// 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`
Expand Down
33 changes: 33 additions & 0 deletions lib/core/list-files-and-perform-sanity-checks.ts
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 5 additions & 0 deletions lib/core/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit c773162

Please sign in to comment.