-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworker.js
More file actions
551 lines (487 loc) · 13.5 KB
/
worker.js
File metadata and controls
551 lines (487 loc) · 13.5 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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/**
* @flow
*/
/* global ErrnoError */
import {transform} from '@babel/core'
import {
createReadStream,
createWriteStream,
readdir,
readFile,
type ReadStream,
stat,
type Stats,
unlink,
writeFile,
type WriteStream,
} from 'fs'
import mkdirp from 'mkdirp'
import path from 'path'
import {
IDLE,
REMOVE_FILE,
type RemoveFileAction,
TRANSFORM_FILE,
type TransformFileAction,
} from './actions'
import type {Argv, ProcessSend} from './types'
type WriteOptions = {|
encoding?: ?string,
flag?: string,
mode?: number,
|}
/**
* Copy file.
* @param filePath - full path of file to copy
* @param outputFilePath - path to copy file to
* @param verbose - whether or not to have verbose logging
* @returns resolves once file has been copied or rejects with error
*/
function copyFile(
filePath: string,
outputFilePath: string,
verbose: boolean,
): Promise<void> {
let readStream: ReadStream
let writeStream: WriteStream
return new Promise((resolve: () => void, reject: (err: Error) => void) => {
stat(filePath, (err: ?ErrnoError, stats: Stats) => {
const writeStreamOptions = {}
if (!err) {
writeStreamOptions.mode = stats.mode
}
readStream = createReadStream(filePath)
writeStream = createWriteStream(outputFilePath, writeStreamOptions)
readStream.on('error', (err2: Error) => {
if (verbose) {
console.error(`Failed reading ${filePath}`)
}
reject(err2)
})
writeStream
.on('error', (err2: Error) => {
if (verbose) {
console.error(`Failed writing ${outputFilePath}`)
}
reject(err2)
})
.on('finish', () => {
resolve()
})
readStream.pipe(writeStream)
})
}).catch((err: Error) => {
// NOTE: destroy was added in Node v8.0.0 and thus we need the if check
// until Node 8 becomes the minimum versions supported by this project.
// $FlowFixMe - Flow doesn't know about the destroy() method
if (readStream && readStream.destroy) {
readStream.destroy()
}
if (writeStream) {
writeStream.end()
}
throw err
})
}
/**
* Create output directory for file if it doesn't already exist.
* @param source - source directory
* @param output - output directory
* @param filePath - full path to source file
* @param verbose - whether or not to have verbose logging
* @returns resolves with output file path or rejects with error
*/
function createDirectoryForFile(
source: string,
output: string,
filePath: string,
verbose: boolean,
): Promise<string> {
const relativeFilePath = path.relative(source, filePath)
const relativeDirectoryPath = path.dirname(relativeFilePath)
const outputDirectoryPath = path.join(output, relativeDirectoryPath)
if (verbose) {
console.info(`Making sure directory ${source} exists…`)
}
return new Promise(
(
resolve: (outputFilePath: string) => void,
reject: (err: Error) => void,
) => {
mkdirp(outputDirectoryPath, (err: ?Error) => {
if (err) {
reject(Error(`Failed to create directory ${outputDirectoryPath}`))
}
const fileName = path.basename(filePath)
const outputFilePath = path.join(outputDirectoryPath, fileName)
resolve(outputFilePath)
})
},
)
}
/**
* Log error and inform master worker is now idle again.
* @param message - error message
* @param send - process send method
*/
function error(message: string, send: ProcessSend) {
console.error(message)
send({
erred: true,
type: IDLE,
})
}
/* eslint-disable flowtype/no-weak-types */
/**
* Get Babel configuration for transforming Javascript files
* @param target - Node target
* @returns Babel configuration
*/
function getBabelConfig(target?: string): Promise<Object> {
const cwd = process.cwd()
return new Promise(
(resolve: (babelConfig: Object) => void, reject: (err: Error) => void) => {
readdir(cwd, (err: ?ErrnoError, files: Array<string>) => {
if (!err) {
const configFile = files.find((fileName: string): boolean => {
return /^\.babelrc(\.[a-zA-Z]+)?$/.test(fileName)
})
if (configFile) {
const filePath = path.join(cwd, configFile)
switch (path.extname(configFile)) {
case '.js':
try {
// $FlowFixMe - Flow doesn't like dynamic require statements
resolve(require(filePath))
} catch (err3) {
reject(err3)
}
return
case '.json':
readFile(
filePath,
'utf8',
(err2: ?ErrnoError, data: string) => {
if (err2) {
reject(err2)
} else {
try {
resolve(JSON.parse(data))
} catch (err3) {
reject(err3)
}
}
},
)
return
}
}
}
const env = target
? ['@babel/env', {targets: {node: target}}]
: '@babel/env'
resolve({
presets: [env, '@babel/flow', '@babel/react'],
})
})
},
)
}
/* eslint-enable flowtype/no-weak-types */
/**
* Get contents of a file.
* @param filePath - full path of file to get contents of
* @param verbose - whether or not to have verbose logging
* @returns resolves with file contents or rejects with error
*/
function getFileContents(filePath: string, verbose: boolean): Promise<string> {
if (verbose) {
console.info(`Getting contents of ${filePath}…`)
}
return new Promise(
(resolve: (data: string) => void, reject: (err: Error) => void) => {
readFile(filePath, 'utf8', (err: ?Error, data: string) => {
if (err) {
reject(new Error(`Failed to get contents of file ${filePath}`))
}
resolve(data)
})
},
)
}
/**
* Process actions from master.
* @param includeRegex - included files regex
* @param source - source directory
* @param output - output directory
* @param verbose - whether or not to have verbose logging
* @param babelConfig - Babel config
* @param send - process send method
* @param data - action from master
*/
function processActionFromMaster(
includeRegex: ?RegExp,
source: string,
output: string,
verbose: boolean,
babelConfig: Object, // eslint-disable-line flowtype/no-weak-types
send: ProcessSend,
data: RemoveFileAction | TransformFileAction,
): void {
if (typeof data !== 'object') {
return error(
`Expected message from master to be an object but instead received type ${typeof data}`,
send,
)
}
if (data === null) {
return error(
'Expected message from master to be present but instead received null',
send,
)
}
switch (data.type) {
case REMOVE_FILE:
return removeOutputFile(source, output, data.filePath, verbose, send)
case TRANSFORM_FILE:
return transformFile(
includeRegex,
source,
output,
data.filePath,
verbose,
babelConfig,
send,
)
default:
return error(
`Master sent message with unknown action type ${data.type}`,
send,
)
}
}
/**
* Remove file.
* @param source - source directory
* @param output - output directory
* @param filePath - full path of file to transform
* @param verbose - whether or not to have verbose logging
* @param send - process send method
*/
function removeOutputFile(
source: string,
output: string,
filePath: string,
verbose: boolean,
send: ProcessSend,
) {
const relativeFilePath = path.relative(source, filePath)
const relativeDirectoryPath = path.dirname(relativeFilePath)
const outputDirectoryPath = path.join(output, relativeDirectoryPath)
const fileName = path.basename(filePath)
const outputFilePath = path.join(outputDirectoryPath, fileName)
if (verbose) {
console.info(`Removing ${outputFilePath}`)
}
unlink(outputFilePath, (err: ?Error) => {
if (err) {
console.error(`Failed to remove file ${outputFilePath}`)
if (verbose) {
console.error(err)
}
}
send({
erred: !!err,
type: IDLE,
})
})
}
/**
* Transform file.
* @param includeRegex - included files regex
* @param source - source directory
* @param output - output directory
* @param filePath - full path of file to transform
* @param verbose - whether or not to have verbose logging
* @param babelConfig - Babel configuration
* @param send - process send method
*/
function transformFile(
includeRegex: ?RegExp,
source: string,
output: string,
filePath: string,
verbose: boolean,
babelConfig: Object, // eslint-disable-line flowtype/no-weak-types
send: ProcessSend,
) {
const extension = path.extname(filePath)
if (includeRegex && !includeRegex.test(filePath)) {
send({
erred: false,
type: IDLE,
})
return
}
createDirectoryForFile(source, output, filePath, verbose)
.then((outputFilePath: string): ?Promise<void> => {
switch (extension) {
case '': // Ignoring empty directories
return
case '.js':
return transformJavascriptFile(
filePath,
outputFilePath,
verbose,
babelConfig,
)
default:
return copyFile(filePath, outputFilePath, verbose)
}
})
.then((): boolean => {
return false
})
.catch((err: Error): boolean => {
console.error(`Failed to process file ${filePath}`)
if (verbose) {
console.error(err)
}
return true
})
.then((erred: boolean) => {
send({
erred,
type: IDLE,
})
})
}
/**
* Transform file.
* @param filePath - full path of file to transform
* @param outputFilePath - path to write transformed file to
* @param verbose - whether or not to have verbose logging
* @param babelConfig - Babel configuration
* @returns resolves once file has been written or rejects with error
*/
function transformJavascriptFile(
filePath: string,
outputFilePath: string,
verbose: boolean,
babelConfig: Object, // eslint-disable-line flowtype/no-weak-types
): Promise<void> {
/* eslint-disable flowtype/generic-spacing */
return getFileContents(filePath, verbose).then(
(contents: string): Promise<void> => {
return new Promise(
(resolve: () => void, reject: (err: Error) => void) => {
stat(filePath, (err1: ?ErrnoError, stats: Stats) => {
const mode = err1 ? null : stats.mode
if (verbose) {
console.info(`Transforming ${filePath}…`)
}
transform(
contents,
Object.assign({filename: filePath}, babelConfig),
(err2: ?Error, result: {|code: string|}) => {
if (err2) {
if (verbose) {
console.error(`Failed to transform ${filePath}`)
}
reject(err2)
} else {
writeDataToFile(outputFilePath, result.code, mode, verbose)
.then(resolve)
.catch(reject)
}
},
)
})
},
)
},
)
/* eslint-enable flowtype/generic-spacing */
}
/**
* Write data to file.
* @param data - data to write
* @param filePath - path of file to write to
* @param mode - permission and sticky bits
* @param verbose - whether or not to have verbose logging
* @returns resolves once file is written or rejects with an error
*/
function writeDataToFile(
filePath: string,
data: string,
mode: ?number,
verbose: boolean,
): Promise<void> {
const options: WriteOptions = {
encoding: 'utf8',
}
if (mode !== null) {
options.mode = mode
}
if (verbose) {
console.info(`Writing ${filePath}…`)
}
return new Promise((resolve: () => void, reject: (err: Error) => void) => {
writeFile(filePath, data, options, (err: ?Error) => {
if (err) {
reject(new Error(`Failed to write file ${filePath}`))
}
resolve()
})
})
}
// eslint-disable flowtype/no-weak-types
/**
* Spin up worker process.
* @param argv - command line arguments
*/
export function worker(
argv: Argv,
on: (event: string, listener: Function) => mixed, // eslint-disable-line
send: ProcessSend,
): Promise<void> {
let {include, output, source, target, verbose} = argv
let includeRegex
if (include) {
try {
includeRegex = new RegExp(include)
} catch (err) {
return Promise.reject(new Error('Include option is an invalid regex.'))
}
}
// Make sure source does not have a trailing separator
if (source[source.length - 1] === path.sep) {
source = source.substr(0, source.length - 1)
}
// Make sure output does not have a trailing separator
if (output[output.length - 1] === path.sep) {
output = output.substr(0, output.length - 1)
}
return getBabelConfig(target)
.then((babelConfig: Object) => { // eslint-disable-line
on(
'message',
processActionFromMaster.bind(
null,
includeRegex,
source,
output,
verbose,
babelConfig,
send,
),
)
})
.then(() => {
// Let master know we are now ready to start processing files
send({
erred: false,
type: IDLE,
})
})
}