@doki-land/dejavu-engine 0.0.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/bin/dejavu.mjs +379 -0
- package/dist/index.d.ts +1445 -0
- package/dist/index.js +3465 -0
- package/package.json +41 -0
- package/readme.md +112 -0
package/bin/dejavu.mjs
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
DejavuEngine,
|
|
5
|
+
DejavuLanguage,
|
|
6
|
+
parse,
|
|
7
|
+
} from "../dist/index.js";
|
|
8
|
+
import {
|
|
9
|
+
readFileSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
existsSync,
|
|
12
|
+
readdirSync,
|
|
13
|
+
statSync,
|
|
14
|
+
mkdirSync,
|
|
15
|
+
} from "fs";
|
|
16
|
+
import { join, resolve, extname, basename, relative } from "path";
|
|
17
|
+
|
|
18
|
+
const VERSION = "0.1.0";
|
|
19
|
+
|
|
20
|
+
const COLORS = {
|
|
21
|
+
reset: "\x1b[0m",
|
|
22
|
+
red: "\x1b[31m",
|
|
23
|
+
green: "\x1b[32m",
|
|
24
|
+
yellow: "\x1b[33m",
|
|
25
|
+
blue: "\x1b[34m",
|
|
26
|
+
cyan: "\x1b[36m",
|
|
27
|
+
white: "\x1b[37m",
|
|
28
|
+
gray: "\x1b[90m",
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function color(text, colorName) {
|
|
32
|
+
return `${COLORS[colorName] || ""}${text}${COLORS.reset}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function printBanner() {
|
|
36
|
+
console.log(color("╭──────────────────────────────────────╮", "cyan"));
|
|
37
|
+
console.log(color("│", "cyan") + color(" DejaVu 模板引擎 CLI ", "white") + color("│", "cyan"));
|
|
38
|
+
console.log(color("╰──────────────────────────────────────╯", "cyan"));
|
|
39
|
+
console.log();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function printHelp() {
|
|
43
|
+
console.log(color("DejaVu Engine - 模板引擎命令行工具", "white"));
|
|
44
|
+
console.log();
|
|
45
|
+
console.log(color("用法:", "cyan"));
|
|
46
|
+
console.log(" dejavu <命令> [选项]");
|
|
47
|
+
console.log();
|
|
48
|
+
console.log(color("命令:", "cyan"));
|
|
49
|
+
console.log(" render <template> [data] 渲染模板文件");
|
|
50
|
+
console.log(" check <template> 检查模板语法");
|
|
51
|
+
console.log(" build <dir> [output] 批量编译模板目录");
|
|
52
|
+
console.log(" help 显示帮助信息");
|
|
53
|
+
console.log(" version 显示版本号");
|
|
54
|
+
console.log();
|
|
55
|
+
console.log(color("选项:", "cyan"));
|
|
56
|
+
console.log(" -d, --data <json> JSON 数据文件路径");
|
|
57
|
+
console.log(" -o, --output <path> 输出文件路径");
|
|
58
|
+
console.log(" -e, --encoding <enc> 文件编码 (默认 utf-8)");
|
|
59
|
+
console.log(" --start <str> 代码块开始定界符 (默认 {{)");
|
|
60
|
+
console.log(" --end <str> 代码块结束定界符 (默认 }})");
|
|
61
|
+
console.log(" --comment-start <str> 注释开始定界符 (默认 {#)");
|
|
62
|
+
console.log(" --comment-end <str> 注释结束定界符 (默认 #})");
|
|
63
|
+
console.log();
|
|
64
|
+
console.log(color("示例:", "cyan"));
|
|
65
|
+
console.log(" dejavu render template.html");
|
|
66
|
+
console.log(" dejavu render template.html -d data.json");
|
|
67
|
+
console.log(" dejavu render template.html -o output.html");
|
|
68
|
+
console.log(" dejavu check template.html");
|
|
69
|
+
console.log(" dejavu build ./templates ./dist");
|
|
70
|
+
console.log();
|
|
71
|
+
console.log(color("文档: https://github.com/doki-land/dejavu-engine", "gray"));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function printVersion() {
|
|
75
|
+
console.log(`DejaVu Engine v${VERSION}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function parseArgs(args) {
|
|
79
|
+
const result = {
|
|
80
|
+
command: null,
|
|
81
|
+
template: null,
|
|
82
|
+
data: null,
|
|
83
|
+
output: null,
|
|
84
|
+
encoding: "utf-8",
|
|
85
|
+
startDelimiter: "{{",
|
|
86
|
+
endDelimiter: "}}",
|
|
87
|
+
commentStart: "{#",
|
|
88
|
+
commentEnd: "#}",
|
|
89
|
+
extraArgs: [],
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
let i = 0;
|
|
93
|
+
while (i < args.length) {
|
|
94
|
+
const arg = args[i];
|
|
95
|
+
|
|
96
|
+
if (arg === "-d" || arg === "--data") {
|
|
97
|
+
result.data = args[++i];
|
|
98
|
+
} else if (arg === "-o" || arg === "--output") {
|
|
99
|
+
result.output = args[++i];
|
|
100
|
+
} else if (arg === "-e" || arg === "--encoding") {
|
|
101
|
+
result.encoding = args[++i];
|
|
102
|
+
} else if (arg === "--start") {
|
|
103
|
+
result.startDelimiter = args[++i];
|
|
104
|
+
} else if (arg === "--end") {
|
|
105
|
+
result.endDelimiter = args[++i];
|
|
106
|
+
} else if (arg === "--comment-start") {
|
|
107
|
+
result.commentStart = args[++i];
|
|
108
|
+
} else if (arg === "--comment-end") {
|
|
109
|
+
result.commentEnd = args[++i];
|
|
110
|
+
} else if (!arg.startsWith("-")) {
|
|
111
|
+
if (!result.command) {
|
|
112
|
+
result.command = arg;
|
|
113
|
+
} else if (!result.template) {
|
|
114
|
+
result.template = arg;
|
|
115
|
+
} else {
|
|
116
|
+
result.extraArgs.push(arg);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
i++;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function createLanguage(options) {
|
|
126
|
+
return new DejavuLanguage().withTemplate({
|
|
127
|
+
codeStart: options.startDelimiter,
|
|
128
|
+
codeEnd: options.endDelimiter,
|
|
129
|
+
commentStart: options.commentStart,
|
|
130
|
+
commentEnd: options.commentEnd,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function loadJsonData(dataPath) {
|
|
135
|
+
if (!dataPath) return {};
|
|
136
|
+
|
|
137
|
+
if (!existsSync(dataPath)) {
|
|
138
|
+
console.error(color("✗ 数据文件未找到:", "red"), color(dataPath, "blue"));
|
|
139
|
+
process.exit(1);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
const content = readFileSync(dataPath, "utf-8");
|
|
144
|
+
return JSON.parse(content);
|
|
145
|
+
} catch (error) {
|
|
146
|
+
console.error(color("✗ 解析 JSON 数据失败:", "red"), error.message);
|
|
147
|
+
process.exit(1);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function renderCommand(options) {
|
|
152
|
+
if (!options.template) {
|
|
153
|
+
console.error(color("✗ 缺少模板文件路径", "red"));
|
|
154
|
+
console.log(color(" 用法: dejavu render <template> [-d data.json] [-o output]", "gray"));
|
|
155
|
+
process.exit(1);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const templatePath = resolve(options.template);
|
|
159
|
+
|
|
160
|
+
if (!existsSync(templatePath)) {
|
|
161
|
+
console.error(color("✗ 模板文件未找到:", "red"), color(templatePath, "blue"));
|
|
162
|
+
process.exit(1);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
console.log(color("渲染模板:", "cyan"), color(relative(process.cwd(), templatePath), "blue"));
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
const templateContent = readFileSync(templatePath, options.encoding);
|
|
169
|
+
const language = createLanguage(options);
|
|
170
|
+
const data = loadJsonData(options.data);
|
|
171
|
+
|
|
172
|
+
const engine = new DejavuEngine();
|
|
173
|
+
const templateName = basename(templatePath);
|
|
174
|
+
engine.registerTemplate(templateName, templateContent, language);
|
|
175
|
+
|
|
176
|
+
const result = engine.renderTemplate(templateName, data);
|
|
177
|
+
|
|
178
|
+
if (options.output) {
|
|
179
|
+
const outputPath = resolve(options.output);
|
|
180
|
+
writeFileSync(outputPath, result, options.encoding);
|
|
181
|
+
console.log(color("✓ 输出已写入:", "green"), color(relative(process.cwd(), outputPath), "blue"));
|
|
182
|
+
} else {
|
|
183
|
+
console.log();
|
|
184
|
+
console.log(color("─".repeat(40), "gray"));
|
|
185
|
+
console.log(result);
|
|
186
|
+
console.log(color("─".repeat(40), "gray"));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
console.log(color("✓ 渲染完成", "green"));
|
|
190
|
+
|
|
191
|
+
} catch (error) {
|
|
192
|
+
console.error(color("✗ 渲染失败:", "red"), error.message);
|
|
193
|
+
if (error.stack) {
|
|
194
|
+
console.error(color(" 堆栈:", "gray"), error.stack.split("\n").slice(1, 3).join("\n "));
|
|
195
|
+
}
|
|
196
|
+
process.exit(1);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function checkCommand(options) {
|
|
201
|
+
if (!options.template) {
|
|
202
|
+
console.error(color("✗ 缺少模板文件路径", "red"));
|
|
203
|
+
console.log(color(" 用法: dejavu check <template>", "gray"));
|
|
204
|
+
process.exit(1);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const templatePath = resolve(options.template);
|
|
208
|
+
|
|
209
|
+
if (!existsSync(templatePath)) {
|
|
210
|
+
console.error(color("✗ 模板文件未找到:", "red"), color(templatePath, "blue"));
|
|
211
|
+
process.exit(1);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
console.log(color("检查模板:", "cyan"), color(relative(process.cwd(), templatePath), "blue"));
|
|
215
|
+
|
|
216
|
+
try {
|
|
217
|
+
const templateContent = readFileSync(templatePath, options.encoding);
|
|
218
|
+
const language = createLanguage(options);
|
|
219
|
+
|
|
220
|
+
const ast = parse(templateContent, language);
|
|
221
|
+
|
|
222
|
+
let statementCount = 0;
|
|
223
|
+
let expressionCount = 0;
|
|
224
|
+
let textCount = 0;
|
|
225
|
+
|
|
226
|
+
for (const node of ast.body) {
|
|
227
|
+
if (node.type === "Statement") statementCount++;
|
|
228
|
+
else if (node.type === "Expression") expressionCount++;
|
|
229
|
+
else if (node.type === "Text") textCount++;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
console.log();
|
|
233
|
+
console.log(color("✓ 语法检查通过", "green"));
|
|
234
|
+
console.log();
|
|
235
|
+
console.log(color(" 文本节点: ", "white") + color(textCount, "blue"));
|
|
236
|
+
console.log(color(" 表达式: ", "white") + color(expressionCount, "blue"));
|
|
237
|
+
console.log(color(" 语句: ", "white") + color(statementCount, "blue"));
|
|
238
|
+
|
|
239
|
+
} catch (error) {
|
|
240
|
+
console.error(color("✗ 语法错误:", "red"), error.message);
|
|
241
|
+
process.exit(1);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function buildCommand(options) {
|
|
246
|
+
const inputDir = options.template;
|
|
247
|
+
const outputDir = options.output || options.extraArgs[0] || "./dist";
|
|
248
|
+
|
|
249
|
+
if (!inputDir) {
|
|
250
|
+
console.error(color("✗ 缺少输入目录路径", "red"));
|
|
251
|
+
console.log(color(" 用法: dejavu build <input-dir> [output-dir]", "gray"));
|
|
252
|
+
process.exit(1);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const inputPath = resolve(inputDir);
|
|
256
|
+
const outputPath = resolve(outputDir);
|
|
257
|
+
|
|
258
|
+
if (!existsSync(inputPath)) {
|
|
259
|
+
console.error(color("✗ 输入目录未找到:", "red"), color(inputPath, "blue"));
|
|
260
|
+
process.exit(1);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
console.log(color("输入目录:", "cyan"), color(relative(process.cwd(), inputPath), "blue"));
|
|
264
|
+
console.log(color("输出目录:", "cyan"), color(relative(process.cwd(), outputPath), "blue"));
|
|
265
|
+
console.log();
|
|
266
|
+
|
|
267
|
+
try {
|
|
268
|
+
if (!existsSync(outputPath)) {
|
|
269
|
+
mkdirSync(outputPath, { recursive: true });
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const engine = new DejavuEngine();
|
|
273
|
+
const language = createLanguage(options);
|
|
274
|
+
let processedCount = 0;
|
|
275
|
+
let errorCount = 0;
|
|
276
|
+
|
|
277
|
+
function processDirectory(dir, outDir) {
|
|
278
|
+
const entries = readdirSync(dir);
|
|
279
|
+
|
|
280
|
+
for (const entry of entries) {
|
|
281
|
+
const fullPath = join(dir, entry);
|
|
282
|
+
const stat = statSync(fullPath);
|
|
283
|
+
|
|
284
|
+
if (stat.isDirectory()) {
|
|
285
|
+
const newOutDir = join(outDir, entry);
|
|
286
|
+
if (!existsSync(newOutDir)) {
|
|
287
|
+
mkdirSync(newOutDir, { recursive: true });
|
|
288
|
+
}
|
|
289
|
+
processDirectory(fullPath, newOutDir);
|
|
290
|
+
} else {
|
|
291
|
+
const ext = extname(entry);
|
|
292
|
+
if (ext === ".html" || ext === ".htm" || ext === ".dejavu" || ext === ".doki") {
|
|
293
|
+
try {
|
|
294
|
+
const content = readFileSync(fullPath, options.encoding);
|
|
295
|
+
const templateName = relative(inputPath, fullPath);
|
|
296
|
+
|
|
297
|
+
engine.registerTemplate(templateName, content, language);
|
|
298
|
+
const result = engine.renderTemplate(templateName, {});
|
|
299
|
+
|
|
300
|
+
const outFileName = entry.replace(/\.(dejavu|doki)$/, ".html");
|
|
301
|
+
const outFilePath = join(outDir, outFileName);
|
|
302
|
+
|
|
303
|
+
writeFileSync(outFilePath, result, options.encoding);
|
|
304
|
+
|
|
305
|
+
console.log(color(" ✓ ", "green") + color(relative(process.cwd(), fullPath), "gray"));
|
|
306
|
+
processedCount++;
|
|
307
|
+
} catch (error) {
|
|
308
|
+
console.log(color(" ✗ ", "red") + color(relative(process.cwd(), fullPath), "gray") + color(` - ${error.message}`, "red"));
|
|
309
|
+
errorCount++;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
processDirectory(inputPath, outputPath);
|
|
317
|
+
|
|
318
|
+
console.log();
|
|
319
|
+
console.log(color("✓ 构建完成", "green"));
|
|
320
|
+
console.log(color(" 处理文件: ", "white") + color(processedCount, "blue"));
|
|
321
|
+
if (errorCount > 0) {
|
|
322
|
+
console.log(color(" 失败文件: ", "white") + color(errorCount, "red"));
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
} catch (error) {
|
|
326
|
+
console.error(color("✗ 构建失败:", "red"), error.message);
|
|
327
|
+
process.exit(1);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function main() {
|
|
332
|
+
const args = process.argv.slice(2);
|
|
333
|
+
|
|
334
|
+
if (args.length === 0) {
|
|
335
|
+
printBanner();
|
|
336
|
+
printHelp();
|
|
337
|
+
process.exit(0);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const options = parseArgs(args);
|
|
341
|
+
|
|
342
|
+
switch (options.command) {
|
|
343
|
+
case "help":
|
|
344
|
+
case "--help":
|
|
345
|
+
case "-h":
|
|
346
|
+
printBanner();
|
|
347
|
+
printHelp();
|
|
348
|
+
break;
|
|
349
|
+
|
|
350
|
+
case "version":
|
|
351
|
+
case "--version":
|
|
352
|
+
case "-v":
|
|
353
|
+
printVersion();
|
|
354
|
+
break;
|
|
355
|
+
|
|
356
|
+
case "render":
|
|
357
|
+
printBanner();
|
|
358
|
+
renderCommand(options);
|
|
359
|
+
break;
|
|
360
|
+
|
|
361
|
+
case "check":
|
|
362
|
+
printBanner();
|
|
363
|
+
checkCommand(options);
|
|
364
|
+
break;
|
|
365
|
+
|
|
366
|
+
case "build":
|
|
367
|
+
printBanner();
|
|
368
|
+
buildCommand(options);
|
|
369
|
+
break;
|
|
370
|
+
|
|
371
|
+
default:
|
|
372
|
+
console.error(color("✗ 未知命令:", "red"), options.command || "");
|
|
373
|
+
console.log();
|
|
374
|
+
printHelp();
|
|
375
|
+
process.exit(1);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
main();
|