create-zhd-app 1.0.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/index.js ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { Command } from "commander";
4
+ import chalk from "chalk";
5
+ import packageJson from "./package.json" with { type: 'json' };
6
+ import create from './lib/create.js'
7
+
8
+
9
+ const program = new Command();
10
+
11
+ program
12
+ .command("create <name>")
13
+ .description("请输入项目名称")
14
+ .action((name) => {
15
+ console.log(`你要创建的项目名称:${name}`);
16
+ create(name);
17
+ });
18
+
19
+ program.on("--help", () => {
20
+ console.log();
21
+ console.log(
22
+ `Run ${chalk.cyan("zhdCli <command> --help")} to show detail of this command`,
23
+ )
24
+ console.log();
25
+ });
26
+
27
+ program
28
+ .version(packageJson.version)
29
+ // 说明使用方式
30
+ .usage("<command [option]");
31
+
32
+ program.parse(process.argv);
package/lib/create.js ADDED
@@ -0,0 +1,10 @@
1
+ import path from 'path';
2
+ import Creator from './creator.js'
3
+
4
+ export default function create(projectName, options, cmd) {
5
+
6
+ const cwd = process.cwd();
7
+ const targetDir = path.join(cwd, projectName)
8
+ const createIns = new Creator(projectName, targetDir);
9
+ createIns.create();
10
+ }
package/lib/creator.js ADDED
@@ -0,0 +1,57 @@
1
+ import downloadGitRepo from 'download-git-repo'
2
+ import util from 'util'
3
+ import inquirer from 'inquirer';
4
+ import chalk from 'chalk';
5
+ import { loading } from './utils.js'
6
+ import { fetchRepoBranchList } from './request.js'
7
+ import { execa } from 'execa'
8
+
9
+ class Creator {
10
+ constructor(projectName, targetDir) {
11
+ this.name = projectName;
12
+ this.targetDir = targetDir;
13
+ this.downloadGitRepo = util.promisify(downloadGitRepo)
14
+ }
15
+
16
+ async getRepoBranches() {
17
+ // 加loading效果
18
+ const branches = await loading(fetchRepoBranchList, '等待获取资源')
19
+ return branches;
20
+ }
21
+
22
+ async download(curBranch) {
23
+ const fullDownloadUrl = `git@github.com:fe-zhanghongdou/react-demo.git#${curBranch}`
24
+ console.log('fullDownloadUrl', fullDownloadUrl)
25
+ console.log('targetDir', this.targetDir)
26
+ try {
27
+ // await this.downloadGitRepo(fullDownloadUrl, this.targetDir, { clone: true })
28
+ await execa('git', ['clone', '-b', 'dev', 'git@github.com:fe-zhanghongdou/react-demo.git', this.targetDir]);
29
+ } catch(err) {
30
+ console.log('捕获到错误', err)
31
+ }
32
+ console.log(chalk.green('下载完毕!'))
33
+ }
34
+
35
+ async create() {
36
+ // 1. 拉取分支
37
+ const branches = await this.getRepoBranches()
38
+ console.log('branches', branches)
39
+
40
+ const { curBranch } = await inquirer.prompt([
41
+ {
42
+ name: 'curBranch',
43
+ type: 'list',
44
+ // 提示信息
45
+ message: '选择拉取分支',
46
+ // 选项
47
+ choices: [{ name: 'dev', value: 'dev' }, { name: 'master', value: 'master' }],
48
+ }
49
+ ])
50
+
51
+ // 2. 下载代码
52
+ await this.download(curBranch);
53
+ }
54
+ }
55
+
56
+
57
+ export default Creator
package/lib/request.js ADDED
@@ -0,0 +1,13 @@
1
+ // https://api.github.com/repos/fe-zhanghongdou/react-demo/branches
2
+ import axios from 'axios'
3
+
4
+
5
+ axios.interceptors.response.use((res) => {
6
+ return res.data;
7
+ })
8
+
9
+
10
+ export function fetchRepoBranchList() {
11
+ return axios.get('https://api.github.com/repos/fe-zhanghongdou/react-demo/branches')
12
+ }
13
+
@@ -0,0 +1,10 @@
1
+ import inquirer from 'inquirer';
2
+
3
+ async function test() {
4
+ const { answer } = await inquirer.prompt([
5
+ { type: 'list', name: 'answer', message: '选择', choices: [{ name: 'dev', value: 'dev' }, { name: 'master', value: 'master' }] }
6
+ ]);
7
+ console.log('你选了:', answer);
8
+ }
9
+
10
+ test();
package/lib/utils.js ADDED
@@ -0,0 +1,45 @@
1
+ import ora from "ora";
2
+
3
+ export const loading = function (fn, msg, ...args) {
4
+ let count = 0;
5
+ const run = async () => {
6
+ const spinner = ora(msg);
7
+ spinner.start();
8
+ try {
9
+ const result = await fn(...args);
10
+ spinner.succeed();
11
+ return result;
12
+ } catch (err) {
13
+ spinner.fail("something go wrong, refetching...");
14
+ if (++count < 3) {
15
+ run();
16
+ } else {
17
+ return Promise.reject();
18
+ }
19
+ }
20
+ };
21
+ return run();
22
+ };
23
+
24
+ // export const loading = async (fn, msg, ...args) => {
25
+ // // 计数器,失败自动重试最大次数为3,超过3次就直接返回失败
26
+ // let counter = 0;
27
+ // const run = async () => {
28
+ // const spinner = ora(msg);
29
+ // spinner.start();
30
+ // try {
31
+ // const result = await fn(...args);
32
+ // spinner.succeed();
33
+ // return result;
34
+ // } catch (error) {
35
+ // spinner.fail('something go wrong, refetching...');
36
+ // if (++counter < 3) {
37
+ // return run();
38
+ // } else {
39
+ // return Promise.reject();
40
+ // }
41
+ // }
42
+ // };
43
+ // return run();
44
+ // };
45
+
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "create-zhd-app",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "bin": {
10
+ "create-zhd-app": "./index.js"
11
+ },
12
+ "type": "module",
13
+ "keywords": [],
14
+ "author": "",
15
+ "license": "ISC",
16
+ "dependencies": {
17
+ "axios": "^1.13.6",
18
+ "chalk": "^5.6.2",
19
+ "commander": "^14.0.3",
20
+ "download-git-repo": "^3.0.2",
21
+ "execa": "^9.6.1",
22
+ "inquirer": "^13.3.0",
23
+ "ora": "^9.3.0"
24
+ }
25
+ }