-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathrunner.ts
More file actions
132 lines (122 loc) · 4.06 KB
/
Copy pathrunner.ts
File metadata and controls
132 lines (122 loc) · 4.06 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
import { readFile, rm } from 'node:fs/promises';
import type {
CMKConfig,
CSSModule,
Diagnostic,
DiagnosticWithLocation,
MatchesPattern,
ParseCSSModuleResult,
Resolver,
} from '@css-modules-kit/core';
import {
checkCSSModule,
createDts,
createExportBuilder,
createMatchesPattern,
createResolver,
getFileNamesByPattern,
parseCSSModule,
readConfigFile,
} from '@css-modules-kit/core';
import ts from 'typescript';
import type { ParsedArgs } from './cli.js';
import { writeDtsFile } from './dts-writer.js';
import { ReadCSSModuleFileError } from './error.js';
import type { Logger } from './logger/logger.js';
/**
* @throws {ReadCSSModuleFileError} When failed to read CSS Module file.
*/
async function parseCSSModuleByFileName(fileName: string): Promise<ParseCSSModuleResult> {
let text: string;
try {
text = await readFile(fileName, 'utf-8');
} catch (error) {
throw new ReadCSSModuleFileError(fileName, error);
}
return parseCSSModule(text, { fileName, safe: false });
}
/**
* @throws {WriteDtsFileError}
*/
async function writeDtsByCSSModule(
cssModule: CSSModule,
{ dtsOutDir, basePath, arbitraryExtensions, namedExports, prioritizeNamedImports }: CMKConfig,
resolver: Resolver,
matchesPattern: MatchesPattern,
): Promise<void> {
const dts = createDts(
cssModule,
{ resolver, matchesPattern },
{ namedExports, prioritizeNamedImports, forTsPlugin: false },
);
await writeDtsFile(dts.text, cssModule.fileName, {
outDir: dtsOutDir,
basePath,
arbitraryExtensions,
});
}
/**
* 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}
*/
export async function runCMK(args: ParsedArgs, logger: Logger): Promise<void> {
const config = readConfigFile(args.project);
if (config.diagnostics.length > 0) {
logger.logDiagnostics(config.diagnostics);
// eslint-disable-next-line n/no-process-exit
process.exit(1);
}
const getCanonicalFileName = (fileName: string) =>
ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
const moduleResolutionCache = ts.createModuleResolutionCache(
config.basePath,
getCanonicalFileName,
config.compilerOptions,
);
const resolver = createResolver(config.compilerOptions, moduleResolutionCache);
const matchesPattern = createMatchesPattern(config);
const cssModuleMap = new Map<string, CSSModule>();
const syntacticDiagnostics: DiagnosticWithLocation[] = [];
const fileNames = getFileNamesByPattern(config);
if (fileNames.length === 0) {
logger.logDiagnostics([
{
category: 'warning',
text: `The file specified in tsconfig.json not found.`,
},
]);
return;
}
const parseResults = await Promise.all(fileNames.map(async (fileName) => parseCSSModuleByFileName(fileName)));
for (const parseResult of parseResults) {
cssModuleMap.set(parseResult.cssModule.fileName, parseResult.cssModule);
syntacticDiagnostics.push(...parseResult.diagnostics);
}
if (syntacticDiagnostics.length > 0) {
logger.logDiagnostics(syntacticDiagnostics);
// eslint-disable-next-line n/no-process-exit
process.exit(1);
}
const getCSSModule = (path: string) => cssModuleMap.get(path);
const exportBuilder = createExportBuilder({ getCSSModule, matchesPattern, resolver });
const semanticDiagnostics: Diagnostic[] = [];
for (const { cssModule } of parseResults) {
const diagnostics = checkCSSModule(cssModule, config, exportBuilder, matchesPattern, resolver, getCSSModule);
semanticDiagnostics.push(...diagnostics);
}
if (semanticDiagnostics.length > 0) {
logger.logDiagnostics(semanticDiagnostics);
// eslint-disable-next-line n/no-process-exit
process.exit(1);
}
if (args.clean) {
await rm(config.dtsOutDir, { recursive: true, force: true });
}
await Promise.all(
parseResults.map(async (parseResult) =>
writeDtsByCSSModule(parseResult.cssModule, config, resolver, matchesPattern),
),
);
}