Skip to content

Commit

Permalink
Optimization command code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 10, 2018
1 parent 884b10b commit ce2c48f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 39 deletions.
65 changes: 43 additions & 22 deletions .bin/kkt.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,57 @@
#!/usr/bin/env node

const program = require('commander');
const chalk = require('colors-cli');
const pkg = require('../package.json');
const Start = require('../script/start');
const Build = require('../script/build');
const Deploy = require('../script/deploy');

process.env.HOST = '0.0.0.0';
process.env.PORT = 19870;

program
.description('Rapid React development, Cli tool for creating react apps.')
.version(pkg.version, '-v, --version')
.usage('<command> [options]')

if (program.host) {
program.host = program.host.split(':');
process.env.HOST = program.host[0] || '0.0.0.0';
process.env.PORT = parseInt(program.host[1]) || 19870;
}

program
.command('create <app-name>')
.description('create a new project powered by kkt')
.option('-c, --clone', 'Use git clone when fetching remote preset')
.action((name, cmd) => {
// console.log('create:app-name');
// require('../lib/create')(name, cleanArgs(cmd))
.option('-f, --force', 'Overwrite target directory if it exists')
.on('--help', () => {
console.log()
console.log(' Examples:')
console.log()
console.log(' # create a new project with an official template')
console.log(' $ kkt init default my-project')
console.log()
console.log(' # create a new project straight from a gitlab.net template')
console.log(' $ kkt init username/repo my-project')
console.log()
})
.action((cmd) => {
// require('../src/create')(cmd)
})

program
.command('build')
.description('Builds the app for production to the dist folder.')
.option('--host <host>', 'The port and host.', `${process.env.HOST}:${process.env.PORT}`)
.action((name, cmd) => {
Build(name, cmd);
.action((cmd) => {
require('../src/build')(cmd)
})

program
.command('start')
.description('Runs the app in development mode.')
.option('--host <host>', 'The port and host.', `${process.env.HOST}:${process.env.PORT}`)
.action((name, cmd) => {
Start(name, cmd)
.on('--help', () => {
console.log()
console.log(' Examples:')
console.log()
console.log(' $ kkt start')
console.log(' $ kkt start --host 127.0.0.0:8118')
console.log()
})
.action((cmd) => {
require('../src/start')(cmd)
})

program
Expand All @@ -48,8 +60,16 @@ program
.option('-b, --branch [name]', 'Specify branch.', 'gh-pages')
.option('-d, --dir [path]', 'Specify the deployment directory.', 'dist')
.option('-u, --url <dir>', 'Specify repository URL.')
.action((name, cmd) => {
Deploy(name, cmd);
.action((cmd) => {
require('../src/deploy')(cmd)
})

program
.arguments('<command>')
.action((cmd) => {
program.outputHelp()
console.log(` ` + chalk.red(`Unknown command ${chalk.yellow(cmd)}.`))
console.log()
})

program.on('--help', function () {
Expand All @@ -58,10 +78,11 @@ program.on('--help', function () {
console.log(' $ kkt start');
console.log(' $ kkt build');
console.log();
console.log();
})

program.parse(process.argv);

if (!process.argv.slice(2).length) {
program.outputHelp();
}

program.parse(process.argv);
6 changes: 2 additions & 4 deletions conf/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ const resolveApp = relativePath => PATH.resolve(appDirectory, relativePath);

function getKKTRCPath(_path) {
const pathRc = resolveApp(_path);
if (!FS.existsSync(pathRc)) {
return null;
}
return pathRc;
if (FS.existsSync(pathRc)) return pathRc;
return null;
}

module.exports = {
Expand Down
8 changes: 3 additions & 5 deletions script/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ const paths = require('../conf/path');
require('colors-cli/toxic');

module.exports = function serve() {
const webpackConf = conf();
let compiler = null;
let webpackConf = conf();
if (paths.appKKTRC) {
const kktrc = require(paths.appKKTRC); // eslint-disable-line
compiler = webpack(kktrc(webpackConf, null) || webpackConf);
} else {
compiler = webpack(webpackConf);
webpackConf = kktrc(webpackConf, null) || webpackConf;
}

const compiler = webpack(webpackConf);
compiler.run((err, stats) => {
// 官方输出参数
// https://webpack.js.org/configuration/stats/
Expand Down
32 changes: 24 additions & 8 deletions script/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ const paths = require('../conf/path');
const webpackDevConf = require('../conf/webpack.config.dev');
const createDevServerConfig = require('../conf/webpack.config.server');

function clearConsole() {
process.stdout.write(process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H');
}

module.exports = function server() {
let DEFAULT_PORT = process.env.PORT || 19870;
let DEFAULT_PORT = parseInt(process.env.PORT, 10) || 19870;
const HOST = process.env.HOST || '0.0.0.0';
const webpackConf = webpackDevConf();
// 如果配置文件存在读取配置文件
let compiler = null;
let webpackConf = webpackDevConf();

let webpackServerConf = null;
// 如果配置文件存在读取配置文件
if (paths.appKKTRC) {
const kktrc = require(paths.appKKTRC); // eslint-disable-line
compiler = webpack(kktrc(webpackConf, null) || webpackConf);
webpackConf = kktrc(webpackConf, null) || webpackConf;
webpackServerConf = kktrc(null, createDevServerConfig(webpackConf)) || createDevServerConfig(webpackConf);
} else {
compiler = webpack(webpackConf);
webpackServerConf = createDevServerConfig(webpackConf);
}

const compiler = webpack(webpackConf);
// https://webpack.js.org/api/compiler-hooks/#aftercompile
// 编译完成之后打印日志
compiler.hooks.done.tap('done', () => {
Expand All @@ -32,13 +36,25 @@ module.exports = function server() {

detect(DEFAULT_PORT).then((_port) => {
if (DEFAULT_PORT !== _port) DEFAULT_PORT = _port;
new WebpackDevServer(compiler, webpackServerConf).listen(DEFAULT_PORT, HOST, (err) => {
const devServer = new WebpackDevServer(compiler, webpackServerConf);
devServer.listen(DEFAULT_PORT, HOST, (err) => {
if (err) {
return console.log(err); // eslint-disable-line
}
clearConsole();
openBrowsers(`http://${HOST}:${DEFAULT_PORT}`);
});

['SIGINT', 'SIGTERM'].forEach((sig) => {
process.on(sig, () => {
devServer.close();
process.exit();
});
});
}).catch((err) => {
console.log('~~~::::', err); // eslint-disable-line
if (err && err.message) {
console.log(err.message); // eslint-disable-line
}
process.exit(1);
});
};

0 comments on commit ce2c48f

Please sign in to comment.