-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·172 lines (150 loc) · 3.72 KB
/
cli.js
File metadata and controls
executable file
·172 lines (150 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const meow = require('meow');
const findup = require('find-up');
const readPkg = require('read-pkg-up').sync;
const openBrowser = require('react-dev-utils/openBrowser');
const chalk = require('chalk');
const clipboard = require('clipboardy');
const config = require('pkg-conf').sync('reunify');
const pkg = readPkg().pkg;
const cli = meow(
`
${chalk.gray('Usage')}
${chalk.gray('Server')}
${chalk.cyan('reunify pages')}
${chalk.gray('Export')}
${chalk.cyan('reunify export pages')}
${chalk.gray('Options')}
--webpack Path to webpack config file
--match String to match routes against using minimatch
${chalk.gray('Server')}
-o --open Open server in default browser
-p --port Port for server
-h --host Host address for server
--analyze Runs with webpack-bundle-analyzer plugin
${chalk.gray('Export')}
-d --out-dir Output directory (default dist)
-s --static Output static HTML without JS bundle
-t --template Path to custom HTML template
--basename Basename for URL paths
--title Page title
`,
{
flags: {
// dev
open: {
type: 'boolean',
alias: 'o'
},
port: {
type: 'number',
alias: 'p'
},
host: {
type: 'string',
alias: 'h'
},
analyze: {},
// export
outDir: {
type: 'string',
alias: 'd'
},
static: {
type: 'boolean'
},
template: {
type: 'string',
alias: 't'
},
// shared
config: {
type: 'string',
alias: 'c'
},
match: {
type: 'string'
},
scope: {
type: 'string'
},
webpack: {
type: 'string'
},
debug: {
type: 'boolean'
}
}
}
);
const [cmd, file] = cli.input;
if (!cmd) {
cli.showHelp(0);
}
const input = path.resolve(file || cmd);
const stats = fs.statSync(input);
const dirname = stats.isDirectory() ? input : path.dirname(input);
const filename = stats.isDirectory() ? null : input;
const opts = Object.assign(
{
input,
dirname,
filename,
stats,
outDir: 'dist',
basename: '',
scope: {},
pkg
},
config,
cli.flags
);
opts.outDir = path.resolve(opts.outDir);
if (opts.config) opts.config = path.resolve(opts.config);
if (opts.webpack) {
opts.webpack = require(path.resolve(opts.webpack));
} else {
const webpackConfig = findup.sync('webpack.config.js', {cwd: dirname});
if (webpackConfig) opts.webpack = require(webpackConfig);
}
if (opts.template) {
opts.template = require(path.resolve(opts.template));
}
const handleError = err => {
console.error(err);
process.exit(1);
};
process.stdout.write(chalk.cyan('Reunify '));
switch (cmd) {
case 'export':
console.log('Exporting static site');
const exportSite = require('./lib/export');
exportSite(opts)
.then(res => {
console.log('Site saved to ' + opts.outDir);
})
.catch(handleError);
break;
case 'run':
default:
console.log(`Starting server (${process.env.NODE_ENV})`);
const run = require('./lib/run');
opts.mode = 'development';
run(opts)
.then(({server}) => {
const {port, host} = server.options;
const url = `http://${host}:${port}`;
console.log('Server listening on', chalk.green(url), chalk.gray('(copied to clipboard)'));
clipboard.write(url);
if (opts.open) {
openBrowser(url);
}
})
.catch(handleError);
break;
}
require('update-notifier')({
pkg: require('./package.json')
}).notify();