-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrunner.ts
More file actions
169 lines (155 loc) · 5.51 KB
/
Copy pathrunner.ts
File metadata and controls
169 lines (155 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import type { Stats } from 'node:fs';
import { rm } from 'node:fs/promises';
import chokidar, { type FSWatcher } from 'chokidar';
import type { Logger } from './logger/logger.js';
import { createProject, type Project } from './project.js';
interface RunnerArgs {
project: string;
clean: boolean;
preserveWatchOutput: boolean;
}
export interface Watcher {
/** Exported for testing purposes */
project: Project;
close(): Promise<void>;
}
/**
* Run css-modules-kit .d.ts generation.
* @param project The absolute path to the project directory or the path to `tsconfig.json`.
* @throws {ReadCSSModuleFileError} When failed to read CSS Module file.
* @throws {WriteDtsFileError}
* @returns Whether the process succeeded without errors.
*/
export async function runCMK(args: RunnerArgs, logger: Logger): Promise<boolean> {
const project = createProject(args);
if (args.clean) {
await rm(project.config.dtsOutDir, { recursive: true, force: true });
}
await project.emitDtsFiles();
const diagnostics = project.getDiagnostics();
if (diagnostics.length > 0) {
logger.logDiagnostics(diagnostics);
return false;
}
return true;
}
/**
* Run css-modules-kit .d.ts generation in watch mode.
*
* The promise resolves when the initial diagnostics report, emit, and watcher initialization are complete.
* Errors are reported through the logger.
*
* NOTE: For implementation simplicity, config file changes are not watched.
* @param project The absolute path to the project directory or the path to `tsconfig.json`.
* @throws {TsConfigFileNotFoundError}
* @throws {ReadCSSModuleFileError}
* @throws {WriteDtsFileError}
*/
export async function runCMKInWatchMode(args: RunnerArgs, logger: Logger): Promise<Watcher> {
const fsWatchers: FSWatcher[] = [];
const project = createProject(args);
let emitAndReportDiagnosticsTimer: NodeJS.Timeout | undefined = undefined;
if (args.clean) {
await rm(project.config.dtsOutDir, { recursive: true, force: true });
}
await emitAndReportDiagnostics();
// Watch project files and report diagnostics on changes
const readyPromises: Promise<void>[] = [];
for (const wildcardDirectory of project.config.wildcardDirectories) {
const { promise, resolve } = promiseWithResolvers<void>();
readyPromises.push(promise);
fsWatchers.push(
chokidar
.watch(wildcardDirectory.fileName, {
ignored: (fileName: string, stats?: Stats) => {
// The ignored function is called twice for the same path. The first time with stats undefined,
// and the second time with stats provided.
// In the first call, we can't determine if the path is a directory or file.
// So we include it in the watch target considering it might be a directory.
if (!stats) return false;
// In the second call, we include directories or files that match wildcards in the watch target.
// However, `dtsOutDir` is excluded from the watch target.
if (stats.isDirectory()) {
return fileName === project.config.dtsOutDir;
} else {
return !project.isWildcardMatchedFile(fileName);
}
},
ignoreInitial: true,
...(wildcardDirectory.recursive ? {} : { depth: 0 }),
})
.on('add', (fileName) => {
try {
project.addFile(fileName);
} catch (e) {
logger.logError(e);
return;
}
scheduleEmitAndReportDiagnostics();
})
.on('change', (fileName) => {
try {
project.updateFile(fileName);
} catch (e) {
logger.logError(e);
return;
}
scheduleEmitAndReportDiagnostics();
})
.on('unlink', (fileName: string) => {
project.removeFile(fileName);
scheduleEmitAndReportDiagnostics();
})
.on('error', (e) => logger.logError(e))
.on('ready', () => resolve()),
);
}
await Promise.all(readyPromises);
function scheduleEmitAndReportDiagnostics() {
// Switching between git branches results in numerous file changes occurring rapidly.
// Reporting diagnostics for each file change would overwhelm users.
// Therefore, we batch the processing.
if (emitAndReportDiagnosticsTimer !== undefined) clearTimeout(emitAndReportDiagnosticsTimer);
emitAndReportDiagnosticsTimer = setTimeout(() => {
emitAndReportDiagnosticsTimer = undefined;
emitAndReportDiagnostics().catch(logger.logError.bind(logger));
}, 250);
}
/**
* @throws {WriteDtsFileError}
*/
async function emitAndReportDiagnostics() {
if (!args.preserveWatchOutput) {
logger.clearScreen();
}
await project.emitDtsFiles();
const diagnostics = project.getDiagnostics();
if (diagnostics.length > 0) {
logger.logDiagnostics(diagnostics);
}
logger.logMessage(
`Found ${diagnostics.length} error${diagnostics.length === 1 ? '' : 's'}. Watching for file changes.`,
{ time: true },
);
if (args.preserveWatchOutput) {
logger.logMessage('');
}
}
async function close() {
await Promise.all(fsWatchers.map(async (watcher) => watcher.close()));
}
return { project, close };
}
function promiseWithResolvers<T>() {
let resolve;
let reject;
const promise = new Promise<T>((res, rej) => {
resolve = res;
reject = rej;
});
return {
promise,
resolve: resolve as unknown as (value: T) => void,
reject: reject as unknown as (reason?: unknown) => void,
};
}