-
Notifications
You must be signed in to change notification settings - Fork 153
/
hookers.js
431 lines (344 loc) · 15.4 KB
/
hookers.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
module.exports = function (ctx) {
const Q = require('q'),
path = require('path'),
fs = require('fs'),
cp = require('child_process'),
os = require('os'),
ifaces = os.networkInterfaces(),
spawn = cp.spawn,
exec = cp.exec,
pRoot = ctx.opts.projectRoot,
nodeModulesPath = path.resolve(pRoot, 'node_modules/'),
wwwFolder = path.resolve(pRoot, 'www/'),
staticFolder = path.resolve(pRoot, 'src/static/'),
targetStaticFolder = path.resolve(wwwFolder, 'static/'),
manifestFileSrcPath = path.resolve(pRoot, 'src/manifest.json'),
manifestFileCopyPath = path.resolve(wwwFolder, 'manifest.json'),
webpackPath = path.resolve(nodeModulesPath, '.bin/webpack'),
epipeBombPath = path.resolve(nodeModulesPath, '.bin/epipebomb'),
webpackDevServerPath = path.resolve(nodeModulesPath, '.bin/webpack-dev-server'),
packageJsonPath = path.resolve(__dirname, '../package.json'),
packageJson = require(packageJsonPath)
function getRouterIpAddr () {
for (let key in ifaces) {
if (ifaces.hasOwnProperty(key)) {
for (let ipInfoKey in ifaces[key]) {
if (ifaces[key].hasOwnProperty(ipInfoKey)) {
let ipInfo = ifaces[key][ipInfoKey]
if (ipInfo.family === 'IPv4' && !ipInfo.internal)
return ipInfo.address
}
}
}
}
return '127.0.0.1'
}
const sys = {
toKebabCase (txt) {
return txt.replace(/(\s)+/g, '-').replace(/[A-Z]/g, function (t) {
return t.toLowerCase()
})
},
checkPackageName () {
if (typeof packageJson.name === 'undefined' || packageJson.name === '') {
packageJson.name = 'hello-world'
} else if (/\s/g.test(packageJson.name)) {
packageJson.name = sys.toKebabCase(packageJson.name)
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson), 'utf-8')
}
},
deleteFolderRecursive (path, doNotDeleteSelf = false) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach((file) => {
let curPath = path + '/' + file
if (fs.lstatSync(curPath).isDirectory())
sys.deleteFolderRecursive(curPath);
else
fs.unlinkSync(curPath)
})
if (!doNotDeleteSelf)
fs.rmdirSync(path)
}
},
cleanWww () {
let wwwDir = path.resolve(__dirname, '../www/')
sys.deleteFolderRecursive(wwwDir, true)
},
checkManifestFile () {
if (fs.existsSync(manifestFileSrcPath)) {
console.log('Manifest.json found in src folder. Copying...')
fs.writeFileSync(manifestFileCopyPath, fs.readFileSync(manifestFileSrcPath))
}
},
checkNodeModules () {
let defer = new Q.defer()
console.log('Checking is node modules installed...')
if (!fs.existsSync(nodeModulesPath) || !fs.existsSync(path.resolve(nodeModulesPath, 'cheerio/'))) {
console.log('Node modules not found. Installing...')
exec('npm i', { cwd: pRoot, maxBuffer: 1024 * 1024 * 5 }, (error) => {
if (error) {
console.error(`Error happened when npm install: ${error}`);
defer.reject(new Error(`Error happened when npm install: ${error}`))
}
console.log('Node modules installed successfully!')
defer.resolve()
})
}
else {
console.log('Node modules already installed.')
defer.resolve()
}
return defer.promise
},
makeNonDevServerChanges () {
let defer = new Q.defer(),
cheerio = require('cheerio'),
configFile = path.resolve(__dirname, '../config.xml'),
conf = cheerio.load(fs.readFileSync(configFile), { xmlMode: true })
if (conf('allow-navigation').length > 0) {
let target = conf('allow-navigation')
if (target.attr('data-href') !== '') {
target.attr('href', target.attr('data-href'))
target.removeAttr('data-href')
}
}
fs.writeFileSync(configFile, conf.html(), 'utf-8')
sys.cleanWww()
defer.resolve()
return defer.promise
},
makeDevServerChanges () {
let defer = new Q.defer(),
configFile = path.resolve(__dirname, '../config.xml'),
srcFile = path.resolve(__dirname, '../webpack/dev_helpers/device_router.html'),
targetFile = path.resolve(wwwFolder, 'index.html'),
defaultCsp = `default-src *; script-src 'self' data: 'unsafe-inline' 'unsafe-eval' http://127.0.0.1:8081 http://LOCIP:8081; object-src 'self' data: http://127.0.0.1:8081 http://LOCIP:8081; style-src 'self' 'unsafe-inline' data: ; img-src *; media-src 'self' data: http://127.0.0.1:8081 http://LOCIP:8081; frame-src 'self' data: http://127.0.0.1:8081 http://LOCIP:8081; font-src *; connect-src 'self' data: http://127.0.0.1:8081 http://LOCIP:8081`,
cheerio = require('cheerio'),
$ = cheerio.load(fs.readFileSync(srcFile, 'utf-8')),
conf = cheerio.load(fs.readFileSync(configFile), { xmlMode: true })
sys.cleanWww()
$('head').prepend(`<meta http-equiv="content-security-policy" content="${defaultCsp.replace(/LOCIP/g, getRouterIpAddr())}">`)
$('body').prepend(`<script>const localServerIp = '${getRouterIpAddr()}'</script>`).append(`<script src="cordova.js"></script>`)
fs.writeFileSync(targetFile, $.html())
if (conf('allow-navigation').length === 0)
conf('widget').append('<allow-navigation href="*" />')
else {
let target = conf('allow-navigation')
if (target.attr('href') !== '*')
target.attr('data-href', target.attr('href')).attr('href', '*')
}
fs.writeFileSync(configFile, conf.html(), 'utf-8')
sys.deleteFolderRecursive(targetStaticFolder, true)
sys.copyRecursiveSync(staticFolder, targetStaticFolder)
sys.watchStaticFolder()
defer.resolve()
return defer.promise
},
startWebpackBuild (isRelease) {
let defer = new Q.defer()
console.log('Starting webpack build...')
let wpPath = webpackPath + (os.platform() === 'win32' ? '.cmd' : '')
exec(`"${wpPath}"` + (isRelease ? ' --env.release' : ''), { cwd: pRoot, maxBuffer: 1024 * 1024 * 5 }, (error, log) => {
if (error) {
console.error(`Error happened when webpack build: ${error}`);
defer.reject(new Error(`Error happened when webpack build: ${error}`))
}
console.log(`Webpack log: ${log}`);
sys.deleteFolderRecursive(targetStaticFolder, true)
sys.copyRecursiveSync(staticFolder, targetStaticFolder)
sys.checkManifestFile()
console.log('Webpack build completed to www folder successfully!')
defer.resolve()
})
return defer.promise
},
startWebpackDevServer (platform) {
let defer = new Q.defer(),
outText = '',
isResultFound = false,
args = [`"${webpackDevServerPath}"`, '--hot', '--inline', '--env.devserver', '--' + platform, `--public ${getRouterIpAddr()}:8081`, '--env.devserver'],
run = epipeBombPath
if (os.platform() === 'win32') {
args = ['--hot', '--inline', '--env.devserver', '--' + platform, `--public ${getRouterIpAddr()}:8081`, '--env.devserver']
run = `"${webpackDevServerPath}.cmd"`
}
let devServerSpawn = spawn(run, args, {
shell: true,
cwd: pRoot,
stdio: [process.stdin, 'pipe', process.stderr]
})
devServerSpawn.on('error', (err) => {
console.log('Failed to start webpack dev server!')
console.log(err)
defer.reject(err)
})
devServerSpawn.stdout.on('data', (data) => {
process.stdout.write(data)
if (!isResultFound) {
outText += data
if (outText.indexOf('bundle is now VALID.') > -1 || outText.indexOf('Compiled successfully.') > -1 || outText.indexOf('Compiled with warnings') > -1) {
isResultFound = true
outText = ''
defer.resolve()
}
}
})
return defer.promise
},
emptyDefer () {
let defer = new Q.defer()
defer.resolve()
return defer.promise
},
checkOption (name) {
return (
typeof ctx.opts !== 'undefined' &&
typeof ctx.opts.options !== 'undefined' &&
typeof ctx.opts.options[name] !== 'undefined' &&
ctx.opts.options[name] === true
)
},
checkArgv (name) {
return (
typeof ctx.opts !== 'undefined' &&
typeof ctx.opts.options !== 'undefined' &&
typeof ctx.opts.options.argv !== 'undefined' &&
(
Array.isArray(ctx.opts.options.argv) &&
ctx.opts.options.argv.indexOf(name) > -1 ||
ctx.opts.options.argv[name] === true
)
)
},
isFoundInCmdline (cmdCommand) {
return (
ctx.cmdLine.indexOf(`cordova ${cmdCommand}`) > -1 ||
ctx.cmdLine.indexOf(`phonegap ${cmdCommand}`) > -1
)
},
deleteFolderRecursive (path, doNotDeleteSelf = false) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach((file) => {
let curPath = path + '/' + file
if (fs.lstatSync(curPath).isDirectory())
sys.deleteFolderRecursive(curPath);
else
fs.unlinkSync(curPath)
})
if (!doNotDeleteSelf)
fs.rmdirSync(path)
}
},
copyRecursiveSync (src, dest) {
let exists = fs.existsSync(src),
stats = exists && fs.statSync(src),
isDirectory = exists && stats.isDirectory()
if (exists && isDirectory) {
if (!fs.existsSync(dest))
fs.mkdirSync(dest)
fs.readdirSync(src).forEach((childItemName) => {
sys.copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName))
})
} else
fs.linkSync(src, dest)
},
createFile (p) {
let rel = path.relative('./src/static/', p)
sys.copyFile(p, path.resolve(targetStaticFolder, rel));
console.log(`${p} created`)
},
addUpdate (p) {
let rel = path.relative('./src/static/', p)
console.log(`${p} copied to ${path.resolve(targetStaticFolder, rel)}`)
},
addDir (p) {
let rel = path.relative('./src/static/', p)
fs.mkdir(path.resolve(targetStaticFolder, rel), () => {
console.log(`Found ${p} folder. Created to ${path.resolve(targetStaticFolder, rel)}`)
})
},
delete (p) {
let rel = path.relative('./src/static/', p)
fs.access(path.resolve(targetStaticFolder, rel), fs.constants.W_OK, (err) => {
if (!err) {
fs.unlink(path.resolve(targetStaticFolder, rel), () => {
console.log(`${path.resolve(targetStaticFolder, rel)} deleted.`)
})
} else {
console.log(`${path.resolve(targetStaticFolder, rel)} not deleted.`)
}
})
},
copyFile (source, target, cb) {
let cbCalled = false
let rd = fs.createReadStream(source)
rd.on("error", done)
let wr = fs.createWriteStream(target)
wr.on("error", done)
wr.on("close", done)
rd.pipe(wr)
function done (err) {
if (!cbCalled && typeof cb === 'function') {
cb(err)
cbCalled = true
}
}
},
watchStaticFolder () {
if (fs.existsSync(staticFolder)) {
const chokidar = require('chokidar'),
watcher = chokidar.watch(staticFolder, {
persistent: true
})
watcher.on('ready', () => {
console.log('Watcher ready!')
watcher
.on('add', sys.createFile)
.on('change', sys.addUpdate)
.on('unlink', sys.delete)
.on('addDir', sys.addDir)
.on('unlinkDir', sys.delete)
})
}
}
}
let deferral = new Q.defer(),
isBuild = sys.isFoundInCmdline('build'),
isRun = sys.isFoundInCmdline('run'),
isEmulate = sys.isFoundInCmdline('emulate'),
isPrepare = sys.isFoundInCmdline('prepare'),
isServe = sys.isFoundInCmdline('serve'),
isLiveReload = sys.checkArgv('--live-reload') || sys.checkArgv('--lr') || sys.checkArgv('lr') || sys.checkArgv('live-reload'),
isNoBuild = sys.checkOption('no-build'),
isRelease = sys.checkOption('release')
if (ctx.opts.platforms.length === 0 && !isPrepare) {
console.log('Update happened. Skipping...')
deferral.resolve()
}
else {
console.log('Before deploy hook started...')
// if package name contains space characters, we'll convert it to kebab case. Required for npm install command to work.
sys.checkPackageName()
sys.checkNodeModules()
.then(() => {
if (isBuild || ((isRun || isEmulate || isPrepare) && !isLiveReload && !isNoBuild)) {
return sys.makeNonDevServerChanges().then(() => sys.startWebpackBuild(isRelease))
} else if (isServe || (isRun || isEmulate) && isLiveReload) {
return sys.makeDevServerChanges().then(() => sys.startWebpackDevServer(ctx.opts.platforms[0]))
}
else
return sys.emptyDefer()
})
.then(() => {
console.log('Cordova hook completed. Resuming to run your cordova command...')
deferral.resolve()
})
.catch((err) => {
console.log('Error happened on main chain:')
console.log(err)
deferral.reject(err)
})
.done()
}
return deferral.promise
}