@css-modules-kit/codegen 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/bin/cmk.mjs +23 -8
- package/dist/cli.d.ts +1 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +3 -0
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/logger/formatter.d.ts +1 -0
- package/dist/logger/formatter.d.ts.map +1 -1
- package/dist/logger/formatter.js +12 -0
- package/dist/logger/formatter.js.map +1 -1
- package/dist/logger/logger.d.ts +6 -3
- package/dist/logger/logger.d.ts.map +1 -1
- package/dist/logger/logger.js +19 -5
- package/dist/logger/logger.js.map +1 -1
- package/dist/project.d.ts +46 -0
- package/dist/project.d.ts.map +1 -0
- package/dist/project.js +168 -0
- package/dist/project.js.map +1 -0
- package/dist/runner.d.ts +26 -1
- package/dist/runner.d.ts.map +1 -1
- package/dist/runner.js +121 -67
- package/dist/runner.js.map +1 -1
- package/package.json +3 -10
- package/src/cli.ts +4 -0
- package/src/index.ts +1 -1
- package/src/logger/formatter.ts +12 -0
- package/src/logger/logger.ts +23 -9
- package/src/project.ts +229 -0
- package/src/runner.ts +138 -106
package/src/runner.ts
CHANGED
|
@@ -1,60 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
CSSModule,
|
|
5
|
-
Diagnostic,
|
|
6
|
-
DiagnosticWithLocation,
|
|
7
|
-
MatchesPattern,
|
|
8
|
-
ParseCSSModuleResult,
|
|
9
|
-
Resolver,
|
|
10
|
-
} from '@css-modules-kit/core';
|
|
11
|
-
import {
|
|
12
|
-
checkCSSModule,
|
|
13
|
-
createDts,
|
|
14
|
-
createExportBuilder,
|
|
15
|
-
createMatchesPattern,
|
|
16
|
-
createResolver,
|
|
17
|
-
getFileNamesByPattern,
|
|
18
|
-
parseCSSModule,
|
|
19
|
-
readConfigFile,
|
|
20
|
-
} from '@css-modules-kit/core';
|
|
21
|
-
import ts from 'typescript';
|
|
22
|
-
import { writeDtsFile } from './dts-writer.js';
|
|
23
|
-
import { ReadCSSModuleFileError } from './error.js';
|
|
1
|
+
import type { Stats } from 'node:fs';
|
|
2
|
+
import { rm } from 'node:fs/promises';
|
|
3
|
+
import chokidar, { type FSWatcher } from 'chokidar';
|
|
24
4
|
import type { Logger } from './logger/logger.js';
|
|
5
|
+
import { createProject, type Project } from './project.js';
|
|
25
6
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
async function parseCSSModuleByFileName(fileName: string): Promise<ParseCSSModuleResult> {
|
|
30
|
-
let text: string;
|
|
31
|
-
try {
|
|
32
|
-
text = await readFile(fileName, 'utf-8');
|
|
33
|
-
} catch (error) {
|
|
34
|
-
throw new ReadCSSModuleFileError(fileName, error);
|
|
35
|
-
}
|
|
36
|
-
return parseCSSModule(text, { fileName, safe: false });
|
|
7
|
+
interface RunnerArgs {
|
|
8
|
+
project: string;
|
|
9
|
+
clean: boolean;
|
|
37
10
|
}
|
|
38
11
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
cssModule: CSSModule,
|
|
44
|
-
{ dtsOutDir, basePath, arbitraryExtensions, namedExports, prioritizeNamedImports }: CMKConfig,
|
|
45
|
-
resolver: Resolver,
|
|
46
|
-
matchesPattern: MatchesPattern,
|
|
47
|
-
): Promise<void> {
|
|
48
|
-
const dts = createDts(
|
|
49
|
-
cssModule,
|
|
50
|
-
{ resolver, matchesPattern },
|
|
51
|
-
{ namedExports, prioritizeNamedImports, forTsPlugin: false },
|
|
52
|
-
);
|
|
53
|
-
await writeDtsFile(dts.text, cssModule.fileName, {
|
|
54
|
-
outDir: dtsOutDir,
|
|
55
|
-
basePath,
|
|
56
|
-
arbitraryExtensions,
|
|
57
|
-
});
|
|
12
|
+
export interface Watcher {
|
|
13
|
+
/** Exported for testing purposes */
|
|
14
|
+
project: Project;
|
|
15
|
+
close(): Promise<void>;
|
|
58
16
|
}
|
|
59
17
|
|
|
60
18
|
/**
|
|
@@ -62,70 +20,144 @@ async function writeDtsByCSSModule(
|
|
|
62
20
|
* @param project The absolute path to the project directory or the path to `tsconfig.json`.
|
|
63
21
|
* @throws {ReadCSSModuleFileError} When failed to read CSS Module file.
|
|
64
22
|
* @throws {WriteDtsFileError}
|
|
23
|
+
* @returns Whether the process succeeded without errors.
|
|
65
24
|
*/
|
|
66
|
-
export async function runCMK(
|
|
67
|
-
const
|
|
68
|
-
if (
|
|
69
|
-
|
|
70
|
-
// eslint-disable-next-line n/no-process-exit
|
|
71
|
-
process.exit(1);
|
|
25
|
+
export async function runCMK(args: RunnerArgs, logger: Logger): Promise<boolean> {
|
|
26
|
+
const project = createProject(args);
|
|
27
|
+
if (args.clean) {
|
|
28
|
+
await rm(project.config.dtsOutDir, { recursive: true, force: true });
|
|
72
29
|
}
|
|
30
|
+
await project.emitDtsFiles();
|
|
31
|
+
const diagnostics = project.getDiagnostics();
|
|
32
|
+
if (diagnostics.length > 0) {
|
|
33
|
+
logger.logDiagnostics(diagnostics);
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
73
38
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Run css-modules-kit .d.ts generation in watch mode.
|
|
41
|
+
*
|
|
42
|
+
* The promise resolves when the initial diagnostics report, emit, and watcher initialization are complete.
|
|
43
|
+
* Errors are reported through the logger.
|
|
44
|
+
*
|
|
45
|
+
* NOTE: For implementation simplicity, config file changes are not watched.
|
|
46
|
+
* @param project The absolute path to the project directory or the path to `tsconfig.json`.
|
|
47
|
+
* @throws {TsConfigFileNotFoundError}
|
|
48
|
+
* @throws {ReadCSSModuleFileError}
|
|
49
|
+
* @throws {WriteDtsFileError}
|
|
50
|
+
*/
|
|
51
|
+
export async function runCMKInWatchMode(args: RunnerArgs, logger: Logger): Promise<Watcher> {
|
|
52
|
+
const fsWatchers: FSWatcher[] = [];
|
|
53
|
+
const project = createProject(args);
|
|
54
|
+
let emitAndReportDiagnosticsTimer: NodeJS.Timeout | undefined = undefined;
|
|
86
55
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
logger.logDiagnostics([
|
|
90
|
-
{
|
|
91
|
-
category: 'warning',
|
|
92
|
-
text: `The file specified in tsconfig.json not found.`,
|
|
93
|
-
},
|
|
94
|
-
]);
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
const parseResults = await Promise.all(fileNames.map(async (fileName) => parseCSSModuleByFileName(fileName)));
|
|
98
|
-
for (const parseResult of parseResults) {
|
|
99
|
-
cssModuleMap.set(parseResult.cssModule.fileName, parseResult.cssModule);
|
|
100
|
-
syntacticDiagnostics.push(...parseResult.diagnostics);
|
|
56
|
+
if (args.clean) {
|
|
57
|
+
await rm(project.config.dtsOutDir, { recursive: true, force: true });
|
|
101
58
|
}
|
|
59
|
+
await emitAndReportDiagnostics();
|
|
60
|
+
|
|
61
|
+
// Watch project files and report diagnostics on changes
|
|
62
|
+
const readyPromises: Promise<void>[] = [];
|
|
63
|
+
for (const wildcardDirectory of project.config.wildcardDirectories) {
|
|
64
|
+
const { promise, resolve } = promiseWithResolvers<void>();
|
|
65
|
+
readyPromises.push(promise);
|
|
66
|
+
fsWatchers.push(
|
|
67
|
+
chokidar
|
|
68
|
+
.watch(wildcardDirectory.fileName, {
|
|
69
|
+
ignored: (fileName: string, stats?: Stats) => {
|
|
70
|
+
// The ignored function is called twice for the same path. The first time with stats undefined,
|
|
71
|
+
// and the second time with stats provided.
|
|
72
|
+
// In the first call, we can't determine if the path is a directory or file.
|
|
73
|
+
// So we include it in the watch target considering it might be a directory.
|
|
74
|
+
if (!stats) return false;
|
|
102
75
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
76
|
+
// In the second call, we include directories or files that match wildcards in the watch target.
|
|
77
|
+
// However, `dtsOutDir` is excluded from the watch target.
|
|
78
|
+
if (stats.isDirectory()) {
|
|
79
|
+
return fileName === project.config.dtsOutDir;
|
|
80
|
+
} else {
|
|
81
|
+
return !project.isWildcardMatchedFile(fileName);
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
ignoreInitial: true,
|
|
85
|
+
...(wildcardDirectory.recursive ? {} : { depth: 0 }),
|
|
86
|
+
})
|
|
87
|
+
.on('add', (fileName) => {
|
|
88
|
+
try {
|
|
89
|
+
project.addFile(fileName);
|
|
90
|
+
} catch (e) {
|
|
91
|
+
logger.logError(e);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
scheduleEmitAndReportDiagnostics();
|
|
95
|
+
})
|
|
96
|
+
.on('change', (fileName) => {
|
|
97
|
+
try {
|
|
98
|
+
project.updateFile(fileName);
|
|
99
|
+
} catch (e) {
|
|
100
|
+
logger.logError(e);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
scheduleEmitAndReportDiagnostics();
|
|
104
|
+
})
|
|
105
|
+
.on('unlink', (fileName: string) => {
|
|
106
|
+
project.removeFile(fileName);
|
|
107
|
+
scheduleEmitAndReportDiagnostics();
|
|
108
|
+
})
|
|
109
|
+
.on('error', (e) => logger.logError(e))
|
|
110
|
+
.on('ready', () => resolve()),
|
|
111
|
+
);
|
|
107
112
|
}
|
|
113
|
+
await Promise.all(readyPromises);
|
|
114
|
+
|
|
115
|
+
function scheduleEmitAndReportDiagnostics() {
|
|
116
|
+
// Switching between git branches results in numerous file changes occurring rapidly.
|
|
117
|
+
// Reporting diagnostics for each file change would overwhelm users.
|
|
118
|
+
// Therefore, we batch the processing.
|
|
108
119
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
120
|
+
if (emitAndReportDiagnosticsTimer !== undefined) clearTimeout(emitAndReportDiagnosticsTimer);
|
|
121
|
+
|
|
122
|
+
emitAndReportDiagnosticsTimer = setTimeout(() => {
|
|
123
|
+
emitAndReportDiagnosticsTimer = undefined;
|
|
124
|
+
emitAndReportDiagnostics().catch(logger.logError.bind(logger));
|
|
125
|
+
}, 250);
|
|
115
126
|
}
|
|
116
127
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
128
|
+
/**
|
|
129
|
+
* @throws {WriteDtsFileError}
|
|
130
|
+
*/
|
|
131
|
+
async function emitAndReportDiagnostics() {
|
|
132
|
+
logger.clearScreen();
|
|
133
|
+
await project.emitDtsFiles();
|
|
134
|
+
const diagnostics = project.getDiagnostics();
|
|
135
|
+
if (diagnostics.length > 0) {
|
|
136
|
+
logger.logDiagnostics(diagnostics);
|
|
137
|
+
}
|
|
138
|
+
logger.logMessage(
|
|
139
|
+
`Found ${diagnostics.length} error${diagnostics.length === 1 ? '' : 's'}. Watching for file changes.`,
|
|
140
|
+
{ time: true },
|
|
141
|
+
);
|
|
121
142
|
}
|
|
122
143
|
|
|
123
|
-
|
|
124
|
-
await
|
|
144
|
+
async function close() {
|
|
145
|
+
await Promise.all(fsWatchers.map(async (watcher) => watcher.close()));
|
|
125
146
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
147
|
+
|
|
148
|
+
return { project, close };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function promiseWithResolvers<T>() {
|
|
152
|
+
let resolve;
|
|
153
|
+
let reject;
|
|
154
|
+
const promise = new Promise<T>((res, rej) => {
|
|
155
|
+
resolve = res;
|
|
156
|
+
reject = rej;
|
|
157
|
+
});
|
|
158
|
+
return {
|
|
159
|
+
promise,
|
|
160
|
+
resolve: resolve as unknown as (value: T) => void,
|
|
161
|
+
reject: reject as unknown as (reason?: unknown) => void,
|
|
162
|
+
};
|
|
131
163
|
}
|