ljr-cli 1.0.6 → 1.0.8
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/commands/demo1.js +12 -0
- package/bin/commands/demo2.js +60 -0
- package/bin/commands/init.js +128 -0
- package/bin/commands/sync.js +40 -0
- package/bin/index.js +24 -206
- package/package.json +3 -5
- package/bin/demo1.js +0 -39
- package/bin/demo2.js +0 -87
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export default function registerDemo1(program) {
|
|
2
|
+
program
|
|
3
|
+
.command("demo1 <name>")
|
|
4
|
+
.description("demo1演示")
|
|
5
|
+
.option("-l, --local", "从本地模板创建目录")
|
|
6
|
+
.option("-g, --git", "从git地址创建目录")
|
|
7
|
+
.action((name, options) => {
|
|
8
|
+
console.log("参数name=====>", name)
|
|
9
|
+
console.log("选项options=====>", options)
|
|
10
|
+
// 这里就是你真正要做的事情了
|
|
11
|
+
})
|
|
12
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import * as prompts from "@clack/prompts"
|
|
2
|
+
|
|
3
|
+
export default function registerDemo2(program, { gradientString }) {
|
|
4
|
+
program
|
|
5
|
+
.command("demo2")
|
|
6
|
+
.description("demo2演示")
|
|
7
|
+
.action(async () => {
|
|
8
|
+
prompts.intro(gradientString("创建项目"))
|
|
9
|
+
|
|
10
|
+
const group = await prompts.group(
|
|
11
|
+
{
|
|
12
|
+
name: () =>
|
|
13
|
+
prompts.text({
|
|
14
|
+
message: "请输入项目名称:",
|
|
15
|
+
placeholder: "项目名称",
|
|
16
|
+
validate: (value) => {
|
|
17
|
+
if (!value.trim().length) {
|
|
18
|
+
return "请输入项目名称"
|
|
19
|
+
}
|
|
20
|
+
if (/[^a-z-_]+/i.test(value)) {
|
|
21
|
+
return "英文开头或下划线开头,且只能包含英文、数字、下划线或中划线"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
}),
|
|
25
|
+
template: () =>
|
|
26
|
+
prompts.select({
|
|
27
|
+
message: "请选择模板:",
|
|
28
|
+
options: [
|
|
29
|
+
{ label: "vue3.x", value: "vue3.x", hint: "vue3项目" },
|
|
30
|
+
{ label: "vue2.7.16", value: "vue2.7.16", hint: "vue2的最后一个版本" },
|
|
31
|
+
],
|
|
32
|
+
}),
|
|
33
|
+
age: () => prompts.text({ message: "What is your age?" }),
|
|
34
|
+
shouldContinue: () =>
|
|
35
|
+
prompts.confirm({
|
|
36
|
+
message: "是否继续?",
|
|
37
|
+
}),
|
|
38
|
+
color: ({ results }) =>
|
|
39
|
+
prompts.multiselect({
|
|
40
|
+
message: `What is your favorite color ${results.name}?`,
|
|
41
|
+
options: [
|
|
42
|
+
{ value: "red", label: "Red" },
|
|
43
|
+
{ value: "green", label: "Green" },
|
|
44
|
+
{ value: "blue", label: "Blue" },
|
|
45
|
+
],
|
|
46
|
+
}),
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
onCancel: ({ results }) => {
|
|
50
|
+
prompts.cancel("取消操作!")
|
|
51
|
+
process.exit(0)
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
console.log("group=====>", group)
|
|
57
|
+
|
|
58
|
+
prompts.outro(gradientString("项目创建完成"))
|
|
59
|
+
})
|
|
60
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import path from "node:path"
|
|
2
|
+
import { existsSync } from "node:fs"
|
|
3
|
+
import { copySync, emptyDirSync } from "fs-extra/esm"
|
|
4
|
+
import * as prompts from "@clack/prompts"
|
|
5
|
+
import download from "download-git-repo"
|
|
6
|
+
import { replaceInFile } from "replace-in-file"
|
|
7
|
+
|
|
8
|
+
export default function registerInit(program, { currentRunningDirPath, cwdPath, gradientString }) {
|
|
9
|
+
function downloadTemplate(gitUrl, projectName) {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
download(gitUrl, path.resolve(cwdPath, projectName), { clone: true }, function (err) {
|
|
12
|
+
if (err) return reject(err)
|
|
13
|
+
resolve()
|
|
14
|
+
})
|
|
15
|
+
})
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
program
|
|
19
|
+
.command("init")
|
|
20
|
+
.description("创建项目")
|
|
21
|
+
.action(async () => {
|
|
22
|
+
prompts.intro(gradientString("开始创建项目"))
|
|
23
|
+
|
|
24
|
+
const group = await prompts.group(
|
|
25
|
+
{
|
|
26
|
+
name: () =>
|
|
27
|
+
prompts.text({
|
|
28
|
+
message: "请输入项目名称:",
|
|
29
|
+
placeholder: "项目名称",
|
|
30
|
+
validate: (value) => {
|
|
31
|
+
if (!value.trim().length) return "请输入项目名称"
|
|
32
|
+
if (/[^a-z-_]+/i.test(value)) return "英文开头或下划线开头,且只能包含英文、数字、下划线或中划线"
|
|
33
|
+
},
|
|
34
|
+
}),
|
|
35
|
+
templateName: () =>
|
|
36
|
+
prompts.select({
|
|
37
|
+
message: "请选择模板:",
|
|
38
|
+
options: [
|
|
39
|
+
{ label: "vue3.5.25-2025.12.4", value: "vue3.5.25-2025.12.4" },
|
|
40
|
+
{ label: "vue2.7.16", value: "vue2.7.16" },
|
|
41
|
+
{ label: "react", value: "react" },
|
|
42
|
+
],
|
|
43
|
+
}),
|
|
44
|
+
source: () =>
|
|
45
|
+
prompts.select({
|
|
46
|
+
message: "请选择来源:",
|
|
47
|
+
options: [
|
|
48
|
+
{ label: "本地", value: "local" },
|
|
49
|
+
{ label: "git仓库", value: "git" },
|
|
50
|
+
],
|
|
51
|
+
}),
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
onCancel: () => {
|
|
55
|
+
prompts.cancel("取消操作!")
|
|
56
|
+
process.exit(0)
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
const s = prompts.spinner({ indicator: "timer" })
|
|
62
|
+
|
|
63
|
+
if (existsSync(group.name)) {
|
|
64
|
+
const confirm = await prompts.confirm({ message: gradientString("文件夹已存在,是否清空?", ["red", "red"]) })
|
|
65
|
+
if (confirm) emptyDirSync(group.name)
|
|
66
|
+
else {
|
|
67
|
+
prompts.cancel("取消操作!")
|
|
68
|
+
process.exit(0)
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
emptyDirSync(group.name)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
s.start("创建文件夹...")
|
|
75
|
+
s.stop("文件夹操作完成!")
|
|
76
|
+
|
|
77
|
+
s.start("写入模版中...")
|
|
78
|
+
try {
|
|
79
|
+
if (group.source === "local") {
|
|
80
|
+
const srcPath = path.join(currentRunningDirPath, "/templates/", group.templateName)
|
|
81
|
+
const destPath = `./${group.name}`
|
|
82
|
+
|
|
83
|
+
copySync(srcPath, destPath, {
|
|
84
|
+
recursive: true,
|
|
85
|
+
filter: (src) => {
|
|
86
|
+
const rel = path.relative(srcPath, src)
|
|
87
|
+
if (!rel) return true
|
|
88
|
+
const parts = rel.split(path.sep)
|
|
89
|
+
if (parts.includes("node_modules") || parts.includes("dist") || parts.includes(".eslintcache"))
|
|
90
|
+
return false
|
|
91
|
+
return true
|
|
92
|
+
},
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (group.source === "git") {
|
|
97
|
+
await downloadTemplate("direct:https://gitee.com/ljr-395181403/git-test.git#master", group.name)
|
|
98
|
+
}
|
|
99
|
+
} catch (error) {
|
|
100
|
+
console.log("error=====>", error)
|
|
101
|
+
prompts.cancel("模版写入失败!")
|
|
102
|
+
process.exit(0)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
await new Promise((r) => setTimeout(r, 2000))
|
|
106
|
+
s.stop("模版写入完成!")
|
|
107
|
+
|
|
108
|
+
if (existsSync(path.join(cwdPath, group.name, "package.json"))) {
|
|
109
|
+
s.start("项目名操作...")
|
|
110
|
+
try {
|
|
111
|
+
const pkgFile = path.join(cwdPath, group.name, "package.json")
|
|
112
|
+
await replaceInFile({
|
|
113
|
+
files: pkgFile,
|
|
114
|
+
from: /("name"\s*:\s*)".*?"/,
|
|
115
|
+
to: `$1"${group.name}"`,
|
|
116
|
+
})
|
|
117
|
+
} catch (err) {
|
|
118
|
+
console.error("替换 package.json name 失败:", err)
|
|
119
|
+
prompts.cancel("项目名修改失败")
|
|
120
|
+
process.exit(1)
|
|
121
|
+
}
|
|
122
|
+
await new Promise((r) => setTimeout(r, 1000))
|
|
123
|
+
s.stop("项目名操作完成!")
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
prompts.outro(gradientString("项目创建完成! 🎉"))
|
|
127
|
+
})
|
|
128
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import path from "node:path"
|
|
2
|
+
import { existsSync } from "node:fs"
|
|
3
|
+
import { copySync } from "fs-extra/esm"
|
|
4
|
+
import * as prompts from "@clack/prompts"
|
|
5
|
+
|
|
6
|
+
export default function registerSync(program, { currentRunningDirPath, cwdPath, gradientString }) {
|
|
7
|
+
program
|
|
8
|
+
.command("sync")
|
|
9
|
+
.description("同步文件")
|
|
10
|
+
.action(async () => {
|
|
11
|
+
prompts.intro(gradientString("开始同步"))
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
const moduleBase = path.join(currentRunningDirPath, "../node_modules/l-global")
|
|
15
|
+
const cssSrc = path.join(moduleBase, "css")
|
|
16
|
+
const utilsSrc = path.join(moduleBase, "utils")
|
|
17
|
+
const destBase = path.join(cwdPath, "src")
|
|
18
|
+
|
|
19
|
+
if (existsSync(cssSrc)) {
|
|
20
|
+
copySync(cssSrc, path.join(destBase, "css"), { recursive: true, overwrite: true })
|
|
21
|
+
console.log("已复制:l-global/css -> src/css")
|
|
22
|
+
} else {
|
|
23
|
+
console.warn("未找到 l-global/css:", cssSrc)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (existsSync(utilsSrc)) {
|
|
27
|
+
copySync(utilsSrc, path.join(destBase, "utils"), { recursive: true, overwrite: true })
|
|
28
|
+
console.log("已复制:l-global/utils -> src/utils")
|
|
29
|
+
} else {
|
|
30
|
+
console.warn("未找到 l-global/utils:", utilsSrc)
|
|
31
|
+
}
|
|
32
|
+
} catch (err) {
|
|
33
|
+
console.error("同步出错:", err)
|
|
34
|
+
prompts.cancel("同步失败")
|
|
35
|
+
process.exit(1)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
prompts.outro(gradientString("同步完成! 🎉"))
|
|
39
|
+
})
|
|
40
|
+
}
|
package/bin/index.js
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import { program } from "commander"
|
|
4
4
|
import gradient from "gradient-string"
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
8
|
-
import { fileURLToPath } from "url"
|
|
9
|
-
import path from "path"
|
|
5
|
+
import { readFileSync } from "node:fs"
|
|
6
|
+
import { fileURLToPath } from "node:url"
|
|
7
|
+
import path from "node:path"
|
|
10
8
|
import { dateFormat } from "l-global/utils/date.js"
|
|
11
|
-
import download from "download-git-repo"
|
|
12
|
-
import { replaceInFile } from "replace-in-file"
|
|
13
9
|
import updateNotifier from "update-notifier"
|
|
10
|
+
import registerInit from "./commands/init.js"
|
|
11
|
+
import registerSync from "./commands/sync.js"
|
|
12
|
+
import registerDemo1 from "./commands/demo1.js"
|
|
13
|
+
import registerDemo2 from "./commands/demo2.js"
|
|
14
|
+
|
|
15
|
+
// 当前运行文件的路径
|
|
16
|
+
const currentRunningFilePath = fileURLToPath(import.meta.url)
|
|
17
|
+
// 当前运行文件的文件夹
|
|
18
|
+
const currentRunningDirPath = path.dirname(currentRunningFilePath)
|
|
19
|
+
// 当前命令行的路径
|
|
20
|
+
const cwdPath = process.cwd()
|
|
14
21
|
|
|
15
22
|
/** 渐变字符串 */
|
|
16
23
|
const gradientString = (str, gradientColor = ["#42d392", "#647eff"]) => {
|
|
@@ -27,212 +34,23 @@ program.hook("postAction", () => {
|
|
|
27
34
|
console.log(gradientString(`-------------------- 结束时间 - ${dateFormat()} --------------------`, ["red", "yellow"]))
|
|
28
35
|
})
|
|
29
36
|
|
|
30
|
-
|
|
31
|
-
// 当前运行文件的路径
|
|
32
|
-
const __filename = fileURLToPath(import.meta.url)
|
|
33
|
-
// 当前运行文件的文件夹
|
|
34
|
-
const __dirname = path.dirname(__filename)
|
|
35
|
-
const pkg = JSON.parse(readFileSync(path.join(__dirname, "../package.json"), "utf-8"))
|
|
37
|
+
const pkg = JSON.parse(readFileSync(path.join(currentRunningDirPath, "../package.json"), "utf-8"))
|
|
36
38
|
const version = pkg.version
|
|
37
39
|
|
|
38
|
-
const notifier = updateNotifier({ pkg
|
|
39
|
-
//
|
|
40
|
-
|
|
40
|
+
// const notifier = updateNotifier({ pkg })
|
|
41
|
+
// TAG:update-notifier 会把检查结果缓存(默认间隔 1 天),短时间内不会再次提示,updateCheckInterval: 0 表示每次都检查(演示的时候放开注释)
|
|
42
|
+
const notifier = updateNotifier({ pkg, updateCheckInterval: 0 })
|
|
41
43
|
|
|
44
|
+
// 如果有更新
|
|
42
45
|
if (notifier.update && notifier.update.latest && notifier.update.latest !== pkg.version) {
|
|
43
46
|
notifier.notify()
|
|
44
47
|
}
|
|
45
48
|
|
|
46
|
-
// 当前命令行的路径
|
|
47
|
-
const cwdPath = process.cwd()
|
|
48
|
-
|
|
49
49
|
program.version(version, "-v, --version", "查看版本号信息")
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return reject(err)
|
|
56
|
-
}
|
|
57
|
-
resolve()
|
|
58
|
-
})
|
|
59
|
-
})
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// 初始化项目
|
|
63
|
-
program
|
|
64
|
-
.command("init")
|
|
65
|
-
.description("创建项目")
|
|
66
|
-
.action(async () => {
|
|
67
|
-
prompts.intro(gradientString("开始创建项目"))
|
|
68
|
-
|
|
69
|
-
const group = await prompts.group(
|
|
70
|
-
{
|
|
71
|
-
name: () =>
|
|
72
|
-
prompts.text({
|
|
73
|
-
message: "请输入项目名称:",
|
|
74
|
-
placeholder: "项目名称",
|
|
75
|
-
validate: (value) => {
|
|
76
|
-
if (!value.trim().length) {
|
|
77
|
-
return "请输入项目名称"
|
|
78
|
-
}
|
|
79
|
-
if (/[^a-z-_]+/i.test(value)) {
|
|
80
|
-
return "英文开头或下划线开头,且只能包含英文、数字、下划线或中划线"
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
}),
|
|
84
|
-
templateName: () =>
|
|
85
|
-
prompts.select({
|
|
86
|
-
message: "请选择模板:",
|
|
87
|
-
options: [
|
|
88
|
-
{ label: "vue3.5.25-2025.12.4", value: "vue3.5.25-2025.12.4", hint: "2025.12.4创建的" },
|
|
89
|
-
{ label: "vue2.7.16", value: "vue2.7.16", hint: "vue2项目,这是vue2的最后一个版本" },
|
|
90
|
-
{ label: "react", value: "react", hint: "react项目" },
|
|
91
|
-
{ label: "qiankun", value: "qiankun", hint: "微前端-乾坤版本" },
|
|
92
|
-
{ label: "monorepo", value: "monorepo", hint: "monorepo架构-单仓库多项目" },
|
|
93
|
-
{ label: "node", value: "node", hint: "node服务" },
|
|
94
|
-
{ label: "java", value: "java", hint: "java服务" },
|
|
95
|
-
{ label: "mysql", value: "mysql", hint: "mysql数据库" },
|
|
96
|
-
{ label: "wx-miniapp", value: "wx-miniapp", hint: "微信原生小程序" },
|
|
97
|
-
{ label: "uni-app", value: "uni-app", hint: "uni-app项目" },
|
|
98
|
-
{ label: "flutter", value: "flutter", hint: "flutter项目" },
|
|
99
|
-
],
|
|
100
|
-
}),
|
|
101
|
-
source: () =>
|
|
102
|
-
prompts.select({
|
|
103
|
-
message: "请选择来源:",
|
|
104
|
-
options: [
|
|
105
|
-
{ label: "本地", value: "local", hint: "从本地模板创建" },
|
|
106
|
-
{
|
|
107
|
-
label: "git仓库",
|
|
108
|
-
value: "git",
|
|
109
|
-
hint: "从git地址创建,这里为了演示,只是随便用了个git仓库,并不是真正的模版仓库",
|
|
110
|
-
},
|
|
111
|
-
],
|
|
112
|
-
}),
|
|
113
|
-
},
|
|
114
|
-
{
|
|
115
|
-
onCancel: ({ results }) => {
|
|
116
|
-
prompts.cancel("取消操作!")
|
|
117
|
-
process.exit(0)
|
|
118
|
-
},
|
|
119
|
-
},
|
|
120
|
-
)
|
|
121
|
-
|
|
122
|
-
const s = prompts.spinner({ indicator: "timer" })
|
|
123
|
-
|
|
124
|
-
// 判断文件夹是否存在,存在询问是否清空
|
|
125
|
-
if (existsSync(group.name)) {
|
|
126
|
-
const confirm = await prompts.confirm({
|
|
127
|
-
message: gradientString("文件夹已存在,是否清空?", ["red", "red"]),
|
|
128
|
-
})
|
|
129
|
-
if (confirm) {
|
|
130
|
-
emptyDirSync(group.name)
|
|
131
|
-
} else {
|
|
132
|
-
prompts.cancel("取消操作!")
|
|
133
|
-
process.exit(0)
|
|
134
|
-
}
|
|
135
|
-
} else {
|
|
136
|
-
emptyDirSync(group.name)
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
s.start("创建文件夹...")
|
|
140
|
-
s.stop("文件夹操作完成!")
|
|
141
|
-
|
|
142
|
-
s.start("写入模版中...")
|
|
143
|
-
try {
|
|
144
|
-
if (group.source === "local") {
|
|
145
|
-
// 源路径
|
|
146
|
-
const srcPath = path.join(__dirname, "/templates/", group.templateName)
|
|
147
|
-
// 目标路径
|
|
148
|
-
const destPath = `./${group.name}`
|
|
149
|
-
|
|
150
|
-
copySync(srcPath, destPath, {
|
|
151
|
-
recursive: true,
|
|
152
|
-
filter: (src) => {
|
|
153
|
-
// 相对于模板根的相对路径
|
|
154
|
-
const rel = path.relative(srcPath, src)
|
|
155
|
-
// 根目录本身允许
|
|
156
|
-
if (!rel) return true
|
|
157
|
-
const parts = rel.split(path.sep)
|
|
158
|
-
// 如果路径中包含 node_modules 或 dist 则过滤掉
|
|
159
|
-
if (parts.includes("node_modules") || parts.includes("dist") || parts.includes(".eslintcache")) {
|
|
160
|
-
return false
|
|
161
|
-
}
|
|
162
|
-
return true
|
|
163
|
-
},
|
|
164
|
-
})
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (group.source === "git") {
|
|
168
|
-
await downloadTemplate("direct:https://gitee.com/ljr-395181403/git-test.git#master", group.name)
|
|
169
|
-
}
|
|
170
|
-
} catch (error) {
|
|
171
|
-
console.log("error=====>", error)
|
|
172
|
-
prompts.cancel("模版写入失败!")
|
|
173
|
-
process.exit(0)
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
await new Promise((resolve) => setTimeout(resolve, 2000)) // 为了更好的演示效果,强制等待
|
|
177
|
-
s.stop("模版写入完成!")
|
|
178
|
-
|
|
179
|
-
// 判断package.json是否存在
|
|
180
|
-
if (existsSync(path.join(cwdPath, group.name, "package.json"))) {
|
|
181
|
-
s.start("项目名操作...")
|
|
182
|
-
try {
|
|
183
|
-
const pkgFile = path.join(cwdPath, group.name, "package.json")
|
|
184
|
-
await replaceInFile({
|
|
185
|
-
files: pkgFile,
|
|
186
|
-
from: /("name"\s*:\s*)".*?"/,
|
|
187
|
-
to: `$1"${group.name}"`,
|
|
188
|
-
})
|
|
189
|
-
} catch (err) {
|
|
190
|
-
console.error("替换 package.json name 失败:", err)
|
|
191
|
-
prompts.cancel("项目名修改失败")
|
|
192
|
-
process.exit(1)
|
|
193
|
-
}
|
|
194
|
-
await new Promise((resolve) => setTimeout(resolve, 1000)) // 为了更好的演示效果,强制等待
|
|
195
|
-
s.stop("项目名操作完成!")
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
prompts.outro(gradientString("项目创建完成! 🎉"))
|
|
199
|
-
})
|
|
200
|
-
|
|
201
|
-
// 同步文件
|
|
202
|
-
program
|
|
203
|
-
.command("sync")
|
|
204
|
-
.description("同步文件")
|
|
205
|
-
.action(async () => {
|
|
206
|
-
prompts.intro(gradientString("开始同步"))
|
|
207
|
-
|
|
208
|
-
try {
|
|
209
|
-
// 本程序所在包里 node_modules/l-global 的路径
|
|
210
|
-
const moduleBase = path.join(__dirname, "../node_modules/l-global")
|
|
211
|
-
const cssSrc = path.join(moduleBase, "css")
|
|
212
|
-
const utilsSrc = path.join(moduleBase, "utils")
|
|
213
|
-
// 当前命令行运行路径下的目标 src 文件夹
|
|
214
|
-
const destBase = path.join(cwdPath, "src")
|
|
215
|
-
|
|
216
|
-
// 逐项拷贝(fs-extra.copySync 会自动创建目标父目录)
|
|
217
|
-
if (existsSync(cssSrc)) {
|
|
218
|
-
copySync(cssSrc, path.join(destBase, "css"), { recursive: true, overwrite: true })
|
|
219
|
-
console.log("已复制:l-global/css -> src/css")
|
|
220
|
-
} else {
|
|
221
|
-
console.warn("未找到 l-global/css:", cssSrc)
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (existsSync(utilsSrc)) {
|
|
225
|
-
copySync(utilsSrc, path.join(destBase, "utils"), { recursive: true, overwrite: true })
|
|
226
|
-
console.log("已复制:l-global/utils -> src/utils")
|
|
227
|
-
} else {
|
|
228
|
-
console.warn("未找到 l-global/utils:", utilsSrc)
|
|
229
|
-
}
|
|
230
|
-
} catch (err) {
|
|
231
|
-
console.error("同步出错:", err)
|
|
232
|
-
prompts.cancel("同步失败")
|
|
233
|
-
process.exit(1)
|
|
234
|
-
}
|
|
51
|
+
registerInit(program, { currentRunningDirPath, cwdPath, gradientString })
|
|
52
|
+
registerSync(program, { currentRunningDirPath, cwdPath, gradientString })
|
|
53
|
+
registerDemo1(program, { currentRunningDirPath, cwdPath, gradientString })
|
|
54
|
+
registerDemo2(program, { currentRunningDirPath, cwdPath, gradientString })
|
|
235
55
|
|
|
236
|
-
prompts.outro(gradientString("同步完成! 🎉"))
|
|
237
|
-
})
|
|
238
56
|
program.parse(process.argv)
|
package/package.json
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ljr-cli",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"packageManager": "pnpm@10.
|
|
7
|
+
"packageManager": "pnpm@10.28.1",
|
|
8
8
|
"engines": {
|
|
9
9
|
"node": "^20.19.0 || >=22.12.0"
|
|
10
10
|
},
|
|
11
11
|
"bin": {
|
|
12
12
|
"ljr": "bin/index.js",
|
|
13
|
-
"ljr-cli": "bin/index.js"
|
|
14
|
-
"ljr-demo1": "bin/demo1.js",
|
|
15
|
-
"ljr-demo2": "bin/demo2.js"
|
|
13
|
+
"ljr-cli": "bin/index.js"
|
|
16
14
|
},
|
|
17
15
|
"scripts": {
|
|
18
16
|
"发布": "npm publish"
|
package/bin/demo1.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import { program } from "commander"
|
|
4
|
-
import gradient from "gradient-string"
|
|
5
|
-
import { readFileSync } from "fs"
|
|
6
|
-
import { fileURLToPath } from "url"
|
|
7
|
-
import path from "path"
|
|
8
|
-
|
|
9
|
-
/** 渐变字符串 */
|
|
10
|
-
const gradientString = (str, gradientColor = ["#42d392", "#647eff"]) => {
|
|
11
|
-
return gradient(gradientColor)(str)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
console.log(
|
|
15
|
-
gradientString(`------------------------------- ljr-cli --------------------------------`, ["red", "yellow"]),
|
|
16
|
-
)
|
|
17
|
-
|
|
18
|
-
// 读取 package.json 里的 version(在 ESM 下安全做法)
|
|
19
|
-
// 当前运行文件的路径
|
|
20
|
-
const __filename = fileURLToPath(import.meta.url)
|
|
21
|
-
// 当前运行文件的文件夹
|
|
22
|
-
const __dirname = path.dirname(__filename)
|
|
23
|
-
const pkg = JSON.parse(readFileSync(path.join(__dirname, "../package.json"), "utf-8"))
|
|
24
|
-
const version = pkg.version
|
|
25
|
-
|
|
26
|
-
program.version(version, "-v, --version", "查看版本号信息")
|
|
27
|
-
|
|
28
|
-
program
|
|
29
|
-
.command("init <name>")
|
|
30
|
-
.description("demo1演示")
|
|
31
|
-
.option("-l, --local", "从本地模板创建目录")
|
|
32
|
-
.option("-g, --git", "从git地址创建目录")
|
|
33
|
-
.action((name, options) => {
|
|
34
|
-
console.log("参数name=====>", name)
|
|
35
|
-
console.log("选项options=====>", options)
|
|
36
|
-
// 这里就是你真正要做的事情了
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
program.parse(process.argv)
|
package/bin/demo2.js
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import { program } from "commander"
|
|
4
|
-
import gradient from "gradient-string"
|
|
5
|
-
import * as prompts from "@clack/prompts"
|
|
6
|
-
import { readFileSync } from "fs"
|
|
7
|
-
import { fileURLToPath } from "url"
|
|
8
|
-
import path from "path"
|
|
9
|
-
|
|
10
|
-
/** 渐变字符串 */
|
|
11
|
-
const gradientString = (str, gradientColor = ["#42d392", "#647eff"]) => {
|
|
12
|
-
return gradient(gradientColor)(str)
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
console.log(
|
|
16
|
-
gradientString(`------------------------------- ljr-cli --------------------------------`, ["red", "yellow"]),
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
// 读取 package.json 里的 version(在 ESM 下安全做法)
|
|
20
|
-
// 当前运行文件的路径
|
|
21
|
-
const __filename = fileURLToPath(import.meta.url)
|
|
22
|
-
// 当前运行文件的文件夹
|
|
23
|
-
const __dirname = path.dirname(__filename)
|
|
24
|
-
const pkg = JSON.parse(readFileSync(path.join(__dirname, "../package.json"), "utf-8"))
|
|
25
|
-
const version = pkg.version
|
|
26
|
-
|
|
27
|
-
program.version(version, "-v, --version", "查看版本号信息")
|
|
28
|
-
|
|
29
|
-
// 案例2
|
|
30
|
-
program
|
|
31
|
-
.command("init")
|
|
32
|
-
.description("demo2演示")
|
|
33
|
-
.action(async () => {
|
|
34
|
-
prompts.intro(gradientString("创建项目"))
|
|
35
|
-
|
|
36
|
-
const group = await prompts.group(
|
|
37
|
-
{
|
|
38
|
-
name: () =>
|
|
39
|
-
prompts.text({
|
|
40
|
-
message: "请输入项目名称:",
|
|
41
|
-
placeholder: "项目名称",
|
|
42
|
-
validate: (value) => {
|
|
43
|
-
if (!value.trim().length) {
|
|
44
|
-
return "请输入项目名称"
|
|
45
|
-
}
|
|
46
|
-
if (/[^a-z-_]+/i.test(value)) {
|
|
47
|
-
return "英文开头或下划线开头,且只能包含英文、数字、下划线或中划线"
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
}),
|
|
51
|
-
template: () =>
|
|
52
|
-
prompts.select({
|
|
53
|
-
message: "请选择模板:",
|
|
54
|
-
options: [
|
|
55
|
-
{ label: "vue3.x", value: "vue3.x", hint: "vue3项目" },
|
|
56
|
-
{ label: "vue2.7.16", value: "vue2.7.16", hint: "vue2的最后一个版本" },
|
|
57
|
-
],
|
|
58
|
-
}),
|
|
59
|
-
age: () => prompts.text({ message: "What is your age?" }),
|
|
60
|
-
shouldContinue: () =>
|
|
61
|
-
prompts.confirm({
|
|
62
|
-
message: "是否继续?",
|
|
63
|
-
}),
|
|
64
|
-
color: ({ results }) =>
|
|
65
|
-
prompts.multiselect({
|
|
66
|
-
message: `What is your favorite color ${results.name}?`,
|
|
67
|
-
options: [
|
|
68
|
-
{ value: "red", label: "Red" },
|
|
69
|
-
{ value: "green", label: "Green" },
|
|
70
|
-
{ value: "blue", label: "Blue" },
|
|
71
|
-
],
|
|
72
|
-
}),
|
|
73
|
-
},
|
|
74
|
-
{
|
|
75
|
-
onCancel: ({ results }) => {
|
|
76
|
-
prompts.cancel("取消操作!")
|
|
77
|
-
process.exit(0)
|
|
78
|
-
},
|
|
79
|
-
},
|
|
80
|
-
)
|
|
81
|
-
|
|
82
|
-
console.log("group=====>", group)
|
|
83
|
-
|
|
84
|
-
prompts.outro(gradientString("项目创建完成"))
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
program.parse(process.argv)
|