proto-update-tool 1.0.0 → 1.0.1
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/git.factory.d.ts +4 -4
- package/bin/git.factory.d.ts.map +1 -1
- package/bin/git.factory.js +162 -50
- package/bin/git.factory.js.map +1 -1
- package/bin/index.js +2 -9
- package/bin/index.js.map +1 -1
- package/bin/transform.d.ts +2 -2
- package/bin/transform.d.ts.map +1 -1
- package/bin/transform.js +1 -1
- package/bin/transform.js.map +1 -1
- package/package.json +1 -1
- package/src/git.factory.ts +176 -46
- package/src/index.ts +1 -9
- package/src/transform.ts +3 -3
package/bin/git.factory.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type RepoConfig = {
|
|
2
2
|
repository: string;
|
|
3
3
|
ref: string;
|
|
4
4
|
outDir: string;
|
|
5
|
-
fileFilter: string;
|
|
6
5
|
destPath: string;
|
|
7
|
-
}
|
|
8
|
-
export declare function
|
|
6
|
+
};
|
|
7
|
+
export declare function gitConfigFactory(configs: RepoConfig[]): Promise<void>;
|
|
8
|
+
export declare function gitFactory(configPath: string): Promise<void>;
|
|
9
9
|
//# sourceMappingURL=git.factory.d.ts.map
|
package/bin/git.factory.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"git.factory.d.ts","sourceRoot":"","sources":["../src/git.factory.ts"],"names":[],"mappings":"AAKA,MAAM,
|
|
1
|
+
{"version":3,"file":"git.factory.d.ts","sourceRoot":"","sources":["../src/git.factory.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,UAAU,GAAG;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;CACpB,CAAC;AA2IF,wBAAsB,gBAAgB,CAAC,OAAO,EAAE,UAAU,EAAE,iBAiC3D;AAED,wBAAsB,UAAU,CAAC,UAAU,EAAE,MAAM,iBAGlD"}
|
package/bin/git.factory.js
CHANGED
|
@@ -3,58 +3,170 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.gitConfigFactory = gitConfigFactory;
|
|
6
7
|
exports.gitFactory = gitFactory;
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const os_1 = __importDefault(require("os"));
|
|
10
|
+
const child_process_1 = require("child_process");
|
|
7
11
|
const fs_extra_1 = require("fs-extra");
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const
|
|
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
|
-
(0,
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
(0, fs_extra_1.copySync)((0, path_1.join)(tempPath, config.outDir), destPath, {
|
|
43
|
-
overwrite: true,
|
|
44
|
-
filter: (src) => {
|
|
45
|
-
if (src.startsWith('.git'))
|
|
46
|
-
return false;
|
|
47
|
-
if ((0, fs_extra_1.statSync)(src).isDirectory())
|
|
48
|
-
return true;
|
|
49
|
-
const regExp = new RegExp(eval(config.fileFilter));
|
|
50
|
-
if (regExp.test(src)) {
|
|
51
|
-
console.info(`copy ${src} to .${destPath.replace(process.cwd(), '')}`);
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
return false;
|
|
55
|
-
},
|
|
12
|
+
async function readConfig(filename = "proto.config.json") {
|
|
13
|
+
const cfgPath = path_1.default.resolve(process.cwd(), filename);
|
|
14
|
+
const raw = await (0, fs_extra_1.readFileSync)(cfgPath, "utf-8");
|
|
15
|
+
const data = JSON.parse(raw);
|
|
16
|
+
if (!Array.isArray(data)) {
|
|
17
|
+
throw new Error("格式错误:应为数组");
|
|
18
|
+
}
|
|
19
|
+
return data.map((x) => {
|
|
20
|
+
if (!x.repository || !x.ref || !x.outDir || !x.destPath) {
|
|
21
|
+
throw new Error("配置缺少必要字段 repository/ref/outDir/destPath");
|
|
22
|
+
}
|
|
23
|
+
return { repository: String(x.repository), ref: String(x.ref), outDir: String(x.outDir), destPath: String(x.destPath) };
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
function execCmd(cmd, args, cwd) {
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
const child = (0, child_process_1.spawn)(cmd, args, {
|
|
29
|
+
cwd,
|
|
30
|
+
stdio: "inherit",
|
|
31
|
+
});
|
|
32
|
+
child.on("error", reject);
|
|
33
|
+
child.on("exit", (code) => {
|
|
34
|
+
if (code === 0)
|
|
35
|
+
resolve();
|
|
36
|
+
else
|
|
37
|
+
reject(new Error(`${cmd} ${args.join(" ")} 退出码: ${code}`));
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function execCmdCapture(cmd, args, cwd) {
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
const child = (0, child_process_1.spawn)(cmd, args, {
|
|
44
|
+
cwd,
|
|
45
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
56
46
|
});
|
|
57
|
-
|
|
58
|
-
|
|
47
|
+
let stdout = "";
|
|
48
|
+
let stderr = "";
|
|
49
|
+
child.stdout.on("data", (d) => {
|
|
50
|
+
stdout += d.toString();
|
|
51
|
+
});
|
|
52
|
+
child.stderr.on("data", (d) => {
|
|
53
|
+
stderr += d.toString();
|
|
54
|
+
});
|
|
55
|
+
child.on("error", reject);
|
|
56
|
+
child.on("exit", (code) => {
|
|
57
|
+
resolve({ code: code ?? 0, stdout, stderr });
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
async function copyDirFilterTs(src, dest) {
|
|
62
|
+
const entries = await (0, fs_extra_1.readdir)(src, { withFileTypes: true });
|
|
63
|
+
await (0, fs_extra_1.mkdirSync)(dest, { recursive: true });
|
|
64
|
+
for (const entry of entries) {
|
|
65
|
+
const s = path_1.default.join(src, entry.name);
|
|
66
|
+
const d = path_1.default.join(dest, entry.name);
|
|
67
|
+
if (entry.isDirectory()) {
|
|
68
|
+
await copyDirFilterTs(s, d);
|
|
69
|
+
}
|
|
70
|
+
else if (entry.isFile() && entry.name.endsWith(".ts")) {
|
|
71
|
+
await (0, fs_extra_1.copyFile)(s, d);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async function processRepo(conf) {
|
|
76
|
+
const tmpBase = path_1.default.join(os_1.default.tmpdir(), '.proto-import-cache');
|
|
77
|
+
const repoDirName = conf.destPath.replace(/[^\w.-]/g, "_") + "_" + conf.ref.replace(/[^\w.-]/g, "_");
|
|
78
|
+
const cloneDir = path_1.default.join(tmpBase, repoDirName);
|
|
79
|
+
await (0, fs_extra_1.mkdirSync)(tmpBase, { recursive: true });
|
|
80
|
+
await (0, fs_extra_1.removeSync)(cloneDir);
|
|
81
|
+
console.log(`开始处理: ${conf.repository} @ ${conf.ref}`);
|
|
82
|
+
try {
|
|
83
|
+
await execCmd("git", ["clone", "--depth", "1", "--branch", conf.ref, conf.repository, cloneDir]);
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
console.warn(`直接克隆分支失败,回退到默认分支后切换: ${e.message}`);
|
|
87
|
+
await execCmd("git", ["clone", conf.repository, cloneDir]);
|
|
88
|
+
await execCmd("git", ["fetch", "--all"], cloneDir);
|
|
89
|
+
try {
|
|
90
|
+
await execCmd("git", ["checkout", conf.ref], cloneDir);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
await execCmd("git", ["checkout", `origin/${conf.ref}`], cloneDir);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const srcGen = path_1.default.join(cloneDir, conf.outDir);
|
|
97
|
+
const destRoot = path_1.default.resolve(process.cwd(), conf.destPath);
|
|
98
|
+
const destGen = path_1.default.join(destRoot, conf.outDir);
|
|
99
|
+
let last = undefined;
|
|
100
|
+
try {
|
|
101
|
+
if (!(0, fs_extra_1.statSync)(srcGen).isDirectory()) {
|
|
102
|
+
throw new Error(`源仓库中不存在 ${conf.outDir} 目录`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
throw new Error(`源仓库 ${conf.repository} 的 ${conf.ref} 未找到 ${conf.outDir} 目录`);
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
const info = await execCmdCapture("git", ["log", "-1", "--pretty=format:%H|||%an|||%ad|||%s", "--date=iso"], cloneDir);
|
|
110
|
+
const body = await execCmdCapture("git", ["log", "-1", "--pretty=format:%B"], cloneDir);
|
|
111
|
+
const parts = info.stdout.trim().split("|||");
|
|
112
|
+
if (parts.length >= 4) {
|
|
113
|
+
last = {
|
|
114
|
+
hash: parts[0] ?? "",
|
|
115
|
+
author: parts[1] ?? "",
|
|
116
|
+
date: parts[2] ?? "",
|
|
117
|
+
subject: parts[3] ?? "",
|
|
118
|
+
body: body.stdout ?? "",
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
}
|
|
124
|
+
await (0, fs_extra_1.mkdirSync)(destRoot, { recursive: true });
|
|
125
|
+
await (0, fs_extra_1.removeSync)(destGen);
|
|
126
|
+
await (0, fs_extra_1.mkdirSync)(destGen);
|
|
127
|
+
await copyDirFilterTs(srcGen, destGen);
|
|
128
|
+
console.log(`已拷贝 ${conf.repository} 的 ${conf.ref} 下 ${conf.outDir} 目录下 .ts 文件到 ${destGen}`);
|
|
129
|
+
await (0, fs_extra_1.removeSync)(cloneDir);
|
|
130
|
+
return { path: conf.repository, version: conf.ref, destGen, ...last ? { last } : {} };
|
|
131
|
+
}
|
|
132
|
+
async function gitConfigFactory(configs) {
|
|
133
|
+
const results = [];
|
|
134
|
+
for (const conf of configs) {
|
|
135
|
+
const r = await processRepo(conf);
|
|
136
|
+
results.push(r);
|
|
137
|
+
}
|
|
138
|
+
console.log("所有 Proto TS 更新完成");
|
|
139
|
+
await (0, fs_extra_1.removeSync)(path_1.default.join(os_1.default.tmpdir(), '.proto-import-cache'));
|
|
140
|
+
for (const r of results) {
|
|
141
|
+
await execCmd("git", ["add", r.destGen], process.cwd());
|
|
142
|
+
const staged = await execCmdCapture("git", ["diff", "--cached", "--name-only", "--", r.destGen], process.cwd());
|
|
143
|
+
if (staged.stdout.trim().length > 0) {
|
|
144
|
+
const msg1 = `更新${r.path}仓库协议版本${r.version}`;
|
|
145
|
+
const extras = [];
|
|
146
|
+
if (r.last) {
|
|
147
|
+
extras.push(`上游提交 ${r.last.hash}`);
|
|
148
|
+
extras.push(`作者: ${r.last.author} 日期: ${r.last.date}`);
|
|
149
|
+
extras.push(`内容: ${r.last.subject}`);
|
|
150
|
+
const bodyTrim = (r.last.body || "").trim();
|
|
151
|
+
if (bodyTrim.length) {
|
|
152
|
+
const bodyShort = bodyTrim.length > 1000 ? bodyTrim.slice(0, 1000) + "…" : bodyTrim;
|
|
153
|
+
extras.push(bodyShort);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const args = ["commit", "-m", msg1];
|
|
157
|
+
for (const e of extras)
|
|
158
|
+
args.push("-m", e);
|
|
159
|
+
args.push("--", r.destGen);
|
|
160
|
+
await execCmd("git", args, process.cwd());
|
|
161
|
+
console.log("已提交更新提交:", msg1);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
console.log(`无变更,跳过提交:${r.path}`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async function gitFactory(configPath) {
|
|
169
|
+
const configs = await readConfig(configPath);
|
|
170
|
+
return await gitConfigFactory(configs);
|
|
59
171
|
}
|
|
60
172
|
//# sourceMappingURL=git.factory.js.map
|
package/bin/git.factory.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"git.factory.js","sourceRoot":"","sources":["../src/git.factory.ts"],"names":[],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"git.factory.js","sourceRoot":"","sources":["../src/git.factory.ts"],"names":[],"mappings":";;;;;AAqJA,4CAiCC;AAED,gCAGC;AA3LD,gDAAwB;AACxB,4CAAoB;AACpB,iDAAsC;AACtC,uCAA4F;AAsB5F,KAAK,UAAU,UAAU,CAAC,WAAmB,mBAAmB;IAC5D,MAAM,OAAO,GAAG,cAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,QAAQ,CAAC,CAAC;IACtD,MAAM,GAAG,GAAG,MAAM,IAAA,uBAAY,EAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACjD,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAiB,CAAC;IAC7C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,WAAW,CAAC,CAAC;IACjC,CAAC;IACD,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;QAClB,IAAI,CAAC,CAAC,CAAC,UAAU,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC;YACtD,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;QAC/D,CAAC;QACD,OAAO,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC,EAAE,GAAG,EAA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,EAAE,CAAC;gBAC5C,IAAI,QAAQ,CAAC,MAAM,EAAE,CAAC;oBAClB,MAAM,SAAS,GAAG,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC;oBACpF,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;gBAC3B,CAAC;YACL,CAAC;YACD,MAAM,IAAI,GAAG,CAAC,QAAQ,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;YACpC,KAAK,MAAM,CAAC,IAAI,MAAM;gBAAE,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;YAC3C,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC;YAC3B,MAAM,OAAO,CAAC,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;YAC1C,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;QAClC,CAAC;aAAM,CAAC;YACJ,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;QACtC,CAAC;IACL,CAAC;AACL,CAAC;AAEM,KAAK,UAAU,UAAU,CAAC,UAAkB;IAC/C,MAAM,OAAO,GAAG,MAAM,UAAU,CAAC,UAAU,CAAC,CAAC;IAC7C,OAAO,MAAM,gBAAgB,CAAC,OAAO,CAAC,CAAC;AAC3C,CAAC"}
|
package/bin/index.js
CHANGED
|
@@ -3,9 +3,6 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
const commander_1 = require("commander");
|
|
5
5
|
const package_json_1 = require("../package.json");
|
|
6
|
-
const path_1 = require("path");
|
|
7
|
-
const fs_extra_1 = require("fs-extra");
|
|
8
|
-
const fs_1 = require("fs");
|
|
9
6
|
const git_factory_1 = require("./git.factory");
|
|
10
7
|
async function bootstrap() {
|
|
11
8
|
const program = new commander_1.Command();
|
|
@@ -21,12 +18,8 @@ async function bootstrap() {
|
|
|
21
18
|
process.exit(0);
|
|
22
19
|
}
|
|
23
20
|
const path = options.path || './proto.config.json';
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
throw new Error(`Proto 配置文件不存在: ${configPath}`);
|
|
27
|
-
}
|
|
28
|
-
const config = JSON.parse((0, fs_1.readFileSync)(configPath, 'utf-8'));
|
|
29
|
-
await (0, git_factory_1.gitFactory)(config);
|
|
21
|
+
if (path)
|
|
22
|
+
(0, git_factory_1.gitFactory)(path);
|
|
30
23
|
return true;
|
|
31
24
|
}
|
|
32
25
|
bootstrap();
|
package/bin/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAEA,yCAAoC;AACpC,kDAA0C;AAC1C,+
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAEA,yCAAoC;AACpC,kDAA0C;AAC1C,+CAA2C;AAE3C,KAAK,UAAU,SAAS;IACpB,MAAM,OAAO,GAAG,IAAI,mBAAO,EAAE,CAAC;IAE9B,OAAO,CAAC,OAAO,CAAC,sBAAO,CAAC;SACnB,IAAI,CAAC,UAAU,CAAC;SAChB,WAAW,CAAC,2BAA2B,CAAC;SACxC,MAAM,CAAC,YAAY,EAAE,QAAQ,CAAC;SAC9B,MAAM,CAAC,mBAAmB,EAAE,iBAAiB,EAAE,qBAAqB,CAAC;SACrE,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAEzB,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;IAC/B,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;QACf,OAAO,CAAC,UAAU,EAAE,CAAC;QACrB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACpB,CAAC;IACD,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,qBAAqB,CAAC;IACnD,IAAG,IAAI;QAAE,IAAA,wBAAU,EAAC,IAAI,CAAC,CAAC;IAC1B,OAAO,IAAI,CAAC;AAChB,CAAC;AAED,SAAS,EAAE,CAAC"}
|
package/bin/transform.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as ts from 'typescript';
|
|
2
|
-
import {
|
|
2
|
+
import { RepoConfig } from './git.factory';
|
|
3
3
|
export interface GitImportPluginOptions {
|
|
4
4
|
transform: string;
|
|
5
5
|
type: string;
|
|
6
|
-
options:
|
|
6
|
+
options: RepoConfig[];
|
|
7
7
|
}
|
|
8
8
|
export declare enum GitImportStatus {
|
|
9
9
|
Waiting = "waiting",
|
package/bin/transform.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../src/transform.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AACjC,OAAO,
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../src/transform.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AACjC,OAAO,EAAoB,UAAU,EAAE,MAAM,eAAe,CAAC;AAE7D,MAAM,WAAW,sBAAsB;IACnC,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,UAAU,EAAE,CAAC;CACzB;AACD,oBAAY,eAAe;IACvB,OAAO,YAAY;IACnB,OAAO,YAAY;IACnB,OAAO,YAAY;IACnB,MAAM,WAAW;CACpB;AAKD,MAAM,CAAC,OAAO,WAAW,OAAO,EAAE,EAAE,CAAC,OAAO,EAAE,EAAE,OAAO,EAAE,EAAE,sBAAsB;uBAUlD,EAAE,CAAC,qBAAqB,MAEvC,YAAY,EAAE,CAAC,UAAU;EAOxC"}
|
package/bin/transform.js
CHANGED
|
@@ -18,7 +18,7 @@ function default_1(program, { options }) {
|
|
|
18
18
|
if (buildStatus.status === GitImportStatus.Waiting) {
|
|
19
19
|
// console.log(111111, buildStatus, options);
|
|
20
20
|
buildStatus.status = GitImportStatus.Pending;
|
|
21
|
-
(0, git_factory_1.
|
|
21
|
+
(0, git_factory_1.gitConfigFactory)(options).then(() => {
|
|
22
22
|
buildStatus.status = GitImportStatus.Success;
|
|
23
23
|
}).catch(() => {
|
|
24
24
|
buildStatus.status = GitImportStatus.Failed;
|
package/bin/transform.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform.js","sourceRoot":"","sources":["../src/transform.ts"],"names":[],"mappings":";;;AAkBA,4BAmBC;AApCD,+
|
|
1
|
+
{"version":3,"file":"transform.js","sourceRoot":"","sources":["../src/transform.ts"],"names":[],"mappings":";;;AAkBA,4BAmBC;AApCD,+CAA6D;AAO7D,IAAY,eAKX;AALD,WAAY,eAAe;IACvB,sCAAmB,CAAA;IACnB,sCAAmB,CAAA;IACnB,sCAAmB,CAAA;IACnB,oCAAiB,CAAA;AACrB,CAAC,EALW,eAAe,+BAAf,eAAe,QAK1B;AACD,MAAM,WAAW,GAAG;IAChB,EAAE,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC;IAClD,MAAM,EAAE,eAAe,CAAC,OAAO;CAClC,CAAC;AACF,mBAAyB,OAAmB,EAAE,EAAE,OAAO,EAA0B;IAC7E,IAAI,WAAW,CAAC,MAAM,KAAK,eAAe,CAAC,OAAO,EAAE,CAAC;QACjD,6CAA6C;QAC7C,WAAW,CAAC,MAAM,GAAG,eAAe,CAAC,OAAO,CAAC;QAC7C,IAAA,8BAAgB,EAAC,OAAO,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;YAChC,WAAW,CAAC,MAAM,GAAG,eAAe,CAAC,OAAO,CAAC;QACjD,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE;YACV,WAAW,CAAC,MAAM,GAAG,eAAe,CAAC,MAAM,CAAC;QAChD,CAAC,CAAC,CAAC;IACP,CAAC;IACD,SAAS,QAAQ,CAAC,OAAiC;QAC/C,qBAAqB;QACrB,OAAO,CAAC,UAAyB,EAAE,EAAE;YACjC,0CAA0C;YAC1C,OAAO;YACP,OAAO,UAAU,CAAC;QACtB,CAAC,CAAC;IACN,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC;AAClC,CAAC"}
|
package/package.json
CHANGED
package/src/git.factory.ts
CHANGED
|
@@ -1,58 +1,188 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
1
|
+
import path from "path";
|
|
2
|
+
import os from "os";
|
|
3
|
+
import { spawn } from "child_process";
|
|
4
|
+
import { copyFile, mkdirSync, readdir, readFileSync, removeSync, statSync } from "fs-extra";
|
|
5
5
|
|
|
6
|
-
export
|
|
6
|
+
export type RepoConfig = {
|
|
7
7
|
repository: string;
|
|
8
8
|
ref: string;
|
|
9
9
|
outDir: string;
|
|
10
|
-
fileFilter: string;
|
|
11
10
|
destPath: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type RepoResult = {
|
|
14
|
+
path: string;
|
|
15
|
+
version: string;
|
|
16
|
+
destGen: string;
|
|
17
|
+
last?: {
|
|
18
|
+
hash: string;
|
|
19
|
+
author: string;
|
|
20
|
+
date: string;
|
|
21
|
+
subject: string;
|
|
22
|
+
body: string;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
async function readConfig(filename: string = "proto.config.json"): Promise<RepoConfig[]> {
|
|
27
|
+
const cfgPath = path.resolve(process.cwd(), filename);
|
|
28
|
+
const raw = await readFileSync(cfgPath, "utf-8");
|
|
29
|
+
const data = JSON.parse(raw) as RepoConfig[];
|
|
30
|
+
if (!Array.isArray(data)) {
|
|
31
|
+
throw new Error("格式错误:应为数组");
|
|
32
|
+
}
|
|
33
|
+
return data.map((x) => {
|
|
34
|
+
if (!x.repository || !x.ref || !x.outDir || !x.destPath) {
|
|
35
|
+
throw new Error("配置缺少必要字段 repository/ref/outDir/destPath");
|
|
36
|
+
}
|
|
37
|
+
return { repository: String(x.repository), ref: String(x.ref), outDir: String(x.outDir), destPath: String(x.destPath) };
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function execCmd(cmd: string, args: string[], cwd?: string): Promise<void> {
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
const child = spawn(cmd, args, {
|
|
44
|
+
cwd,
|
|
45
|
+
stdio: "inherit",
|
|
46
|
+
});
|
|
47
|
+
child.on("error", reject);
|
|
48
|
+
child.on("exit", (code) => {
|
|
49
|
+
if (code === 0) resolve();
|
|
50
|
+
else reject(new Error(`${cmd} ${args.join(" ")} 退出码: ${code}`));
|
|
51
|
+
});
|
|
52
|
+
});
|
|
12
53
|
}
|
|
13
54
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
55
|
+
function execCmdCapture(cmd: string, args: string[], cwd?: string): Promise<{ code: number; stdout: string; stderr: string }> {
|
|
56
|
+
return new Promise((resolve, reject) => {
|
|
57
|
+
const child = spawn(cmd, args, {
|
|
58
|
+
cwd,
|
|
59
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
60
|
+
});
|
|
61
|
+
let stdout = "";
|
|
62
|
+
let stderr = "";
|
|
63
|
+
child.stdout.on("data", (d) => {
|
|
64
|
+
stdout += d.toString();
|
|
65
|
+
});
|
|
66
|
+
child.stderr.on("data", (d) => {
|
|
67
|
+
stderr += d.toString();
|
|
68
|
+
});
|
|
69
|
+
child.on("error", reject);
|
|
70
|
+
child.on("exit", (code) => {
|
|
71
|
+
resolve({ code: code ?? 0, stdout, stderr });
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function copyDirFilterTs(src: string, dest: string) {
|
|
77
|
+
const entries = await readdir(src, { withFileTypes: true });
|
|
78
|
+
await mkdirSync(dest, { recursive: true });
|
|
79
|
+
for (const entry of entries) {
|
|
80
|
+
const s = path.join(src, entry.name);
|
|
81
|
+
const d = path.join(dest, entry.name);
|
|
82
|
+
if (entry.isDirectory()) {
|
|
83
|
+
await copyDirFilterTs(s, d);
|
|
84
|
+
} else if (entry.isFile() && entry.name.endsWith(".ts")) {
|
|
85
|
+
await copyFile(s, d);
|
|
22
86
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function processRepo(conf: RepoConfig): Promise<RepoResult> {
|
|
91
|
+
const tmpBase = path.join(os.tmpdir(), '.proto-import-cache');
|
|
92
|
+
const repoDirName = conf.destPath.replace(/[^\w.-]/g, "_") + "_" + conf.ref.replace(/[^\w.-]/g, "_");
|
|
93
|
+
const cloneDir = path.join(tmpBase, repoDirName);
|
|
94
|
+
|
|
95
|
+
await mkdirSync(tmpBase, { recursive: true });
|
|
96
|
+
await removeSync(cloneDir);
|
|
97
|
+
|
|
98
|
+
console.log(`开始处理: ${conf.repository} @ ${conf.ref}`);
|
|
99
|
+
try {
|
|
100
|
+
await execCmd("git", ["clone", "--depth", "1", "--branch", conf.ref, conf.repository, cloneDir]);
|
|
101
|
+
} catch (e) {
|
|
102
|
+
console.warn(`直接克隆分支失败,回退到默认分支后切换: ${(e as Error).message}`);
|
|
103
|
+
await execCmd("git", ["clone", conf.repository, cloneDir]);
|
|
104
|
+
await execCmd("git", ["fetch", "--all"], cloneDir);
|
|
105
|
+
try {
|
|
106
|
+
await execCmd("git", ["checkout", conf.ref], cloneDir);
|
|
107
|
+
} catch {
|
|
108
|
+
await execCmd("git", ["checkout", `origin/${conf.ref}`], cloneDir);
|
|
28
109
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const srcGen = path.join(cloneDir, conf.outDir);
|
|
113
|
+
const destRoot = path.resolve(process.cwd(), conf.destPath);
|
|
114
|
+
const destGen = path.join(destRoot, conf.outDir);
|
|
115
|
+
let last: RepoResult["last"] | undefined = undefined;
|
|
116
|
+
|
|
117
|
+
try {
|
|
118
|
+
if (!statSync(srcGen).isDirectory()) {
|
|
119
|
+
throw new Error(`源仓库中不存在 ${conf.outDir} 目录`);
|
|
32
120
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
121
|
+
} catch {
|
|
122
|
+
throw new Error(`源仓库 ${conf.repository} 的 ${conf.ref} 未找到 ${conf.outDir} 目录`);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
const info = await execCmdCapture("git", ["log", "-1", "--pretty=format:%H|||%an|||%ad|||%s", "--date=iso"], cloneDir);
|
|
127
|
+
const body = await execCmdCapture("git", ["log", "-1", "--pretty=format:%B"], cloneDir);
|
|
128
|
+
const parts = info.stdout.trim().split("|||");
|
|
129
|
+
if (parts.length >= 4) {
|
|
130
|
+
last = {
|
|
131
|
+
hash: parts[0] ?? "",
|
|
132
|
+
author: parts[1] ?? "",
|
|
133
|
+
date: parts[2] ?? "",
|
|
134
|
+
subject: parts[3] ?? "",
|
|
135
|
+
body: body.stdout ?? "",
|
|
136
|
+
};
|
|
38
137
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
138
|
+
} catch {
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
await mkdirSync(destRoot, { recursive: true });
|
|
142
|
+
await removeSync(destGen);
|
|
143
|
+
await mkdirSync(destGen);
|
|
144
|
+
await copyDirFilterTs(srcGen, destGen);
|
|
145
|
+
console.log(`已拷贝 ${conf.repository} 的 ${conf.ref} 下 ${conf.outDir} 目录下 .ts 文件到 ${destGen}`);
|
|
146
|
+
await removeSync(cloneDir);
|
|
147
|
+
return { path: conf.repository, version: conf.ref, destGen, ...last ? { last } : {} };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export async function gitConfigFactory(configs: RepoConfig[]) {
|
|
151
|
+
const results: RepoResult[] = [];
|
|
152
|
+
for (const conf of configs) {
|
|
153
|
+
const r = await processRepo(conf);
|
|
154
|
+
results.push(r);
|
|
155
|
+
}
|
|
156
|
+
console.log("所有 Proto TS 更新完成");
|
|
157
|
+
await removeSync(path.join(os.tmpdir(), '.proto-import-cache'));
|
|
158
|
+
for (const r of results) {
|
|
159
|
+
await execCmd("git", ["add", r.destGen], process.cwd());
|
|
160
|
+
const staged = await execCmdCapture("git", ["diff", "--cached", "--name-only", "--", r.destGen], process.cwd());
|
|
161
|
+
if (staged.stdout.trim().length > 0) {
|
|
162
|
+
const msg1 = `更新${r.path}仓库协议版本${r.version}`;
|
|
163
|
+
const extras: string[] = [];
|
|
164
|
+
if (r.last) {
|
|
165
|
+
extras.push(`上游提交 ${r.last.hash}`);
|
|
166
|
+
extras.push(`作者: ${r.last.author} 日期: ${r.last.date}`);
|
|
167
|
+
extras.push(`内容: ${r.last.subject}`);
|
|
168
|
+
const bodyTrim = (r.last.body || "").trim();
|
|
169
|
+
if (bodyTrim.length) {
|
|
170
|
+
const bodyShort = bodyTrim.length > 1000 ? bodyTrim.slice(0, 1000) + "…" : bodyTrim;
|
|
171
|
+
extras.push(bodyShort);
|
|
52
172
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
173
|
+
}
|
|
174
|
+
const args = ["commit", "-m", msg1];
|
|
175
|
+
for (const e of extras) args.push("-m", e);
|
|
176
|
+
args.push("--", r.destGen);
|
|
177
|
+
await execCmd("git", args, process.cwd());
|
|
178
|
+
console.log("已提交更新提交:", msg1);
|
|
179
|
+
} else {
|
|
180
|
+
console.log(`无变更,跳过提交:${r.path}`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export async function gitFactory(configPath: string) {
|
|
186
|
+
const configs = await readConfig(configPath);
|
|
187
|
+
return await gitConfigFactory(configs);
|
|
188
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -2,9 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
import { Command } from "commander";
|
|
4
4
|
import { version } from '../package.json';
|
|
5
|
-
import { join } from 'path';
|
|
6
|
-
import { existsSync } from "fs-extra";
|
|
7
|
-
import { readFileSync } from "fs";
|
|
8
5
|
import { gitFactory } from "./git.factory";
|
|
9
6
|
|
|
10
7
|
async function bootstrap() {
|
|
@@ -23,12 +20,7 @@ async function bootstrap() {
|
|
|
23
20
|
process.exit(0);
|
|
24
21
|
}
|
|
25
22
|
const path = options.path || './proto.config.json';
|
|
26
|
-
|
|
27
|
-
if (!existsSync(configPath)) {
|
|
28
|
-
throw new Error(`Proto 配置文件不存在: ${configPath}`);
|
|
29
|
-
}
|
|
30
|
-
const config = JSON.parse(readFileSync(configPath, 'utf-8'));
|
|
31
|
-
await gitFactory(config);
|
|
23
|
+
if(path) gitFactory(path);
|
|
32
24
|
return true;
|
|
33
25
|
}
|
|
34
26
|
|
package/src/transform.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as ts from 'typescript';
|
|
2
|
-
import {
|
|
2
|
+
import { gitConfigFactory, RepoConfig } from './git.factory';
|
|
3
3
|
|
|
4
4
|
export interface GitImportPluginOptions {
|
|
5
5
|
transform: string;
|
|
6
6
|
type: string;
|
|
7
|
-
options:
|
|
7
|
+
options: RepoConfig[];
|
|
8
8
|
}
|
|
9
9
|
export enum GitImportStatus {
|
|
10
10
|
Waiting = 'waiting',
|
|
@@ -20,7 +20,7 @@ export default function (program: ts.Program, { options }: GitImportPluginOption
|
|
|
20
20
|
if (buildStatus.status === GitImportStatus.Waiting) {
|
|
21
21
|
// console.log(111111, buildStatus, options);
|
|
22
22
|
buildStatus.status = GitImportStatus.Pending;
|
|
23
|
-
|
|
23
|
+
gitConfigFactory(options).then(() => {
|
|
24
24
|
buildStatus.status = GitImportStatus.Success;
|
|
25
25
|
}).catch(() => {
|
|
26
26
|
buildStatus.status = GitImportStatus.Failed;
|