UNPKG

3.23 kBJavaScriptView Raw
1#!/usr/bin/env node
2var commander = require('commander');
3var htmlBundler = require('../src/index.js');
4var logger = require('../src/utils/logger.js');
5var process = require('process');
6var fs = require('fs-extra');
7var path = require('path');
8var version = JSON.parse(fs.readFileSync(path.join(__dirname, '../package.json'), 'utf8')).version;
9var currentPath = process.cwd();
10
11const DEFAULT_CONFIG = path.join(__dirname, '../src/config.example.js');
12const REAL_CONFIG = path.join(currentPath, './html-bundler.config.js');
13const DEFAULT_WEBPACK_CONFIG = path.join(__dirname, '../src/webpack.config.example.js');
14const REAL_WEBPACK_CONFIG = path.join(currentPath, './webpack.config.js');
15const DEFAULT_DLL_CONFIG = path.join(__dirname, '../src/webpack.dll.example.js');
16const REAL_DLL_CONFIG = path.join(currentPath, './webpack.dll.js');
17const DEFAULT_STRUCTURE = path.join(__dirname, '../src/structure');
18
19
20/*
21 * 如果不存在则创建html-bundler.config.js和webpack.config.js, 根据自己项目进行修改
22 */
23commander.version(version)
24
25
26commander.command('init')
27 .description('if your project rootpath has not `html-bundler.config.js` & `webpack.config.js`, this command will create these files')
28 .option('-w --webpack')
29 .action(function(webpack) {
30 if (!fs.existsSync(REAL_CONFIG)) {
31 fs.copySync(DEFAULT_CONFIG, REAL_CONFIG);
32 logger.info('html-bundler配置文件创建成功, 请根据项目情况进行配置');
33 }
34 if (!fs.existsSync(REAL_WEBPACK_CONFIG) && webpack.webpack) {
35 fs.copySync(DEFAULT_WEBPACK_CONFIG, REAL_WEBPACK_CONFIG);
36 fs.copySync(DEFAULT_DLL_CONFIG, path.join(currentPath, './webpack.dll.js'));
37 logger.info('webpack配置文件创建成功, 请根据项目情况进行修改并安装依赖');
38 }
39
40 })
41/*
42 * 直接创建一个测试项目
43 */
44commander.command('create [project]')
45 .description('create a empty project')
46 .option('-w --webpack')
47 .action(function(project, webpack) {
48 fs.copySync(DEFAULT_STRUCTURE, path.join(currentPath, project));
49 fs.copySync(DEFAULT_CONFIG, path.join(currentPath, project, './html-bundler.config.js'));
50 logger.notice('项目' + project + '创建成功');
51 if (webpack.webpack) {
52 fs.copySync(DEFAULT_WEBPACK_CONFIG, path.join(currentPath, project, './webpack.config.js'));
53 fs.copySync(DEFAULT_DLL_CONFIG, path.join(currentPath, project, './webpack.dll.js'));
54 logger.info('webpack配置文件创建成功, 请根据项目情况进行修改并安装依赖');
55 }
56 })
57
58/*
59 * 开发环境
60 */
61commander.command('dev')
62 .description('dev')
63 .option('-p --port <port>')
64 .action(function(port) {
65 htmlBundler('dev', port.port);
66 })
67
68/*
69 * 生产环境打包
70 */
71commander.command('dest')
72 .description('dest')
73 .action(function() {
74 htmlBundler('dest');
75 })
76
77/*
78 * QA环境打包
79 */
80commander.command('qa')
81 .description('qa')
82 .action(function() {
83 htmlBundler('qa');
84 })
85
86/*
87 * RD环境打包
88 */
89commander.command('rd')
90 .description('rd')
91 .action(function() {
92 htmlBundler('rd');
93 })
94
95commander.parse(process.argv);
\No newline at end of file