dai-cli

f

2022/11/9 16:48:09

更改列表

index.js 50(+33 -17)

详细信息

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

index.js 50(+33 -17)

diff --git a/index.js b/index.js
index d26e267..189b460 100644
--- a/index.js
+++ b/index.js
@@ -8,17 +8,30 @@ import fs from 'fs/promises';
 import path from 'path';
 import { fileURLToPath } from 'url';
 
+async function rmdirasync(filepath) {
+    let stat = await fs.stat(filepath)
+    if (stat.isfile()) {
+        await fs.unlink(filepath)
+    } else {
+        let dirs = await fs.readdir(filepath)
+        dirs = dirs.map(dir => rmdirasync(path.join(filepath, dir)))
+        await promise.all(dirs)
+        await fs.rmdir(filepath)
+    }
+}
+
+
 const projectList = [
-    { name: 'site-m 移动端项目', value: 'http://git.daicms.com/dai-cli.git#master', path: '/site-m' },
-    { name: 'site-w 电脑端项目', value: 'http://git.daicms.com/dai-cli.git#master', path: '/site-w' },
+    { name: 'site-m 移动端项目', value: { url: 'http://git.daicms.com/dai-cli.git#master', path: '/site-m/' } },
+    { name: 'site-w 电脑端项目', value: { url: 'http://git.daicms.com/dai-cli.git#master', path: '/site-w/' } },
     {
         name: 'site-admin-iview 电脑端后台模板(iview)',
-        value: 'http://git.daicms.com/dai-cli.git#master', path: '/site-admin-iview'
+        value: { url: 'http://git.daicms.com/dai-cli.git#master', path: '/site-admin-iview/' }
     }
 ];
 const moduleList = [
-    { name: 'module-m 移动端模块', value: 'http://git.daicms.com/dai-cli.git#master', path: '/module-m' },
-    { name: 'module-w 电脑端模块', value: 'http://git.daicms.com/dai-cli.git#master', path: '/module-w' }
+    { name: 'module-m 移动端模块', value: { url: 'http://git.daicms.com/dai-cli.git#master', path: '/module-m/' } },
+    { name: 'module-w 电脑端模块', value: { url: 'http://git.daicms.com/dai-cli.git#master', path: '/module-w/' } }
 ];
 //下载项目
 let downloadProject = function (answers) {
@@ -28,19 +41,19 @@ let downloadProject = function (answers) {
         let temporaryDir = downDir + '_gitDownload/';
         const spinner = ora('下载中...');
         spinner.start();
-        downloadGit('direct:' + answers.url, temporaryDir, { clone: true }, async (err) => {
+        downloadGit('direct:' + answers.git.url, temporaryDir, { clone: true }, async (err) => {
             if (err) {
                 spinner.fail();
                 // console.log(chalk.red(err));
                 b(err);
             } else {
                 //转移
-                let list = await fs.readdir(temporaryDir);
+                let list = await fs.readdir(temporaryDir + answers.git.path);
                 // console.log(list);
                 for (let file of list) {
-                    await fs.rename(temporaryDir + file, downDir + file);
+                    await fs.rename(temporaryDir + answers.git.path + file, downDir + file);
                 }
-                await fs.rmdir(temporaryDir);
+                await fs.rmdir(temporaryDir, { recursive: true, maxRetries: 5, retryDelay: 100 });
                 spinner.succeed();
                 a();
             }
@@ -54,17 +67,19 @@ let downloadModule = function (options) {
         let temporaryDir = downDir + '_gitDownload/';
         const spinner = ora('下载中...');
         spinner.start();
-        downloadGit('direct:' + options.url, temporaryDir, { clone: true }, async (err) => {
+        
+                console.log(111,options);
+        downloadGit('direct:' + options.git.url, temporaryDir, { clone: true }, async (err) => {
             if (err) {
                 spinner.fail();
                 // console.log(chalk.red(err));
                 b(err);
             } else {
                 //转移
-                let list = await fs.readdir(temporaryDir);
+                let list = await fs.readdir(temporaryDir + options.git.path);
                 // console.log(list);
                 for (let file of list) {
-                    await fs.rename(temporaryDir + file, downDir + file);
+                    await fs.rename(temporaryDir + options.git.path + file, downDir + file);
                 }
                 await fs.rmdir(temporaryDir, { recursive: true, maxRetries: 5, retryDelay: 100 });
                 spinner.succeed();
@@ -84,6 +99,7 @@ let exec = function () {
             // .description('初始化')
             // .option('-x, --xxx', 'xxx') // 有参数时使用
             .action(async () => {
+                debugger
                 let answers = await inquirer.prompt([
                     {
                         name: 'name',
@@ -101,7 +117,7 @@ let exec = function () {
                     },
                     {
                         type: 'list',
-                        name: 'url',
+                        name: 'git',
                         message: '请选择项目类型',
                         choices: projectList
                     }
@@ -125,7 +141,7 @@ let exec = function () {
                     if (list.length == 1 && list[0] == '.git') {
                     } else {
                         console.error(chalk.redBright('目录不为空,创建失败'));
-                        // return;
+                        return;
                     }
                 }
                 await downloadProject(answers);
@@ -140,7 +156,7 @@ let exec = function () {
                 content = JSON.parse(content.toString());
                 Object.assign(content, meta);
                 await fs.writeFile(fileName, JSON.stringify(content, null, 4));
-                console.log(chalk.green('项目创建成功'));
+                console.log(chalk.green('项目创建成功,请使用 npm i 安装依赖,npm run dev 启动开发,npm run build 打包发布'));
             });
         program
             .command('add')
@@ -155,7 +171,7 @@ let exec = function () {
                     },
                     {
                         type: 'list',
-                        name: 'url',
+                        name: 'git',
                         message: '请选择模块类型',
                         choices: moduleList
                     }
@@ -187,7 +203,7 @@ let exec = function () {
                         return;
                     }
                 }
-                await downloadModule({ path, url: answers.url });
+                await downloadModule({ path, git: answers.git });
                 //替换文件内的关键字
                 {
                     let fileList = [path + 'static/style/style.less', path + 'api.js', path + 'routes.js'];