@css-modules-kit/codegen 0.0.1
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 +21 -0
- package/bin/cmk.mjs +22 -0
- package/dist/dts-writer.d.ts +23 -0
- package/dist/dts-writer.d.ts.map +1 -0
- package/dist/dts-writer.js +42 -0
- package/dist/dts-writer.js.map +1 -0
- package/dist/error.d.ts +8 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +17 -0
- package/dist/error.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/logger/formatter.d.ts +4 -0
- package/dist/logger/formatter.d.ts.map +1 -0
- package/dist/logger/formatter.js +48 -0
- package/dist/logger/formatter.js.map +1 -0
- package/dist/logger/logger.d.ts +7 -0
- package/dist/logger/logger.d.ts.map +1 -0
- package/dist/logger/logger.js +19 -0
- package/dist/logger/logger.js.map +1 -0
- package/dist/runner.d.ts +10 -0
- package/dist/runner.d.ts.map +1 -0
- package/dist/runner.js +85 -0
- package/dist/runner.js.map +1 -0
- package/package.json +54 -0
- package/src/LICENSE +21 -0
- package/src/dts-writer.ts +50 -0
- package/src/error.ts +13 -0
- package/src/index.ts +3 -0
- package/src/logger/formatter.ts +53 -0
- package/src/logger/logger.ts +22 -0
- package/src/runner.ts +118 -0
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# `@css-modules-kit/codegen`
|
|
2
|
+
|
|
3
|
+
A tool for generating `*.d.ts` files for `*.module.css`.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i -D @css-modules-kit/codegen
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
From the command line, run the `cmk` command.
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
cmk
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Options
|
|
20
|
+
|
|
21
|
+
See [css-modules-kit's README](https://github.com/mizdra/css-modules-kit?tab=readme-ov-file#options).
|
package/bin/cmk.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { SystemError } from '@css-modules-kit/core';
|
|
4
|
+
import { createLogger, runCMK } from '../dist/index.js';
|
|
5
|
+
|
|
6
|
+
// TODO: Support `--help` option
|
|
7
|
+
// TODO: Support `--version` option
|
|
8
|
+
// TODO: Support `--project` option
|
|
9
|
+
|
|
10
|
+
const cwd = process.cwd();
|
|
11
|
+
const project = cwd;
|
|
12
|
+
const logger = createLogger(cwd);
|
|
13
|
+
try {
|
|
14
|
+
await runCMK(project, logger);
|
|
15
|
+
} catch (e) {
|
|
16
|
+
if (e instanceof SystemError) {
|
|
17
|
+
logger.logSystemError(e);
|
|
18
|
+
// eslint-disable-next-line n/no-process-exit
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
throw e;
|
|
22
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get .d.ts file path.
|
|
3
|
+
* @param cssModuleFileName The path to the CSS Module file (i.e. `/src/foo.module.css`). It is absolute.
|
|
4
|
+
* @param options Output directory options
|
|
5
|
+
* @returns The path to the .d.ts file. It is absolute.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getDtsFilePath(cssModuleFileName: string, options: WriteDtsFileOption): string;
|
|
8
|
+
export interface WriteDtsFileOption {
|
|
9
|
+
/** Directory to write the d.ts file. This is an absolute path. */
|
|
10
|
+
outDir: string;
|
|
11
|
+
basePath: string;
|
|
12
|
+
/** Generate `.d.css.ts` instead of `.css.d.ts`. */
|
|
13
|
+
arbitraryExtensions: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Write a d.ts file to the file system.
|
|
17
|
+
* @param text The d.ts text to write.
|
|
18
|
+
* @param cssModuleFileName The filename of the CSS module file.
|
|
19
|
+
* @param options Options for writing the d.ts file.
|
|
20
|
+
* @throws {WriteDtsFileError} When the file cannot be written.
|
|
21
|
+
*/
|
|
22
|
+
export declare function writeDtsFile(text: string, cssModuleFileName: string, options: WriteDtsFileOption): Promise<void>;
|
|
23
|
+
//# sourceMappingURL=dts-writer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dts-writer.d.ts","sourceRoot":"","sources":["../src/dts-writer.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,iBAAiB,EAAE,MAAM,EAAE,OAAO,EAAE,kBAAkB,GAAG,MAAM,CAU7F;AAED,MAAM,WAAW,kBAAkB;IACjC,kEAAkE;IAClE,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,mDAAmD;IACnD,mBAAmB,EAAE,OAAO,CAAC;CAC9B;AAED;;;;;;GAMG;AACH,wBAAsB,YAAY,CAChC,IAAI,EAAE,MAAM,EACZ,iBAAiB,EAAE,MAAM,EACzB,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,IAAI,CAAC,CAQf"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDtsFilePath = getDtsFilePath;
|
|
4
|
+
exports.writeDtsFile = writeDtsFile;
|
|
5
|
+
const promises_1 = require("node:fs/promises");
|
|
6
|
+
const core_1 = require("@css-modules-kit/core");
|
|
7
|
+
const error_js_1 = require("./error.js");
|
|
8
|
+
/**
|
|
9
|
+
* Get .d.ts file path.
|
|
10
|
+
* @param cssModuleFileName The path to the CSS Module file (i.e. `/src/foo.module.css`). It is absolute.
|
|
11
|
+
* @param options Output directory options
|
|
12
|
+
* @returns The path to the .d.ts file. It is absolute.
|
|
13
|
+
*/
|
|
14
|
+
function getDtsFilePath(cssModuleFileName, options) {
|
|
15
|
+
const relativePath = (0, core_1.relative)(options.basePath, cssModuleFileName);
|
|
16
|
+
const outputFilePath = (0, core_1.resolve)(options.outDir, relativePath);
|
|
17
|
+
if (options.arbitraryExtensions) {
|
|
18
|
+
const { dir, name, ext } = (0, core_1.parse)(outputFilePath);
|
|
19
|
+
return (0, core_1.join)(dir, `${name}.d${ext}.ts`);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
return `${outputFilePath}.d.ts`;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Write a d.ts file to the file system.
|
|
27
|
+
* @param text The d.ts text to write.
|
|
28
|
+
* @param cssModuleFileName The filename of the CSS module file.
|
|
29
|
+
* @param options Options for writing the d.ts file.
|
|
30
|
+
* @throws {WriteDtsFileError} When the file cannot be written.
|
|
31
|
+
*/
|
|
32
|
+
async function writeDtsFile(text, cssModuleFileName, options) {
|
|
33
|
+
const dtsFileName = getDtsFilePath(cssModuleFileName, options);
|
|
34
|
+
try {
|
|
35
|
+
await (0, promises_1.mkdir)((0, core_1.dirname)(dtsFileName), { recursive: true });
|
|
36
|
+
await (0, promises_1.writeFile)(dtsFileName, text);
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
throw new error_js_1.WriteDtsFileError(dtsFileName, error);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=dts-writer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dts-writer.js","sourceRoot":"","sources":["../src/dts-writer.ts"],"names":[],"mappings":";;AAUA,wCAUC;AAiBD,oCAYC;AAjDD,+CAAoD;AACpD,gDAAgF;AAChF,yCAA+C;AAE/C;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,iBAAyB,EAAE,OAA2B;IACnF,MAAM,YAAY,GAAG,IAAA,eAAQ,EAAC,OAAO,CAAC,QAAQ,EAAE,iBAAiB,CAAC,CAAC;IACnE,MAAM,cAAc,GAAG,IAAA,cAAO,EAAC,OAAO,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IAE7D,IAAI,OAAO,CAAC,mBAAmB,EAAE,CAAC;QAChC,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,IAAA,YAAK,EAAC,cAAc,CAAC,CAAC;QACjD,OAAO,IAAA,WAAI,EAAC,GAAG,EAAE,GAAG,IAAI,KAAK,GAAG,KAAK,CAAC,CAAC;IACzC,CAAC;SAAM,CAAC;QACN,OAAO,GAAG,cAAc,OAAO,CAAC;IAClC,CAAC;AACH,CAAC;AAUD;;;;;;GAMG;AACI,KAAK,UAAU,YAAY,CAChC,IAAY,EACZ,iBAAyB,EACzB,OAA2B;IAE3B,MAAM,WAAW,GAAG,cAAc,CAAC,iBAAiB,EAAE,OAAO,CAAC,CAAC;IAC/D,IAAI,CAAC;QACH,MAAM,IAAA,gBAAK,EAAC,IAAA,cAAO,EAAC,WAAW,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACvD,MAAM,IAAA,oBAAS,EAAC,WAAW,EAAE,IAAI,CAAC,CAAC;IACrC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,4BAAiB,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;IAClD,CAAC;AACH,CAAC"}
|
package/dist/error.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { SystemError } from '@css-modules-kit/core';
|
|
2
|
+
export declare class WriteDtsFileError extends SystemError {
|
|
3
|
+
constructor(fileName: string, cause: unknown);
|
|
4
|
+
}
|
|
5
|
+
export declare class ReadCSSModuleFileError extends SystemError {
|
|
6
|
+
constructor(fileName: string, cause: unknown);
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEpD,qBAAa,iBAAkB,SAAQ,WAAW;gBACpC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO;CAG7C;AAED,qBAAa,sBAAuB,SAAQ,WAAW;gBACzC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO;CAG7C"}
|
package/dist/error.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReadCSSModuleFileError = exports.WriteDtsFileError = void 0;
|
|
4
|
+
const core_1 = require("@css-modules-kit/core");
|
|
5
|
+
class WriteDtsFileError extends core_1.SystemError {
|
|
6
|
+
constructor(fileName, cause) {
|
|
7
|
+
super('WRITE_DTS_FILE_ERROR', `Failed to write .d.ts file ${fileName}.`, { cause });
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.WriteDtsFileError = WriteDtsFileError;
|
|
11
|
+
class ReadCSSModuleFileError extends core_1.SystemError {
|
|
12
|
+
constructor(fileName, cause) {
|
|
13
|
+
super('READ_CSS_MODULE_FILE_ERROR', `Failed to read CSS Module file ${fileName}.`, { cause });
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.ReadCSSModuleFileError = ReadCSSModuleFileError;
|
|
17
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":";;;AAAA,gDAAoD;AAEpD,MAAa,iBAAkB,SAAQ,kBAAW;IAChD,YAAY,QAAgB,EAAE,KAAc;QAC1C,KAAK,CAAC,sBAAsB,EAAE,8BAA8B,QAAQ,GAAG,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;IACtF,CAAC;CACF;AAJD,8CAIC;AAED,MAAa,sBAAuB,SAAQ,kBAAW;IACrD,YAAY,QAAgB,EAAE,KAAc;QAC1C,KAAK,CAAC,4BAA4B,EAAE,kCAAkC,QAAQ,GAAG,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;IAChG,CAAC;CACF;AAJD,wDAIC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,KAAK,MAAM,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReadCSSModuleFileError = exports.WriteDtsFileError = exports.createLogger = exports.runCMK = void 0;
|
|
4
|
+
var runner_js_1 = require("./runner.js");
|
|
5
|
+
Object.defineProperty(exports, "runCMK", { enumerable: true, get: function () { return runner_js_1.runCMK; } });
|
|
6
|
+
var logger_js_1 = require("./logger/logger.js");
|
|
7
|
+
Object.defineProperty(exports, "createLogger", { enumerable: true, get: function () { return logger_js_1.createLogger; } });
|
|
8
|
+
var error_js_1 = require("./error.js");
|
|
9
|
+
Object.defineProperty(exports, "WriteDtsFileError", { enumerable: true, get: function () { return error_js_1.WriteDtsFileError; } });
|
|
10
|
+
Object.defineProperty(exports, "ReadCSSModuleFileError", { enumerable: true, get: function () { return error_js_1.ReadCSSModuleFileError; } });
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,yCAAqC;AAA5B,mGAAA,MAAM,OAAA;AACf,gDAA+D;AAAzC,yGAAA,YAAY,OAAA;AAClC,uCAAuE;AAA9D,6GAAA,iBAAiB,OAAA;AAAE,kHAAA,sBAAsB,OAAA"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type Diagnostic, type SystemError } from '@css-modules-kit/core';
|
|
2
|
+
export declare function formatDiagnostic(diagnostic: Diagnostic, cwd: string): string;
|
|
3
|
+
export declare function formatSystemError(error: SystemError): string;
|
|
4
|
+
//# sourceMappingURL=formatter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatter.d.ts","sourceRoot":"","sources":["../../src/logger/formatter.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,UAAU,EAIf,KAAK,WAAW,EACjB,MAAM,uBAAuB,CAAC;AAE/B,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAS5E;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,CAS5D"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatDiagnostic = formatDiagnostic;
|
|
4
|
+
exports.formatSystemError = formatSystemError;
|
|
5
|
+
const node_util_1 = require("node:util");
|
|
6
|
+
const core_1 = require("@css-modules-kit/core");
|
|
7
|
+
function formatDiagnostic(diagnostic, cwd) {
|
|
8
|
+
let result = '';
|
|
9
|
+
if (diagnostic.fileName) {
|
|
10
|
+
result += `${formatLocation(diagnostic.fileName, diagnostic.start, cwd)} - `;
|
|
11
|
+
}
|
|
12
|
+
result += `${formatCategory(diagnostic.category)}: `;
|
|
13
|
+
result += diagnostic.text;
|
|
14
|
+
// TODO: Add source code if diagnostics has a location
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
function formatSystemError(error) {
|
|
18
|
+
let result = '';
|
|
19
|
+
result += `${formatCategory('error')}`;
|
|
20
|
+
result += ' ';
|
|
21
|
+
result += (0, node_util_1.styleText)('gray', error.code);
|
|
22
|
+
result += ': ';
|
|
23
|
+
result += error.message;
|
|
24
|
+
// TODO: Include cause if exists
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
function formatLocation(fileName, start, cwd) {
|
|
28
|
+
let result = '';
|
|
29
|
+
result += (0, node_util_1.styleText)('cyan', (0, core_1.relative)(cwd, fileName));
|
|
30
|
+
if (start !== undefined) {
|
|
31
|
+
result += ':';
|
|
32
|
+
result += (0, node_util_1.styleText)('yellow', start.line.toString());
|
|
33
|
+
result += ':';
|
|
34
|
+
result += (0, node_util_1.styleText)('yellow', start.column.toString());
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
function formatCategory(category) {
|
|
39
|
+
switch (category) {
|
|
40
|
+
case 'error':
|
|
41
|
+
return (0, node_util_1.styleText)('red', 'error');
|
|
42
|
+
case 'warning':
|
|
43
|
+
return (0, node_util_1.styleText)('yellow', 'warning');
|
|
44
|
+
default:
|
|
45
|
+
throw new Error(`Unknown diagnostic category: ${String(category)}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=formatter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatter.js","sourceRoot":"","sources":["../../src/logger/formatter.ts"],"names":[],"mappings":";;AASA,4CASC;AAED,8CASC;AA7BD,yCAAsC;AACtC,gDAM+B;AAE/B,SAAgB,gBAAgB,CAAC,UAAsB,EAAE,GAAW;IAClE,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,IAAI,UAAU,CAAC,QAAQ,EAAE,CAAC;QACxB,MAAM,IAAI,GAAG,cAAc,CAAC,UAAU,CAAC,QAAQ,EAAE,UAAU,CAAC,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC;IAC/E,CAAC;IACD,MAAM,IAAI,GAAG,cAAc,CAAC,UAAU,CAAC,QAAQ,CAAC,IAAI,C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ACE,MAAM,IAAI,KAAK,CAAC,gCAAgC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IACxE,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Diagnostic, SystemError } from '@css-modules-kit/core';
|
|
2
|
+
export interface Logger {
|
|
3
|
+
logDiagnostics(diagnostics: Diagnostic[]): void;
|
|
4
|
+
logSystemError(error: SystemError): void;
|
|
5
|
+
}
|
|
6
|
+
export declare function createLogger(cwd: string): Logger;
|
|
7
|
+
//# sourceMappingURL=logger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/logger/logger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAGrE,MAAM,WAAW,MAAM;IACrB,cAAc,CAAC,WAAW,EAAE,UAAU,EAAE,GAAG,IAAI,CAAC;IAChD,cAAc,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI,CAAC;CAC1C;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAahD"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createLogger = createLogger;
|
|
4
|
+
const formatter_js_1 = require("./formatter.js");
|
|
5
|
+
function createLogger(cwd) {
|
|
6
|
+
return {
|
|
7
|
+
logDiagnostics(diagnostics) {
|
|
8
|
+
let result = '';
|
|
9
|
+
for (const diagnostic of diagnostics) {
|
|
10
|
+
result += `${(0, formatter_js_1.formatDiagnostic)(diagnostic, cwd)}\n\n`;
|
|
11
|
+
}
|
|
12
|
+
process.stderr.write(result);
|
|
13
|
+
},
|
|
14
|
+
logSystemError(error) {
|
|
15
|
+
process.stderr.write(`${(0, formatter_js_1.formatSystemError)(error)}\n`);
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/logger/logger.ts"],"names":[],"mappings":";;AAQA,oCAaC;AApBD,iDAAqE;AAOrE,SAAgB,YAAY,CAAC,GAAW;IACtC,OAAO;QACL,cAAc,CAAC,WAAyB;YACtC,IAAI,MAAM,GAAG,EAAE,CAAC;YAChB,KAAK,MAAM,UAAU,IAAI,WAAW,EAAE,CAAC;gBACrC,MAAM,IAAI,GAAG,IAAA,+BAAgB,EAAC,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC;YACvD,CAAC;YACD,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAC/B,CAAC;QACD,cAAc,CAAC,KAAkB;YAC/B,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,IAAA,gCAAiB,EAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACxD,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/runner.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Logger } from './logger/logger.js';
|
|
2
|
+
/**
|
|
3
|
+
* Run css-modules-kit .d.ts generation.
|
|
4
|
+
* @param project The absolute path to the project directory or the path to `tsconfig.json`.
|
|
5
|
+
* @throws {GlobError} When failed to retrieve files by glob pattern.
|
|
6
|
+
* @throws {ReadCSSModuleFileError} When failed to read CSS Module file.
|
|
7
|
+
* @throws {WriteDtsFileError}
|
|
8
|
+
*/
|
|
9
|
+
export declare function runCMK(project: string, logger: Logger): Promise<void>;
|
|
10
|
+
//# sourceMappingURL=runner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../src/runner.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAgCjD;;;;;;GAMG;AACH,wBAAsB,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAwD3E"}
|
package/dist/runner.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.runCMK = runCMK;
|
|
4
|
+
const promises_1 = require("node:fs/promises");
|
|
5
|
+
const core_1 = require("@css-modules-kit/core");
|
|
6
|
+
const dts_writer_js_1 = require("./dts-writer.js");
|
|
7
|
+
const error_js_1 = require("./error.js");
|
|
8
|
+
/**
|
|
9
|
+
* @throws {ReadCSSModuleFileError} When failed to read CSS Module file.
|
|
10
|
+
*/
|
|
11
|
+
async function parseCSSModuleByFileName(fileName, { dashedIdents }) {
|
|
12
|
+
let text;
|
|
13
|
+
try {
|
|
14
|
+
text = await (0, promises_1.readFile)(fileName, 'utf-8');
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
throw new error_js_1.ReadCSSModuleFileError(fileName, error);
|
|
18
|
+
}
|
|
19
|
+
return (0, core_1.parseCSSModule)(text, { fileName, dashedIdents, safe: false });
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* @throws {WriteDtsFileError}
|
|
23
|
+
*/
|
|
24
|
+
async function writeDtsByCSSModule(cssModule, { dtsOutDir, basePath, arbitraryExtensions }, resolver, matchesPattern) {
|
|
25
|
+
const dts = (0, core_1.createDts)(cssModule, { resolver, matchesPattern });
|
|
26
|
+
await (0, dts_writer_js_1.writeDtsFile)(dts.text, cssModule.fileName, {
|
|
27
|
+
outDir: dtsOutDir,
|
|
28
|
+
basePath,
|
|
29
|
+
arbitraryExtensions,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Run css-modules-kit .d.ts generation.
|
|
34
|
+
* @param project The absolute path to the project directory or the path to `tsconfig.json`.
|
|
35
|
+
* @throws {GlobError} When failed to retrieve files by glob pattern.
|
|
36
|
+
* @throws {ReadCSSModuleFileError} When failed to read CSS Module file.
|
|
37
|
+
* @throws {WriteDtsFileError}
|
|
38
|
+
*/
|
|
39
|
+
async function runCMK(project, logger) {
|
|
40
|
+
const config = (0, core_1.readConfigFile)(project);
|
|
41
|
+
if (config.diagnostics.length > 0) {
|
|
42
|
+
logger.logDiagnostics(config.diagnostics);
|
|
43
|
+
// eslint-disable-next-line n/no-process-exit
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
const resolver = (0, core_1.createResolver)(config.paths);
|
|
47
|
+
const matchesPattern = (0, core_1.createMatchesPattern)(config);
|
|
48
|
+
const cssModuleMap = new Map();
|
|
49
|
+
const syntacticDiagnostics = [];
|
|
50
|
+
const fileNames = (0, core_1.getFileNamesByPattern)(config);
|
|
51
|
+
if (fileNames.length === 0) {
|
|
52
|
+
logger.logDiagnostics([
|
|
53
|
+
{
|
|
54
|
+
type: 'semantic',
|
|
55
|
+
category: 'warning',
|
|
56
|
+
text: `The file specified in tsconfig.json not found.`,
|
|
57
|
+
},
|
|
58
|
+
]);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const parseResults = await Promise.all(fileNames.map(async (fileName) => parseCSSModuleByFileName(fileName, config)));
|
|
62
|
+
for (const parseResult of parseResults) {
|
|
63
|
+
cssModuleMap.set(parseResult.cssModule.fileName, parseResult.cssModule);
|
|
64
|
+
syntacticDiagnostics.push(...parseResult.diagnostics);
|
|
65
|
+
}
|
|
66
|
+
if (syntacticDiagnostics.length > 0) {
|
|
67
|
+
logger.logDiagnostics(syntacticDiagnostics);
|
|
68
|
+
// eslint-disable-next-line n/no-process-exit
|
|
69
|
+
process.exit(1);
|
|
70
|
+
}
|
|
71
|
+
const getCSSModule = (path) => cssModuleMap.get(path);
|
|
72
|
+
const exportBuilder = (0, core_1.createExportBuilder)({ getCSSModule, matchesPattern, resolver });
|
|
73
|
+
const semanticDiagnostics = [];
|
|
74
|
+
for (const { cssModule } of parseResults) {
|
|
75
|
+
const diagnostics = (0, core_1.checkCSSModule)(cssModule, exportBuilder, matchesPattern, resolver, getCSSModule);
|
|
76
|
+
semanticDiagnostics.push(...diagnostics);
|
|
77
|
+
}
|
|
78
|
+
if (semanticDiagnostics.length > 0) {
|
|
79
|
+
logger.logDiagnostics(semanticDiagnostics);
|
|
80
|
+
// eslint-disable-next-line n/no-process-exit
|
|
81
|
+
process.exit(1);
|
|
82
|
+
}
|
|
83
|
+
await Promise.all(parseResults.map(async (parseResult) => writeDtsByCSSModule(parseResult.cssModule, config, resolver, matchesPattern)));
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=runner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runner.js","sourceRoot":"","sources":["../src/runner.ts"],"names":[],"mappings":";;AA6DA,wBAwDC;AArHD,+CAA4C;AAU5C,gDAS+B;AAC/B,mDAA+C;AAC/C,yCAAoD;AAGpD;;GAEG;AACH,KAAK,UAAU,wBAAwB,CAAC,QAAgB,EAAE,EAAE,YAAY,EAAa;IACnF,IAAI,IAAY,CAAC;IACjB,IAAI,CAAC;QACH,IAAI,GAAG,MAAM,IAAA,mBAAQ,EAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC3C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,iCAAsB,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IACpD,CAAC;IACD,OAAO,IAAA,qBAAc,EAAC,IAAI,EAAE,EAAE,QAAQ,EAAE,YAAY,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;AACvE,CAAC;AAED;;GAEG;AACH,KAAK,UAAU,mBAAmB,CAChC,SAAoB,EACpB,EAAE,SAAS,EAAE,QAAQ,EAAE,mBAAmB,EAAa,EACvD,QAAkB,EAClB,cAA8B;IAE9B,MAAM,GAAG,GAAG,IAAA,gBAAS,EAAC,SA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7E,CACF,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@css-modules-kit/codegen",
|
|
3
|
+
"description": "A tool for generating `*.d.ts` files for `*.module.css`.",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/mizdra/css-modules-kit.git",
|
|
10
|
+
"directory": "packages/codegen"
|
|
11
|
+
},
|
|
12
|
+
"author": "mizdra <pp.mizdra@gmail.com>",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"private": false,
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"default": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc -b tsconfig.build.json"
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=22.0.0"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public",
|
|
31
|
+
"registry": "https://registry.npmjs.org/"
|
|
32
|
+
},
|
|
33
|
+
"bin": {
|
|
34
|
+
"cmk": "bin/cmk.mjs"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"css-modules",
|
|
38
|
+
"typescript"
|
|
39
|
+
],
|
|
40
|
+
"files": [
|
|
41
|
+
"bin",
|
|
42
|
+
"src",
|
|
43
|
+
"!src/**/*.test.ts",
|
|
44
|
+
"!src/**/__snapshots__",
|
|
45
|
+
"!src/test",
|
|
46
|
+
"dist"
|
|
47
|
+
],
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@css-modules-kit/core": "^0.0.1"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"typescript": "^5.7.3"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 mizdra
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join, parse, relative, resolve } from '@css-modules-kit/core';
|
|
3
|
+
import { WriteDtsFileError } from './error.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Get .d.ts file path.
|
|
7
|
+
* @param cssModuleFileName The path to the CSS Module file (i.e. `/src/foo.module.css`). It is absolute.
|
|
8
|
+
* @param options Output directory options
|
|
9
|
+
* @returns The path to the .d.ts file. It is absolute.
|
|
10
|
+
*/
|
|
11
|
+
export function getDtsFilePath(cssModuleFileName: string, options: WriteDtsFileOption): string {
|
|
12
|
+
const relativePath = relative(options.basePath, cssModuleFileName);
|
|
13
|
+
const outputFilePath = resolve(options.outDir, relativePath);
|
|
14
|
+
|
|
15
|
+
if (options.arbitraryExtensions) {
|
|
16
|
+
const { dir, name, ext } = parse(outputFilePath);
|
|
17
|
+
return join(dir, `${name}.d${ext}.ts`);
|
|
18
|
+
} else {
|
|
19
|
+
return `${outputFilePath}.d.ts`;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface WriteDtsFileOption {
|
|
24
|
+
/** Directory to write the d.ts file. This is an absolute path. */
|
|
25
|
+
outDir: string;
|
|
26
|
+
basePath: string;
|
|
27
|
+
/** Generate `.d.css.ts` instead of `.css.d.ts`. */
|
|
28
|
+
arbitraryExtensions: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Write a d.ts file to the file system.
|
|
33
|
+
* @param text The d.ts text to write.
|
|
34
|
+
* @param cssModuleFileName The filename of the CSS module file.
|
|
35
|
+
* @param options Options for writing the d.ts file.
|
|
36
|
+
* @throws {WriteDtsFileError} When the file cannot be written.
|
|
37
|
+
*/
|
|
38
|
+
export async function writeDtsFile(
|
|
39
|
+
text: string,
|
|
40
|
+
cssModuleFileName: string,
|
|
41
|
+
options: WriteDtsFileOption,
|
|
42
|
+
): Promise<void> {
|
|
43
|
+
const dtsFileName = getDtsFilePath(cssModuleFileName, options);
|
|
44
|
+
try {
|
|
45
|
+
await mkdir(dirname(dtsFileName), { recursive: true });
|
|
46
|
+
await writeFile(dtsFileName, text);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
throw new WriteDtsFileError(dtsFileName, error);
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/error.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { SystemError } from '@css-modules-kit/core';
|
|
2
|
+
|
|
3
|
+
export class WriteDtsFileError extends SystemError {
|
|
4
|
+
constructor(fileName: string, cause: unknown) {
|
|
5
|
+
super('WRITE_DTS_FILE_ERROR', `Failed to write .d.ts file ${fileName}.`, { cause });
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class ReadCSSModuleFileError extends SystemError {
|
|
10
|
+
constructor(fileName: string, cause: unknown) {
|
|
11
|
+
super('READ_CSS_MODULE_FILE_ERROR', `Failed to read CSS Module file ${fileName}.`, { cause });
|
|
12
|
+
}
|
|
13
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { styleText } from 'node:util';
|
|
2
|
+
import {
|
|
3
|
+
type Diagnostic,
|
|
4
|
+
type DiagnosticCategory,
|
|
5
|
+
type DiagnosticPosition,
|
|
6
|
+
relative,
|
|
7
|
+
type SystemError,
|
|
8
|
+
} from '@css-modules-kit/core';
|
|
9
|
+
|
|
10
|
+
export function formatDiagnostic(diagnostic: Diagnostic, cwd: string): string {
|
|
11
|
+
let result = '';
|
|
12
|
+
if (diagnostic.fileName) {
|
|
13
|
+
result += `${formatLocation(diagnostic.fileName, diagnostic.start, cwd)} - `;
|
|
14
|
+
}
|
|
15
|
+
result += `${formatCategory(diagnostic.category)}: `;
|
|
16
|
+
result += diagnostic.text;
|
|
17
|
+
// TODO: Add source code if diagnostics has a location
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function formatSystemError(error: SystemError): string {
|
|
22
|
+
let result = '';
|
|
23
|
+
result += `${formatCategory('error')}`;
|
|
24
|
+
result += ' ';
|
|
25
|
+
result += styleText('gray', error.code);
|
|
26
|
+
result += ': ';
|
|
27
|
+
result += error.message;
|
|
28
|
+
// TODO: Include cause if exists
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function formatLocation(fileName: string, start: DiagnosticPosition | undefined, cwd: string): string {
|
|
33
|
+
let result = '';
|
|
34
|
+
result += styleText('cyan', relative(cwd, fileName));
|
|
35
|
+
if (start !== undefined) {
|
|
36
|
+
result += ':';
|
|
37
|
+
result += styleText('yellow', start.line.toString());
|
|
38
|
+
result += ':';
|
|
39
|
+
result += styleText('yellow', start.column.toString());
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function formatCategory(category: DiagnosticCategory): string {
|
|
45
|
+
switch (category) {
|
|
46
|
+
case 'error':
|
|
47
|
+
return styleText('red', 'error');
|
|
48
|
+
case 'warning':
|
|
49
|
+
return styleText('yellow', 'warning');
|
|
50
|
+
default:
|
|
51
|
+
throw new Error(`Unknown diagnostic category: ${String(category)}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Diagnostic, SystemError } from '@css-modules-kit/core';
|
|
2
|
+
import { formatDiagnostic, formatSystemError } from './formatter.js';
|
|
3
|
+
|
|
4
|
+
export interface Logger {
|
|
5
|
+
logDiagnostics(diagnostics: Diagnostic[]): void;
|
|
6
|
+
logSystemError(error: SystemError): void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function createLogger(cwd: string): Logger {
|
|
10
|
+
return {
|
|
11
|
+
logDiagnostics(diagnostics: Diagnostic[]): void {
|
|
12
|
+
let result = '';
|
|
13
|
+
for (const diagnostic of diagnostics) {
|
|
14
|
+
result += `${formatDiagnostic(diagnostic, cwd)}\n\n`;
|
|
15
|
+
}
|
|
16
|
+
process.stderr.write(result);
|
|
17
|
+
},
|
|
18
|
+
logSystemError(error: SystemError): void {
|
|
19
|
+
process.stderr.write(`${formatSystemError(error)}\n`);
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
package/src/runner.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import type {
|
|
3
|
+
CMKConfig,
|
|
4
|
+
CSSModule,
|
|
5
|
+
MatchesPattern,
|
|
6
|
+
ParseCSSModuleResult,
|
|
7
|
+
Resolver,
|
|
8
|
+
SemanticDiagnostic,
|
|
9
|
+
SyntacticDiagnostic,
|
|
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 { writeDtsFile } from './dts-writer.js';
|
|
22
|
+
import { ReadCSSModuleFileError } from './error.js';
|
|
23
|
+
import type { Logger } from './logger/logger.js';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @throws {ReadCSSModuleFileError} When failed to read CSS Module file.
|
|
27
|
+
*/
|
|
28
|
+
async function parseCSSModuleByFileName(fileName: string, { dashedIdents }: CMKConfig): Promise<ParseCSSModuleResult> {
|
|
29
|
+
let text: string;
|
|
30
|
+
try {
|
|
31
|
+
text = await readFile(fileName, 'utf-8');
|
|
32
|
+
} catch (error) {
|
|
33
|
+
throw new ReadCSSModuleFileError(fileName, error);
|
|
34
|
+
}
|
|
35
|
+
return parseCSSModule(text, { fileName, dashedIdents, safe: false });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @throws {WriteDtsFileError}
|
|
40
|
+
*/
|
|
41
|
+
async function writeDtsByCSSModule(
|
|
42
|
+
cssModule: CSSModule,
|
|
43
|
+
{ dtsOutDir, basePath, arbitraryExtensions }: CMKConfig,
|
|
44
|
+
resolver: Resolver,
|
|
45
|
+
matchesPattern: MatchesPattern,
|
|
46
|
+
): Promise<void> {
|
|
47
|
+
const dts = createDts(cssModule, { resolver, matchesPattern });
|
|
48
|
+
await writeDtsFile(dts.text, cssModule.fileName, {
|
|
49
|
+
outDir: dtsOutDir,
|
|
50
|
+
basePath,
|
|
51
|
+
arbitraryExtensions,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Run css-modules-kit .d.ts generation.
|
|
57
|
+
* @param project The absolute path to the project directory or the path to `tsconfig.json`.
|
|
58
|
+
* @throws {GlobError} When failed to retrieve files by glob pattern.
|
|
59
|
+
* @throws {ReadCSSModuleFileError} When failed to read CSS Module file.
|
|
60
|
+
* @throws {WriteDtsFileError}
|
|
61
|
+
*/
|
|
62
|
+
export async function runCMK(project: string, logger: Logger): Promise<void> {
|
|
63
|
+
const config = readConfigFile(project);
|
|
64
|
+
if (config.diagnostics.length > 0) {
|
|
65
|
+
logger.logDiagnostics(config.diagnostics);
|
|
66
|
+
// eslint-disable-next-line n/no-process-exit
|
|
67
|
+
process.exit(1);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const resolver = createResolver(config.paths);
|
|
71
|
+
const matchesPattern = createMatchesPattern(config);
|
|
72
|
+
|
|
73
|
+
const cssModuleMap = new Map<string, CSSModule>();
|
|
74
|
+
const syntacticDiagnostics: SyntacticDiagnostic[] = [];
|
|
75
|
+
|
|
76
|
+
const fileNames = getFileNamesByPattern(config);
|
|
77
|
+
if (fileNames.length === 0) {
|
|
78
|
+
logger.logDiagnostics([
|
|
79
|
+
{
|
|
80
|
+
type: 'semantic',
|
|
81
|
+
category: 'warning',
|
|
82
|
+
text: `The file specified in tsconfig.json not found.`,
|
|
83
|
+
},
|
|
84
|
+
]);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const parseResults = await Promise.all(fileNames.map(async (fileName) => parseCSSModuleByFileName(fileName, config)));
|
|
88
|
+
for (const parseResult of parseResults) {
|
|
89
|
+
cssModuleMap.set(parseResult.cssModule.fileName, parseResult.cssModule);
|
|
90
|
+
syntacticDiagnostics.push(...parseResult.diagnostics);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (syntacticDiagnostics.length > 0) {
|
|
94
|
+
logger.logDiagnostics(syntacticDiagnostics);
|
|
95
|
+
// eslint-disable-next-line n/no-process-exit
|
|
96
|
+
process.exit(1);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const getCSSModule = (path: string) => cssModuleMap.get(path);
|
|
100
|
+
const exportBuilder = createExportBuilder({ getCSSModule, matchesPattern, resolver });
|
|
101
|
+
const semanticDiagnostics: SemanticDiagnostic[] = [];
|
|
102
|
+
for (const { cssModule } of parseResults) {
|
|
103
|
+
const diagnostics = checkCSSModule(cssModule, exportBuilder, matchesPattern, resolver, getCSSModule);
|
|
104
|
+
semanticDiagnostics.push(...diagnostics);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (semanticDiagnostics.length > 0) {
|
|
108
|
+
logger.logDiagnostics(semanticDiagnostics);
|
|
109
|
+
// eslint-disable-next-line n/no-process-exit
|
|
110
|
+
process.exit(1);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
await Promise.all(
|
|
114
|
+
parseResults.map(async (parseResult) =>
|
|
115
|
+
writeDtsByCSSModule(parseResult.cssModule, config, resolver, matchesPattern),
|
|
116
|
+
),
|
|
117
|
+
);
|
|
118
|
+
}
|