@done-coding/cli-template 0.1.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/README.md +7 -0
- package/es/cli.mjs +3 -0
- package/es/handler.mjs +136 -0
- package/es/index.mjs +7 -0
- package/es/injectInfo.json.mjs +9 -0
- package/es/main.mjs +70 -0
- package/es/utils/types.mjs +5 -0
- package/package.json +67 -0
- package/types/cli.d.ts +2 -0
- package/types/handler.d.ts +3 -0
- package/types/index.d.ts +2 -0
- package/types/injectInfo.json.d.ts +7 -0
- package/types/main.d.ts +4 -0
- package/types/utils/index.d.ts +1 -0
- package/types/utils/types.d.ts +27 -0
package/README.md
ADDED
package/es/cli.mjs
ADDED
package/es/handler.mjs
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { OutputModeEnum as p } from "./utils/types.mjs";
|
|
3
|
+
import f from "node:path";
|
|
4
|
+
import c from "node:fs";
|
|
5
|
+
import o from "chalk";
|
|
6
|
+
import E from "lodash.template";
|
|
7
|
+
import x from "prompts";
|
|
8
|
+
const $ = ({
|
|
9
|
+
filePath: r,
|
|
10
|
+
dataInit: t,
|
|
11
|
+
json: u,
|
|
12
|
+
filePathKey: l,
|
|
13
|
+
dataInitKey: g
|
|
14
|
+
}) => {
|
|
15
|
+
if (r) {
|
|
16
|
+
if (u && !r.endsWith(".json"))
|
|
17
|
+
return console.log(
|
|
18
|
+
o.red(`${l}必须是json文件,请检查文件后缀名`)
|
|
19
|
+
), process.exit(1);
|
|
20
|
+
const s = c.readFileSync(f.resolve(r), "utf-8");
|
|
21
|
+
return u ? JSON.parse(s) : s;
|
|
22
|
+
} else
|
|
23
|
+
return t ? (console.log(o.green(`${l} 为空,将使用${t}作为数据`)), u ? JSON.parse(t) : t) : (console.log(o.red(`${l}与${g}不能同时为空`)), process.exit(1));
|
|
24
|
+
}, d = (r, t) => {
|
|
25
|
+
if (!t)
|
|
26
|
+
return console.log(o.red(`${r}模式下output不能为空`)), process.exit(1);
|
|
27
|
+
}, y = (r, t) => {
|
|
28
|
+
if (t && r === t)
|
|
29
|
+
return console.log(o.red("output与input不能相同")), process.exit(1);
|
|
30
|
+
}, N = (r, t) => {
|
|
31
|
+
if (!t)
|
|
32
|
+
return console.log(o.red(`${r}模式下input不能为空`)), process.exit(1);
|
|
33
|
+
}, O = async (r) => {
|
|
34
|
+
const {
|
|
35
|
+
envData: t,
|
|
36
|
+
envJson: u,
|
|
37
|
+
input: l,
|
|
38
|
+
inputTemplate: g,
|
|
39
|
+
output: s,
|
|
40
|
+
mode: n,
|
|
41
|
+
rollback: a
|
|
42
|
+
} = r;
|
|
43
|
+
if (a)
|
|
44
|
+
switch (n) {
|
|
45
|
+
case p.REPLACE:
|
|
46
|
+
case p.RETURN: {
|
|
47
|
+
console.log(o.red(`${n}模式不支持回滚`));
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
console.log(
|
|
52
|
+
o.blue(`开始处理模板
|
|
53
|
+
mode: ${n}`)
|
|
54
|
+
);
|
|
55
|
+
const v = $({
|
|
56
|
+
filePath: u,
|
|
57
|
+
dataInit: t,
|
|
58
|
+
json: !0,
|
|
59
|
+
filePathKey: "envJson",
|
|
60
|
+
dataInitKey: "envData"
|
|
61
|
+
}), w = $({
|
|
62
|
+
filePath: l,
|
|
63
|
+
dataInit: g,
|
|
64
|
+
json: !1,
|
|
65
|
+
filePathKey: "input",
|
|
66
|
+
dataInitKey: "inputTemplate"
|
|
67
|
+
}), i = E(w)(v);
|
|
68
|
+
switch (n) {
|
|
69
|
+
case p.OVERWRITE: {
|
|
70
|
+
d(n, s), y(s, l);
|
|
71
|
+
const e = f.resolve(s);
|
|
72
|
+
if (a) {
|
|
73
|
+
if (c.existsSync(e))
|
|
74
|
+
if ((await x({
|
|
75
|
+
type: "confirm",
|
|
76
|
+
name: "remove",
|
|
77
|
+
message: `${n}模式下回滚将删除${e},是否继续?`
|
|
78
|
+
})).remove) {
|
|
79
|
+
c.rmSync(e, { force: !0 }), console.log(o.green(`${n}模式下${e}已删除`));
|
|
80
|
+
return;
|
|
81
|
+
} else {
|
|
82
|
+
console.log(
|
|
83
|
+
o.yellow(`${n}模式下${e}不存在,无需回滚`)
|
|
84
|
+
);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
c.writeFileSync(e, i, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${e}`));
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
case p.APPEND: {
|
|
93
|
+
d(n, s), y(s, l);
|
|
94
|
+
const e = f.resolve(s);
|
|
95
|
+
if (c.existsSync(e)) {
|
|
96
|
+
const m = c.readFileSync(e, "utf-8");
|
|
97
|
+
if (a) {
|
|
98
|
+
c.writeFileSync(
|
|
99
|
+
e,
|
|
100
|
+
m.replace(i, ""),
|
|
101
|
+
"utf-8"
|
|
102
|
+
), console.log(o.green(`${n}模式下${e}回滚完成`));
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const S = m + i;
|
|
106
|
+
c.writeFileSync(e, S, "utf-8"), console.log(o.green(`模板处理完成,追加到 ${e}`));
|
|
107
|
+
} else {
|
|
108
|
+
if (a) {
|
|
109
|
+
console.log(
|
|
110
|
+
o.yellow(`${n}模式下${e}不存在,无需回滚`)
|
|
111
|
+
);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
console.log(o.blue(`output:${e} 不存在,将创建`)), c.writeFileSync(e, i, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${e}`));
|
|
115
|
+
}
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
case p.REPLACE: {
|
|
119
|
+
if (s && console.log(o.yellow(`output ${s} 将被忽略`)), N(n, l), u && u === l)
|
|
120
|
+
return console.log(o.red("envJson与input不能相同")), process.exit(1);
|
|
121
|
+
const e = f.resolve(l);
|
|
122
|
+
c.writeFileSync(e, i, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${e}`));
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
case p.RETURN:
|
|
126
|
+
return console.log(
|
|
127
|
+
o.green("模板处理完成,返回结果(函数调用才会拿到返回值)")
|
|
128
|
+
), i;
|
|
129
|
+
default:
|
|
130
|
+
return console.log(o.red(`mode ${n} 不支持`)), process.exit(1);
|
|
131
|
+
}
|
|
132
|
+
return i;
|
|
133
|
+
};
|
|
134
|
+
export {
|
|
135
|
+
O as handler
|
|
136
|
+
};
|
package/es/index.mjs
ADDED
package/es/main.mjs
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import l from "yargs";
|
|
3
|
+
import { hideBin as p } from "yargs/helpers";
|
|
4
|
+
import { OutputModeEnum as s } from "./utils/types.mjs";
|
|
5
|
+
import { handler as i } from "./handler.mjs";
|
|
6
|
+
import o from "chalk";
|
|
7
|
+
import n from "./injectInfo.json.mjs";
|
|
8
|
+
const m = () => ({
|
|
9
|
+
envJson: {
|
|
10
|
+
alias: "j",
|
|
11
|
+
describe: "环境数据文件JSON文件相对路径(优先级高于envData)",
|
|
12
|
+
type: "string"
|
|
13
|
+
},
|
|
14
|
+
envData: {
|
|
15
|
+
alias: "e",
|
|
16
|
+
describe: "环境变量数据(JSON字符串)",
|
|
17
|
+
type: "string"
|
|
18
|
+
},
|
|
19
|
+
input: {
|
|
20
|
+
alias: "i",
|
|
21
|
+
describe: "模板文件相对路径(优先级高于inputTemplate)",
|
|
22
|
+
type: "string"
|
|
23
|
+
},
|
|
24
|
+
inputTemplate: {
|
|
25
|
+
alias: "t",
|
|
26
|
+
describe: "模板文件内容",
|
|
27
|
+
type: "string"
|
|
28
|
+
},
|
|
29
|
+
mode: {
|
|
30
|
+
alias: "m",
|
|
31
|
+
describe: "输出模式",
|
|
32
|
+
type: "string",
|
|
33
|
+
choices: [
|
|
34
|
+
s.OVERWRITE,
|
|
35
|
+
s.APPEND,
|
|
36
|
+
s.REPLACE,
|
|
37
|
+
s.RETURN
|
|
38
|
+
],
|
|
39
|
+
default: s.OVERWRITE
|
|
40
|
+
},
|
|
41
|
+
output: {
|
|
42
|
+
alias: "o",
|
|
43
|
+
describe: "输出文件路径",
|
|
44
|
+
type: "string",
|
|
45
|
+
demandOption: !0
|
|
46
|
+
},
|
|
47
|
+
rollback: {
|
|
48
|
+
alias: "r",
|
|
49
|
+
describe: "是否回滚",
|
|
50
|
+
type: "boolean",
|
|
51
|
+
default: !1
|
|
52
|
+
}
|
|
53
|
+
}), a = "template", d = (e, t) => {
|
|
54
|
+
console.log(e ? o.red(e) : o.red(t.message)), process.exit(1);
|
|
55
|
+
}, g = n.description, u = `Usage: $0 ${a} [options]`, f = "Usage: $0 [options]", r = (e, t) => {
|
|
56
|
+
const c = m();
|
|
57
|
+
return e.strict().usage(t).help("help").version(n.version).alias("v", "version").alias("h", "help").options(c).fail(d).argv;
|
|
58
|
+
}, b = (e) => r(e, u), C = {
|
|
59
|
+
command: a,
|
|
60
|
+
describe: g,
|
|
61
|
+
builder: b,
|
|
62
|
+
handler: i
|
|
63
|
+
}, N = async () => {
|
|
64
|
+
const e = l(p(process.argv)), t = await r(e, f);
|
|
65
|
+
return i(t);
|
|
66
|
+
};
|
|
67
|
+
export {
|
|
68
|
+
C as command,
|
|
69
|
+
N as createCli
|
|
70
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@done-coding/cli-template",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "预编译命令行工具",
|
|
5
|
+
"private": false,
|
|
6
|
+
"module": "es/index.mjs",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"types": "types/index.d.ts",
|
|
9
|
+
"bin": {
|
|
10
|
+
"dc-template": "es/cli.mjs"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./es/index.mjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"es",
|
|
19
|
+
"lib",
|
|
20
|
+
"types",
|
|
21
|
+
"gif"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"clean": "rimraf es lib types",
|
|
25
|
+
"predev": "pnpm run clean",
|
|
26
|
+
"dev": "vite build --watch",
|
|
27
|
+
"prebuild": "pnpm run clean",
|
|
28
|
+
"build": "vite build",
|
|
29
|
+
"prepack": "pnpm build"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://gitee.com/justsosu/done-coding-cli.git",
|
|
34
|
+
"directory": "packages/template"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"author": "JustSoSu",
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"sideEffects": false,
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@done-coding/cli-inject": "^0.2.0",
|
|
44
|
+
"@types/lodash.get": "^4.4.9",
|
|
45
|
+
"@types/lodash.set": "^4.3.9",
|
|
46
|
+
"@types/lodash.template": "^4.5.3",
|
|
47
|
+
"@types/node": "^20.0.0",
|
|
48
|
+
"@types/prompts": "^2.4.6",
|
|
49
|
+
"@types/yargs": "^17.0.28",
|
|
50
|
+
"rimraf": "^6.0.1",
|
|
51
|
+
"typescript": "^5.2.2",
|
|
52
|
+
"vite": "^4.4.11",
|
|
53
|
+
"vite-plugin-dts": "^3.6.0"
|
|
54
|
+
},
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=16.0.0"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"chalk": "^5.3.0",
|
|
60
|
+
"lodash.get": "^4.4.2",
|
|
61
|
+
"lodash.set": "^4.3.2",
|
|
62
|
+
"lodash.template": "^4.5.0",
|
|
63
|
+
"prompts": "^2.4.2",
|
|
64
|
+
"yargs": "^17.7.2"
|
|
65
|
+
},
|
|
66
|
+
"gitHead": "caa230835cb90705a00646511ae437a13439b267"
|
|
67
|
+
}
|
package/types/cli.d.ts
ADDED
package/types/index.d.ts
ADDED
package/types/main.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./types";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** 输出模式 */
|
|
2
|
+
export declare enum OutputModeEnum {
|
|
3
|
+
/** 覆盖模式 */
|
|
4
|
+
OVERWRITE = "overwrite",
|
|
5
|
+
/** 追加模式 */
|
|
6
|
+
APPEND = "append",
|
|
7
|
+
/** 替换模式 */
|
|
8
|
+
REPLACE = "replace",
|
|
9
|
+
/** 返回模式--函数调用方式可用 */
|
|
10
|
+
RETURN = "return"
|
|
11
|
+
}
|
|
12
|
+
export interface Options {
|
|
13
|
+
/** 环境数据文件JSON文件相对路径(优先级高于envData) */
|
|
14
|
+
envJson: string;
|
|
15
|
+
/** 环境变量数据(JSON字符串) */
|
|
16
|
+
envData: string;
|
|
17
|
+
/** 模板文件相对路径(优先级高于inputTemplate) */
|
|
18
|
+
input: string;
|
|
19
|
+
/** 模板文件内容 */
|
|
20
|
+
inputTemplate: string;
|
|
21
|
+
/** 输出文件相对路径 */
|
|
22
|
+
output: string;
|
|
23
|
+
/** 输出模式 */
|
|
24
|
+
mode: OutputModeEnum;
|
|
25
|
+
/** 是否回滚 */
|
|
26
|
+
rollback: boolean;
|
|
27
|
+
}
|