@doki-land/dejavu-engine 0.0.0 → 0.0.8

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 DELETED
@@ -1,379 +0,0 @@
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();
package/readme.md DELETED
@@ -1,112 +0,0 @@
1
-
2
- # DejaVu Tools (TypeScript)
3
-
4
- Déjà-Vu 模板引擎的 TypeScript 工具库,提供各种通用的工具函数、类型定义、错误处理和配置管理功能。
5
-
6
- ## 定位
7
-
8
- - **TypeScript 工具库**:提供 DejaVu 前端开发所需的通用工具
9
- - **完整的工具集**:包含工具函数、错误处理、配置管理等
10
- - **核心功能**:
11
- - 通用工具函数(空值检查、延迟、重试等)
12
- - 类型定义(文件路径、环境变量等)
13
- - 错误处理系统
14
- - 事件发射器
15
- - 缓存系统
16
- - 配置管理
17
-
18
- ## 特性
19
-
20
- - 完全 TypeScript 编写,类型安全
21
- - 零运行时依赖
22
- - 模块化设计,按需导入
23
- - 提供丰富的工具函数
24
- - 完整的错误处理机制
25
- - 事件驱动架构支持
26
-
27
- ## 使用
28
-
29
- ### 安装
30
-
31
- ```bash
32
- pnpm add dejavu-tools
33
- ```
34
-
35
- ### 基本示例
36
-
37
- ```typescript
38
- import {
39
- isNil,
40
- isEmpty,
41
- delay,
42
- deepClone,
43
- uuidv4,
44
- EventEmitter,
45
- Cache,
46
- ErrorLevel,
47
- ToolException,
48
- createDefaultToolsConfig,
49
- ToolsConfigManager
50
- } from 'dejavu-tools';
51
-
52
- // 使用工具函数
53
- if (isNil(value)) {
54
- console.log('值为空');
55
- }
56
-
57
- // 使用事件发射器
58
- const emitter = new EventEmitter();
59
- emitter.on('data', (data) =&gt; console.log(data));
60
- emitter.emit('data', 'hello');
61
-
62
- // 使用缓存
63
- const cache = new Cache&lt;string, number&gt;(60000);
64
- cache.set('key', 123);
65
- console.log(cache.get('key'));
66
- ```
67
-
68
- ### 工具函数列表
69
-
70
- #### 空值检查
71
- - `isNil` - 检查值是否为 null 或 undefined
72
- - `isNotNil` - 检查值不为 null 或 undefined
73
- - `isEmpty` - 检查值是否为空
74
- - `isNotEmpty` - 检查值不为空
75
-
76
- #### 异步工具
77
- - `delay` - 延迟执行
78
- - `cancellableDelay` - 可取消的延迟
79
- - `retry` - 重试操作
80
- - `debounce` - 防抖函数
81
- - `throttle` - 节流函数
82
- - `memoize` - 记忆化函数
83
-
84
- #### 对象工具
85
- - `deepClone` - 深拷贝
86
- - `deepEqual` - 深比较
87
- - `safeJsonParse` - 安全的 JSON 解析
88
- - `safeJsonStringify` - 安全的 JSON 序列化
89
-
90
- #### 字符串工具
91
- - `uniqueId` - 生成唯一 ID
92
- - `uuidv4` - 生成 UUID v4
93
- - `capitalize` - 首字母大写
94
- - `camelCase` - 驼峰命名
95
- - `kebabCase` - 短横线命名
96
- - `snakeCase` - 下划线命名
97
- - `truncate` - 截断字符串
98
-
99
- #### 数组工具
100
- - `unique` - 数组去重
101
- - `chunk` - 数组分块
102
-
103
- ## 模块结构
104
-
105
- - `types` - 类型定义
106
- - `errors` - 错误处理
107
- - `utils` - 工具函数
108
- - `options` - 配置和选项
109
-
110
- ## 依赖关系
111
-
112
- dejavu-tools (TypeScript) 是一个独立的前端工具库,无外部依赖。