1 | #!/usr/bin/env node
|
2 |
|
3 | let argv = require('minimist')(process.argv.slice(2));
|
4 | const log = require('../dist/lib/log')
|
5 | const nps = require('path')
|
6 |
|
7 | const opts = {
|
8 | verbose: !!argv.v || !!argv.verbose,
|
9 | help: !!argv.h || !!argv.help,
|
10 | version: !!argv.version,
|
11 | openSync: !argv.noopen,
|
12 | port: argv.port || argv.p,
|
13 | path: argv._.length > 0 ? argv._[0] : process.cwd(),
|
14 | type: argv.type || argv.t || 'js',
|
15 | build: argv.build || argv.b,
|
16 | buildCopyPath: argv.buildcopy,
|
17 | initPath: argv.i || argv.init
|
18 | }
|
19 |
|
20 | opts.path = nps.resolve(opts.path)
|
21 |
|
22 |
|
23 | if (opts.help) {
|
24 | console.log(`Usage: gojs [path] --options`);
|
25 | console.log('');
|
26 | console.log('Options:');
|
27 | console.log('');
|
28 | console.log(' --version get current version');
|
29 | console.log(' -h --help how to use it');
|
30 | console.log(' -v --verbose verbose talk');
|
31 | console.log(' --noopen disable open browser synchronously');
|
32 | console.log(' -p --port set server port');
|
33 | console.log(' -t --type set type (only support `js` now, and will add `ts/coffee` in the future)');
|
34 | console.log(' -b --build set entry for build.');
|
35 | console.log(' --buildcopy set copy file or directory for build.');
|
36 | console.log(' -i --init [path] open path automatically when server start. (not work when `--noopen`)');
|
37 | console.log('');
|
38 | process.exit(0)
|
39 | }
|
40 |
|
41 | if (opts.version) {
|
42 | console.log(require('../package.json').version)
|
43 | process.exit(0)
|
44 | }
|
45 |
|
46 |
|
47 | if (opts.build) {
|
48 | const template = require('lodash').template
|
49 | const readFileSync = require('fs').readFileSync
|
50 | const fsExtra = require('fs-extra')
|
51 | const getPlugins = require('../dist/lib/get-webpack-plugins')
|
52 | const defaultTemplatePath = nps.join(__dirname, '../dist/template/build.html')
|
53 | const build = require('../dist/build')
|
54 | const ed = require('../dist/lib/encode-decode')
|
55 | const ConfigAdaptor = require('../dist/lib/ConfigAdaptor')
|
56 | const dest = nps.join(opts.path, '.dist')
|
57 | const adaptor = new ConfigAdaptor(opts.path, opts.type, false)
|
58 | adaptor.addEntry(opts.build)
|
59 | const config = adaptor.getConfig()
|
60 | const callback = function (err, stats) {
|
61 | if (err) {
|
62 | throw err
|
63 | } else {
|
64 | log.info(`Build Done in ${dest}`)
|
65 | }
|
66 | }
|
67 |
|
68 | config.plugins = getPlugins({dev: false, config})
|
69 | config.output.path = dest
|
70 |
|
71 | config.module.loaders = config.module.loaders.map(loader => {
|
72 |
|
73 | return loader
|
74 | })
|
75 |
|
76 | fsExtra.emptyDirSync(dest)
|
77 |
|
78 |
|
79 | if (!opts.buildCopyPath) {
|
80 | build({
|
81 | compile: true,
|
82 | isBuf: true,
|
83 | config: config,
|
84 | src: template(readFileSync(defaultTemplatePath).toString())({
|
85 | encodeName: ed.encode(opts.build),
|
86 | name: opts.build
|
87 | }),
|
88 | dest: nps.join(dest, 'index.html')
|
89 | }, callback)
|
90 | } else {
|
91 | build({
|
92 | compile: true,
|
93 | isBuf: false,
|
94 | config: config,
|
95 | src: opts.buildCopyPath,
|
96 | dest: dest,
|
97 | }, callback)
|
98 | }
|
99 |
|
100 | } else {
|
101 | const GoJS = require('../dist')
|
102 | const listen = require('../dist/default-listen')
|
103 | const gojs = new GoJS(opts)
|
104 | listen(gojs)
|
105 |
|
106 | gojs.start((err, port) => {
|
107 | log.info(`current work directory ${gojs.opts.path}`)
|
108 | })
|
109 |
|
110 | process.on('SIGINT', () => {
|
111 |
|
112 | gojs.stop()
|
113 | process.exit()
|
114 | })
|
115 |
|
116 | } |
\ | No newline at end of file |