dai-cli

脚手架

2022/1/8 11:52:22

更改列表

.gitignore copy 2(+2 -0)

.vscode/launch.json 18(+18 -0)

index.js 74(+74 -0)

package.json 4(+3 -1)

详细信息

.gitignore copy 2(+2 -0)

diff --git a/.gitignore copy b/.gitignore copy
new file mode 100644
index 0000000..04c01ba
--- /dev/null
+++ b/.gitignore copy
@@ -0,0 +1,2 @@
+node_modules/
+dist/
\ No newline at end of file

.vscode/launch.json 18(+18 -0)

diff --git a/.vscode/launch.json b/.vscode/launch.json
new file mode 100644
index 0000000..d23d70e
--- /dev/null
+++ b/.vscode/launch.json
@@ -0,0 +1,18 @@
+{
+    // 使用 IntelliSense 了解相关属性。 
+    // 悬停以查看现有属性的描述。
+    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
+    "version": "0.2.0",
+    "configurations": [
+        {
+            "type": "pwa-node",
+            "request": "launch",
+            "name": "Launch Program",
+            "skipFiles": [
+                "<node_internals>/**"
+            ],
+            "program": "${workspaceFolder}\\index.js",
+            "args": ["init"]
+        }
+    ]
+}
\ No newline at end of file

index.js 74(+74 -0)

diff --git a/index.js b/index.js
index e69de29..b422808 100644
--- a/index.js
+++ b/index.js
@@ -0,0 +1,74 @@
+import program from 'commander';
+import inquirer from 'inquirer';
+import downloadGit from 'download-git-repo';
+import ora from 'ora';
+import chalk from 'chalk';
+import fs from 'fs/promises';
+const projectList = [
+    // { name: 'cd-web-m', value: 'http://git.daicms.com/cd-web-m.git#master' },
+    { name: 'dai-vue-m', value: 'http://git.daicms.com/dai-vue-temp-m.git#master' },
+    { name: 'dai-web-w', value: 'http://git.daicms.com/dai-vue-temp-m.git#master' }
+];
+program
+    .command('init')
+    // .alias('i')
+    // .description('初始化')
+    // .option('-x, --xxx', 'xxx') // 有参数时使用
+    .action(async () => {
+        let answers = await inquirer.prompt([
+            {
+                name: 'name',
+                message: '请输入项目名称'
+            },
+            {
+                name: 'description',
+                message: '请输入项目描述(可为空)',
+                default: 'description'
+            },
+            {
+                name: 'author',
+                message: '请输入作者名称(可为空)',
+                default: 'description'
+            },
+            {
+                type: 'list',
+                name: 'url',
+                message: '请选择项目类型',
+                choices: projectList
+            }
+        ]);
+        console.log(answers);
+        let isExist = false;
+        try {
+            await fs.stat(answers.name);
+            isExist = true;
+            console.error(chalk.red('目录已经存在,创建失败'));
+        } catch (ex) {}
+        if (!isExist) {
+            const spinner = ora('下载中...');
+            spinner.start();
+            downloadGit('direct:' + answers.url, answers.name, { clone: true }, async (err) => {
+                if (err) {
+                    spinner.fail();
+                    console.log(chalk.red(err));
+                } else {
+                    spinner.succeed();
+                    const fileName = `${answers.name}/package.json`;
+                    const meta = {
+                        name: answers.name,
+                        description: answers.description,
+                        author: answers.author
+                    };
+                    // if (await fs.access(fileName)) {
+                    let content = await fs.readFile(fileName);
+                    content = JSON.parse(content.toString());
+                    Object.assign(content, meta);
+                    await fs.writeFile(fileName, JSON.stringify(content, null, 4));
+                    // }
+                    console.log(chalk.green('项目创建成功'));
+                }
+            });
+        }
+    });
+
+program.parse(process.argv);

package.json 4(+3 -1)

diff --git a/package.json b/package.json
index eec4e23..16c21dc 100644
--- a/package.json
+++ b/package.json
@@ -7,13 +7,15 @@
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1"
   },
+  "bin":{
+    "dai-cli":"./index.js"
+  },
   "author": "",
   "license": "ISC",
   "dependencies": {
     "chalk": "^5.0.0",
     "commander": "^8.3.0",
     "download-git-repo": "^3.0.2",
-    "handlebars": "^4.7.7",
     "inquirer": "^8.2.0",
     "ora": "^6.0.1"
   }