@done-coding/cli-template 0.1.0 → 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 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 c from "node:fs";
4
+ import n from "node:fs";
5
5
  import o from "chalk";
6
- import E from "lodash.template";
7
- import x from "prompts";
8
- const $ = ({
6
+ import b from "lodash.template";
7
+ import k from "prompts";
8
+ const d = ({
9
9
  filePath: r,
10
- dataInit: t,
11
- json: u,
12
- filePathKey: l,
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 (u && !r.endsWith(".json"))
17
+ if (i && !r.endsWith(".json"))
17
18
  return console.log(
18
- o.red(`${l}必须是json文件,请检查文件后缀名`)
19
+ o.red(`${s}必须是json文件,请检查文件后缀名`)
19
20
  ), process.exit(1);
20
- const s = c.readFileSync(f.resolve(r), "utf-8");
21
- return u ? JSON.parse(s) : s;
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 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)
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
- }, y = (r, t) => {
28
- if (t && r === t)
34
+ }, v = (r, l) => {
35
+ if (l && r === l)
29
36
  return console.log(o.red("output与input不能相同")), process.exit(1);
30
- }, N = (r, t) => {
31
- if (!t)
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: t,
36
- envJson: u,
37
- input: l,
38
- inputTemplate: g,
39
- output: s,
40
- mode: n,
41
- rollback: a
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 (a)
44
- switch (n) {
51
+ if (c)
52
+ switch (t) {
45
53
  case p.REPLACE:
46
54
  case p.RETURN: {
47
- console.log(o.red(`${n}模式不支持回滚`));
55
+ console.log(o.red(`${t}模式不支持回滚`));
48
56
  return;
49
57
  }
50
58
  }
51
59
  console.log(
52
60
  o.blue(`开始处理模板
53
- mode: ${n}`)
61
+ mode: ${t}
62
+ rollback: ${c}
63
+ `)
54
64
  );
55
- const v = $({
56
- filePath: u,
57
- dataInit: t,
65
+ const S = d({
66
+ filePath: i,
67
+ dataInit: l,
58
68
  json: !0,
59
- filePathKey: "envJson",
60
- dataInitKey: "envData"
61
- }), w = $({
62
- filePath: l,
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: "inputTemplate"
67
- }), i = E(w)(v);
68
- switch (n) {
77
+ dataInitKey: "inputData",
78
+ dealMarkdown: m
79
+ }), a = b(w)(S);
80
+ switch (t) {
69
81
  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({
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: `${n}模式下回滚将删除${e},是否继续?`
89
+ message: `${t}模式下回滚将删除${e},是否继续?`
78
90
  })).remove) {
79
- c.rmSync(e, { force: !0 }), console.log(o.green(`${n}模式下${e}已删除`));
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
- return;
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
- c.writeFileSync(e, i, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${e}`));
107
+ n.writeFileSync(e, a, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${e}`));
90
108
  break;
91
109
  }
92
110
  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(
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
- m.replace(i, ""),
118
+ $.replace(a, ""),
101
119
  "utf-8"
102
- ), console.log(o.green(`${n}模式下${e}回滚完成`));
120
+ ), console.log(o.green(`${t}模式下${e}回滚完成`));
103
121
  return;
104
122
  }
105
- const S = m + i;
106
- c.writeFileSync(e, S, "utf-8"), console.log(o.green(`模板处理完成,追加到 ${e}`));
123
+ const E = $ + a;
124
+ n.writeFileSync(e, E, "utf-8"), console.log(o.green(`模板处理完成,追加到 ${e}`));
107
125
  } else {
108
- if (a) {
126
+ if (c) {
109
127
  console.log(
110
- o.yellow(`${n}模式下${e}不存在,无需回滚`)
128
+ o.yellow(`${t}模式下${e}不存在,无需回滚`)
111
129
  );
112
130
  return;
113
131
  }
114
- console.log(o.blue(`output:${e} 不存在,将创建`)), c.writeFileSync(e, i, "utf-8"), console.log(o.green(`模板处理完成,输出到 ${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 (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}`));
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
- ), i;
146
+ ), a;
129
147
  default:
130
- return console.log(o.red(`mode ${n} 不支持`)), process.exit(1);
148
+ return console.log(o.red(`mode ${t} 不支持`)), process.exit(1);
131
149
  }
132
- return i;
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 m } from "./main.mjs";
3
+ import { command as n } from "./main.mjs";
4
+ import { OutputModeEnum as d } from "./utils/types.mjs";
4
5
  export {
5
- m as command,
6
+ d as OutputModeEnum,
7
+ n as command,
6
8
  e as handler
7
9
  };
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  const e = {
3
3
  name: "@done-coding/cli-template",
4
- version: "0.1.0",
4
+ version: "0.2.1",
5
5
  description: "预编译命令行工具"
6
6
  };
7
7
  export {
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 s } from "./utils/types.mjs";
5
- import { handler as i } from "./handler.mjs";
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 n from "./injectInfo.json.mjs";
8
- const m = () => ({
9
- envJson: {
10
- alias: "j",
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: "e",
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
- inputTemplate: {
25
- alias: "t",
26
- describe: "模板文件内容",
24
+ inputData: {
25
+ alias: "I",
26
+ describe: "模板数据",
27
27
  type: "string"
28
28
  },
29
29
  mode: {
@@ -31,40 +31,45 @@ const m = () => ({
31
31
  describe: "输出模式",
32
32
  type: "string",
33
33
  choices: [
34
- s.OVERWRITE,
35
- s.APPEND,
36
- s.REPLACE,
37
- s.RETURN
34
+ t.OVERWRITE,
35
+ t.APPEND,
36
+ t.REPLACE,
37
+ t.RETURN
38
38
  ],
39
- default: s.OVERWRITE
39
+ default: t.OVERWRITE
40
40
  },
41
41
  output: {
42
42
  alias: "o",
43
43
  describe: "输出文件路径",
44
- type: "string",
45
- demandOption: !0
44
+ type: "string"
46
45
  },
47
46
  rollback: {
48
47
  alias: "r",
49
48
  describe: "是否回滚",
50
49
  type: "boolean",
51
50
  default: !1
51
+ },
52
+ dealMarkdown: {
53
+ alias: "d",
54
+ describe: "(检测是markdown)是否处理(单个)代码块包裹",
55
+ type: "boolean",
56
+ default: !1
52
57
  }
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
+ }), 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;
58
63
  }, b = (e) => r(e, u), C = {
59
- command: a,
64
+ command: n,
60
65
  describe: g,
61
66
  builder: b,
62
- handler: i
63
- }, N = async () => {
64
- const e = l(p(process.argv)), t = await r(e, f);
65
- return i(t);
67
+ handler: a
68
+ }, D = async () => {
69
+ const e = l(p(process.argv)), s = await r(e, f);
70
+ return a(s);
66
71
  };
67
72
  export {
68
73
  C as command,
69
- N as createCli
74
+ D as createCli
70
75
  };
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@done-coding/cli-template",
3
- "version": "0.1.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.0",
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": "caa230835cb90705a00646511ae437a13439b267"
67
+ "gitHead": "e2dc7c4b0fa74aa83dc6173416aaffe0a0a445ad"
67
68
  }
package/types/cli.d.ts CHANGED
File without changes
File without changes
package/types/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { handler } from "./handler";
2
2
  export { command } from "./main";
3
+ export * from './utils/types';
@@ -1,6 +1,6 @@
1
1
  declare const _default: {
2
2
  "name": "@done-coding/cli-template",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "预编译命令行工具"
5
5
  };
6
6
 
package/types/main.d.ts CHANGED
File without changes
File without changes
@@ -10,18 +10,20 @@ export declare enum OutputModeEnum {
10
10
  RETURN = "return"
11
11
  }
12
12
  export interface Options {
13
- /** 环境数据文件JSON文件相对路径(优先级高于envData) */
14
- envJson: string;
13
+ /** 环境数据(json)文件(优先级高于 envData ) */
14
+ env?: string;
15
15
  /** 环境变量数据(JSON字符串) */
16
- envData: string;
17
- /** 模板文件相对路径(优先级高于inputTemplate) */
18
- input: string;
19
- /** 模板文件内容 */
20
- inputTemplate: string;
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: boolean;
25
+ /** 是否回滚 @default false */
26
+ rollback?: boolean;
27
+ /** (检测是markdown)是否处理(单个)代码块包裹 */
28
+ dealMarkdown?: boolean;
27
29
  }