create-done-coding 0.9.0 → 0.9.2-alpha.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/es/cli.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { c as r } from "./index-3ddda083.js";
2
+ import { c as r } from "./index-44fde806.js";
3
3
  import "chalk";
4
4
  import "node:child_process";
5
5
  import "node:path";
@@ -0,0 +1,200 @@
1
+ #!/usr/bin/env node
2
+ import o from "chalk";
3
+ import { execSync as g } from "node:child_process";
4
+ import p, { resolve as R } from "node:path";
5
+ import f, { existsSync as y, rmSync as h } from "node:fs";
6
+ import a from "prompts";
7
+ import { onPromptFormStateForSigint as l, lookForParentTarget as D } from "@done-coding/node-tools";
8
+ import { getConfigPath as M, batchHandler as _ } from "@done-coding/cli-template";
9
+ import { getTargetRepoUrl as O } from "@done-coding/cli-git";
10
+ import L from "yargs";
11
+ import { hideBin as I } from "yargs/helpers";
12
+ const d = {
13
+ version: "0.9.2-alpha.0",
14
+ name: "create-done-coding",
15
+ description: "项目创建命令行工具",
16
+ cliConfig: {
17
+ namespaceDir: ".done-coding",
18
+ moduleName: "create"
19
+ }
20
+ }, j = "git@gitee.com:justsosu/done-coding-cli-config-json.git", C = "自定义模版路径", S = "某个git平台用户仓库", U = `./.${d.name}_temp`;
21
+ let $;
22
+ const A = async () => ($ || ($ = (await q()).templateList), $), x = async () => [
23
+ ...await A(),
24
+ { name: C },
25
+ { name: S }
26
+ ], H = {
27
+ type: "text",
28
+ name: "projectName",
29
+ message: "请输入项目名称",
30
+ format: (e) => e.trim(),
31
+ validate: (e) => e.length > 0 || "项目名称不能为空",
32
+ onState: l
33
+ }, k = async () => ({
34
+ type: "select",
35
+ name: "template",
36
+ message: "请选择模板",
37
+ choices: (await x()).map((t) => {
38
+ var i;
39
+ return {
40
+ title: t.branch ? `${t.name}(${t.branch})` : t.name,
41
+ value: t.name,
42
+ description: `${t.description || ""}${(i = t.instances) != null && i.length ? `, 已应用于: ${[""].concat(t.instances).join(`
43
+ - `)}` : ""}` || void 0
44
+ };
45
+ }),
46
+ validate: (t) => t.trim().length > 0 || "模板不能为空",
47
+ onState: l
48
+ }), B = {
49
+ type: "confirm",
50
+ name: "saveGitHistory",
51
+ message: "是否保留git历史",
52
+ initial: !1,
53
+ onState: l
54
+ }, b = (e = "项目已存在,是否删除") => ({
55
+ type: "confirm",
56
+ name: "isRemove",
57
+ message: e,
58
+ onState: l
59
+ }), Y = {
60
+ type: "text",
61
+ name: "customUrl",
62
+ message: "请输入自定义模板路径",
63
+ validate: (e) => e.trim().length > 0 || "路径不能为空",
64
+ onState: l
65
+ }, J = (e) => ({
66
+ type: "text",
67
+ name: "gitCommitMessage",
68
+ message: "请输入git提交信息",
69
+ initial: `feat: 初始化项目${e}`,
70
+ validate: (t) => t.trim().length > 0 || "提交信息不能为空",
71
+ onState: l
72
+ }), q = async () => {
73
+ console.log(o.blue("拉取模板列表,请稍等..."));
74
+ const e = p.resolve(
75
+ process.cwd(),
76
+ U
77
+ );
78
+ if (y(e))
79
+ return console.log(o.red(`${e} 已存在,请手动删除该目录再试`)), process.exit(1);
80
+ if (console.log(o.blue(`配置临时目录:${e}`)), f.existsSync(e)) {
81
+ const i = `${e}目录已存在,是否删除?`, { isRemove: r } = await a(b(i));
82
+ if (r)
83
+ f.rmSync(e, { recursive: !0, force: !0 });
84
+ else
85
+ return console.log(o.red(`${e}已存在,请手动删除后再试!`)), process.exit(1);
86
+ }
87
+ let t;
88
+ try {
89
+ g(`git clone ${j} ${e} --depth=1`);
90
+ const i = d.name, r = p.resolve(e, `${i}.json`);
91
+ if (t = JSON.parse(f.readFileSync(r, "utf-8")), !Array.isArray(t.templateList)) {
92
+ const n = `远程配置文件出错, templateList 不是数组, 请检查 ${j} ${i}.json`;
93
+ throw new Error(n);
94
+ }
95
+ console.log(o.green("模板列表拉取成功!"));
96
+ } finally {
97
+ f.rmSync(e, { recursive: !0, force: !0 });
98
+ }
99
+ return t;
100
+ }, E = async (e) => {
101
+ const { projectName: t } = e, r = ((t ?? (await a(H)).projectName) || "").trim();
102
+ if (!r)
103
+ return console.log(o.red("项目名称不能为空")), process.exit(1);
104
+ if (r.includes(" ") || r.includes("\\") || r.includes("/"))
105
+ return console.log(o.red(`项目名称\`${r}\`不能包含空格或者\\或者/`)), process.exit(1);
106
+ const n = R(process.cwd(), r);
107
+ if (y(n)) {
108
+ const { isRemove: s } = await a(b());
109
+ if (s === !0)
110
+ h(n, { recursive: !0, force: !0 });
111
+ else
112
+ return console.log(o.red("项目已存在")), process.exit(1);
113
+ }
114
+ const { template: m } = await a(await k());
115
+ let u = "", v = "";
116
+ if (m === C) {
117
+ const { customUrl: s } = await a(Y);
118
+ u = s;
119
+ } else if (m === S)
120
+ u = await O();
121
+ else {
122
+ const s = (await x()).find(
123
+ (c) => c.name === m
124
+ );
125
+ if (!s)
126
+ return console.log(o.red(`模板${m}不存在`)), process.exit(1);
127
+ if (!s.url)
128
+ return console.log(o.red(`模板${m}仓库地址不存在`)), process.exit(1);
129
+ u = s.url, v = s.branch;
130
+ }
131
+ const w = D(".git");
132
+ console.log(o.blue("正在初始化项目,请稍等...")), g(
133
+ `git clone${v ? ` -b ${v}` : ""} ${u} ${r} --depth=1`,
134
+ { stdio: "inherit" }
135
+ );
136
+ const N = M(n);
137
+ if (N && (await _({
138
+ rootDir: n,
139
+ extraEnvData: {
140
+ $projectName: r
141
+ }
142
+ }), h(N, { force: !0 }), console.log(o.blue("模板项目配置注入成功, 模版项目配置文件已删除"))), console.log(o.blue("项目初始化完成")), w) {
143
+ const s = p.resolve(n), c = p.resolve(s, ".git");
144
+ if (!y(c))
145
+ throw new Error("git目录不存在");
146
+ h(c, { recursive: !0, force: !0 }), console.log(
147
+ o.green(
148
+ `项目创建在父级git仓库${w}中,已删除${r}目录下的.git(${c})`
149
+ )
150
+ );
151
+ } else if ((await a(B)).saveGitHistory)
152
+ g("git remote rename origin upstream && git fetch --unshallow", {
153
+ cwd: n,
154
+ stdio: "inherit"
155
+ }), console.log(
156
+ o.green(
157
+ "已经将origin重命名为upstream,后续可以与模板git仓库有完整的交互"
158
+ )
159
+ ), console.log(o.green("已保存git历史记录"));
160
+ else {
161
+ const c = p.resolve(n, ".git");
162
+ h(c, { recursive: !0, force: !0 }), g("git init", {
163
+ cwd: n,
164
+ stdio: "inherit"
165
+ });
166
+ }
167
+ const { gitCommitMessage: P } = await a(
168
+ J(r)
169
+ );
170
+ g(`git add . && git commit -m '${P}'`, {
171
+ cwd: n,
172
+ stdio: "inherit"
173
+ }), console.log(o.green(`项目${r}初始化完成`)), console.log(
174
+ o.blue(`
175
+ 使用步骤:
176
+ 1. cd ${r}
177
+ 2. pnpm install
178
+ 3. pnpm run dev
179
+ `)
180
+ );
181
+ }, G = d.cliConfig.moduleName, z = (e, t) => {
182
+ console.log(e ? o.red(e) : o.red(t.message)), process.exit(1);
183
+ }, T = d.description, K = `$0 ${G} [projectName]`, Q = "$0 [projectName]", F = async (e, t) => e.strict().usage(`Usage: ${t}`).help("help").version(d.version).alias("v", "version").alias("h", "help").command({
184
+ command: t,
185
+ describe: T,
186
+ handler: E
187
+ }).fail(z).argv, ie = {
188
+ command: G,
189
+ describe: T,
190
+ builder: (e) => F(e, K),
191
+ handler: E
192
+ }, ce = async () => {
193
+ const e = L(I(process.argv));
194
+ return await F(e, Q);
195
+ };
196
+ export {
197
+ ie as a,
198
+ ce as c,
199
+ E as h
200
+ };
package/es/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { a as b, h as c } from "./index-3ddda083.js";
2
+ import { a as b, h as c } from "./index-44fde806.js";
3
3
  import "chalk";
