-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathaction.js
More file actions
178 lines (157 loc) · 4.97 KB
/
action.js
File metadata and controls
178 lines (157 loc) · 4.97 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
173
174
175
176
177
178
'use strict';
const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
const _ = require('lodash.get');
const Archive = require('archive-tool');
const tool = require('node-tool-utils');
const Boilerplate = require('./init');
const builder = require('./builder');
const utils = require('./utils');
module.exports = class Action {
constructor(command) {
this.command = command;
this.program = command.program;
this.baseDir = command.baseDir;
}
init(boilerplate, options) {
if (options.sync) {
const filepath = path.resolve(__dirname, 'ask-sync.js');
const url = options.sync === true ? process.env.EASY_INIT_ASK_URL || 'https://raw.githubusercontent.com/easy-team/easywebpack-cli/master/lib/ask.js' : options.sync;
utils.request(url).then(res => {
fs.writeFileSync(filepath, res.data);
console.log(`${chalk.green('easy sync successfully, please run [easy init] again')}`);
}).catch(err => {
console.log(chalk.red('easy sync error:'), err);
});
} else {
return new Boilerplate(boilerplate).init(options);
}
}
install(options) {
const config = utils.initWebpackConfig(this.program, {
install: {
check: true,
npm: options.mode || 'npm'
}
});
builder.getWebpackConfig(config);
}
dev(options) {
const config = utils.initWebpackConfig(this.program, options);
builder.server(config);
}
start(options) {
const config = utils.initWebpackConfig(this.program, options);
builder.server(config);
}
build(env, options) {
const config = utils.initWebpackConfig(this.program, { env, cliDevtool : options.devtool}, { speed: options.speed });
// 编译完成, 启动 HTTP Server 访问静态页面
if (options.server) {
const done = config.config.done;
config.config.done = (multiCompiler, compilation) => {
done && done(multiCompiler, compilation);
const compiler = multiCompiler.compilers.find(item => {
return item.options.target === 'web';
});
if (compiler) { // 自动解析 output.path
const dist = compiler.options.output.path;
const port = options.server === true ? undefined : options.server;
tool.httpServer({
dist,
port
});
}
};
}
builder.build(config);
}
dll(env, options) {
const config = utils.initWebpackConfig(this.program, { env, framework: 'dll' }, { dll: true });
builder.build(config);
}
/**
* //error: zsh: no matches found
* 1.在 ~/.zshrc 中加入:setopt no_nomatch
* 2.执行 source ~/.zshrc
* @param {*} env
* @param {*} options
*/
print(env, options) {
const config = utils.initWebpackConfig(this.program, { env });
const webpackConfig = builder.getWebpackConfig(config);
const webpackConfigList = Array.isArray(webpackConfig) ? webpackConfig : (webpackConfig ? [webpackConfig] : []);
if (webpackConfigList.length) {
const key = options.key || options.node;
if (key) {
webpackConfigList.forEach(item => {
console.log(chalk.green(`easywebpack-cli: webpack ${this.program.type || item.target || ''} ${key} info:\r\n`), _(item, key));
});
} else {
console.log(chalk.green('easywebpack-cli: webpack config info:\r\n'), webpackConfig);
}
} else {
console.warn(chalk.yellow('easywebpack-cli: webpack config is empty'));
}
}
server(options) {
options.port = tool.getPort(options.port || 8888);
tool.httpServer(options);
}
zip(options) {
const config = utils.initArchiveOption(this.baseDir, this.program, options);
const archive = new Archive(config);
archive.zip();
}
tar(options) {
const config = utils.initArchiveOption(this.baseDir, this.program, options);
const archive = new Archive(config);
archive.tar();
}
deploy(options) {
console.log('doing.....');
}
upgrade(options) {
if (options.babel) {
require('./babel')(this.baseDir, options);
} else {
require('./upgrade')(this.baseDir, options);
}
}
clean(dir) {
if (dir === 'all') {
utils.clearTempDir(this.baseDir);
utils.clearManifest(this.baseDir);
utils.clearBuildDir(this.baseDir);
} else if (dir) {
tool.rm(dir);
} else {
utils.clearTempDir(this.baseDir);
}
}
kill(port) {
tool.kill(port || '7001,9000,9001');
}
open(dir) {
const filepath = dir ? dir : utils.getCompileTempDir(this.baseDir);
tool.open(filepath);
process.exit();
}
puppeteer(options) {
const puppeteer = require('easy-puppeteer-html');
return puppeteer.capture(options);
}
debug() {
// console.log(chalk.yellow('[debug] command not implemented'));
}
test() {
// console.log(chalk.yellow('[test] command not implemented'));
}
cov() {
// console.log(chalk.yellow('[cov] command not implemented'));
}
add() {
// console.log(chalk.yellow('[add] command not implemented'));
}
};