@done-coding/cli-template 0.1.1 → 0.2.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/README.md +0 -0
- package/es/cli.mjs +0 -0
- package/es/handler.mjs +90 -72
- package/es/index.mjs +4 -2
- package/es/injectInfo.json.mjs +1 -1
- package/es/main.mjs +32 -26
- package/es/utils/types.mjs +0 -0
- package/package.json +5 -4
- package/types/cli.d.ts +0 -0
- package/types/handler.d.ts +0 -0
- package/types/index.d.ts +1 -0
- package/types/injectInfo.json.d.ts +1 -1
- package/types/main.d.ts +0 -0
- package/types/utils/index.d.ts +0 -0
- package/types/utils/types.d.ts +12 -10
package/README.md
CHANGED
|
File without changes
|
package/es/cli.mjs
CHANGED
|
File without changes
|
package/es/handler.mjs
CHANGED
|
@@ -1,135 +1,153 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { OutputModeEnum as p } from "./utils/types.mjs";
|
|
3
3
|
import f from "node:path";
|
|
4
|
-
import
|
|
4
|
+
import n from "node:fs";
|
|
5
5
|
import o from "chalk";
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
const
|
|
6
|
+
import b from "lodash.template";
|
|
7
|
+
import k from "prompts";
|
|
8
|
+
const d = ({
|
|
9
9
|
filePath: r,
|
|
10
|
-
dataInit:
|
|
11
|
-
json:
|
|
12
|
-
filePathKey:
|
|
13
|
-
dataInitKey: g
|
|
10
|
+
dataInit: l,
|
|
11
|
+
json: i,
|
|
12
|
+
filePathKey: s,
|
|
13
|
+
dataInitKey: g,
|
|
14
|
+
dealMarkdown: u = !1
|
|
14
15
|
}) => {
|
|
15
16
|
if (r) {
|
|
16
|
-
if (
|
|
17
|
+
if (i && !r.endsWith(".json"))
|
|
17
18
|
return console.log(
|
|
18
|
-
o.red(`${
|
|
19
|
+
o.red(`${s}必须是json文件,请检查文件后缀名`)
|
|
19
20
|
), process.exit(1);
|
|
20
|
-
const
|
|
21
|
-
|
|
21
|
+
const t = n.readFileSync(f.resolve(r), "utf-8");
|
|
22
|
+
let c = t;
|
|
23
|
+
return u && r.endsWith(".md") && (c = t.replace(
|
|
24
|
+
/^\s*```[a-zA-Z0-9]+\s*[\r\n]+([\s\S]+?)```\s*$/,
|
|
25
|
+
"$1"
|
|
26
|
+
)), i ? JSON.parse(c) : c;
|
|
22
27
|
} else
|
|
23
|
-
return
|
|
24
|
-
}
|
|
25
|
-
|
|
28
|
+
return l ? (console.log(
|
|
29
|
+
o.green(`${s} 为空,将使用${g}作为数据`)
|
|
30
|
+
), i ? JSON.parse(l) : l) : (console.log(o.red(`${s}与${g}不能同时为空`)), process.exit(1));
|
|
31
|
+
}, y = (r, l) => {
|
|
32
|
+
if (!l)
|
|
26
33
|
return console.log(o.red(`${r}模式下output不能为空`)), process.exit(1);
|
|
27
|
-
},
|
|
28
|
-
if (
|
|
34
|
+
}, v = (r, l) => {
|
|
35
|
+
if (l && r === l)
|
|
29
36
|
return console.log(o.red("output与input不能相同")), process.exit(1);
|
|
30
|
-
},
|
|
31
|
-
if (!
|
|
37
|
+
}, x = (r, l) => {
|
|
38
|
+
if (!l)
|
|
32
39
|
return console.log(o.red(`${r}模式下input不能为空`)), process.exit(1);
|
|
33
40
|
}, O = async (r) => {
|
|
34
41
|
const {
|
|
35
|
-
envData:
|
|
36
|
-
|
|
37
|
-
input:
|
|
38
|
-
|
|
39
|
-
output:
|
|
40
|
-
mode:
|
|
41
|
-
rollback:
|
|
42
|
+
envData: l,
|
|
43
|
+
env: i,
|
|
44
|
+
input: s,
|
|
45
|
+
inputData: g,
|
|
46
|
+
output: u,
|
|
47
|
+
mode: t = p.OVERWRITE,
|
|
48
|
+
rollback: c = !1,
|
|
49
|
+
dealMarkdown: m = !1
|
|
42
50
|
} = r;
|
|
43
|
-
if (
|
|
44
|
-
switch (
|
|
51
|
+
if (c)
|
|
52
|
+
switch (t) {
|
|
45
53
|
case p.REPLACE:
|
|
46
54
|
case p.RETURN: {
|
|
47
|
-
console.log(o.red(`${
|
|
55
|
+
console.log(o.red(`${t}模式不支持回滚`));
|
|
48
56
|
return;
|
|
49
57
|
}
|
|
50
58
|
}
|
|
51
59
|
console.log(
|
|
52
60
|
o.blue(`开始处理模板
|
|
53
|
-
mode: ${
|
|
61
|
+
mode: ${t}
|
|
62
|
+
rollback: ${c}
|
|
63
|
+
`)
|
|
54
64
|
);
|
|
55
|
-
const
|
|
56
|
-
filePath:
|
|
57
|
-
dataInit:
|
|
65
|
+
const S = d({
|
|
66
|
+
filePath: i,
|
|
67
|
+
dataInit: l,
|
|
58
68
|
json: !0,
|
|
59
|
-
filePathKey: "
|
|
60
|
-
dataInitKey: "envData"
|
|
61
|
-
|
|
62
|
-
|
|
69
|
+
filePathKey: "env",
|
|
70
|
+
dataInitKey: "envData",
|
|
71
|
+
dealMarkdown: m
|
|
72
|
+
}), w = d({
|
|
73
|
+
filePath: s,
|
|
63
74
|
dataInit: g,
|
|
64
75
|
json: !1,
|
|
65
76
|
filePathKey: "input",
|
|
66
|
-
dataInitKey: "
|
|
67
|
-
|
|
68
|
-
|
|
77
|
+
dataInitKey: "inputData",
|
|
78
|
+
dealMarkdown: m
|
|
79
|
+
}), a = b(w)(S);
|
|
80
|
+
switch (t) {
|
|
69
81
|
case p.OVERWRITE: {
|
|
70
|
-
|
|
71
|
-
const e = f.resolve(
|
|
72
|
-
if (
|
|
73
|
-
if (c
|
|
74
|
-
if ((await
|
|
82
|
+
y(t, u), v(u, s);
|
|
83
|
+
const e = f.resolve(u);
|
|
84
|
+
if (n.mkdirSync(f.dirname(e), { recursive: !0 }), n.existsSync(e)) {
|
|
85
|
+
if (c)
|
|
86
|
+
if ((await k({
|
|
75
87
|
type: "confirm",
|
|
76
88
|
name: "remove",
|
|
77
|
-
message: `${
|
|
89
|
+
message: `${t}模式下回滚将删除${e},是否继续?`
|
|
78
90
|
})).remove) {
|
|
79
|
-
|
|
91
|
+
n.rmSync(e, { force: !0 }), console.log(o.green(`${t}模式下${e}已删除`));
|
|
80
92
|
return;
|
|
81
93
|
} else {
|
|
82
|
-
console.log(
|
|
83
|
-
o.yellow(`${n}模式下${e}不存在,无需回滚`)
|
|
84
|
-
);
|
|
94
|
+
console.log(o.yellow(`${t}模式下${e}回滚取消`));
|
|
85
95
|
return;
|
|
86
96
|
}
|
|
87
|
-
|
|
97
|
+
console.log(o.blue(`output:${e} 已存在,将覆盖`));
|
|
98
|
+
} else {
|
|
99
|
+
if (c) {
|
|
100
|
+
console.log(
|
|
101
|
+
o.yellow(`${t}模式下${e}不存在,无需回滚`)
|
|
102
|
+
);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
console.log(o.blue(`output:${e} 不存在,将创建`));
|
|
88
106
|
}
|
|
89
|
-
|
|
107
|
+
n.writeFileSync(e, a, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${e}`));
|
|
90
108
|
break;
|
|
91
109
|
}
|
|
92
110
|
case p.APPEND: {
|
|
93
|
-
|
|
94
|
-
const e = f.resolve(
|
|
95
|
-
if (
|
|
96
|
-
const
|
|
97
|
-
if (
|
|
98
|
-
|
|
111
|
+
y(t, u), v(u, s);
|
|
112
|
+
const e = f.resolve(u);
|
|
113
|
+
if (n.mkdirSync(f.dirname(e), { recursive: !0 }), n.existsSync(e)) {
|
|
114
|
+
const $ = n.readFileSync(e, "utf-8");
|
|
115
|
+
if (c) {
|
|
116
|
+
n.writeFileSync(
|
|
99
117
|
e,
|
|
100
|
-
|
|
118
|
+
$.replace(a, ""),
|
|
101
119
|
"utf-8"
|
|
102
|
-
), console.log(o.green(`${
|
|
120
|
+
), console.log(o.green(`${t}模式下${e}回滚完成`));
|
|
103
121
|
return;
|
|
104
122
|
}
|
|
105
|
-
const
|
|
106
|
-
|
|
123
|
+
const E = $ + a;
|
|
124
|
+
n.writeFileSync(e, E, "utf-8"), console.log(o.green(`模板处理完成,追加到 ${e}`));
|
|
107
125
|
} else {
|
|
108
|
-
if (
|
|
126
|
+
if (c) {
|
|
109
127
|
console.log(
|
|
110
|
-
o.yellow(`${
|
|
128
|
+
o.yellow(`${t}模式下${e}不存在,无需回滚`)
|
|
111
129
|
);
|
|
112
130
|
return;
|
|
113
131
|
}
|
|
114
|
-
console.log(o.blue(`output:${e} 不存在,将创建`)),
|
|
132
|
+
console.log(o.blue(`output:${e} 不存在,将创建`)), n.writeFileSync(e, a, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${e}`));
|
|
115
133
|
}
|
|
116
134
|
break;
|
|
117
135
|
}
|
|
118
136
|
case p.REPLACE: {
|
|
119
|
-
if (
|
|
120
|
-
return console.log(o.red("
|
|
121
|
-
const e = f.resolve(
|
|
122
|
-
|
|
137
|
+
if (u && console.log(o.yellow(`output ${u} 将被忽略`)), x(t, s), i && i === s)
|
|
138
|
+
return console.log(o.red("env 与 input 不能相同")), process.exit(1);
|
|
139
|
+
const e = f.resolve(s);
|
|
140
|
+
n.mkdirSync(f.dirname(e), { recursive: !0 }), n.writeFileSync(e, a, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${e}`));
|
|
123
141
|
break;
|
|
124
142
|
}
|
|
125
143
|
case p.RETURN:
|
|
126
144
|
return console.log(
|
|
127
145
|
o.green("模板处理完成,返回结果(函数调用才会拿到返回值)")
|
|
128
|
-
),
|
|
146
|
+
), a;
|
|
129
147
|
default:
|
|
130
|
-
return console.log(o.red(`mode ${
|
|
148
|
+
return console.log(o.red(`mode ${t} 不支持`)), process.exit(1);
|
|
131
149
|
}
|
|
132
|
-
return
|
|
150
|
+
return a;
|
|
133
151
|
};
|
|
134
152
|
export {
|
|
135
153
|
O as handler
|
package/es/index.mjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { handler as e } from "./handler.mjs";
|
|
3
|
-
import { command as
|
|
3
|
+
import { command as n } from "./main.mjs";
|
|
4
|
+
import { OutputModeEnum as d } from "./utils/types.mjs";
|
|
4
5
|
export {
|
|
5
|
-
|
|
6
|
+
d as OutputModeEnum,
|
|
7
|
+
n as command,
|
|
6
8
|
e as handler
|
|
7
9
|
};
|
package/es/injectInfo.json.mjs
CHANGED
package/es/main.mjs
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import l from "yargs";
|
|
3
3
|
import { hideBin as p } from "yargs/helpers";
|
|
4
|
-
import { OutputModeEnum as
|
|
5
|
-
import { handler as
|
|
4
|
+
import { OutputModeEnum as t } from "./utils/types.mjs";
|
|
5
|
+
import { handler as a } from "./handler.mjs";
|
|
6
6
|
import o from "chalk";
|
|
7
|
-
import
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
alias: "
|
|
7
|
+
import i from "./injectInfo.json.mjs";
|
|
8
|
+
const d = () => ({
|
|
9
|
+
env: {
|
|
10
|
+
alias: "e",
|
|
11
11
|
describe: "环境数据文件JSON文件相对路径(优先级高于envData)",
|
|
12
12
|
type: "string"
|
|
13
13
|
},
|
|
14
14
|
envData: {
|
|
15
|
-
alias: "
|
|
15
|
+
alias: "E",
|
|
16
16
|
describe: "环境变量数据(JSON字符串)",
|
|
17
17
|
type: "string"
|
|
18
18
|
},
|
|
@@ -21,9 +21,9 @@ const m = () => ({
|
|
|
21
21
|
describe: "模板文件相对路径(优先级高于inputTemplate)",
|
|
22
22
|
type: "string"
|
|
23
23
|
},
|
|
24
|
-
|
|
25
|
-
alias: "
|
|
26
|
-
describe: "
|
|
24
|
+
inputData: {
|
|
25
|
+
alias: "I",
|
|
26
|
+
describe: "模板数据",
|
|
27
27
|
type: "string"
|
|
28
28
|
},
|
|
29
29
|
mode: {
|
|
@@ -31,12 +31,12 @@ const m = () => ({
|
|
|
31
31
|
describe: "输出模式",
|
|
32
32
|
type: "string",
|
|
33
33
|
choices: [
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
t.OVERWRITE,
|
|
35
|
+
t.APPEND,
|
|
36
|
+
t.REPLACE,
|
|
37
|
+
t.RETURN
|
|
38
38
|
],
|
|
39
|
-
default:
|
|
39
|
+
default: t.OVERWRITE
|
|
40
40
|
},
|
|
41
41
|
output: {
|
|
42
42
|
alias: "o",
|
|
@@ -48,22 +48,28 @@ const m = () => ({
|
|
|
48
48
|
describe: "是否回滚",
|
|
49
49
|
type: "boolean",
|
|
50
50
|
default: !1
|
|
51
|
+
},
|
|
52
|
+
dealMarkdown: {
|
|
53
|
+
alias: "d",
|
|
54
|
+
describe: "(检测是markdown)是否处理(单个)代码块包裹",
|
|
55
|
+
type: "boolean",
|
|
56
|
+
default: !1
|
|
51
57
|
}
|
|
52
|
-
}),
|
|
53
|
-
console.log(e ? o.red(e) : o.red(
|
|
54
|
-
}, g =
|
|
55
|
-
const c =
|
|
56
|
-
return e.strict().usage(
|
|
58
|
+
}), n = "template", m = (e, s) => {
|
|
59
|
+
console.log(e ? o.red(e) : o.red(s.message)), process.exit(1);
|
|
60
|
+
}, g = i.description, u = `Usage: $0 ${n} [options]`, f = "Usage: $0 [options]", r = (e, s) => {
|
|
61
|
+
const c = d();
|
|
62
|
+
return e.strict().usage(s).help("help").version(i.version).alias("v", "version").alias("h", "help").options(c).fail(m).argv;
|
|
57
63
|
}, b = (e) => r(e, u), C = {
|
|
58
|
-
command:
|
|
64
|
+
command: n,
|
|
59
65
|
describe: g,
|
|
60
66
|
builder: b,
|
|
61
|
-
handler:
|
|
62
|
-
},
|
|
63
|
-
const e = l(p(process.argv)),
|
|
64
|
-
return
|
|
67
|
+
handler: a
|
|
68
|
+
}, D = async () => {
|
|
69
|
+
const e = l(p(process.argv)), s = await r(e, f);
|
|
70
|
+
return a(s);
|
|
65
71
|
};
|
|
66
72
|
export {
|
|
67
73
|
C as command,
|
|
68
|
-
|
|
74
|
+
D as createCli
|
|
69
75
|
};
|
package/es/utils/types.mjs
CHANGED
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@done-coding/cli-template",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "预编译命令行工具",
|
|
5
5
|
"private": false,
|
|
6
6
|
"module": "es/index.mjs",
|
|
@@ -34,13 +34,14 @@
|
|
|
34
34
|
"directory": "packages/template"
|
|
35
35
|
},
|
|
36
36
|
"publishConfig": {
|
|
37
|
-
"access": "public"
|
|
37
|
+
"access": "public",
|
|
38
|
+
"registry": "https://registry.npmjs.org/"
|
|
38
39
|
},
|
|
39
40
|
"author": "JustSoSu",
|
|
40
41
|
"license": "MIT",
|
|
41
42
|
"sideEffects": false,
|
|
42
43
|
"devDependencies": {
|
|
43
|
-
"@done-coding/cli-inject": "^0.2.
|
|
44
|
+
"@done-coding/cli-inject": "^0.2.2",
|
|
44
45
|
"@types/lodash.get": "^4.4.9",
|
|
45
46
|
"@types/lodash.set": "^4.3.9",
|
|
46
47
|
"@types/lodash.template": "^4.5.3",
|
|
@@ -63,5 +64,5 @@
|
|
|
63
64
|
"prompts": "^2.4.2",
|
|
64
65
|
"yargs": "^17.7.2"
|
|
65
66
|
},
|
|
66
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "e2dc7c4b0fa74aa83dc6173416aaffe0a0a445ad"
|
|
67
68
|
}
|
package/types/cli.d.ts
CHANGED
|
File without changes
|
package/types/handler.d.ts
CHANGED
|
File without changes
|
package/types/index.d.ts
CHANGED
package/types/main.d.ts
CHANGED
|
File without changes
|
package/types/utils/index.d.ts
CHANGED
|
File without changes
|
package/types/utils/types.d.ts
CHANGED
|
@@ -10,18 +10,20 @@ export declare enum OutputModeEnum {
|
|
|
10
10
|
RETURN = "return"
|
|
11
11
|
}
|
|
12
12
|
export interface Options {
|
|
13
|
-
/**
|
|
14
|
-
|
|
13
|
+
/** 环境数据(json)文件(优先级高于 envData ) */
|
|
14
|
+
env?: string;
|
|
15
15
|
/** 环境变量数据(JSON字符串) */
|
|
16
|
-
envData
|
|
17
|
-
/** 模板文件相对路径(优先级高于
|
|
18
|
-
input
|
|
19
|
-
/**
|
|
20
|
-
|
|
16
|
+
envData?: string;
|
|
17
|
+
/** 模板文件相对路径(优先级高于 inputData ) */
|
|
18
|
+
input?: string;
|
|
19
|
+
/** 模板数据 */
|
|
20
|
+
inputData?: string;
|
|
21
21
|
/** 输出文件相对路径 */
|
|
22
22
|
output: string;
|
|
23
|
-
/** 输出模式 */
|
|
23
|
+
/** 输出模式 @default OutputModeEnum.OVERWRITE */
|
|
24
24
|
mode: OutputModeEnum;
|
|
25
|
-
/** 是否回滚 */
|
|
26
|
-
rollback
|
|
25
|
+
/** 是否回滚 @default false */
|
|
26
|
+
rollback?: boolean;
|
|
27
|
+
/** (检测是markdown)是否处理(单个)代码块包裹 */
|
|
28
|
+
dealMarkdown?: boolean;
|
|
27
29
|
}
|