-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathresolve.ts
176 lines (151 loc) · 4.91 KB
/
resolve.ts
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
170
171
172
173
174
175
176
#!/usr/bin/env node
import { ParseResult } from "./type";
import { codegenReturnWithElements } from "./codegen";
import { Config } from "./config";
import * as fs from "node:fs";
import * as path from "path";
function camelcase(str: string): string {
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
}
export type RelativePath = string;
export type FileWithDependence = {
relativePath: string;
dependencies: string[];
};
export function resolveDependencies(
importPath: RelativePath,
baseDir: string,
resolved: Set<RelativePath>
): FileWithDependence[] {
const dependencies: FileWithDependence[] = [];
// check if the file exist
const realPath = path.join(baseDir, importPath);
if (!fs.existsSync(realPath)) {
console.error(`Schema file ${realPath} does not exist.`);
process.exit(1);
}
const cur: FileWithDependence = {
relativePath: importPath,
dependencies: [],
};
const schema = fs.readFileSync(realPath, "utf-8");
if (!schema) {
return [cur];
}
const matched = schema.match(/.*import\s+"(.*)".*;/g);
if (!matched) {
return [cur];
}
// collect all import filenames
const importFileNames = matched
.map((item: string) => {
// if is comment statement, continue
if (item.trim().startsWith("//")) {
return "";
}
const m = item.match(/.*"(.*)".*/);
return m ? m[1] : "";
})
.filter(Boolean);
// loop all import files
for (const importFileName of importFileNames) {
const mFilePath = path.join(baseDir, importFileName + ".mol");
const mRelativePath = path.relative(baseDir, mFilePath);
cur.dependencies.push(importFileName);
if (!resolved.has(mFilePath)) {
// mask this file has resolved
resolved.add(mFilePath);
const _dependencies = resolveDependencies(
mRelativePath,
baseDir,
resolved
);
dependencies.push(..._dependencies);
}
}
dependencies.push(cur);
return dependencies;
}
export function extractAndEraseImportClauses(code: string): string {
const lines = code.split("\n");
const delImportLines = lines.filter((line: string) => {
return !line.trim().startsWith("import");
});
return delImportLines.join("\n");
}
function printOrWrite(resultMap: Map<string, ParseResult>, config: Config) {
for (const name of resultMap.keys()) {
if (config.output < 2) {
console.log(`// ${String("-").repeat(66)} //`);
console.log(`// generate from ${name}`);
console.log(`// ${String("-").repeat(66)} //`);
console.log(resultMap.get(name)?.code);
if (config.output === 1) {
const dir = path.join(config.dir, "mols");
if (!fs.existsSync(dir)) {
console.log(`mkdir mols`);
fs.mkdirSync(dir);
}
const tsName = name.replace(".mol", ".ts");
const targetDir = path.dirname(path.join(dir, tsName));
if (!fs.existsSync(targetDir)) {
console.log(`mkdir ${targetDir}`);
fs.mkdirSync(targetDir, { recursive: true });
}
console.log(`writing file ${tsName}`);
fs.writeFileSync(
path.join(dir, tsName),
resultMap.get(name)?.code || ""
);
console.log(`write file ${tsName} finish`);
}
}
}
}
export function loopCodegen(config: Config): Map<string, ParseResult> {
const result: Map<string, ParseResult> = new Map();
const baseDir = path.dirname(config.schemaFile);
const relativePath = path.basename(config.schemaFile);
const dependencies = resolveDependencies(relativePath, baseDir, new Set());
if (dependencies.length === 0) {
return result;
}
const parsed: Set<string> = new Set();
dependencies.forEach((cur) => {
// has generated, continue
if (parsed.has(cur.relativePath)) {
return;
}
// erase the import clause from the schema when calling the codegen method
const realPath = path.join(baseDir, cur.relativePath);
const schema = extractAndEraseImportClauses(
fs.readFileSync(realPath, "utf-8")
);
let optionPrepend = config.prepend;
// append all ESM import to config.prepend
for (const importName of cur.dependencies) {
const importAbsolutePath = path.join(
path.dirname(realPath),
importName + ".mol"
);
const importRelativePath = path.relative(baseDir, importAbsolutePath);
if (result.has(importRelativePath)) {
const imptDesc = `\nimport { ${result
.get(importRelativePath)
?.elements.join(", ")} } from './${importName}'`;
optionPrepend += imptDesc;
}
}
const codegenReturn = codegenReturnWithElements(schema, {
prepend: optionPrepend,
formatObjectKeys:
String(config.objectKeyFormat).toLowerCase() === "camelcase"
? camelcase
: undefined,
});
parsed.add(cur.relativePath);
result.set(cur.relativePath, codegenReturn);
});
printOrWrite(result, config);
return result;
}