4
4
  import "node:child_process";
5
5
  import "node:path";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-done-coding",
3
- "version": "0.9.0",
3
+ "version": "0.9.2-alpha.0",
4
4
  "description": "项目创建命令行工具",
5
5
  "module": "es/index.mjs",
6
6
  "type": "module",
@@ -14,8 +14,7 @@
14
14
  "files": [
15
15
  "es",
16
16
  "lib",
17
- "types",
18
- "gif"
17
+ "types"
19
18
  ],
20
19
  "scripts": {
21
20
  "clean": "rimraf es lib types",
@@ -38,7 +37,6 @@
38
37
  "license": "MIT",
39
38
  "sideEffects": false,
40
39
  "devDependencies": {
41
- "@done-coding/cli-inject": "^0.4.5",
42
40
  "@types/node": "^18.0.0",
43
41
  "@types/prompts": "^2.4.6",
44
42
  "@types/yargs": "^17.0.28",
@@ -58,5 +56,5 @@
58
56
  "prompts": "^2.4.2",
59
57
  "yargs": "^17.7.2"
60
58
  },
61
- "gitHead": "0dde53af70143475f5149714152d714802f846f8"
59
+ "gitHead": "e37be971d497d243ec60f956a34cb09a09ae787d"
62
60
  }
@@ -1,5 +1,5 @@
1
1
  declare const _default: {
2
- "version": "0.9.0",
2
+ "version": "0.9.2-alpha.0",
3
3
  "name": "create-done-coding",
4
4
  "description": "项目创建命令行工具",
5
5
  "cliConfig": {
package/types/main.d.ts CHANGED
@@ -1,4 +1,14 @@
1
1
  import type { CommandModule } from "yargs";
2
2
  import type { Options } from './utils';
3
3
  export declare const command: CommandModule<Options, Options>;
4
- export declare const createCli: () => Promise<undefined>;
4
+ export declare const createCli: () => Promise<{
5
+ [x: string]: unknown;
6
+ projectName: string;
7
+ _: (string | number)[];
8
+ $0: string;
9
+ } | {
10
+ [x: string]: unknown;
11
+ projectName: string;
12
+ _: (string | number)[];
13
+ $0: string;
14
+ }>;
@@ -1,8 +1,17 @@
1
1
  import type { PromptObject } from "prompts";
2
+ import { onPromptFormStateForSigint } from "@done-coding/node-tools";
2
3
  /** 模版选项 */
3
4
  export interface TemplateChoiceItem {
5
+ /** 模板名 */
4
6
  name: string;
7
+ /** 仓库地址 */
5
8
  url?: string;
9
+ /** 描述 */
10
+ description?: string;
11
+ /** 目标分支 */
12
+ branch?: string;
13
+ /** 应用实例 */
14
+ instances?: string[];
6
15
  }
7
16
  /** 获取模版选项 */
8
17
  export declare const getTemplateList: () => Promise<TemplateChoiceItem[]>;
@@ -15,12 +24,14 @@ export declare const saveGitHistoryForm: {
15
24
  name: string;
16
25
  message: string;
17
26
  initial: boolean;
27
+ onState: typeof onPromptFormStateForSigint;
18
28
  };
19
29
  /** 获取删除目录的表单 */
20
30
  export declare const getRemoveDirForm: (message?: string) => {
21
31
  type: "confirm";
22
32
  name: string;
23
33
  message: string;
34
+ onState: typeof onPromptFormStateForSigint;
24
35
  };
25
36
  /** 自定义模板路径表单 */
26
37
  export declare const customUrlForm: PromptObject<string>;
@@ -1,6 +1,4 @@
1
1
  export interface Options {
2
2
  /** 项目名称 */
3
3
  projectName: string;
4
- /** 项目模板 */
5
- template: string;
6
4
  }
@@ -1,181 +0,0 @@
1
- #!/usr/bin/env node
2
- import t from "chalk";
3
- import { execSync as l } from "node:child_process";
4
- import g, { resolve as R } from "node:path";
5
- import d, { existsSync as C, rmSync as f } from "node:fs";
6
- import a from "prompts";
7
- import { onPromptFormStateForSigint as v, lookForParentTarget as D } from "@done-coding/node-tools";
8
- import { getConfigPath as O, batchHandler as U } from "@done-coding/cli-template";
9
- import { getTargetRepoUrl as _, gitCloneCommand as M } from "@done-coding/cli-git";
10
- import I from "yargs";
11
- import { hideBin as L } from "yargs/helpers";
12
- const p = {
13
- version: "0.9.0",
14
- name: "create-done-coding",
15
- description: "项目创建命令行工具",
16
- cliConfig: {
17
- namespaceDir: ".done-coding",
18
- moduleName: "create"
19
- }
20
- }, A = "git@gitee.com:justsosu/done-coding-cli-config-json.git", j = "自定义模版路径", S = "某个git平台用户仓库", H = `./.${p.name}_temp`;
21
- let y;
22
- const k = async () => (y || (y = (await q()).templateList), y), $ = async () => [
23
- ...await k(),
24
- { name: j },
25
- { name: S }
26
- ], x = {
27
- type: "text",
28
- name: "projectName",
29
- message: "请输入项目名称",
30
- format: (e) => e.trim(),
31
- validate: (e) => e.length > 0 || "项目名称不能为空",
32
- onState: v
33
- }, E = async () => ({
34
- type: "select",
35
- name: "template",
36
- message: "请选择模板",
37
- choices: (await $()).map((o) => ({
38
- title: o.name,
39
- value: o.name
40
- })),
41
- validate: (o) => o.trim().length > 0 || "模板不能为空",
42
- onState: v
43
- }), B = {
44
- type: "confirm",
45
- name: "saveGitHistory",
46
- message: "是否保留git历史",
47
- initial: !1
48
- }, G = (e = "项目已存在,是否删除") => ({
49
- type: "confirm",
50
- name: "isRemove",
51
- message: e
52
- }), Y = {
53
- type: "text",
54
- name: "customUrl",
55
- message: "请输入自定义模板路径",
56
- validate: (e) => e.trim().length > 0 || "路径不能为空",
57
- onState: v
58
- }, J = (e) => ({
59
- type: "text",
60
- name: "gitCommitMessage",
61
- message: "请输入git提交信息",
62
- initial: `feat: 初始化项目${e}`,
63
- validate: (o) => o.trim().length > 0 || "提交信息不能为空",
64
- onState: v
65
- }), q = async () => {
66
- console.log(t.blue("拉取模板列表,请稍等..."));
67
- const e = g.resolve(
68
- process.cwd(),
69
- H
70
- );
71
- if (console.log(t.blue(`配置临时目录:${e}`)), d.existsSync(e)) {
72
- const s = `${e}目录已存在,是否删除?`, { isRemove: r } = await a(G(s));
73
- r ? d.rmSync(e, { recursive: !0, force: !0 }) : (console.log(t.red(`${e}已存在,请手动删除后再试!`)), process.exit(1));
74
- }
75
- l(`git clone ${A} ${e} --depth=1`);
76
- const o = p.name, c = g.resolve(e, `${o}.json`), h = JSON.parse(d.readFileSync(c, "utf-8"));
77
- if (!Array.isArray(h.templateList))
78
- throw new Error("远程配置文件出错,templateList 不是数组");
79
- return console.log(t.green("模板列表拉取成功!")), d.rmSync(e, { recursive: !0, force: !0 }), h;
80
- }, T = async (e) => {
81
- const { projectName: o, template: c } = e, s = ((o ?? (await a(x)).projectName) || "").trim();
82
- if (s.includes(" ") || s.includes("\\") || s.includes("/"))
83
- return console.log(t.red(`项目名称\`${s}\`不能包含空格或者\\或者/`)), process.exit(1);
84
- const r = R(process.cwd(), s);
85
- if (C(r)) {
86
- const { isRemove: i } = await a(G());
87
- if (i === !0)
88
- f(r, { recursive: !0, force: !0 });
89
- else
90
- return console.log(t.red("项目已存在")), process.exit(1);
91
- }
92
- const m = c ?? (await a(await E())).template;
93
- let u = "";
94
- if (m === j) {
95
- const { customUrl: i } = await a(Y);
96
- u = i;
97
- } else if (m === S)
98
- u = await _();
99
- else {
100
- const i = (await $()).find(
101
- (n) => n.name === m
102
- );
103
- if (!i)
104
- return console.log(t.red(`模板${m}不存在`)), process.exit(1);
105
- if (!i.url)
106
- return console.log(t.red(`模板${m}仓库地址不存在`)), process.exit(1);
107
- u = i.url;
108
- }
109
- const w = D(".git");
110
- console.log(t.blue("正在初始化项目,请稍等...")), l(`git clone ${u} ${s} --depth=1 1>&2`);
111
- const N = O(r);
112
- if (N && (await U({
113
- rootDir: r,
114
- extraEnvData: {
115
- $projectName: s
116
- }
117
- }), f(N, { force: !0 }), console.log(t.blue("模板项目配置注入成功, 模版项目配置文件已删除"))), console.log(t.blue("项目初始化完成")), w) {
118
- const i = g.resolve(r), n = g.resolve(i, ".git");
119
- if (!C(n))
120
- throw new Error("git目录不存在");
121
- f(n, { recursive: !0, force: !0 }), console.log(
122
- t.green(
123
- `项目创建在父级git仓库${w}中,已删除${s}目录下的.git(${n})`
124
- )
125
- );
126
- } else if ((await a(B)).saveGitHistory)
127
- l(
128
- `cd ${r} && git remote rename origin upstream && git fetch --unshallow`
129
- ), console.log(
130
- t.green(
131
- "已经将origin重命名为upstream,后续可以与模板git仓库有完整的交互"
132
- )
133
- ), console.log(t.green("已保存git历史记录"));
134
- else {
135
- const n = g.resolve(r, ".git");
136
- f(n, { recursive: !0, force: !0 }), l(`cd ${r} && git init`);
137
- }
138
- const { gitCommitMessage: P } = await a(
139
- J(s)
140
- );
141
- l(
142
- `cd ${r} && git add . && git commit -m '${P}'`
143
- ), console.log(t.green(`项目${s}初始化完成`)), console.log(
144
- t.blue(`
145
- 使用步骤:
146
- 1. cd ${s}
147
- 2. pnpm install
148
- 3. pnpm run dev
149
- `)
150
- );
151
- }, z = async () => ({
152
- projectName: {
153
- type: "string",
154
- alias: "p",
155
- describe: x.message
156
- },
157
- template: {
158
- type: "string",
159
- alias: "t",
160
- choices: (await $()).map((e) => e.name),
161
- describe: (await E()).message
162
- }
163
- }), b = p.cliConfig.moduleName, K = (e, o) => {
164
- console.log(e ? t.red(e) : t.red(o.message)), process.exit(1);
165
- }, Q = p.description, V = `Usage: $0 ${b} [options]`, W = "Usage: $0 [options]", F = async (e, o) => {
166
- const c = await z();
167
- return e.strict().usage(o).help("help").version(p.version).alias("v", "version").alias("h", "help").options(c).command(M).fail(K).argv;
168
- }, X = (e) => F(e, V), me = {
169
- command: b,
170
- describe: Q,
171
- builder: X,
172
- handler: T
173
- }, le = async () => {
174
- const e = I(L(process.argv)), o = await F(e, W);
175
- return T(o);
176
- };
177
- export {
178
- me as a,
179
- le as c,
180
- T as h
181
- };