|
| 1 | +const utils = require("./utils"); |
| 2 | +const { spawn } = require("child_process"); |
| 3 | +const { join } = require("path"); |
| 4 | +let hasBeenInvoked = false; |
| 5 | + |
| 6 | +function escapeWithQuotes(arg) { |
| 7 | + return `"${arg}"`; |
| 8 | +} |
| 9 | + |
| 10 | +function spawnChildProcess(projectDir, command, ...args) { |
| 11 | + return new Promise((resolve, reject) => { |
| 12 | + const escapedArgs = args.map(escapeWithQuotes) |
| 13 | + |
| 14 | + const childProcess = spawn(command, escapedArgs, { |
| 15 | + stdio: "inherit", |
| 16 | + pwd: projectDir, |
| 17 | + shell: true, |
| 18 | + }); |
| 19 | + |
| 20 | + childProcess.on("close", code => { |
| 21 | + if (code === 0) { |
| 22 | + resolve(); |
| 23 | + } else { |
| 24 | + reject({ |
| 25 | + code, |
| 26 | + message: `child process exited with code ${code}`, |
| 27 | + }); |
| 28 | + } |
| 29 | + }); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +function throwError(error) { |
| 34 | + console.error(error.message); |
| 35 | + process.exit(error.code || 1); |
| 36 | +} |
| 37 | + |
| 38 | +function prepareJSWebpack(config, $mobileHelper, $projectData, originalArgs, originalMethod) { |
| 39 | + if (config.bundle && config.release) { |
| 40 | + return new Promise(function (resolve, reject) { |
| 41 | + console.log(`Running webpack for ${config.platform}...`); |
| 42 | + const envFlagNames = Object.keys(config.env).concat([config.platform.toLowerCase()]); |
| 43 | + if (utils.shouldSnapshot($mobileHelper, config.platform, config.bundle)) { |
| 44 | + envFlagNames.push("snapshot"); |
| 45 | + } |
| 46 | + |
| 47 | + const args = [ |
| 48 | + $projectData.projectDir, |
| 49 | + "node", |
| 50 | + "--preserve-symlinks", |
| 51 | + join($projectData.projectDir, "node_modules", "webpack", "bin", "webpack.js"), |
| 52 | + "--config=webpack.config.js", |
| 53 | + "--progress", |
| 54 | + ...envFlagNames.map(item => `--env.${item}`) |
| 55 | + ].filter(a => !!a); |
| 56 | + |
| 57 | + // TODO: require webpack instead of spawning |
| 58 | + spawnChildProcess(...args) |
| 59 | + .then(resolve) |
| 60 | + .catch(throwError); |
| 61 | + }); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +module.exports = function ($mobileHelper, $projectData, hookArgs) { |
| 66 | + const env = hookArgs.config.env || {}; |
| 67 | + const platform = hookArgs.config.platform; |
| 68 | + const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions; |
| 69 | + const config = { |
| 70 | + env, |
| 71 | + platform, |
| 72 | + release: appFilesUpdaterOptions.release, |
| 73 | + bundle: appFilesUpdaterOptions.bundle |
| 74 | + }; |
| 75 | + |
| 76 | + return config.release && config.bundle && prepareJSWebpack.bind(prepareJSWebpack, config, $mobileHelper, $projectData); |
| 77 | +} |
0 commit comments