@farris/cli 2.2.4 → 2.2.6
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/lib/commands/build-components.js +50 -50
- package/lib/commands/build-components.js.map +1 -1
- package/lib/commands/build-css.js +38 -38
- package/lib/commands/build-css.js.map +1 -1
- package/lib/commands/build-lib.js +25 -25
- package/lib/commands/build.js +14 -14
- package/lib/commands/build.js.map +1 -1
- package/lib/commands/create-app-options.js +74 -74
- package/lib/commands/create-app-options.js.map +1 -1
- package/lib/commands/create-app.js +126 -126
- package/lib/commands/create-app.js.map +1 -1
- package/lib/commands/create-options.js +29 -29
- package/lib/commands/create-options.js.map +1 -1
- package/lib/commands/create.js +224 -202
- package/lib/commands/create.js.map +1 -1
- package/lib/commands/dev-serve.js +18 -18
- package/lib/commands/extract-i18n.js +74 -74
- package/lib/commands/extract-i18n.js.map +1 -1
- package/lib/commands/generate/api.js +124 -124
- package/lib/commands/generate/api.js.map +1 -1
- package/lib/commands/generate/common.js +100 -100
- package/lib/commands/generate/common.js.map +1 -1
- package/lib/commands/generate/component.js +156 -156
- package/lib/commands/generate/component.js.map +1 -1
- package/lib/commands/generate/view.js +168 -168
- package/lib/commands/generate/view.js.map +1 -1
- package/lib/commands/preview-serve.js +16 -16
- package/lib/commands/workspace/add-app.js +180 -180
- package/lib/commands/workspace/add-app.js.map +1 -1
- package/lib/commands/workspace/create.js +379 -379
- package/lib/commands/workspace/create.js.map +1 -1
- package/lib/commands/workspace/inspect.js +30 -30
- package/lib/commands/workspace/inspect.js.map +1 -1
- package/lib/commands/workspace/verify.js +38 -38
- package/lib/commands/workspace/verify.js.map +1 -1
- package/lib/common/apply-catalog-deps.js +15 -15
- package/lib/common/apply-catalog-deps.js.map +1 -1
- package/lib/common/component-file-name.js +3 -3
- package/lib/common/constant.js +64 -64
- package/lib/common/constant.js.map +1 -1
- package/lib/common/create-app-target.js +46 -46
- package/lib/common/create-app-target.js.map +1 -1
- package/lib/common/generate-app.js +85 -85
- package/lib/common/generate-app.js.map +1 -1
- package/lib/common/get-dependencies.js +9 -9
- package/lib/common/get-farris-config.js +25 -25
- package/lib/common/get-farris-config.js.map +1 -1
- package/lib/common/get-version.js +6 -6
- package/lib/common/get-vite-config.js +106 -106
- package/lib/common/get-vite-config.js.map +1 -1
- package/lib/common/lib-package-exports.js +24 -24
- package/lib/common/lib-package-exports.js.map +1 -1
- package/lib/common/list-lib-components.js +23 -23
- package/lib/common/list-lib-components.js.map +1 -1
- package/lib/common/output.js +94 -94
- package/lib/common/output.js.map +1 -1
- package/lib/common/project-package-name.js +4 -4
- package/lib/common/safety.js +171 -171
- package/lib/common/safety.js.map +1 -1
- package/lib/common/template-manifest.js +16 -16
- package/lib/common/template-manifest.js.map +1 -1
- package/lib/common/template-renderer.js +114 -114
- package/lib/common/template-renderer.js.map +1 -1
- package/lib/common/transaction.js +83 -83
- package/lib/common/transaction.js.map +1 -1
- package/lib/common/verifier.js +150 -150
- package/lib/common/verifier.js.map +1 -1
- package/lib/config/vite-app.js +13 -13
- package/lib/config/vite-common.js +20 -20
- package/lib/config/vite-common.js.map +1 -1
- package/lib/config/vite-component.js +32 -32
- package/lib/config/vite-component.js.map +1 -1
- package/lib/config/vite-lib.js +26 -26
- package/lib/index.js +205 -205
- package/lib/index.js.map +1 -1
- package/lib/plugins/create-component-style.js +44 -44
- package/lib/plugins/create-package-json.js +48 -48
- package/lib/plugins/create-package-json.js.map +1 -1
- package/lib/plugins/dts.js +8 -8
- package/lib/plugins/html-system.js +9 -9
- package/lib/plugins/replace.js +17 -17
- package/lib/plugins/systemjs-bundle.js +15 -15
- package/lib/plugins/systemjs-bundle.js.map +1 -1
- package/package.json +1 -1
- package/templates/create/.npmrc.tpl +1 -0
- package/templates/lib/package.json +1 -1
- package/templates/mobile/package.json +1 -1
- package/templates/mobile/project.json +1 -1
- package/templates/web/package.json +1 -1
- package/templates/web/project.json +1 -1
|
@@ -1,115 +1,115 @@
|
|
|
1
|
-
import { existsSync, readdirSync, statSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
2
|
-
import { join, dirname, relative } from 'node:path';
|
|
3
|
-
/**
|
|
4
|
-
* 将变量替换为模板内容
|
|
5
|
-
*/
|
|
6
|
-
export function renderTemplateString(content, variables) {
|
|
7
|
-
let result = content;
|
|
8
|
-
Object.keys(variables).forEach((key) => {
|
|
9
|
-
const value = variables[key];
|
|
10
|
-
const regexp = new RegExp(`<%=\\s*${key}\\s*%>`, 'g');
|
|
11
|
-
let strValue = '';
|
|
12
|
-
if (typeof value === 'object' && value !== null) {
|
|
13
|
-
if (Array.isArray(value)) {
|
|
14
|
-
strValue = JSON.stringify(value);
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
strValue = JSON.stringify(value, null, 2);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
strValue = String(value ?? '');
|
|
22
|
-
}
|
|
23
|
-
result = result.replace(regexp, () => strValue);
|
|
24
|
-
});
|
|
25
|
-
return result;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* 检测文件内容中未被替换的 <%= xxx %> 占位符
|
|
29
|
-
*/
|
|
30
|
-
export function findUnreplacedPlaceholders(content) {
|
|
31
|
-
const matches = content.match(/<%=\s*[\w.-]+\s*%>/g);
|
|
32
|
-
return matches ? Array.from(new Set(matches)) : [];
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* 仅枚举模板渲染后会写入的相对路径,不产生任何文件系统写入。
|
|
36
|
-
*/
|
|
37
|
-
export function collectTemplateOutputPaths(sourceDir, filter) {
|
|
38
|
-
const outputs = [];
|
|
39
|
-
function walk(currentSrc) {
|
|
40
|
-
if (!existsSync(currentSrc)) {
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
for (const entry of readdirSync(currentSrc)) {
|
|
44
|
-
const srcPath = join(currentSrc, entry);
|
|
45
|
-
const relativePath = relative(sourceDir, srcPath).replace(/\\/g, '/');
|
|
46
|
-
if (filter && !filter(relativePath)) {
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
if (statSync(srcPath).isDirectory()) {
|
|
50
|
-
walk(srcPath);
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
outputs.push(relativePath.endsWith('.tpl') ? relativePath.slice(0, -4) : relativePath);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
walk(sourceDir);
|
|
58
|
-
return outputs;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* 递归渲染模板目录到目标目录
|
|
62
|
-
*/
|
|
63
|
-
export function renderTemplateDir(options) {
|
|
64
|
-
const { sourceDir, targetDir, variables, skipExisting = false, filter } = options;
|
|
65
|
-
const filesCreated = [];
|
|
66
|
-
const unreplaced = [];
|
|
67
|
-
function walk(currentSrc, currentTarget) {
|
|
68
|
-
if (!existsSync(currentSrc)) {
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
const entries = readdirSync(currentSrc);
|
|
72
|
-
for (const entry of entries) {
|
|
73
|
-
const srcPath = join(currentSrc, entry);
|
|
74
|
-
const stat = statSync(srcPath);
|
|
75
|
-
const relPath = relative(sourceDir, srcPath).replace(/\\/g, '/');
|
|
76
|
-
if (filter && !filter(relPath)) {
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
if (stat.isDirectory()) {
|
|
80
|
-
const nextTarget = join(currentTarget, entry);
|
|
81
|
-
walk(srcPath, nextTarget);
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
// 去掉 .tpl 后缀
|
|
85
|
-
let targetFileName = entry;
|
|
86
|
-
if (targetFileName.endsWith('.tpl')) {
|
|
87
|
-
targetFileName = targetFileName.slice(0, -4);
|
|
88
|
-
}
|
|
89
|
-
const targetFilePath = join(currentTarget, targetFileName);
|
|
90
|
-
if (existsSync(targetFilePath) && skipExisting) {
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
const rawContent = readFileSync(srcPath, 'utf-8');
|
|
94
|
-
const rendered = renderTemplateString(rawContent, variables);
|
|
95
|
-
const leftovers = findUnreplacedPlaceholders(rendered);
|
|
96
|
-
if (leftovers.length > 0) {
|
|
97
|
-
leftovers.forEach((p) => {
|
|
98
|
-
unreplaced.push({ file: targetFilePath, placeholder: p });
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
if (!existsSync(dirname(targetFilePath))) {
|
|
102
|
-
mkdirSync(dirname(targetFilePath), { recursive: true });
|
|
103
|
-
}
|
|
104
|
-
writeFileSync(targetFilePath, rendered, 'utf-8');
|
|
105
|
-
filesCreated.push(targetFilePath);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
walk(sourceDir, targetDir);
|
|
110
|
-
return {
|
|
111
|
-
filesCreated,
|
|
112
|
-
unreplacedPlaceholders: unreplaced
|
|
113
|
-
};
|
|
114
|
-
}
|
|
1
|
+
import { existsSync, readdirSync, statSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
2
|
+
import { join, dirname, relative } from 'node:path';
|
|
3
|
+
/**
|
|
4
|
+
* 将变量替换为模板内容
|
|
5
|
+
*/
|
|
6
|
+
export function renderTemplateString(content, variables) {
|
|
7
|
+
let result = content;
|
|
8
|
+
Object.keys(variables).forEach((key) => {
|
|
9
|
+
const value = variables[key];
|
|
10
|
+
const regexp = new RegExp(`<%=\\s*${key}\\s*%>`, 'g');
|
|
11
|
+
let strValue = '';
|
|
12
|
+
if (typeof value === 'object' && value !== null) {
|
|
13
|
+
if (Array.isArray(value)) {
|
|
14
|
+
strValue = JSON.stringify(value);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
strValue = JSON.stringify(value, null, 2);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
strValue = String(value ?? '');
|
|
22
|
+
}
|
|
23
|
+
result = result.replace(regexp, () => strValue);
|
|
24
|
+
});
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 检测文件内容中未被替换的 <%= xxx %> 占位符
|
|
29
|
+
*/
|
|
30
|
+
export function findUnreplacedPlaceholders(content) {
|
|
31
|
+
const matches = content.match(/<%=\s*[\w.-]+\s*%>/g);
|
|
32
|
+
return matches ? Array.from(new Set(matches)) : [];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* 仅枚举模板渲染后会写入的相对路径,不产生任何文件系统写入。
|
|
36
|
+
*/
|
|
37
|
+
export function collectTemplateOutputPaths(sourceDir, filter) {
|
|
38
|
+
const outputs = [];
|
|
39
|
+
function walk(currentSrc) {
|
|
40
|
+
if (!existsSync(currentSrc)) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
for (const entry of readdirSync(currentSrc)) {
|
|
44
|
+
const srcPath = join(currentSrc, entry);
|
|
45
|
+
const relativePath = relative(sourceDir, srcPath).replace(/\\/g, '/');
|
|
46
|
+
if (filter && !filter(relativePath)) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (statSync(srcPath).isDirectory()) {
|
|
50
|
+
walk(srcPath);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
outputs.push(relativePath.endsWith('.tpl') ? relativePath.slice(0, -4) : relativePath);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
walk(sourceDir);
|
|
58
|
+
return outputs;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 递归渲染模板目录到目标目录
|
|
62
|
+
*/
|
|
63
|
+
export function renderTemplateDir(options) {
|
|
64
|
+
const { sourceDir, targetDir, variables, skipExisting = false, filter } = options;
|
|
65
|
+
const filesCreated = [];
|
|
66
|
+
const unreplaced = [];
|
|
67
|
+
function walk(currentSrc, currentTarget) {
|
|
68
|
+
if (!existsSync(currentSrc)) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const entries = readdirSync(currentSrc);
|
|
72
|
+
for (const entry of entries) {
|
|
73
|
+
const srcPath = join(currentSrc, entry);
|
|
74
|
+
const stat = statSync(srcPath);
|
|
75
|
+
const relPath = relative(sourceDir, srcPath).replace(/\\/g, '/');
|
|
76
|
+
if (filter && !filter(relPath)) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (stat.isDirectory()) {
|
|
80
|
+
const nextTarget = join(currentTarget, entry);
|
|
81
|
+
walk(srcPath, nextTarget);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
// 去掉 .tpl 后缀
|
|
85
|
+
let targetFileName = entry;
|
|
86
|
+
if (targetFileName.endsWith('.tpl')) {
|
|
87
|
+
targetFileName = targetFileName.slice(0, -4);
|
|
88
|
+
}
|
|
89
|
+
const targetFilePath = join(currentTarget, targetFileName);
|
|
90
|
+
if (existsSync(targetFilePath) && skipExisting) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const rawContent = readFileSync(srcPath, 'utf-8');
|
|
94
|
+
const rendered = renderTemplateString(rawContent, variables);
|
|
95
|
+
const leftovers = findUnreplacedPlaceholders(rendered);
|
|
96
|
+
if (leftovers.length > 0) {
|
|
97
|
+
leftovers.forEach((p) => {
|
|
98
|
+
unreplaced.push({ file: targetFilePath, placeholder: p });
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
if (!existsSync(dirname(targetFilePath))) {
|
|
102
|
+
mkdirSync(dirname(targetFilePath), { recursive: true });
|
|
103
|
+
}
|
|
104
|
+
writeFileSync(targetFilePath, rendered, 'utf-8');
|
|
105
|
+
filesCreated.push(targetFilePath);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
walk(sourceDir, targetDir);
|
|
110
|
+
return {
|
|
111
|
+
filesCreated,
|
|
112
|
+
unreplacedPlaceholders: unreplaced
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
115
|
//# sourceMappingURL=template-renderer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"template-renderer.js","sourceRoot":"","sources":["../../src/common/template-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,YAAY,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpG,OAAO,EAAW,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAe7D;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAe,EAAE,SAA8B;IAClF,IAAI,MAAM,GAAG,OAAO,CAAC;IACrB,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QACrC,MAAM,KAAK,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;QAC7B,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,UAAU,GAAG,QAAQ,EAAE,GAAG,CAAC,CAAC;QACtD,IAAI,QAAQ,GAAG,EAAE,CAAC;QAClB,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE
|
|
1
|
+
{"version":3,"file":"template-renderer.js","sourceRoot":"","sources":["../../src/common/template-renderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,YAAY,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpG,OAAO,EAAW,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAe7D;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAe,EAAE,SAA8B;IAClF,IAAI,MAAM,GAAG,OAAO,CAAC;IACrB,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QACrC,MAAM,KAAK,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;QAC7B,MAAM,MAAM,GA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;gBACjD,YAAY,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;aACnC;SACF;IACH,CAAC;IAED,IAAI,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;IAE3B,OAAO;QACL,YAAY;QACZ,sBAAsB,EAAE,UAAU;KACnC,CAAC;AACJ,CAAC"}
|
|
@@ -1,84 +1,84 @@
|
|
|
1
|
-
import { existsSync, readFileSync, writeFileSync, unlinkSync, rmdirSync, readdirSync } from 'node:fs';
|
|
2
|
-
export class Transaction {
|
|
3
|
-
constructor() {
|
|
4
|
-
this.createdFiles = new Set();
|
|
5
|
-
this.modifiedFiles = new Map(); // path -> original content
|
|
6
|
-
this.createdDirs = new Set();
|
|
7
|
-
this.committed = false;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* 记录新建目录
|
|
11
|
-
*/
|
|
12
|
-
recordCreatedDir(dirPath) {
|
|
13
|
-
if (!this.createdDirs.has(dirPath)) {
|
|
14
|
-
this.createdDirs.add(dirPath);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* 记录即将写入/修改的文件
|
|
19
|
-
*/
|
|
20
|
-
prepareWrite(filePath) {
|
|
21
|
-
if (this.createdFiles.has(filePath) || this.modifiedFiles.has(filePath)) {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
if (existsSync(filePath)) {
|
|
25
|
-
const original = readFileSync(filePath, 'utf-8');
|
|
26
|
-
this.modifiedFiles.set(filePath, original);
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
this.createdFiles.add(filePath);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* 提交事务
|
|
34
|
-
*/
|
|
35
|
-
commit() {
|
|
36
|
-
this.committed = true;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* 回滚事务
|
|
40
|
-
*/
|
|
41
|
-
rollback() {
|
|
42
|
-
if (this.committed) {
|
|
43
|
-
return { rolledBackFiles: [], errors: [] };
|
|
44
|
-
}
|
|
45
|
-
const rolledBackFiles = [];
|
|
46
|
-
const errors = [];
|
|
47
|
-
// 恢复修改的文件
|
|
48
|
-
for (const [filePath, originalContent] of this.modifiedFiles.entries()) {
|
|
49
|
-
try {
|
|
50
|
-
writeFileSync(filePath, originalContent, 'utf-8');
|
|
51
|
-
rolledBackFiles.push(filePath);
|
|
52
|
-
}
|
|
53
|
-
catch (err) {
|
|
54
|
-
errors.push(`恢复文件失败: ${filePath}, 原因: ${err.message}`);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
// 删除新建的文件
|
|
58
|
-
for (const filePath of this.createdFiles) {
|
|
59
|
-
try {
|
|
60
|
-
if (existsSync(filePath)) {
|
|
61
|
-
unlinkSync(filePath);
|
|
62
|
-
rolledBackFiles.push(filePath);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
catch (err) {
|
|
66
|
-
errors.push(`删除新建文件失败: ${filePath}, 原因: ${err.message}`);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
// 尝试清理新建的空目录(由深到浅排序)
|
|
70
|
-
const sortedDirs = Array.from(this.createdDirs).sort((a, b) => b.length - a.length);
|
|
71
|
-
for (const dir of sortedDirs) {
|
|
72
|
-
try {
|
|
73
|
-
if (existsSync(dir) && readdirSync(dir).length === 0) {
|
|
74
|
-
rmdirSync(dir);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
catch {
|
|
78
|
-
// 忽略非空目录清理异常
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
return { rolledBackFiles, errors };
|
|
82
|
-
}
|
|
83
|
-
}
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync, unlinkSync, rmdirSync, readdirSync } from 'node:fs';
|
|
2
|
+
export class Transaction {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.createdFiles = new Set();
|
|
5
|
+
this.modifiedFiles = new Map(); // path -> original content
|
|
6
|
+
this.createdDirs = new Set();
|
|
7
|
+
this.committed = false;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* 记录新建目录
|
|
11
|
+
*/
|
|
12
|
+
recordCreatedDir(dirPath) {
|
|
13
|
+
if (!this.createdDirs.has(dirPath)) {
|
|
14
|
+
this.createdDirs.add(dirPath);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* 记录即将写入/修改的文件
|
|
19
|
+
*/
|
|
20
|
+
prepareWrite(filePath) {
|
|
21
|
+
if (this.createdFiles.has(filePath) || this.modifiedFiles.has(filePath)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (existsSync(filePath)) {
|
|
25
|
+
const original = readFileSync(filePath, 'utf-8');
|
|
26
|
+
this.modifiedFiles.set(filePath, original);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
this.createdFiles.add(filePath);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 提交事务
|
|
34
|
+
*/
|
|
35
|
+
commit() {
|
|
36
|
+
this.committed = true;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 回滚事务
|
|
40
|
+
*/
|
|
41
|
+
rollback() {
|
|
42
|
+
if (this.committed) {
|
|
43
|
+
return { rolledBackFiles: [], errors: [] };
|
|
44
|
+
}
|
|
45
|
+
const rolledBackFiles = [];
|
|
46
|
+
const errors = [];
|
|
47
|
+
// 恢复修改的文件
|
|
48
|
+
for (const [filePath, originalContent] of this.modifiedFiles.entries()) {
|
|
49
|
+
try {
|
|
50
|
+
writeFileSync(filePath, originalContent, 'utf-8');
|
|
51
|
+
rolledBackFiles.push(filePath);
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
errors.push(`恢复文件失败: ${filePath}, 原因: ${err.message}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// 删除新建的文件
|
|
58
|
+
for (const filePath of this.createdFiles) {
|
|
59
|
+
try {
|
|
60
|
+
if (existsSync(filePath)) {
|
|
61
|
+
unlinkSync(filePath);
|
|
62
|
+
rolledBackFiles.push(filePath);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
errors.push(`删除新建文件失败: ${filePath}, 原因: ${err.message}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// 尝试清理新建的空目录(由深到浅排序)
|
|
70
|
+
const sortedDirs = Array.from(this.createdDirs).sort((a, b) => b.length - a.length);
|
|
71
|
+
for (const dir of sortedDirs) {
|
|
72
|
+
try {
|
|
73
|
+
if (existsSync(dir) && readdirSync(dir).length === 0) {
|
|
74
|
+
rmdirSync(dir);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
// 忽略非空目录清理异常
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return { rolledBackFiles, errors };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
84
|
//# sourceMappingURL=transaction.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transaction.js","sourceRoot":"","sources":["../../src/common/transaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAQtG,MAAM,OAAO,WAAW;IAAxB;QACU,iBAAY,GAAgB,IAAI,GAAG,EAAE,CAAC;QACtC,kBAAa,GAAwB,IAAI,GAAG,EAAE,CAAC,CAAC,2BAA2B;QAC3E,gBAAW,GAAgB,IAAI,GAAG,EAAE,CAAC;QACrC,cAAS,GAAY,KAAK,CAAC;IAiFrC,CAAC;IA/EC;;OAEG;IACI,gBAAgB,CAAC,OAAe;QACrC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE
|
|
1
|
+
{"version":3,"file":"transaction.js","sourceRoot":"","sources":["../../src/common/transaction.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