@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/dist/runner.js
CHANGED
|
@@ -4,89 +4,143 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.runCMK = runCMK;
|
|
7
|
+
exports.runCMKInWatchMode = runCMKInWatchMode;
|
|
7
8
|
const promises_1 = require("node:fs/promises");
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const dts_writer_js_1 = require("./dts-writer.js");
|
|
11
|
-
const error_js_1 = require("./error.js");
|
|
9
|
+
const chokidar_1 = __importDefault(require("chokidar"));
|
|
10
|
+
const project_js_1 = require("./project.js");
|
|
12
11
|
/**
|
|
12
|
+
* Run css-modules-kit .d.ts generation.
|
|
13
|
+
* @param project The absolute path to the project directory or the path to `tsconfig.json`.
|
|
13
14
|
* @throws {ReadCSSModuleFileError} When failed to read CSS Module file.
|
|
15
|
+
* @throws {WriteDtsFileError}
|
|
16
|
+
* @returns Whether the process succeeded without errors.
|
|
14
17
|
*/
|
|
15
|
-
async function
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
async function runCMK(args, logger) {
|
|
19
|
+
const project = (0, project_js_1.createProject)(args);
|
|
20
|
+
if (args.clean) {
|
|
21
|
+
await (0, promises_1.rm)(project.config.dtsOutDir, { recursive: true, force: true });
|
|
19
22
|
}
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
await project.emitDtsFiles();
|
|
24
|
+
const diagnostics = project.getDiagnostics();
|
|
25
|
+
if (diagnostics.length > 0) {
|
|
26
|
+
logger.logDiagnostics(diagnostics);
|
|
27
|
+
return false;
|
|
22
28
|
}
|
|
23
|
-
return
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* @throws {WriteDtsFileError}
|
|
27
|
-
*/
|
|
28
|
-
async function writeDtsByCSSModule(cssModule, { dtsOutDir, basePath, arbitraryExtensions, namedExports, prioritizeNamedImports }, resolver, matchesPattern) {
|
|
29
|
-
const dts = (0, core_1.createDts)(cssModule, { resolver, matchesPattern }, { namedExports, prioritizeNamedImports, forTsPlugin: false });
|
|
30
|
-
await (0, dts_writer_js_1.writeDtsFile)(dts.text, cssModule.fileName, {
|
|
31
|
-
outDir: dtsOutDir,
|
|
32
|
-
basePath,
|
|
33
|
-
arbitraryExtensions,
|
|
34
|
-
});
|
|
29
|
+
return true;
|
|
35
30
|
}
|
|
36
31
|
/**
|
|
37
|
-
* Run css-modules-kit .d.ts generation.
|
|
32
|
+
* Run css-modules-kit .d.ts generation in watch mode.
|
|
33
|
+
*
|
|
34
|
+
* The promise resolves when the initial diagnostics report, emit, and watcher initialization are complete.
|
|
35
|
+
* Errors are reported through the logger.
|
|
36
|
+
*
|
|
37
|
+
* NOTE: For implementation simplicity, config file changes are not watched.
|
|
38
38
|
* @param project The absolute path to the project directory or the path to `tsconfig.json`.
|
|
39
|
-
* @throws {
|
|
39
|
+
* @throws {TsConfigFileNotFoundError}
|
|
40
|
+
* @throws {ReadCSSModuleFileError}
|
|
40
41
|
* @throws {WriteDtsFileError}
|
|
41
42
|
*/
|
|
42
|
-
async function
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
async function runCMKInWatchMode(args, logger) {
|
|
44
|
+
const fsWatchers = [];
|
|
45
|
+
const project = (0, project_js_1.createProject)(args);
|
|
46
|
+
let emitAndReportDiagnosticsTimer = undefined;
|
|
47
|
+
if (args.clean) {
|
|
48
|
+
await (0, promises_1.rm)(project.config.dtsOutDir, { recursive: true, force: true });
|
|
48
49
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
50
|
+
await emitAndReportDiagnostics();
|
|
51
|
+
// Watch project files and report diagnostics on changes
|
|
52
|
+
const readyPromises = [];
|
|
53
|
+
for (const wildcardDirectory of project.config.wildcardDirectories) {
|
|
54
|
+
const { promise, resolve } = promiseWithResolvers();
|
|
55
|
+
readyPromises.push(promise);
|
|
56
|
+
fsWatchers.push(chokidar_1.default
|
|
57
|
+
.watch(wildcardDirectory.fileName, {
|
|
58
|
+
ignored: (fileName, stats) => {
|
|
59
|
+
// The ignored function is called twice for the same path. The first time with stats undefined,
|
|
60
|
+
// and the second time with stats provided.
|
|
61
|
+
// In the first call, we can't determine if the path is a directory or file.
|
|
62
|
+
// So we include it in the watch target considering it might be a directory.
|
|
63
|
+
if (!stats)
|
|
64
|
+
return false;
|
|
65
|
+
// In the second call, we include directories or files that match wildcards in the watch target.
|
|
66
|
+
// However, `dtsOutDir` is excluded from the watch target.
|
|
67
|
+
if (stats.isDirectory()) {
|
|
68
|
+
return fileName === project.config.dtsOutDir;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
return !project.isWildcardMatchedFile(fileName);
|
|
72
|
+
}
|
|
61
73
|
},
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
74
|
+
ignoreInitial: true,
|
|
75
|
+
...(wildcardDirectory.recursive ? {} : { depth: 0 }),
|
|
76
|
+
})
|
|
77
|
+
.on('add', (fileName) => {
|
|
78
|
+
try {
|
|
79
|
+
project.addFile(fileName);
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
logger.logError(e);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
scheduleEmitAndReportDiagnostics();
|
|
86
|
+
})
|
|
87
|
+
.on('change', (fileName) => {
|
|
88
|
+
try {
|
|
89
|
+
project.updateFile(fileName);
|
|
90
|
+
}
|
|
91
|
+
catch (e) {
|
|
92
|
+
logger.logError(e);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
scheduleEmitAndReportDiagnostics();
|
|
96
|
+
})
|
|
97
|
+
.on('unlink', (fileName) => {
|
|
98
|
+
project.removeFile(fileName);
|
|
99
|
+
scheduleEmitAndReportDiagnostics();
|
|
100
|
+
})
|
|
101
|
+
.on('error', (e) => logger.logError(e))
|
|
102
|
+
.on('ready', () => resolve()));
|
|
69
103
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
//
|
|
73
|
-
|
|
104
|
+
await Promise.all(readyPromises);
|
|
105
|
+
function scheduleEmitAndReportDiagnostics() {
|
|
106
|
+
// Switching between git branches results in numerous file changes occurring rapidly.
|
|
107
|
+
// Reporting diagnostics for each file change would overwhelm users.
|
|
108
|
+
// Therefore, we batch the processing.
|
|
109
|
+
if (emitAndReportDiagnosticsTimer !== undefined)
|
|
110
|
+
clearTimeout(emitAndReportDiagnosticsTimer);
|
|
111
|
+
emitAndReportDiagnosticsTimer = setTimeout(() => {
|
|
112
|
+
emitAndReportDiagnosticsTimer = undefined;
|
|
113
|
+
emitAndReportDiagnostics().catch(logger.logError.bind(logger));
|
|
114
|
+
}, 250);
|
|
74
115
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
116
|
+
/**
|
|
117
|
+
* @throws {WriteDtsFileError}
|
|
118
|
+
*/
|
|
119
|
+
async function emitAndReportDiagnostics() {
|
|
120
|
+
logger.clearScreen();
|
|
121
|
+
await project.emitDtsFiles();
|
|
122
|
+
const diagnostics = project.getDiagnostics();
|
|
123
|
+
if (diagnostics.length > 0) {
|
|
124
|
+
logger.logDiagnostics(diagnostics);
|
|
125
|
+
}
|
|
126
|
+
logger.logMessage(`Found ${diagnostics.length} error${diagnostics.length === 1 ? '' : 's'}. Watching for file changes.`, { time: true });
|
|
81
127
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
// eslint-disable-next-line n/no-process-exit
|
|
85
|
-
process.exit(1);
|
|
128
|
+
async function close() {
|
|
129
|
+
await Promise.all(fsWatchers.map(async (watcher) => watcher.close()));
|
|
86
130
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
131
|
+
return { project, close };
|
|
132
|
+
}
|
|
133
|
+
function promiseWithResolvers() {
|
|
134
|
+
let resolve;
|
|
135
|
+
let reject;
|
|
136
|
+
const promise = new Promise((res, rej) => {
|
|
137
|
+
resolve = res;
|
|
138
|
+
reject = rej;
|
|
139
|
+
});
|
|
140
|
+
return {
|
|
141
|
+
promise,
|
|
142
|
+
resolve: resolve,
|
|
143
|
+
reject: reject,
|
|
144
|
+
};
|
|
91
145
|
}
|
|
92
146
|
//# sourceMappingURL=runner.js.map
|
package/dist/runner.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runner.js","sourceRoot":"","sources":["../src/runner.ts"],"names":[],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"runner.js","sourceRoot":"","sources":["../src/runner.ts"],"names":[],"mappings":";;;;;AAwBA,wBAYC;AAcD,8CAkGC;AAnJD,+CAAsC;AACtC,wDAAoD;AAEpD,6CAA2D;AAa3D;;;;;;GAMG;AACI,KAAK,UAAU,MAAM,CAAC,IAAgB,EAAE,MAAc;IAC3D,MAAM,OAAO,GAAG,IAAA,0BAAa,EAAC,IAAI,CAAC,CAAC;IACpC,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,MAAM,IAAA,aAAE,EAAC,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IACvE,CAAC;IACD,MAAM,OAAO,CAAC,YAAY,EAAE,CAAC;IAC7B,MAAM,WAAW,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;IAC7C,IAAI,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC3B,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC;QACnC,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;;;;;;GAWG;AACI,KAAK,UAAU,iBAAiB,CAAC,IAAgB,EAAE,MAAc;IACtE,MAAM,UAAU,GAAgB,EAAE,CAAC;IACnC,MAAM,OAAO,GAAG,IAAA,0BAAa,EAAC,IAAI,CAAC,CAAC;IACpC,IAAI,6BAA6B,GAA+B,SAAS,CAAC;IAE1E,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,MAAM,IAAA,aAAE,EAAC,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IACvE,CAAC;I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,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC3B,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC;QACrC,CAAC;QACD,MAAM,CAAC,UAAU,CACf,SAAS,WAAW,CAAC,MAAM,SAAS,WAAW,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,8BAA8B,EACrG,EAAE,IAAI,EAAE,IAAI,EAAE,CACf,CAAC;IACJ,CAAC;IAED,KAAK,UAAU,KAAK;QAClB,MAAM,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IACxE,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AAC5B,CAAC;AAED,SAAS,oBAAoB;IAC3B,IAAI,OAAO,CAAC;IACZ,IAAI,MAAM,CAAC;IACX,MAAM,OAAO,GAAG,IAAI,OAAO,CAAI,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAC1C,OAAO,GAAG,GAAG,CAAC;QACd,MAAM,GAAG,GAAG,CAAC;IACf,CAAC,CAAC,CAAC;IACH,OAAO;QACL,OAAO;QACP,OAAO,EAAE,OAAwC;QACjD,MAAM,EAAE,MAA+C;KACxD,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@css-modules-kit/codegen",
|
|
3
3
|
"description": "A tool for generating `*.d.ts` files for `*.module.css`.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"repository": {
|
|
@@ -12,14 +12,6 @@
|
|
|
12
12
|
"author": "mizdra <pp.mizdra@gmail.com>",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"private": false,
|
|
15
|
-
"exports": {
|
|
16
|
-
".": {
|
|
17
|
-
"default": {
|
|
18
|
-
"types": "./dist/index.d.ts",
|
|
19
|
-
"default": "./dist/index.js"
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
15
|
"scripts": {
|
|
24
16
|
"build": "tsc -b tsconfig.build.json"
|
|
25
17
|
},
|
|
@@ -46,7 +38,8 @@
|
|
|
46
38
|
"dist"
|
|
47
39
|
],
|
|
48
40
|
"dependencies": {
|
|
49
|
-
"@css-modules-kit/core": "^0.
|
|
41
|
+
"@css-modules-kit/core": "^0.7.0",
|
|
42
|
+
"chokidar": "^4.0.3"
|
|
50
43
|
},
|
|
51
44
|
"peerDependencies": {
|
|
52
45
|
"typescript": "^5.7.3"
|
package/src/cli.ts
CHANGED
|
@@ -12,6 +12,7 @@ Options:
|
|
|
12
12
|
--project, -p The path to its configuration file, or to a folder with a 'tsconfig.json'.
|
|
13
13
|
--pretty Enable color and formatting in output to make errors easier to read.
|
|
14
14
|
--clean Remove the output directory before generating files. [default: false]
|
|
15
|
+
--watch, -w Watch for changes and regenerate files. [default: false]
|
|
15
16
|
`;
|
|
16
17
|
|
|
17
18
|
export function printHelpText(): void {
|
|
@@ -30,6 +31,7 @@ export interface ParsedArgs {
|
|
|
30
31
|
project: string;
|
|
31
32
|
pretty: boolean | undefined;
|
|
32
33
|
clean: boolean;
|
|
34
|
+
watch: boolean;
|
|
33
35
|
}
|
|
34
36
|
|
|
35
37
|
/**
|
|
@@ -46,6 +48,7 @@ export function parseCLIArgs(args: string[], cwd: string): ParsedArgs {
|
|
|
46
48
|
project: { type: 'string', short: 'p', default: '.' },
|
|
47
49
|
pretty: { type: 'boolean' },
|
|
48
50
|
clean: { type: 'boolean', default: false },
|
|
51
|
+
watch: { type: 'boolean', short: 'w', default: false },
|
|
49
52
|
},
|
|
50
53
|
allowNegative: true,
|
|
51
54
|
});
|
|
@@ -55,6 +58,7 @@ export function parseCLIArgs(args: string[], cwd: string): ParsedArgs {
|
|
|
55
58
|
project: resolve(cwd, values.project),
|
|
56
59
|
pretty: values.pretty,
|
|
57
60
|
clean: values.clean,
|
|
61
|
+
watch: values.watch,
|
|
58
62
|
};
|
|
59
63
|
} catch (cause) {
|
|
60
64
|
throw new ParseCLIArgsError(cause);
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { runCMK } from './runner.js';
|
|
1
|
+
export { runCMK, runCMKInWatchMode } from './runner.js';
|
|
2
2
|
export { type Logger, createLogger } from './logger/logger.js';
|
|
3
3
|
export { WriteDtsFileError, ReadCSSModuleFileError } from './error.js';
|
|
4
4
|
export { parseCLIArgs, printHelpText, printVersion } from './cli.js';
|
package/src/logger/formatter.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
|
|
3
|
+
const GRAY = '\u001b[90m';
|
|
4
|
+
const RESET = '\u001b[0m';
|
|
5
|
+
|
|
3
6
|
export function formatDiagnostics(
|
|
4
7
|
diagnostics: ts.Diagnostic[],
|
|
5
8
|
host: ts.FormatDiagnosticsHost,
|
|
@@ -12,3 +15,12 @@ export function formatDiagnostics(
|
|
|
12
15
|
}
|
|
13
16
|
return result;
|
|
14
17
|
}
|
|
18
|
+
|
|
19
|
+
export function formatTime(date: Date, pretty: boolean): string {
|
|
20
|
+
const text = date.toLocaleTimeString('en-US', { timeZone: 'UTC' });
|
|
21
|
+
if (pretty) {
|
|
22
|
+
return `[${GRAY}${text}${RESET}]`;
|
|
23
|
+
} else {
|
|
24
|
+
return `[${text}]`;
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/logger/logger.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
import { inspect } from 'node:util';
|
|
1
2
|
import type { DiagnosticSourceFile } from '@css-modules-kit/core';
|
|
2
|
-
import { convertDiagnostic, convertSystemError, type Diagnostic,
|
|
3
|
+
import { convertDiagnostic, convertSystemError, type Diagnostic, SystemError } from '@css-modules-kit/core';
|
|
3
4
|
import ts from 'typescript';
|
|
4
|
-
import { formatDiagnostics } from './formatter.js';
|
|
5
|
+
import { formatDiagnostics, formatTime } from './formatter.js';
|
|
5
6
|
|
|
6
7
|
export interface Logger {
|
|
7
8
|
logDiagnostics(diagnostics: Diagnostic[]): void;
|
|
8
|
-
|
|
9
|
-
logMessage(message: string): void;
|
|
9
|
+
logError(error: unknown): void;
|
|
10
|
+
logMessage(message: string, options?: { time?: boolean }): void;
|
|
11
|
+
clearScreen(): void;
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
export function createLogger(cwd: string, pretty: boolean): Logger {
|
|
@@ -29,12 +31,24 @@ export function createLogger(cwd: string, pretty: boolean): Logger {
|
|
|
29
31
|
);
|
|
30
32
|
process.stderr.write(result);
|
|
31
33
|
},
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
logError(error: unknown): void {
|
|
35
|
+
// NOTE: SystemErrors are errors expected by the css-modules-kit specification and may occur within normal usage.
|
|
36
|
+
// These errors are formatted clearly and concisely. No stack trace is output.
|
|
37
|
+
//
|
|
38
|
+
// All other errors are unexpected errors. To assist in debugging when these errors occur, a stack trace is output.
|
|
39
|
+
if (error instanceof SystemError) {
|
|
40
|
+
const result = formatDiagnostics([convertSystemError(error)], host, pretty);
|
|
41
|
+
process.stderr.write(result);
|
|
42
|
+
} else {
|
|
43
|
+
process.stderr.write(`${inspect(error, { colors: pretty })}\n`);
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
logMessage(message: string, options?: { time?: boolean }): void {
|
|
47
|
+
const header = options?.time ? `${formatTime(new Date(), pretty)} ` : '';
|
|
48
|
+
process.stdout.write(`${header}${message}\n`);
|
|
35
49
|
},
|
|
36
|
-
|
|
37
|
-
process.stdout.write(
|
|
50
|
+
clearScreen(): void {
|
|
51
|
+
process.stdout.write('\x1B[2J\x1B[3J\x1B[H');
|
|
38
52
|
},
|
|
39
53
|
};
|
|
40
54
|
}
|
package/src/project.ts
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import type { CSSModule, Diagnostic } from '@css-modules-kit/core';
|
|
3
|
+
import {
|
|
4
|
+
checkCSSModule,
|
|
5
|
+
type CMKConfig,
|
|
6
|
+
createExportBuilder,
|
|
7
|
+
createMatchesPattern,
|
|
8
|
+
createResolver,
|
|
9
|
+
generateDts,
|
|
10
|
+
getFileNamesByPattern,
|
|
11
|
+
parseCSSModule,
|
|
12
|
+
readConfigFile,
|
|
13
|
+
} from '@css-modules-kit/core';
|
|
14
|
+
import ts from 'typescript';
|
|
15
|
+
import { writeDtsFile } from './dts-writer.js';
|
|
16
|
+
import { ReadCSSModuleFileError } from './error.js';
|
|
17
|
+
|
|
18
|
+
interface ProjectArgs {
|
|
19
|
+
project: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface Project {
|
|
23
|
+
config: CMKConfig;
|
|
24
|
+
/** Whether the file matches the wildcard patterns in `include` / `exclude` options */
|
|
25
|
+
isWildcardMatchedFile(fileName: string): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Add a file to the project.
|
|
28
|
+
* @throws {ReadCSSModuleFileError}
|
|
29
|
+
*/
|
|
30
|
+
addFile(fileName: string): void;
|
|
31
|
+
/**
|
|
32
|
+
* Update a file in the project.
|
|
33
|
+
* @throws {ReadCSSModuleFileError}
|
|
34
|
+
*/
|
|
35
|
+
updateFile(fileName: string): void;
|
|
36
|
+
/** Remove a file from the project. */
|
|
37
|
+
removeFile(fileName: string): void;
|
|
38
|
+
/**
|
|
39
|
+
* Get all diagnostics.
|
|
40
|
+
* Including three types of diagnostics: project diagnostics, syntactic diagnostics, and semantic diagnostics.
|
|
41
|
+
* - Project diagnostics: For example, it includes configuration errors in tsconfig.json or warnings when there are no target files.
|
|
42
|
+
* - Syntactic diagnostics: Syntax errors in CSS Module files.
|
|
43
|
+
* - Semantic diagnostics: Errors related to the use of imports and exports in CSS module files.
|
|
44
|
+
* If there are any project diagnostics or syntactic diagnostics, semantic diagnostics will be skipped.
|
|
45
|
+
*/
|
|
46
|
+
getDiagnostics(): Diagnostic[];
|
|
47
|
+
/**
|
|
48
|
+
* Emit .d.ts files for all project files.
|
|
49
|
+
* @throws {WriteDtsFileError}
|
|
50
|
+
*/
|
|
51
|
+
emitDtsFiles(): Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Create a Project instance.
|
|
56
|
+
* Project is like a facade that calls core operations such as loading settings, parsing CSS Module files, and performing checks.
|
|
57
|
+
* The parsing and checking results are cached, and methods are also provided to clear the cache when files change.
|
|
58
|
+
* @throws {TsConfigFileNotFoundError}
|
|
59
|
+
* @throws {ReadCSSModuleFileError}
|
|
60
|
+
*/
|
|
61
|
+
export function createProject(args: ProjectArgs): Project {
|
|
62
|
+
const config = readConfigFile(args.project);
|
|
63
|
+
|
|
64
|
+
const getCanonicalFileName = (fileName: string) =>
|
|
65
|
+
ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
|
|
66
|
+
const moduleResolutionCache = ts.createModuleResolutionCache(
|
|
67
|
+
config.basePath,
|
|
68
|
+
getCanonicalFileName,
|
|
69
|
+
config.compilerOptions,
|
|
70
|
+
);
|
|
71
|
+
const resolver = createResolver(config.compilerOptions, moduleResolutionCache);
|
|
72
|
+
const matchesPattern = createMatchesPattern(config);
|
|
73
|
+
|
|
74
|
+
const cssModuleMap = new Map<string, CSSModule>();
|
|
75
|
+
const semanticDiagnosticsMap = new Map<string, Diagnostic[]>();
|
|
76
|
+
// Tracks whether .d.ts has been emitted after the last change
|
|
77
|
+
const emittedSet = new Set<string>();
|
|
78
|
+
const getCSSModule = (path: string) => cssModuleMap.get(path);
|
|
79
|
+
const exportBuilder = createExportBuilder({ getCSSModule, matchesPattern, resolver });
|
|
80
|
+
|
|
81
|
+
for (const fileName of getFileNamesByPattern(config)) {
|
|
82
|
+
// NOTE: Files may be deleted between executing `getFileNamesByPattern` and `tryParseCSSModule`.
|
|
83
|
+
// Therefore, `tryParseCSSModule` may return `undefined`.
|
|
84
|
+
const cssModule = tryParseCSSModule(fileName);
|
|
85
|
+
if (cssModule) cssModuleMap.set(fileName, cssModule);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @throws {ReadCSSModuleFileError}
|
|
90
|
+
*/
|
|
91
|
+
function addFile(fileName: string) {
|
|
92
|
+
if (cssModuleMap.has(fileName)) return;
|
|
93
|
+
|
|
94
|
+
const cssModule = tryParseCSSModule(fileName);
|
|
95
|
+
if (!cssModule) return;
|
|
96
|
+
cssModuleMap.set(fileName, cssModule);
|
|
97
|
+
|
|
98
|
+
// TODO: Delete only the minimum amount of check stage cache
|
|
99
|
+
moduleResolutionCache.clear();
|
|
100
|
+
exportBuilder.clearCache();
|
|
101
|
+
semanticDiagnosticsMap.clear();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @throws {ReadCSSModuleFileError}
|
|
106
|
+
*/
|
|
107
|
+
function updateFile(fileName: string) {
|
|
108
|
+
if (!cssModuleMap.has(fileName)) return;
|
|
109
|
+
|
|
110
|
+
const cssModule = tryParseCSSModule(fileName);
|
|
111
|
+
if (!cssModule) return;
|
|
112
|
+
cssModuleMap.set(fileName, cssModule);
|
|
113
|
+
|
|
114
|
+
// TODO: Delete only the minimum amount of check stage cache
|
|
115
|
+
exportBuilder.clearCache();
|
|
116
|
+
semanticDiagnosticsMap.clear();
|
|
117
|
+
|
|
118
|
+
emittedSet.delete(fileName);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function removeFile(fileName: string) {
|
|
122
|
+
if (!cssModuleMap.has(fileName)) return;
|
|
123
|
+
|
|
124
|
+
cssModuleMap.delete(fileName);
|
|
125
|
+
|
|
126
|
+
// TODO: Delete only the minimum amount of check stage cache
|
|
127
|
+
moduleResolutionCache.clear();
|
|
128
|
+
exportBuilder.clearCache();
|
|
129
|
+
semanticDiagnosticsMap.clear();
|
|
130
|
+
|
|
131
|
+
emittedSet.delete(fileName);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* @throws {ReadCSSModuleFileError}
|
|
136
|
+
*/
|
|
137
|
+
function tryParseCSSModule(fileName: string): CSSModule | undefined {
|
|
138
|
+
let text: string;
|
|
139
|
+
try {
|
|
140
|
+
// NOTE: We are not using asynchronous APIs for the following reasons:
|
|
141
|
+
//
|
|
142
|
+
// - Asynchronous APIs are slow in Node.js.
|
|
143
|
+
// - https://github.com/nodejs/performance/issues/151
|
|
144
|
+
// - Handling race conditions is cumbersome.
|
|
145
|
+
// - Using an asynchronous API makes `addFile` asynchronous too.
|
|
146
|
+
// - If `deleteFile` runs while `addFile` is executing, a race condition occurs.
|
|
147
|
+
// - Avoiding this requires something like a mutex. However, implementing that is cumbersome.
|
|
148
|
+
text = readFileSync(fileName, 'utf-8');
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (isNodeJSSystemError(error) && error.code === 'ENOENT') {
|
|
151
|
+
return undefined;
|
|
152
|
+
}
|
|
153
|
+
throw new ReadCSSModuleFileError(fileName, error);
|
|
154
|
+
}
|
|
155
|
+
return parseCSSModule(text, { fileName, includeSyntaxError: true, keyframes: config.keyframes });
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function getDiagnostics(): Diagnostic[] {
|
|
159
|
+
const diagnostics: Diagnostic[] = [...getProjectDiagnostics(), ...getSyntacticDiagnostics()];
|
|
160
|
+
// If there are project or syntactic diagnostics, skip semantic diagnostics
|
|
161
|
+
if (diagnostics.length > 0) return diagnostics;
|
|
162
|
+
diagnostics.push(...getSemanticDiagnostics());
|
|
163
|
+
return diagnostics;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function getProjectDiagnostics() {
|
|
167
|
+
const diagnostics: Diagnostic[] = [];
|
|
168
|
+
diagnostics.push(...config.diagnostics);
|
|
169
|
+
if (cssModuleMap.size === 0) {
|
|
170
|
+
diagnostics.push({
|
|
171
|
+
category: 'error',
|
|
172
|
+
text: `The file specified in tsconfig.json not found.`,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
return diagnostics;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function getSyntacticDiagnostics() {
|
|
179
|
+
return Array.from(cssModuleMap.values()).flatMap(({ diagnostics }) => diagnostics);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function getSemanticDiagnostics() {
|
|
183
|
+
const allDiagnostics: Diagnostic[] = [];
|
|
184
|
+
for (const cssModule of cssModuleMap.values()) {
|
|
185
|
+
let diagnostics = semanticDiagnosticsMap.get(cssModule.fileName);
|
|
186
|
+
if (!diagnostics) {
|
|
187
|
+
diagnostics = checkCSSModule(cssModule, config, exportBuilder, matchesPattern, resolver, getCSSModule);
|
|
188
|
+
semanticDiagnosticsMap.set(cssModule.fileName, diagnostics);
|
|
189
|
+
}
|
|
190
|
+
allDiagnostics.push(...diagnostics);
|
|
191
|
+
}
|
|
192
|
+
return allDiagnostics;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @throws {WriteDtsFileError}
|
|
197
|
+
*/
|
|
198
|
+
async function emitDtsFiles(): Promise<void> {
|
|
199
|
+
const promises: Promise<void>[] = [];
|
|
200
|
+
for (const cssModule of cssModuleMap.values()) {
|
|
201
|
+
if (emittedSet.has(cssModule.fileName)) continue;
|
|
202
|
+
const dts = generateDts(cssModule, { resolver, matchesPattern }, { ...config, forTsPlugin: false });
|
|
203
|
+
promises.push(
|
|
204
|
+
writeDtsFile(dts.text, cssModule.fileName, {
|
|
205
|
+
outDir: config.dtsOutDir,
|
|
206
|
+
basePath: config.basePath,
|
|
207
|
+
arbitraryExtensions: config.arbitraryExtensions,
|
|
208
|
+
}).then(() => {
|
|
209
|
+
emittedSet.add(cssModule.fileName);
|
|
210
|
+
}),
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
await Promise.all(promises);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return {
|
|
217
|
+
config,
|
|
218
|
+
isWildcardMatchedFile: (fileName) => matchesPattern(fileName),
|
|
219
|
+
addFile,
|
|
220
|
+
updateFile,
|
|
221
|
+
removeFile,
|
|
222
|
+
getDiagnostics,
|
|
223
|
+
emitDtsFiles,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function isNodeJSSystemError(error: unknown): error is NodeJS.ErrnoException {
|
|
228
|
+
return typeof error === 'object' && error !== null && 'code' in error && typeof error.code === 'string';
|
|
229
|
+
}
|