dai-cli

s

2022/1/8 14:22:01

更改列表

index.js 120(+80 -40)

详细信息

index.js 120(+80 -40)

diff --git a/index.js b/index.js
index b422808..77fbce6 100644
--- a/index.js
+++ b/index.js
@@ -9,51 +9,81 @@ const projectList = [
     { 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
+//下载项目
+let downloadProject = function () {
+    return new Promise(function (a, b) {
+        
+        const spinner = ora('下载中...');
+        spinner.start();
+        downloadGit('direct:' + answers.url, './', { clone: true }, async (err) => {
+            if (err) {
+                spinner.fail();
+                console.log(chalk.red(err));
+            } else {
+                spinner.succeed();
+                const fileName = './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('项目创建成功'));
             }
-        ]);
-        console.log(answers);
-        let isExist = false;
-        try {
-            await fs.stat(answers.name);
-            isExist = true;
-            console.error(chalk.red('目录已经存在,创建失败'));
-        } catch (ex) {}
-        if (!isExist) {
+        });
+    });
+};
+let exec = async function () {
+    //判断目录是否为空
+    let list = await fs.readdir('./');
+    if (list.length > 0) {
+        console.error(chalk.redBright('目录不为空,创建失败'));
+        return;
+    }
+    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);
+
             const spinner = ora('下载中...');
             spinner.start();
-            downloadGit('direct:' + answers.url, answers.name, { clone: true }, async (err) => {
+            downloadGit('direct:' + answers.url, './', { clone: true }, async (err) => {
                 if (err) {
                     spinner.fail();
                     console.log(chalk.red(err));
                 } else {
                     spinner.succeed();
-                    const fileName = `${answers.name}/package.json`;
+                    const fileName = './package.json';
                     const meta = {
                         name: answers.name,
                         description: answers.description,
@@ -68,7 +98,17 @@ program
                     console.log(chalk.green('项目创建成功'));
                 }
             });
-        }
-    });
+        });
 
-program.parse(process.argv);
+    program.parse(process.argv);
+};
+exec()
+    .then(function () {
+        console.log('运行结束,退出');
+        process.exit(0);
+    })
+    .catch(function (ex) {
+        console.error(ex);
+        debugger;
+        process.exit(0);
+    });