dai-cli

优化脚手架

2022/4/13 18:34:08

更改列表

index.js 143(+92 -51)

package.json 42(+21 -21)

详细信息

index.js 143(+92 -51)

diff --git a/index.js b/index.js
index 5b1d74c..2151338 100644
--- a/index.js
+++ b/index.js
@@ -14,26 +14,25 @@ const projectList = [
 //下载项目
 let downloadProject = function (answers) {
     return new Promise(function (a, b) {
+        //创建一个临时目录,因为git 只能在空目录 clone
+        let downDir = './';
+        let temporaryDir = downDir + '_gitDownload/';
         const spinner = ora('下载中...');
         spinner.start();
-        downloadGit('direct:' + answers.url, './', { clone: true }, async (err) => {
+        downloadGit('direct:' + answers.url, temporaryDir, { clone: true }, async (err) => {
             if (err) {
                 spinner.fail();
                 // console.log(chalk.red(err));
                 b(err);
             } else {
+                //转移
+                let list = await fs.readdir(temporaryDir);
+                // console.log(list);
+                for (let file of list) {
+                    await fs.rename(temporaryDir + file, downDir + file);
+                }
+                await fs.rmdir(temporaryDir);
                 spinner.succeed();
-                const fileName = './package.json';
-                const meta = {
-                    name: answers.name,
-                    description: answers.description,
-                    author: answers.author
-                };
-                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('项目创建成功'));
                 a();
             }
         });
@@ -41,26 +40,25 @@ let downloadProject = function (answers) {
 };
 let downloadModule = function (options) {
     return new Promise(function (a, b) {
+        //创建一个临时目录,因为git 只能在空目录 clone
+        let downDir = options.path;
+        let temporaryDir = downDir + '_gitDownload/';
         const spinner = ora('下载中...');
         spinner.start();
-        downloadGit('direct:' + options.url, options.path, { clone: true }, async (err) => {
+        downloadGit('direct:' + options.url, temporaryDir, { clone: true }, async (err) => {
             if (err) {
                 spinner.fail();
                 // console.log(chalk.red(err));
                 b(err);
             } else {
+                //转移
+                let list = await fs.readdir(temporaryDir);
+                // console.log(list);
+                for (let file of list) {
+                    await fs.rename(temporaryDir + file, downDir + file);
+                }
+                await fs.rmdir(temporaryDir);
                 spinner.succeed();
-                // const fileName = './package.json';
-                // const meta = {
-                //     name: answers.name,
-                //     description: answers.description,
-                //     author: answers.author
-                // };
-                // 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('模块添加成功'));
                 a();
             }
         });
@@ -112,15 +110,31 @@ let exec = function () {
                     //     ]
                     // }
                 ]);
-                console.log(answers);
+                // console.log(answers);
 
                 //判断目录是否为空
                 let list = await fs.readdir('./');
                 if (list.length > 0) {
-                    console.error(chalk.redBright('目录不为空,创建失败'));
-                    return;
+                    //允许在一个git 空目录中创建项目
+                    if (list.length == 1 && list[0] == '.git') {
+                    } else {
+                        console.error(chalk.redBright('目录不为空,创建失败'));
+                        // return;
+                    }
                 }
                 await downloadProject(answers);
+                //更改配置
+                const fileName = './package.json';
+                const meta = {
+                    name: answers.name,
+                    description: answers.description,
+                    author: answers.author
+                };
+                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
             .command('add')
@@ -134,37 +148,64 @@ let exec = function () {
                         message: '请输入功能模块名称(功能模块将添加到目录 modules 中)'
                     }
                 ]);
-                console.log(answers);
+                // console.log(answers);
+                let path = './modules/' + answers.name + '/';
                 //检测目录是否存在
-                debugger;
-                let path = './modules/' + answers.name;
-                let dir;
-                try {
-                    dir = await fs.stat(path);
-                } catch (ex) {
-                    //没有则创建目录
-                    if (ex.errno == '-4058') {
-                        console.log(chalk.green('目录没找到,创建 ' + path));
-                        let dirFile = await fs.mkdir(path, { recursive: true });
+                {
+                    let dir;
+                    try {
                         dir = await fs.stat(path);
+                    } catch (ex) {
+                        //没有则创建目录
+                        if (ex.errno == '-4058') {
+                            console.log(chalk.green('目录没找到,创建 ' + path));
+                            await fs.mkdir(path, { recursive: true });
+                            dir = await fs.stat(path);
+                        }
+                    }
+                    //判断是否为文件夹
+                    if (!dir.isDirectory()) {
+                        console.log(chalk.red('路径不是目录,模块添加失败'));
+                        return;
+                    }
+                    //判断目录是否为空
+                    let list = await fs.readdir(path);
+                    if (list.length > 0) {
+                        console.error(chalk.redBright('目录不为空,模块添加失败'));
+                        return;
                     }
                 }
-                //判断是否为文件夹
-                if(!dir.isDirectory()){
-                    console.log(chalk.red('路径不是目录,模块添加失败'));
-                    return;
-                }
-                //判断目录是否为空
-                let list = await fs.readdir(path);
-                if (list.length > 0) {
-                    console.error(chalk.redBright('目录不为空,模块添加失败'));
-                    return;
-                }
-
                 await downloadModule({
                     path,
-                    url: 'http://git.daicms.com/dai-vue-module-m.git.git#master'
+                    url: 'http://git.daicms.com/dai-vue-module-m.git#master'
                 });
+                //替换文件内的关键字
+                {
+                    let fileList = [path + 'static/style/style.less', path + 'routes.js'];
+                    let keys = [
+                        { oldval: 'module-test', val: 'module-' + answers.name },
+                        { oldval: 'name-test', val: answers.name }
+                    ];
+                    for (let file of fileList) {
+                        let text = await fs.readFile(file);
+                        text = text.toString();
+                        for (let key of keys) {
+                            // text = text.replaceAll(key.oldval, key.val);
+                            text = text.replace(new RegExp(key.oldval, 'gm'), key.val);
+                        }
+                        await fs.writeFile(file, text);
+                    }
+                }
+                //向项目中添加引用
+                {
+                    let text = await fs.readFile('./main/modules.js');
+                    text = text.toString();
+                    text += `
+    import main from '../modules/${answers.name}/index.js';
+    export { ${answers.name} };`;
+                    await fs.writeFile('./main/modules.js', text);
+                }
+                console.log(chalk.green('模块添加成功'));
             });
         program.command('dev').action(async () => {
             console.log('启动 dev 服务');

package.json 42(+21 -21)

diff --git a/package.json b/package.json
index 16c21dc..be593e1 100644
--- a/package.json
+++ b/package.json
@@ -1,22 +1,22 @@
 {
-  "name": "dai-cli",
-  "type": "module",
-  "version": "1.0.0",
-  "description": "",
-  "main": "index.js",
-  "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",
-    "inquirer": "^8.2.0",
-    "ora": "^6.0.1"
-  }
-}
+    "name": "x",
+    "type": "module",
+    "version": "1.0.0",
+    "description": "description",
+    "main": "index.js",
+    "scripts": {
+        "test": "echo \"Error: no test specified\" && exit 1"
+    },
+    "bin": {
+        "dai-cli": "./index.js"
+    },
+    "author": "description",
+    "license": "ISC",
+    "dependencies": {
+        "chalk": "^5.0.0",
+        "commander": "^8.3.0",
+        "download-git-repo": "^3.0.2",
+        "inquirer": "^8.2.0",
+        "ora": "^6.0.1"
+    }
+}
\ No newline at end of file