@batijs/build 0.0.599 → 0.0.600
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/dist/index.d.ts +6 -5
- package/dist/index.js +283 -392
- package/package.json +8 -7
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { VikeMeta } from
|
|
1
|
+
import { VikeMeta } from "@batijs/core";
|
|
2
2
|
|
|
3
|
+
//#region src/index.d.ts
|
|
3
4
|
declare function walk(dir: string): AsyncGenerator<string>;
|
|
4
5
|
declare function main(options: {
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
source: string | string[];
|
|
7
|
+
dist: string;
|
|
7
8
|
}, meta: VikeMeta): Promise<void>;
|
|
8
|
-
|
|
9
|
-
export { main as default, walk };
|
|
9
|
+
//#endregion
|
|
10
|
+
export { main as default, walk };
|
package/dist/index.js
CHANGED
|
@@ -1,423 +1,314 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
meta
|
|
18
|
-
}) {
|
|
19
|
-
const previousAst = parseModule(previousContent);
|
|
20
|
-
const currentAst = parseModule(fileContent);
|
|
21
|
-
for (const imp of previousAst.imports.$items) {
|
|
22
|
-
currentAst.imports[imp.local] = imp;
|
|
23
|
-
}
|
|
24
|
-
const index = currentAst.$ast.body.findIndex(
|
|
25
|
-
(node) => node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration"
|
|
26
|
-
);
|
|
27
|
-
for (const node of previousAst.$ast.body) {
|
|
28
|
-
if (node.type === "ImportDeclaration" || node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration") {
|
|
29
|
-
continue;
|
|
30
|
-
}
|
|
31
|
-
if (index === -1) {
|
|
32
|
-
currentAst.$ast.body.push(node);
|
|
33
|
-
} else {
|
|
34
|
-
currentAst.$ast.body.splice(index, 0, node);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
const res = await transformAndFormat(currentAst.generate().code, meta, {
|
|
38
|
-
filepath
|
|
39
|
-
});
|
|
40
|
-
return clearExports(res.code, meta);
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { mkdir, opendir, readFile, rm, rmdir, writeFile } from "node:fs/promises";
|
|
3
|
+
import path, { extname, parse, relative } from "node:path";
|
|
4
|
+
import { formatCode, parseModule, transformAndFormat } from "@batijs/core";
|
|
5
|
+
//#region src/operations/merge-dts.ts
|
|
6
|
+
async function mergeDts({ fileContent, previousContent, filepath, meta }) {
|
|
7
|
+
const previousAst = parseModule(previousContent);
|
|
8
|
+
const currentAst = parseModule(fileContent);
|
|
9
|
+
for (const imp of previousAst.imports.$items) currentAst.imports[imp.local] = imp;
|
|
10
|
+
const index = currentAst.$ast.body.findIndex((node) => node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration");
|
|
11
|
+
for (const node of previousAst.$ast.body) {
|
|
12
|
+
if (node.type === "ImportDeclaration" || node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration") continue;
|
|
13
|
+
if (index === -1) currentAst.$ast.body.push(node);
|
|
14
|
+
else currentAst.$ast.body.splice(index, 0, node);
|
|
15
|
+
}
|
|
16
|
+
return clearExports((await transformAndFormat(currentAst.generate().code, meta, { filepath })).code, meta);
|
|
41
17
|
}
|
|
42
18
|
function clearExports(code, meta) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return code.slice(0, index) + "\n// biome-ignore lint/complexity/noUselessEmptyExport: ensure that the file is considered as a module" + code.slice(index);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return code;
|
|
19
|
+
if (code.trim() === "export {};") return;
|
|
20
|
+
if (meta.BATI.has("biome")) {
|
|
21
|
+
const index = code.indexOf("\nexport {};");
|
|
22
|
+
const foundImport = code.match(/^import .* from /gm);
|
|
23
|
+
if (index !== -1 && foundImport) return code.slice(0, index) + "\n// biome-ignore lint/complexity/noUselessEmptyExport: ensure that the file is considered as a module" + code.slice(index);
|
|
24
|
+
}
|
|
25
|
+
return code;
|
|
54
26
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
async function executeOperationFile(op, {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
filepath
|
|
74
|
-
});
|
|
75
|
-
} else {
|
|
76
|
-
fileContent = clearExports(fileContent, meta);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return {
|
|
80
|
-
context: result.context,
|
|
81
|
-
content: fileContent ? fileContent.trimStart() : void 0
|
|
82
|
-
};
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/operations/file.ts
|
|
29
|
+
async function executeOperationFile(op, { meta, previousOperationSameDestination }) {
|
|
30
|
+
const code = await readFile(op.sourceAbsolute, { encoding: "utf-8" });
|
|
31
|
+
const filepath = relative(op.source, op.sourceAbsolute);
|
|
32
|
+
const result = await transformAndFormat(code, meta, { filepath });
|
|
33
|
+
let fileContent = result.code;
|
|
34
|
+
if (op.sourceAbsolute.endsWith(".d.ts")) if (previousOperationSameDestination?.content) fileContent = await mergeDts({
|
|
35
|
+
fileContent,
|
|
36
|
+
previousContent: previousOperationSameDestination.content,
|
|
37
|
+
meta,
|
|
38
|
+
filepath
|
|
39
|
+
});
|
|
40
|
+
else fileContent = clearExports(fileContent, meta);
|
|
41
|
+
return {
|
|
42
|
+
context: result.context,
|
|
43
|
+
content: fileContent ? fileContent.trimStart() : void 0
|
|
44
|
+
};
|
|
83
45
|
}
|
|
84
|
-
|
|
85
|
-
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region src/utils.ts
|
|
86
48
|
function orderBy(array, getter) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
49
|
+
return array.slice().sort((a, b) => {
|
|
50
|
+
const valueA = getter(a);
|
|
51
|
+
const valueB = getter(b);
|
|
52
|
+
if (valueA < valueB) return -1;
|
|
53
|
+
if (valueA > valueB) return 1;
|
|
54
|
+
return 0;
|
|
55
|
+
});
|
|
94
56
|
}
|
|
95
|
-
|
|
96
|
-
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/operations/rearranger.ts
|
|
97
59
|
var OperationsRearranger = class {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
for (const file of this.files.values()) {
|
|
110
|
-
if (file.filter((op) => op.important).length >= 2) {
|
|
111
|
-
throw new Error(`Error while trying to generate file: '${file[0].destination}'.
|
|
60
|
+
files;
|
|
61
|
+
constructor() {
|
|
62
|
+
this.files = /* @__PURE__ */ new Map();
|
|
63
|
+
}
|
|
64
|
+
addFile(file) {
|
|
65
|
+
if (!this.files.has(file.destination)) this.files.set(file.destination, []);
|
|
66
|
+
this.files.get(file.destination).push(file);
|
|
67
|
+
}
|
|
68
|
+
*compute() {
|
|
69
|
+
for (const file of this.files.values()) {
|
|
70
|
+
if (file.filter((op) => op.important).length >= 2) throw new Error(`Error while trying to generate file: '${file[0].destination}'.
|
|
112
71
|
Multiple important file is not yet supported.
|
|
113
72
|
Please report this issue to https://github.com/vikejs/bati`);
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
yield* newOrder.filter((op) => op.kind !== "file");
|
|
131
|
-
}
|
|
132
|
-
}
|
|
73
|
+
const newOrder = orderBy(file, (op) => {
|
|
74
|
+
if (op.kind === "file" && !op.important) return 1;
|
|
75
|
+
if (op.kind === "file" && op.important) return 2;
|
|
76
|
+
if (op.kind === "transform" && !op.important) return 3;
|
|
77
|
+
if (op.kind === "transform" && op.important) return 4;
|
|
78
|
+
throw new Error("Unhandled OperationsRearranger.compute orderBy case");
|
|
79
|
+
});
|
|
80
|
+
if (file[0].sourceAbsolute.endsWith(".d.ts")) yield* newOrder.filter((op) => op.kind === "file");
|
|
81
|
+
else {
|
|
82
|
+
const input = newOrder.filter((op) => op.kind === "file").at(-1);
|
|
83
|
+
if (input) yield input;
|
|
84
|
+
}
|
|
85
|
+
yield* newOrder.filter((op) => op.kind !== "file");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
133
88
|
};
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
import { formatCode } from "@batijs/core";
|
|
138
|
-
var isWin = process.platform === "win32";
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region src/operations/transform.ts
|
|
91
|
+
const isWin = process.platform === "win32";
|
|
139
92
|
async function transformFileAfterExec(filepath, fileContent) {
|
|
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
|
-
|
|
177
|
-
return fileContent.toString();
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
throw new Error(`Unsupported file extension ${parsed.base} (${filepath})`);
|
|
93
|
+
if (fileContent === void 0 || fileContent === null) return null;
|
|
94
|
+
if (typeof fileContent === "object" && typeof fileContent.finalize === "function") {
|
|
95
|
+
fileContent = fileContent.finalize();
|
|
96
|
+
if (typeof fileContent !== "string") throw new Error("finalize() must return a string");
|
|
97
|
+
}
|
|
98
|
+
const parsed = parse(filepath);
|
|
99
|
+
const toTest = [
|
|
100
|
+
parsed.base,
|
|
101
|
+
parsed.ext,
|
|
102
|
+
parsed.name
|
|
103
|
+
].filter(Boolean);
|
|
104
|
+
for (const ext of toTest) switch (ext) {
|
|
105
|
+
case ".ts":
|
|
106
|
+
case ".js":
|
|
107
|
+
case ".tsx":
|
|
108
|
+
case ".jsx": return formatCode(fileContent, { filepath });
|
|
109
|
+
case ".env":
|
|
110
|
+
case ".env.local":
|
|
111
|
+
case ".env.development":
|
|
112
|
+
case ".env.development.local":
|
|
113
|
+
case ".env.test":
|
|
114
|
+
case ".env.test.local":
|
|
115
|
+
case ".env.production":
|
|
116
|
+
case ".env.production.local":
|
|
117
|
+
case ".html":
|
|
118
|
+
case ".md":
|
|
119
|
+
case ".toml": return fileContent;
|
|
120
|
+
case ".json":
|
|
121
|
+
case ".jsonc":
|
|
122
|
+
if (typeof fileContent === "string") return fileContent;
|
|
123
|
+
return JSON.stringify(fileContent, null, 2);
|
|
124
|
+
case ".yml":
|
|
125
|
+
case ".yaml":
|
|
126
|
+
if (typeof fileContent === "string") return fileContent;
|
|
127
|
+
return fileContent.toString();
|
|
128
|
+
}
|
|
129
|
+
throw new Error(`Unsupported file extension ${parsed.base} (${filepath})`);
|
|
181
130
|
}
|
|
182
131
|
async function importTransformer(p) {
|
|
183
|
-
|
|
184
|
-
const f = await import(importFile);
|
|
185
|
-
return f.default;
|
|
132
|
+
return (await (isWin ? import(`file://${p}`) : import(p))).default;
|
|
186
133
|
}
|
|
187
|
-
async function executeOperationTransform(op, {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
meta,
|
|
199
|
-
source: op.source,
|
|
200
|
-
target: op.destination,
|
|
201
|
-
packageJson
|
|
202
|
-
})
|
|
203
|
-
);
|
|
204
|
-
return {
|
|
205
|
-
content: fileContent !== null ? fileContent : void 0
|
|
206
|
-
};
|
|
134
|
+
async function executeOperationTransform(op, { meta, previousOperationSameDestination, packageJson }) {
|
|
135
|
+
const transformer = await importTransformer(op.sourceAbsolute);
|
|
136
|
+
const previousContent = previousOperationSameDestination?.content;
|
|
137
|
+
const fileContent = await transformFileAfterExec(op.destination, await transformer({
|
|
138
|
+
readfile: previousContent ? () => previousContent : void 0,
|
|
139
|
+
meta,
|
|
140
|
+
source: op.source,
|
|
141
|
+
target: op.destination,
|
|
142
|
+
packageJson
|
|
143
|
+
}));
|
|
144
|
+
return { content: fileContent !== null ? fileContent : void 0 };
|
|
207
145
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
static allPathAbsolute = /* @__PURE__ */ new Map();
|
|
221
|
-
static allIncludeIfImported = [];
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/relations.ts
|
|
148
|
+
var RelationFile = class RelationFile {
|
|
149
|
+
static allPathAbsolute = /* @__PURE__ */ new Map();
|
|
150
|
+
static allIncludeIfImported = [];
|
|
151
|
+
constructor(pathAbsolute, includeIfImported) {
|
|
152
|
+
this.pathAbsolute = pathAbsolute;
|
|
153
|
+
this.includeIfImported = includeIfImported;
|
|
154
|
+
RelationFile.allPathAbsolute.set(pathAbsolute, this);
|
|
155
|
+
if (includeIfImported) RelationFile.allIncludeIfImported.push(this);
|
|
156
|
+
}
|
|
222
157
|
};
|
|
223
|
-
var RelationImport = class
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
if (!importedFile) {
|
|
245
|
-
unimportedFiles.push(file);
|
|
246
|
-
} else if (importedFile.source.includeIfImported) {
|
|
247
|
-
importedByVolatileFile.push(importedFile);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
return computeDeepUnimportedFiles(importedByVolatileFile, unimportedFiles);
|
|
251
|
-
}
|
|
158
|
+
var RelationImport = class RelationImport {
|
|
159
|
+
static allImports = [];
|
|
160
|
+
constructor(source, importTarget) {
|
|
161
|
+
this.source = source;
|
|
162
|
+
this.importTarget = importTarget;
|
|
163
|
+
RelationImport.allImports.push(this);
|
|
164
|
+
}
|
|
165
|
+
get importTargetRelationFile() {
|
|
166
|
+
const potentialTargets = importToPotentialTargets(this.importTarget);
|
|
167
|
+
for (const target of potentialTargets) if (RelationFile.allPathAbsolute.has(target)) return RelationFile.allPathAbsolute.get(target);
|
|
168
|
+
}
|
|
169
|
+
static computeUnimportedFiles() {
|
|
170
|
+
const unimportedFiles = [];
|
|
171
|
+
const importedByVolatileFile = [];
|
|
172
|
+
for (const file of RelationFile.allIncludeIfImported) {
|
|
173
|
+
const importedFile = RelationImport.allImports.find((ai) => ai.importTargetRelationFile === file);
|
|
174
|
+
if (!importedFile) unimportedFiles.push(file);
|
|
175
|
+
else if (importedFile.source.includeIfImported) importedByVolatileFile.push(importedFile);
|
|
176
|
+
}
|
|
177
|
+
return computeDeepUnimportedFiles(importedByVolatileFile, unimportedFiles);
|
|
178
|
+
}
|
|
252
179
|
};
|
|
253
180
|
function computeDeepUnimportedFiles(importedByVolatileFile, unimportedFiles) {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
}
|
|
264
|
-
if (redo) {
|
|
265
|
-
computeDeepUnimportedFiles(importedByVolatileFile, unimportedFiles);
|
|
266
|
-
}
|
|
267
|
-
return unimportedFiles;
|
|
181
|
+
const copyImportedByVolatileFile = Array.from(importedByVolatileFile);
|
|
182
|
+
let redo = false;
|
|
183
|
+
for (const relationImport of copyImportedByVolatileFile) if (unimportedFiles.find((uf) => uf === relationImport.source)) {
|
|
184
|
+
redo = true;
|
|
185
|
+
unimportedFiles.push(relationImport.importTargetRelationFile);
|
|
186
|
+
importedByVolatileFile = importedByVolatileFile.filter((i) => i !== relationImport);
|
|
187
|
+
}
|
|
188
|
+
if (redo) computeDeepUnimportedFiles(importedByVolatileFile, unimportedFiles);
|
|
189
|
+
return unimportedFiles;
|
|
268
190
|
}
|
|
269
191
|
function importToPotentialTargets(imp) {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
192
|
+
let subject = imp;
|
|
193
|
+
const ext = extname(imp);
|
|
194
|
+
const targets = [];
|
|
195
|
+
if (ext.match(/^\.m?[jt]sx?$/)) subject = subject.replace(/^\.m?[jt]sx?$/, "");
|
|
196
|
+
if (!ext || subject !== imp) targets.push(...[
|
|
197
|
+
".js",
|
|
198
|
+
".jsx",
|
|
199
|
+
".ts",
|
|
200
|
+
".tsx",
|
|
201
|
+
".cjs",
|
|
202
|
+
".mjs"
|
|
203
|
+
].map((e) => `${subject}${e}`));
|
|
204
|
+
else targets.push(imp);
|
|
205
|
+
return targets;
|
|
282
206
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
207
|
+
//#endregion
|
|
208
|
+
//#region src/index.ts
|
|
209
|
+
const reIgnoreFile = /^(chunk-|asset-|#)/gi;
|
|
286
210
|
function toDist(filepath, source, dist) {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
211
|
+
const split = filepath.split(path.sep);
|
|
212
|
+
split[split.length - 1] = split[split.length - 1].replace(/^\$\$?(.*)\.m?[tj]sx?$/, "$1").replace(/^!(.*)$/, "$1");
|
|
213
|
+
return split.join(path.sep).replace(source, dist);
|
|
290
214
|
}
|
|
291
215
|
async function safeWriteFile(destination, content) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
recursive: true
|
|
295
|
-
});
|
|
296
|
-
await writeFile(destination, content, { encoding: "utf-8" });
|
|
216
|
+
await mkdir(path.dirname(destination), { recursive: true });
|
|
217
|
+
await writeFile(destination, content, { encoding: "utf-8" });
|
|
297
218
|
}
|
|
298
219
|
async function safeRmFile(destination, options) {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
} catch {
|
|
316
|
-
console.warn(`Failed to remove unnecessary file: ${destination}`);
|
|
317
|
-
}
|
|
220
|
+
try {
|
|
221
|
+
await rm(destination, {
|
|
222
|
+
force: true,
|
|
223
|
+
maxRetries: 3,
|
|
224
|
+
recursive: false,
|
|
225
|
+
retryDelay: 150
|
|
226
|
+
});
|
|
227
|
+
if (options?.removeEmptyDir) try {
|
|
228
|
+
await rmdir(path.dirname(destination), {
|
|
229
|
+
maxRetries: 3,
|
|
230
|
+
retryDelay: 150
|
|
231
|
+
});
|
|
232
|
+
} catch {}
|
|
233
|
+
} catch {
|
|
234
|
+
console.warn(`Failed to remove unnecessary file: ${destination}`);
|
|
235
|
+
}
|
|
318
236
|
}
|
|
319
237
|
async function* walk(dir) {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
238
|
+
if (!existsSync(dir)) return;
|
|
239
|
+
for await (const d of await opendir(dir)) {
|
|
240
|
+
if (d.name.match(reIgnoreFile)) continue;
|
|
241
|
+
const entry = path.join(dir, d.name);
|
|
242
|
+
if (d.isDirectory()) yield* walk(entry);
|
|
243
|
+
else if (d.isFile()) yield entry;
|
|
244
|
+
}
|
|
327
245
|
}
|
|
328
246
|
async function main(options, meta) {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
Boolean(report.context?.flags.has("include-if-imported"))
|
|
395
|
-
);
|
|
396
|
-
} else if (op.kind === "transform") {
|
|
397
|
-
report = await executeOperationTransform(op, {
|
|
398
|
-
meta,
|
|
399
|
-
previousOperationSameDestination: previousOp,
|
|
400
|
-
packageJson
|
|
401
|
-
});
|
|
402
|
-
}
|
|
403
|
-
if (report.content) {
|
|
404
|
-
await safeWriteFile(op.destination, report.content.trimStart());
|
|
405
|
-
if (op.destinationAbsolute === packageJsonDistAbsolute) {
|
|
406
|
-
packageJson = JSON.parse(report.content);
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
previousOpContent = report.content ?? previousOpContent;
|
|
410
|
-
previousOp = {
|
|
411
|
-
...op,
|
|
412
|
-
...report,
|
|
413
|
-
content: previousOpContent
|
|
414
|
-
};
|
|
415
|
-
}
|
|
416
|
-
for (const target of RelationImport.computeUnimportedFiles()) {
|
|
417
|
-
await safeRmFile(target.pathAbsolute, { removeEmptyDir: true });
|
|
418
|
-
}
|
|
247
|
+
const sources = Array.isArray(options.source) ? options.source : [options.source];
|
|
248
|
+
function updateAllImports(target, imports, includeIfImported) {
|
|
249
|
+
const rf = new RelationFile(target, includeIfImported);
|
|
250
|
+
if (!imports) return;
|
|
251
|
+
for (const imp of imports.values()) new RelationImport(rf, path.resolve(path.dirname(target), imp));
|
|
252
|
+
}
|
|
253
|
+
const rearranger = new OperationsRearranger();
|
|
254
|
+
for (const source of sources) for await (const p of walk(source)) {
|
|
255
|
+
const target = toDist(p, source, options.dist);
|
|
256
|
+
const targetAbsolute = path.isAbsolute(target) ? target : path.resolve(target);
|
|
257
|
+
const parsed = path.parse(p);
|
|
258
|
+
if (parsed.name.match(reIgnoreFile)) {} else if (parsed.name.startsWith("$") && parsed.ext.match(/\.m?tsx?$/)) throw new Error(`Typescript file needs to be compiled before it can be executed: '${p}'.
|
|
259
|
+
Please report this issue to https://github.com/vikejs/bati`);
|
|
260
|
+
else if ((parsed.name.startsWith("!$") || parsed.name.startsWith("$")) && parsed.ext.match(/\.m?jsx?$/)) rearranger.addFile({
|
|
261
|
+
source,
|
|
262
|
+
sourceAbsolute: p,
|
|
263
|
+
destination: target,
|
|
264
|
+
destinationAbsolute: targetAbsolute,
|
|
265
|
+
kind: "transform",
|
|
266
|
+
parsed,
|
|
267
|
+
important: parsed.name.startsWith("!")
|
|
268
|
+
});
|
|
269
|
+
else rearranger.addFile({
|
|
270
|
+
source,
|
|
271
|
+
sourceAbsolute: p,
|
|
272
|
+
destination: target,
|
|
273
|
+
destinationAbsolute: targetAbsolute,
|
|
274
|
+
kind: "file",
|
|
275
|
+
parsed,
|
|
276
|
+
important: parsed.name.startsWith("!")
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
let previousOp;
|
|
280
|
+
let previousOpContent;
|
|
281
|
+
let packageJson = {};
|
|
282
|
+
const packageJsonDistAbsolute = path.join(path.isAbsolute(options.dist) ? options.dist : path.resolve(options.dist), "package.json");
|
|
283
|
+
for (const op of rearranger.compute()) {
|
|
284
|
+
if (previousOp?.destination !== op.destination) {
|
|
285
|
+
previousOp = void 0;
|
|
286
|
+
previousOpContent = void 0;
|
|
287
|
+
}
|
|
288
|
+
let report = {};
|
|
289
|
+
if (op.kind === "file") {
|
|
290
|
+
report = await executeOperationFile(op, {
|
|
291
|
+
meta,
|
|
292
|
+
previousOperationSameDestination: previousOp
|
|
293
|
+
});
|
|
294
|
+
updateAllImports(op.destinationAbsolute, report.context?.imports, Boolean(report.context?.flags.has("include-if-imported")));
|
|
295
|
+
} else if (op.kind === "transform") report = await executeOperationTransform(op, {
|
|
296
|
+
meta,
|
|
297
|
+
previousOperationSameDestination: previousOp,
|
|
298
|
+
packageJson
|
|
299
|
+
});
|
|
300
|
+
if (report.content) {
|
|
301
|
+
await safeWriteFile(op.destination, report.content.trimStart());
|
|
302
|
+
if (op.destinationAbsolute === packageJsonDistAbsolute) packageJson = JSON.parse(report.content);
|
|
303
|
+
}
|
|
304
|
+
previousOpContent = report.content ?? previousOpContent;
|
|
305
|
+
previousOp = {
|
|
306
|
+
...op,
|
|
307
|
+
...report,
|
|
308
|
+
content: previousOpContent
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
for (const target of RelationImport.computeUnimportedFiles()) await safeRmFile(target.pathAbsolute, { removeEmptyDir: true });
|
|
419
312
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
walk
|
|
423
|
-
};
|
|
313
|
+
//#endregion
|
|
314
|
+
export { main as default, walk };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@batijs/build",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.600",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [],
|
|
@@ -9,12 +9,13 @@
|
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"devDependencies": {
|
|
11
11
|
"@types/node": "^20.19.37",
|
|
12
|
-
"
|
|
13
|
-
"@batijs/
|
|
14
|
-
"@batijs/
|
|
12
|
+
"tsdown": "^0.21.7",
|
|
13
|
+
"@batijs/core": "0.0.600",
|
|
14
|
+
"@batijs/features": "0.0.600",
|
|
15
|
+
"@batijs/compile": "0.0.600"
|
|
15
16
|
},
|
|
16
|
-
"
|
|
17
|
-
"@batijs/core": "0.0.
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@batijs/core": "0.0.600"
|
|
18
19
|
},
|
|
19
20
|
"main": "./dist/index.js",
|
|
20
21
|
"module": "./dist/index.js",
|
|
@@ -33,6 +34,6 @@
|
|
|
33
34
|
],
|
|
34
35
|
"scripts": {
|
|
35
36
|
"check-types": "tsc --noEmit",
|
|
36
|
-
"build": "
|
|
37
|
+
"build": "tsdown"
|
|
37
38
|
}
|
|
38
39
|
}
|