@htyf-mp/cli 0.0.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 +21 -0
- package/cli.js +163 -0
- package/package.json +32 -0
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# 红糖云服app 小程序模板 CLI
|
|
2
|
+
|
|
3
|
+
用于构建红糖云服小程序的前端框架模板
|
|
4
|
+
|
|
5
|
+
## 使用
|
|
6
|
+
|
|
7
|
+
生成一个新项目:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npx @htyf-mp/cli
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
接下来,使用CLI配置项目名和Appid,或者稍后再配置,最后选择模板类型
|
|
14
|
+
|
|
15
|
+
### 运行应用程序
|
|
16
|
+
|
|
17
|
+
进入`mini-apps-template-rn`或`mini-apps-template-taro`目录并运行:
|
|
18
|
+
> 具体模板使用,可参考模板内的README.md
|
|
19
|
+
```sh
|
|
20
|
+
npm start
|
|
21
|
+
```
|
package/cli.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import chalk from 'chalk'
|
|
4
|
+
import path from 'path'
|
|
5
|
+
import fs from 'fs'
|
|
6
|
+
import { Command } from 'commander'
|
|
7
|
+
import select from '@inquirer/select'
|
|
8
|
+
import { input } from '@inquirer/prompts';
|
|
9
|
+
import {execa, execaCommand} from 'execa'
|
|
10
|
+
import ora from 'ora'
|
|
11
|
+
import childProcess from 'child_process'
|
|
12
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
13
|
+
import md5 from 'md5'
|
|
14
|
+
|
|
15
|
+
const v4options = {
|
|
16
|
+
random: [
|
|
17
|
+
0x10, 0x91, 0x56, 0xbe, 0xc4, 0xfb, 0xc1, 0xea, 0x71, 0xb4, 0xef, 0xe1, 0x67, 0x1c, 0x58, 0x36,
|
|
18
|
+
],
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const log = console.log
|
|
22
|
+
const program = new Command()
|
|
23
|
+
const green = chalk.green
|
|
24
|
+
|
|
25
|
+
const repoUrl = 'https://github.com/htyf-mp-community/htyf-mp.git'
|
|
26
|
+
|
|
27
|
+
const taroTempPath = 'mini-apps-template-taro';
|
|
28
|
+
const rnTempPath = 'mini-apps-template-rn';
|
|
29
|
+
|
|
30
|
+
const isYarnInstalled = () => {
|
|
31
|
+
try {
|
|
32
|
+
childProcess.execSync('yarn --version');
|
|
33
|
+
return true;
|
|
34
|
+
} catch {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const isBunInstalled = () => {
|
|
40
|
+
try {
|
|
41
|
+
childProcess.execSync('bun --version')
|
|
42
|
+
return true;
|
|
43
|
+
} catch(err) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function main() {
|
|
49
|
+
const spinner = ora({
|
|
50
|
+
text: '创建代码库'
|
|
51
|
+
})
|
|
52
|
+
try {
|
|
53
|
+
const kebabRegez = /^([a-z]+)(-[a-z0-9]+)*$/
|
|
54
|
+
|
|
55
|
+
program
|
|
56
|
+
.name('红糖云服APP-小程序')
|
|
57
|
+
.description('快速创建红糖云服APP小程序')
|
|
58
|
+
|
|
59
|
+
program.parse(process.argv)
|
|
60
|
+
|
|
61
|
+
const args = program.args
|
|
62
|
+
let appName = args[0]
|
|
63
|
+
|
|
64
|
+
if (!appName || !kebabRegez.test(args[0])) {
|
|
65
|
+
appName = await input({
|
|
66
|
+
message: '输入应用程序目录名称',
|
|
67
|
+
default: 'my-htyf-mp',
|
|
68
|
+
validate: d => {
|
|
69
|
+
if(!kebabRegez.test(d)) {
|
|
70
|
+
return '请以my-app-name格式输入您的应用程序目录名称'
|
|
71
|
+
}
|
|
72
|
+
return true
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const displayNameRegez = /^[\u4e00-\u9fa5a-zA-Z0-9]{2,4}$/
|
|
78
|
+
|
|
79
|
+
let displayName = ''
|
|
80
|
+
if (!displayName) {
|
|
81
|
+
displayName = await input({
|
|
82
|
+
message: '输入应用程序名称',
|
|
83
|
+
default: '小程序',
|
|
84
|
+
validate: d => {
|
|
85
|
+
if(!displayNameRegez.test(d)) {
|
|
86
|
+
return '只能中文、字母和数字, 不能包含特殊字符, 限2-4个字符'
|
|
87
|
+
}
|
|
88
|
+
return true
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const tempType = await select({
|
|
94
|
+
message: '请选择模板类型?',
|
|
95
|
+
choices: [
|
|
96
|
+
{
|
|
97
|
+
name: 'Taro',
|
|
98
|
+
value: 'taro',
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
name: 'ReactNative',
|
|
102
|
+
value: 'react-native',
|
|
103
|
+
}
|
|
104
|
+
]
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
const appid = md5(uuidv4(v4options));
|
|
108
|
+
|
|
109
|
+
let config = {
|
|
110
|
+
"type": "app",
|
|
111
|
+
"name": appName,
|
|
112
|
+
"projectname": displayName,
|
|
113
|
+
"appid": appid,
|
|
114
|
+
"appUrlConfig": "https://xxx.cos.ap-chengdu.myqcloud.com/assets/testMiniApps/htyanimation/app.json",
|
|
115
|
+
"zipUrl": "https://xxxxx.myqcloud.com/assets/testMiniApps/xxxxx/outputs/dist.dgz"
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
log(`\n初始化项目. \n`)
|
|
119
|
+
|
|
120
|
+
spinner.start()
|
|
121
|
+
await execa('git', ['clone', repoUrl, appName])
|
|
122
|
+
let appRootPath = '';
|
|
123
|
+
try {
|
|
124
|
+
if (tempType === 'taro') {
|
|
125
|
+
appRootPath = `${appName}/${taroTempPath}`
|
|
126
|
+
await execa('rm', ['-r', `${appName}/${rnTempPath}`])
|
|
127
|
+
}
|
|
128
|
+
if (tempType === 'react-native') {
|
|
129
|
+
appRootPath = `${appName}/${rnTempPath}`
|
|
130
|
+
await execa('rm', ['-r', `${appName}/${taroTempPath}`])
|
|
131
|
+
}
|
|
132
|
+
await execa('rm', ['-r', `${appName}/cli`])
|
|
133
|
+
await execa('rm', ['-rf', `${appName}/.git`])
|
|
134
|
+
} catch (err) {}
|
|
135
|
+
|
|
136
|
+
if (tempType === 'taro') {
|
|
137
|
+
const dgzJosn = `${appRootPath}/project.dgz.json`
|
|
138
|
+
fs.writeFileSync(dgzJosn, JSON.stringify(config, undefined, 2))
|
|
139
|
+
console.log(dgzJosn)
|
|
140
|
+
}
|
|
141
|
+
if (tempType === 'react-native') {
|
|
142
|
+
const dgzJosn = `${appRootPath}/project.dgz.json`
|
|
143
|
+
fs.writeFileSync(dgzJosn, JSON.stringify(config, undefined, 2))
|
|
144
|
+
console.log(dgzJosn)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
spinner.text = ''
|
|
149
|
+
|
|
150
|
+
spinner.stop()
|
|
151
|
+
log(`${green.bold('Success!')} 创建项目 ${appName} 在 ${appRootPath} \n`)
|
|
152
|
+
log(`切换到【 ${appRootPath} 】目录并运行开发`)
|
|
153
|
+
log('\n')
|
|
154
|
+
} catch (err) {
|
|
155
|
+
console.log(err)
|
|
156
|
+
log('\n')
|
|
157
|
+
if (err.exitCode == 128) {
|
|
158
|
+
log('Error: 目录已经存在。')
|
|
159
|
+
}
|
|
160
|
+
spinner.stop()
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
main()
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@htyf-mp/cli",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "红糖云服app小程序模板-cli",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": "cli.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"hongtangyun",
|
|
12
|
+
"mp",
|
|
13
|
+
"taro",
|
|
14
|
+
"react-native"
|
|
15
|
+
],
|
|
16
|
+
"author": "dgz",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/htyf-mp-community/htyf-mp.git"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@inquirer/prompts": "^3.1.1",
|
|
24
|
+
"chalk": "^5.3.0",
|
|
25
|
+
"cli-spinners": "^2.9.0",
|
|
26
|
+
"commander": "^11.0.0",
|
|
27
|
+
"execa": "^8.0.1",
|
|
28
|
+
"md5": "^2.3.0",
|
|
29
|
+
"ora": "^7.0.1",
|
|
30
|
+
"uuid": "^9.0.1"
|
|
31
|
+
}
|
|
32
|
+
}
|