Skip to content

Commit 165dcfa

Browse files
committed
Watch only for extension / watch mode
1 parent a5a2e10 commit 165dcfa

File tree

7 files changed

+52
-9
lines changed

7 files changed

+52
-9
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ The plugin supports the following configuration options:
5858

5959
// Glob patterns for files to ignore (default: ['**/node_modules/**', '**/dist/**', '**/out/**', '**/build/**'])
6060
ignore: ['**/node_modules/**', '**/dist/**', '**/out/**', '**/build/**']
61+
62+
// Whether to enable the file watcher for changes to CSS files. This is
63+
// useful when used as part of an editor plugin but can cause CI jobs
64+
// to hang. By default, the watcher is enabled only if the running script
65+
// has `serve` or `watch` in its name (the VSCode ESLint extension is
66+
// named `eslintServer`). This behavior can be overridden by running
67+
// your own check and setting this option to true or false accordingly.
68+
watch: 'auto'
6169
}]
6270
}
6371
```

dist/rules/no-unknown-class.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export interface PluginOptions {
88
cssFiles?: string[];
99
/** Patterns to ignore when searching for CSS files */
1010
ignore?: string[];
11+
/**
12+
* Enable watcher? Defaults to "auto", which only runs if the script namae has "watch"
13+
* or "serve" in it (VSCode's extension is named eslintServer).
14+
*/
15+
watch?: boolean | 'auto';
1116
}
1217
declare const rule: RuleModule<'unknownClass', [PluginOptions]>;
1318
export default rule;

dist/rules/no-unknown-class.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const DEFAULT_OPTIONS = {
77
classFunctions: ['clsx', 'classNames', 'cx'],
88
cssFiles: ['**/*.css'],
99
ignore: ['**/node_modules/**', '**/dist/**', '**/out/**', '**/build/**'],
10+
watch: 'auto',
1011
};
1112
const rule = {
1213
// Not sure why we need to do this since it doesn't show up on context.options anyways,
@@ -52,7 +53,15 @@ const rule = {
5253
};
5354
// Initialize watcher if not already done
5455
if (!cssWatcher) {
55-
cssWatcher = new file_watcher_1.CssWatcher(options.cssFiles, options.ignore);
56+
const shouldWatch = (() => {
57+
var _a, _b;
58+
if (options.watch === 'auto') {
59+
const script = (_b = (_a = process.argv[1]) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : '';
60+
return script.includes('watch') || script.includes('serve');
61+
}
62+
return options.watch;
63+
})();
64+
cssWatcher = new file_watcher_1.CssWatcher(options.cssFiles, options.ignore, shouldWatch);
5665
}
5766
/** Helper to check if a class exists in our CSS files */
5867
const validate = (className, node) => {

dist/utils/file-watcher.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ export declare class CssWatcher {
33
private watcher;
44
private patterns;
55
private ignorePatterns;
6-
constructor(patterns?: string[], ignore?: string[]);
7-
private setupWatcher;
6+
private enableWatch;
7+
constructor(patterns?: string[], ignore?: string[], enableWatch?: boolean);
8+
private maybeSetupWatcher;
89
private updateClassesForFile;
910
private initialScan;
1011
hasClass(className: string): boolean;

dist/utils/file-watcher.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const chokidar_1 = __importDefault(require("chokidar"));
99
const css_extractor_1 = require("./css-extractor");
1010
const micromatch_1 = __importDefault(require("micromatch"));
1111
class CssWatcher {
12-
constructor(patterns = ['**/*.css'], ignore = ['**/node_modules/**', '**/dist/**', '**/out/**', '**/build/**']) {
12+
constructor(patterns = ['**/*.css'], ignore = ['**/node_modules/**', '**/dist/**', '**/out/**', '**/build/**'], enableWatch = true) {
1313
this.state = {
1414
fileClasses: new Map(),
1515
lastUpdate: 0,
@@ -18,11 +18,14 @@ class CssWatcher {
1818
this.patterns = patterns;
1919
// Ignore hidden files by default
2020
this.ignorePatterns = ['**/.*', ...ignore];
21+
this.enableWatch = enableWatch;
2122
const cwd = process.cwd();
22-
this.setupWatcher(cwd);
23+
this.maybeSetupWatcher(cwd);
2324
this.initialScan(cwd);
2425
}
25-
setupWatcher(cwd) {
26+
maybeSetupWatcher(cwd) {
27+
if (!this.enableWatch)
28+
return;
2629
this.watcher = chokidar_1.default.watch(cwd, {
2730
persistent: true,
2831
ignoreInitial: true, // We need to do our initial scan synchronously

src/rules/no-unknown-class.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export interface PluginOptions {
1111
cssFiles?: string[];
1212
/** Patterns to ignore when searching for CSS files */
1313
ignore?: string[];
14+
/**
15+
* Enable watcher? Defaults to "auto", which only runs if the script namae has "watch"
16+
* or "serve" in it (VSCode's extension is named eslintServer).
17+
*/
18+
watch?: boolean | 'auto';
1419
}
1520

1621
let cssWatcher: CssWatcher | null = null;
@@ -20,6 +25,7 @@ const DEFAULT_OPTIONS: PluginOptions = {
2025
classFunctions: ['clsx', 'classNames', 'cx'],
2126
cssFiles: ['**/*.css'],
2227
ignore: ['**/node_modules/**', '**/dist/**', '**/out/**', '**/build/**'],
28+
watch: 'auto',
2329
};
2430

2531
const rule: RuleModule<'unknownClass', [PluginOptions]> = {
@@ -68,7 +74,14 @@ const rule: RuleModule<'unknownClass', [PluginOptions]> = {
6874

6975
// Initialize watcher if not already done
7076
if (!cssWatcher) {
71-
cssWatcher = new CssWatcher(options.cssFiles, options.ignore);
77+
const shouldWatch = (() => {
78+
if (options.watch === 'auto') {
79+
const script = process.argv[1]?.toLowerCase() ?? '';
80+
return script.includes('watch') || script.includes('serve');
81+
}
82+
return options.watch;
83+
})();
84+
cssWatcher = new CssWatcher(options.cssFiles, options.ignore, shouldWatch);
7285
}
7386

7487
/** Helper to check if a class exists in our CSS files */

src/utils/file-watcher.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,25 @@ export class CssWatcher {
1212
private watcher: FSWatcher | null = null;
1313
private patterns: string[];
1414
private ignorePatterns: string[];
15+
private enableWatch: boolean;
1516

1617
constructor(
1718
patterns: string[] = ['**/*.css'],
1819
ignore: string[] = ['**/node_modules/**', '**/dist/**', '**/out/**', '**/build/**'],
20+
enableWatch = true,
1921
) {
2022
this.patterns = patterns;
2123
// Ignore hidden files by default
2224
this.ignorePatterns = ['**/.*', ...ignore];
25+
this.enableWatch = enableWatch;
2326

2427
const cwd = process.cwd();
25-
this.setupWatcher(cwd);
28+
this.maybeSetupWatcher(cwd);
2629
this.initialScan(cwd);
2730
}
2831

29-
private setupWatcher(cwd: string) {
32+
private maybeSetupWatcher(cwd: string) {
33+
if (!this.enableWatch) return;
3034
this.watcher = chokidar.watch(cwd, {
3135
persistent: true,
3236
ignoreInitial: true, // We need to do our initial scan synchronously

0 commit comments

Comments
 (0)