task-bootstrap.js
1 const { userAgent } = process.env; 2 const [compileCachePath, taskPath] = process.argv.slice(2); 3 4 const CompileCache = require('./compile-cache'); 5 CompileCache.setCacheDirectory(compileCachePath); 6 CompileCache.install(`${process.resourcesPath}`, require); 7 8 const setupGlobals = function() { 9 global.attachEvent = function() {}; 10 const console = { 11 warn() { 12 return global.emit('task:warn', ...arguments); 13 }, 14 log() { 15 return global.emit('task:log', ...arguments); 16 }, 17 error() { 18 return global.emit('task:error', ...arguments); 19 }, 20 trace() {} 21 }; 22 global.__defineGetter__('console', () => console); 23 24 global.document = { 25 createElement() { 26 return { 27 setAttribute() {}, 28 getElementsByTagName() { 29 return []; 30 }, 31 appendChild() {} 32 }; 33 }, 34 documentElement: { 35 insertBefore() {}, 36 removeChild() {} 37 }, 38 getElementById() { 39 return {}; 40 }, 41 createComment() { 42 return {}; 43 }, 44 createDocumentFragment() { 45 return {}; 46 } 47 }; 48 49 global.emit = (event, ...args) => process.send({ event, args }); 50 global.navigator = { userAgent }; 51 return (global.window = global); 52 }; 53 54 const handleEvents = function() { 55 process.on('uncaughtException', error => 56 console.error(error.message, error.stack) 57 ); 58 59 return process.on('message', function({ event, args } = {}) { 60 if (event !== 'start') { 61 return; 62 } 63 64 let isAsync = false; 65 const async = function() { 66 isAsync = true; 67 return result => global.emit('task:completed', result); 68 }; 69 const result = handler.bind({ async })(...args); 70 if (!isAsync) { 71 return global.emit('task:completed', result); 72 } 73 }); 74 }; 75 76 const setupDeprecations = function() { 77 const Grim = require('grim'); 78 return Grim.on('updated', function() { 79 const deprecations = Grim.getDeprecations().map(deprecation => 80 deprecation.serialize() 81 ); 82 Grim.clearDeprecations(); 83 return global.emit('task:deprecations', deprecations); 84 }); 85 }; 86 87 setupGlobals(); 88 handleEvents(); 89 setupDeprecations(); 90 const handler = require(taskPath);