-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·280 lines (227 loc) · 8.03 KB
/
index.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env node
'use strict';
const shell = require('shelljs');
const spawn = require('child_process').spawn;
const path = require('path');
const fs = require('fs');
const css = require('./lib/compile/cssCompile');
const js = require('./lib/compile/jsCompile');
const createCartridge = require('./lib/createCartridge');
const overlayTester = require('./lib/tests/overlayTester');
const chalk = require('chalk');
const os = require('os');
const webpack = require('webpack');
const chokidar = require('chokidar');
const multiScssConfigs = require('./lib/utils/multiScssConfigs');
const multiJSConfigs = require('./lib/utils/multiJSConfigs');
const cwd = process.cwd();
const pwd = __dirname;
const optionator = require('optionator')(require('./lib/utils/options'));
function checkForDwJson() {
return fs.existsSync(path.join(cwd, 'dw.json'));
}
function getDwJson() {
if (checkForDwJson()) {
return require(path.join(cwd, 'dw.json'));
}
return {};
}
function dwuploadModule() {
let dwupload = fs.existsSync(path.resolve(cwd, './node_modules/.bin/dwupload')) ?
path.resolve(cwd, './node_modules/.bin/dwupload') :
path.resolve(pwd, './node_modules/.bin/dwupload');
if (os.platform() === 'win32') {
dwupload += '.cmd';
}
return dwupload;
}
function shellCommands(param, fileOrCartridge) {
const dwupload = dwuploadModule();
if (os.platform() === 'win32') {
return `cd ./cartridges && ${dwupload} ${param} ${fileOrCartridge} && cd ..`;
}
return `cd ./cartridges && node ${dwupload} ${param} ${fileOrCartridge} && cd ..`;
}
function uploadFiles(files) {
shell.cp('dw.json', './cartridges/'); // copy dw.json file into cartridges directory temporarily
files.forEach(file => {
const relativePath = path.relative(path.join(cwd, './cartridges/'), file);
shell.exec(shellCommands('--file', relativePath));
});
shell.rm('./cartridges/dw.json'); // remove dw.json file from cartridges directory
}
function deleteFiles(files) {
shell.cp('dw.json', './cartridges/'); // copy dw.json file into cartridges directory temporarily
files.forEach(file => {
const relativePath = path.relative(path.join(cwd, './cartridges/'), file);
shell.exec(shellCommands('delete --file', relativePath));
});
shell.rm('./cartridges/dw.json'); // remove dw.json file from cartridges directory
}
function createIstanbulParameter(option, command) {
let commandLine = ' ';
if (option) {
commandLine = option.split(',')
.map(commandPath => ' -' + command + ' ' + commandPath).join(' ') + ' ';
}
return commandLine;
}
const options = optionator.parse(process.argv);
if (options.help) {
console.log(optionator.generateHelp());
process.exit(0);
}
// upload a file
if (options.upload) {
if (!checkForDwJson()) {
console.error(chalk.red('Could not find dw.json file at the root of the project.'));
process.exit(1);
}
uploadFiles(options.upload);
process.exit(0);
}
// upload cartridge
if (options.uploadCartridge) {
if (!checkForDwJson()) {
console.error(chalk.red('Could not find dw.json file at the root of the project.'));
process.exit(1);
}
shell.cp(path.join(cwd, 'dw.json'), path.join(cwd, './cartridges/'));
const cartridges = options.uploadCartridge;
cartridges.forEach(cartridge => {
shell.exec(shellCommands('--cartridge', cartridge));
});
shell.rm(path.join(cwd, './cartridges/dw.json'));
process.exit(0);
}
// run unittests
if (options.test) {
const mocha = fs.existsSync(path.resolve(cwd, './node_modules/.bin/_mocha')) ?
path.resolve(cwd, './node_modules/.bin/_mocha') :
path.resolve(pwd, './node_modules/.bin/_mocha');
console.log(`${mocha} "${options.test.join(' ')}" --reporter spec`);
const subprocess = spawn(
`${mocha} "${options.test.join(' ')}" --reporter spec --recursive `,
{ stdio: 'inherit', shell: true, cwd }
);
subprocess.on('exit', code => {
process.exit(code);
});
}
// run unittest coverage
if (options.cover) {
const istanbul = fs.existsSync(path.resolve(cwd, './node_modules/.bin/istanbul')) ?
path.resolve(cwd, './node_modules/.bin/istanbul') :
path.resolve(pwd, './node_modules/.bin/istanbul');
const mocha = fs.existsSync(path.resolve(cwd, './node_modules/.bin/_mocha')) ?
path.resolve(cwd, './node_modules/mocha/bin/_mocha') :
path.resolve(pwd, './node_modules/mocha/bin/_mocha');
const subprocess = spawn(
`${istanbul} cover ${createIstanbulParameter(options.exclude, 'x')} ${createIstanbulParameter(options.include, 'i')} ${mocha} -- -R spec --recursive "${options.cover.join(' ')}"`,
{ stdio: 'inherit', shell: true, cwd }
);
subprocess.on('exit', code => {
process.exit(code);
});
}
// compile static assetts
if (options.compile) {
const packageFile = require(path.join(cwd, './package.json'));
if (options.compile === 'js') {
js(packageFile, cwd, code => {
process.exit(code);
});
}
if (options.compile === 'css') {
css(packageFile, cwd, code => {
process.exit(code);
});
}
}
if (options.lint) {
if (options.lint === 'js') {
const subprocess = spawn(
path.resolve(cwd, './node_modules/.bin/eslint') +
' .', { stdio: 'inherit', shell: true, cwd });
subprocess.on('exit', code => {
process.exit(code);
});
}
if (options.lint === 'css') {
const subprocess = spawn(
path.resolve(cwd, './node_modules/.bin/stylelint') +
' --syntax scss "**/*.scss"', { stdio: 'inherit', shell: true, cwd });
subprocess.on('exit', code => {
process.exit(code);
});
}
}
if (options.createCartridge) {
const cartridgeName = options.createCartridge;
console.log('Created folders and files for cartridge ' + cartridgeName);
createCartridge(cartridgeName, cwd);
}
if (options.integration) {
const integrationTests = options.integration[0];
let baseUrl = options.baseUrl;
if (!baseUrl && !checkForDwJson()) {
console.error(chalk.red('Could not find dw.json file or --baseUrl parameter'));
process.exit(1);
}
baseUrl = baseUrl || getDwJson().hostname;
global.baseUrl = baseUrl;
let tester = overlayTester.addAllTests(cwd, integrationTests);
tester = overlayTester.dedupeTests(tester);
overlayTester.runTests(tester);
delete global.baseUrl;
}
if (options.watch) {
const jsConfigs = multiJSConfigs.getConfigs();
const scssConfigs = multiScssConfigs.getConfigs();
let config = [];
config = config.concat(jsConfigs);
config = config.concat(scssConfigs);
const compiler = webpack(config);
compiler.watch({
aggregateTimeout: 300,
poll: undefined
}, (err) => {
if (err) {
console.log(err);
}
});
if (!options.onlycompile) {
const watcher = chokidar.watch(path.join(cwd, 'cartridges'), {
ignored: [
'**/cartridge/js/**',
'**/cartridge/client/**',
'**/*.scss'
],
persistent: true,
ignoreInitial: true,
followSymlinks: false,
awaitWriteFinish: {
stabilityThreshold: 300,
pollInterval: 100
}
});
watcher.on('change', filename => {
console.log(`Detected change in file: ${filename}`);
if (!options.skipUpload) {
uploadFiles([filename]);
}
});
watcher.on('add', filename => {
console.log(`Detected added file: ${filename}`);
if (!options.skipUpload) {
uploadFiles([filename]);
}
});
watcher.on('unlink', filename => {
console.log(`Detected deleted file: ${filename}`);
if (!options.skipUpload) {
deleteFiles([filename]);
}
});
}
}