dai-cli

更改列表

index.js 140(+75 -65)

src/webpack.dev.js 13(+13 -0)

详细信息

diff --git a/.vscode/launch.json b/.vscode/launch.json
index d23d70e..27b2eda 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -12,7 +12,7 @@
                 "<node_internals>/**"
             ],
             "program": "${workspaceFolder}\\index.js",
-            "args": ["init"]
+            "args": ["dev"]
         }
     ]
 }
\ No newline at end of file

index.js 140(+75 -65)

diff --git a/index.js b/index.js
index 77fbce6..70f5154 100644
--- a/index.js
+++ b/index.js
@@ -4,21 +4,23 @@ import downloadGit from 'download-git-repo';
 import ora from 'ora';
 import chalk from 'chalk';
 import fs from 'fs/promises';
+
+// import webpackDev from './src/webpack.dev.js';
+
 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' }
 ];
 //下载项目
-let downloadProject = function () {
+let downloadProject = function (answers) {
     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));
+                // console.log(chalk.red(err));
+                b(err);
             } else {
                 spinner.succeed();
                 const fileName = './package.json';
@@ -27,80 +29,88 @@ let downloadProject = function () {
                     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('项目创建成功'));
+                a();
             }
         });
     });
 };
-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);
+let runDev = function () {
+    // debugger
+    // var webpackDev = require('./src/webpack.dev.js');
+    // var config = require(__dirname+'/build/config.js');
+    // webpackDev(config);
+};
+let exec = function () {
+    return new Promise(async function (a, b) {
+        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, './', { 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('项目创建成功'));
+                //判断目录是否为空
+                let list = await fs.readdir('./');
+                if (list.length > 0) {
+                    console.error(chalk.redBright('目录不为空,创建失败'));
+                    return;
                 }
+                await downloadProject(answers);
             });
+        program
+            .command('add')
+            // .alias('i')
+            // .description('初始化')
+            // .option('-x, --xxx', 'xxx') // 有参数时使用
+            .action(async () => {
+                let answers = await inquirer.prompt([
+                    {
+                        type: 'list',
+                        name: 'url',
+                        message: '请选择模块类型',
+                        choices: [{ name: '独立模块', value: 'http://git.daicms.com/dai-vue-temp-m.git#master' }]
+                    }
+                ]);
+                console.log(answers);
+                // await downloadProject(answers);
+            });
+        program.command('dev').action(async () => {
+            console.log('启动 dev 服务');
+            runDev();
         });
-
-    program.parse(process.argv);
+        program.command('build').action(async () => {
+            console.log('启动 build 服务');
+        });
+        program.parse(process.argv);
+    });
 };
 exec()
     .then(function () {
diff --git a/src/webpack.build.js b/src/webpack.build.js
index e69de29..4189a0f 100644
--- a/src/webpack.build.js
+++ b/src/webpack.build.js
@@ -0,0 +1,19 @@
+var webpack = require('webpack');
+var webpackConfigBuild = require('./webpack.config.build.js');
+
+var webpackBuild = function (config) {
+    var webpackConfig = webpackConfigBuild(config);
+    console.log('开始打包');
+    webpack(webpackConfig, (err, stats) => {
+        if (err || stats.hasErrors()) {
+            console.log(err);
+            console.log(stats.compilation.errors);
+            // 构建过程出错
+            console.error('打包失败');
+            return;
+        }
+        // console.log(stats);
+        console.log('打包成功');
+    });
+};
+module.exports = webpackBuild;
diff --git a/src/webpack.config.build.js b/src/webpack.config.build.js
index 9a4077e..a3f3243 100644
--- a/src/webpack.config.build.js
+++ b/src/webpack.config.build.js
@@ -2,18 +2,13 @@ var webpack = require('webpack');
 // 用于生成HTML文件并自动注入依赖文件(link/script)的webpack插件
 var HtmlWebpackPlugin = require('html-webpack-plugin');
 var CopyWebpackPlugin = require('copy-webpack-plugin');
+var { CleanWebpackPlugin } = require('clean-webpack-plugin');
 var VueLoaderPlugin = require('vue-loader/lib/plugin');
 var path = require('path');
 var webpack = require('webpack');
 
 module.exports = function (config) {
     let webpackConfig = {
-        // devtool: 'eval',
-        devServer: {
-            disableHostCheck: true,
-            host: '0.0.0.0',
-            port: 8889
-        },
         entry: {
             // babel: 'babel-polyfill',
             index: './main/index.js'
@@ -37,14 +32,7 @@ module.exports = function (config) {
                 // },
                 {
                     test: /\.css$/,
-                    use: [
-                        {
-                            loader: 'vue-style-loader'
-                        },
-                        {
-                            loader: 'css-loader'
-                        }
-                    ]
+                    use: [{ loader: 'vue-style-loader' }, { loader: 'css-loader' }]
                 },
                 {
                     test: /\.less$/,
@@ -85,19 +73,16 @@ module.exports = function (config) {
             new HtmlWebpackPlugin({
                 filename: './index.html',
                 template: './main/index.html',
-                // favicon: './main/favicon.ico',
+                favicon: './main/favicon.ico',
                 inject: true,
-                title: '兔小乖字帖'
+                title: config.title || 'DaiVUE'
             }),
             new VueLoaderPlugin(),
+            new CleanWebpackPlugin({
+                cleanOnceBeforeBuildPatterns: ['**/*', './dist']
+            }),
             //拷贝文件
-            // new CopyWebpackPlugin([
-            //     {
-            //         from: './src/content/other',
-            //         to: './static/other',
-            //         ignore: ['.*']
-            //     }
-            // ]),
+            new CopyWebpackPlugin(config.copy || []),
             new webpack.DefinePlugin({
                 // 定义环境和变量
                 'process.env': {
diff --git a/src/webpack.config.dev.js b/src/webpack.config.dev.js
index 1456736..81fd527 100644
--- a/src/webpack.config.dev.js
+++ b/src/webpack.config.dev.js
@@ -3,12 +3,14 @@ var HtmlWebpackPlugin = require('html-webpack-plugin');
 var VueLoaderPlugin = require('vue-loader/lib/plugin');
 var webpack = require('webpack');
 module.exports = function (config) {
+    console.log(config);
     var webpackConfig = {
         devtool: 'eval',
         devServer: {
             disableHostCheck: true,
-            host: '0.0.0.0',
-            port: 8881
+            host: 'localhost',
+            port: config.port || 8080,
+            open: true
         },
         entry: {
             index: './main/index.js'
@@ -78,12 +80,10 @@ module.exports = function (config) {
             new HtmlWebpackPlugin({
                 filename: './index.html',
                 template: './main/index.html',
+                favicon: './main/favicon.ico',
                 inject: true,
-                title: '兔小乖字帖'
+                title: config.title || 'DaiVUE'
             }),
-            // new webpack.DefinePlugin({
-            //     'process.env.NODE_ENV': JSON.stringify('production')
-            // }),
             new webpack.DefinePlugin({
                 // 定义环境和变量
                 'process.env': {

src/webpack.dev.js 13(+13 -0)

diff --git a/src/webpack.dev.js b/src/webpack.dev.js
index e69de29..169ae5e 100644
--- a/src/webpack.dev.js
+++ b/src/webpack.dev.js
@@ -0,0 +1,13 @@
+import webpack from 'webpack';
+import WebpackDevServer from 'webpack-dev-server';
+import webpackConfigDev from './webpack.config.dev.js';
+var webpackDev = function (config) {
+    var webpackConfig = webpackConfigDev(config);
+    var compiler = webpack(webpackConfig);
+    var server = new WebpackDevServer(compiler, {
+        hot: true,
+        open:true
+    });
+    server.listen(webpackConfig.devServer.port);
+};
+export default webpackDev;