Skip to content

Commit

Permalink
refactor: use superspawn (#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
erisu authored May 27, 2020
1 parent 8dce55f commit c26d8b1
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 220 deletions.
10 changes: 6 additions & 4 deletions bin/templates/scripts/cordova/lib/Podfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
const fs = require('fs-extra');
const path = require('path');
const util = require('util');
const events = require('cordova-common').events;
const Q = require('q');
const superspawn = require('cordova-common').superspawn;
const CordovaError = require('cordova-common').CordovaError;
const {
CordovaError,
events,
superspawn: { spawn }
} = require('cordova-common');

Podfile.FILENAME = 'Podfile';
Podfile.declarationRegexpMap = {
Expand Down Expand Up @@ -396,7 +398,7 @@ Podfile.prototype.install = function (requirementsCheckerFunction) {
events.emit('verbose', toolOptions.ignoreMessage);
return Q.resolve();
} else {
return superspawn.spawn('pod', ['install', '--verbose'], opts)
return spawn('pod', ['install', '--verbose'], opts)
.progress(stdio => {
if (stdio.stderr) { console.error(stdio.stderr); }
if (stdio.stdout) {
Expand Down
11 changes: 6 additions & 5 deletions bin/templates/scripts/cordova/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@
const Q = require('q');
const path = require('path');
const which = require('which');
const superspawn = require('cordova-common').superspawn;
const {
events,
superspawn: { spawn }
} = require('cordova-common');
const fs = require('fs-extra');
const plist = require('plist');
const util = require('util');

const check_reqs = require('./check_reqs');
const projectFile = require('./projectFile');

const events = require('cordova-common').events;

// These are regular expressions to detect if the user is changing any of the built-in xcodebuildArgs
/* eslint-disable no-useless-escape */
const buildFlagMatchers = {
Expand Down Expand Up @@ -225,7 +226,7 @@ module.exports.run = buildOpts => {
fs.removeSync(buildOutputDir);

const xcodebuildArgs = getXcodeBuildArgs(projectName, projectPath, configuration, buildOpts.device, buildOpts.buildFlag, emulatorTarget, buildOpts.automaticProvisioning);
return superspawn.spawn('xcodebuild', xcodebuildArgs, { cwd: projectPath, printCommand: true, stdio: 'inherit' });
return spawn('xcodebuild', xcodebuildArgs, { cwd: projectPath, printCommand: true, stdio: 'inherit' });
}).then(() => {
if (!buildOpts.device || buildOpts.noSign) {
return;
Expand Down Expand Up @@ -273,7 +274,7 @@ module.exports.run = buildOpts => {

function packageArchive () {
const xcodearchiveArgs = getXcodeArchiveArgs(projectName, projectPath, buildOutputDir, exportOptionsPath, buildOpts.automaticProvisioning);
return superspawn.spawn('xcodebuild', xcodearchiveArgs, { cwd: projectPath, printCommand: true, stdio: 'inherit' });
return spawn('xcodebuild', xcodearchiveArgs, { cwd: projectPath, printCommand: true, stdio: 'inherit' });
}

return Q.nfcall(fs.writeFile, exportOptionsPath, exportOptionsPlist, 'utf-8')
Expand Down
4 changes: 2 additions & 2 deletions bin/templates/scripts/cordova/lib/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
const Q = require('q');
const path = require('path');
const fs = require('fs-extra');
const superspawn = require('cordova-common').superspawn;
const { superspawn: { spawn } } = require('cordova-common');

const projectPath = path.join(__dirname, '..', '..');

Expand All @@ -32,7 +32,7 @@ module.exports.run = () => {
}

const xcodebuildClean = configName => {
return superspawn.spawn(
return spawn(
'xcodebuild',
['-project', projectName, '-configuration', configName, '-alltargets', 'clean'],
{ cwd: projectPath, printCommand: true, stdio: 'inherit' }
Expand Down
38 changes: 13 additions & 25 deletions bin/templates/scripts/cordova/lib/listDevices.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,26 @@
under the License.
*/

var Q = require('q');
var exec = require('child_process').exec;
const { superspawn: { spawn } } = require('cordova-common');

const DEVICE_REGEX = /-o (iPhone|iPad|iPod)@.*?"USB Serial Number" = "([^"]*)"/gs;

/**
* Gets list of connected iOS devices
* @return {Promise} Promise fulfilled with list of available iOS devices
*/
function listDevices () {
var commands = [
Q.nfcall(exec, "ioreg -p IOUSB -l | sed -n -e '/iPad/,/USB Serial Number/p' | grep 'Serial Number' | awk -F\\\" '{print $4 \" iPad\"}'"),
Q.nfcall(exec, "ioreg -p IOUSB -l | sed -n -e '/iPhone/,/USB Serial Number/p' | grep 'Serial Number' | awk -F\\\" '{print $4 \" iPhone\"}'"),
Q.nfcall(exec, "ioreg -p IOUSB -l | sed -n -e '/iPod/,/USB Serial Number/p' | grep 'Serial Number' | awk -F\\\" '{print $4 \" iPod\"}'")
];

// wrap al lexec calls into promises and wait until they're fullfilled
return Q.all(commands).then(function (results) {
var accumulator = [];
results.forEach(function (result) {
var devicefound;
// Each command promise resolves with array [stout, stderr], and we need stdout only
// Append stdout lines to accumulator
devicefound = result[0].trim().split('\n');
if (devicefound && devicefound.length) {
devicefound.forEach(function (device) {
if (device) {
accumulator.push(device);
}
});
}
return spawn('ioreg', ['-p', 'IOUSB', '-l'])
.then(output => {
return [...matchAll(output, DEVICE_REGEX)]
.map(m => m.slice(1).reverse().join(' '));
});
return accumulator;
});
}

// TODO: Should be replaced with String#matchAll once available
function * matchAll (s, r) {
let match;
while ((match = r.exec(s))) yield match;
}

exports.run = listDevices;
9 changes: 3 additions & 6 deletions bin/templates/scripts/cordova/lib/listEmulatorBuildTargets.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
under the License.
*/

var Q = require('q');
var exec = require('child_process').exec;
const { superspawn: { spawn } } = require('cordova-common');

/**
* Returns a list of available simulator build targets of the form
Expand All @@ -32,10 +31,8 @@ var exec = require('child_process').exec;
*
*/
function listEmulatorBuildTargets () {
return Q.nfcall(exec, 'xcrun simctl list --json')
.then(function (stdio) {
return JSON.parse(stdio[0]);
})
return spawn('xcrun', ['simctl', 'list', '--json'])
.then(output => JSON.parse(output))
.then(function (simInfo) {
var devices = simInfo.devices;
var deviceTypes = simInfo.devicetypes;
Expand Down
29 changes: 21 additions & 8 deletions bin/templates/scripts/cordova/lib/listStartedEmulators.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,34 @@
under the License.
*/

var Q = require('q');
var exec = require('child_process').exec;
const { superspawn: { spawn } } = require('cordova-common');

/**
* Gets list of running iOS simulators
* @return {Promise} Promise fulfilled with list of running iOS simulators
*
* @todo In the next PR, I will refactor this entire method.
*
* The process no longer contains the pattern "[i]OS Simulator".
* The process is now called "Simulator.app"
*
* Additionaly, `defaults read com.apple.iphonesimulator "SimulateDevice"` is also not valid aymore.
*
* I will replace this entire method to locate the active simulators though `simctl`
*
* Alternativly, remove this file. It is not documented in Cordova and not used anywhere in our code base.
*/
function listStartedEmulators () {
// wrap exec call into promise
return Q.nfcall(exec, 'ps aux | grep -i "[i]OS Simulator"')
.then(function () {
return Q.nfcall(exec, 'defaults read com.apple.iphonesimulator "SimulateDevice"');
}).then(function (stdio) {
return stdio[0].trim().split('\n');
});
return spawn('ps', ['aux'])
.then(output => {
if (output.match(/[i]OS Simulator/)) {
return spawn('defaults', ['read', 'com.apple.iphonesimulator', '"SimulateDevice"']);
}

return '';
})
.then(output => output.split('\n'));
}

exports.run = listStartedEmulators;
39 changes: 20 additions & 19 deletions bin/templates/scripts/cordova/lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
const Q = require('q');
const path = require('path');
const build = require('./build');
const superspawn = require('cordova-common').superspawn;
const {
events,
superspawn: { spawn }
} = require('cordova-common');
const check_reqs = require('./check_reqs');
const fs = require('fs-extra');

const events = require('cordova-common').events;

const cordovaPath = path.join(__dirname, '..');
const projectPath = path.join(__dirname, '..', '..');

Expand Down Expand Up @@ -74,7 +75,7 @@ module.exports.run = runOptions => {
const ipafile = path.join(buildOutputDir, `${projectName}.ipa`);

// unpack the existing platform/ios/build/device/appname.ipa (zipfile), will create a Payload folder
return superspawn.spawn('unzip', ['-o', '-qq', ipafile], { cwd: buildOutputDir, printCommand: true, stdio: 'inherit' });
return spawn('unzip', ['-o', '-qq', ipafile], { cwd: buildOutputDir, printCommand: true, stdio: 'inherit' });
})
.then(() => {
// Uncompress IPA (zip file)
Expand Down Expand Up @@ -144,7 +145,7 @@ function filterSupportedArgs (args) {
* @return {Promise} Fullfilled when any device is connected, rejected otherwise
*/
function checkDeviceConnected () {
return superspawn.spawn('ios-deploy', ['-c', '-t', '1'], { printCommand: true, stdio: 'inherit' });
return spawn('ios-deploy', ['-c', '-t', '1'], { printCommand: true, stdio: 'inherit' });
}

/**
Expand All @@ -157,9 +158,9 @@ function deployToDevice (appPath, target, extraArgs) {
events.emit('log', 'Deploying to device');
// Deploying to device...
if (target) {
return superspawn.spawn('ios-deploy', ['--justlaunch', '-d', '-b', appPath, '-i', target].concat(extraArgs), { printCommand: true, stdio: 'inherit' });
return spawn('ios-deploy', ['--justlaunch', '-d', '-b', appPath, '-i', target].concat(extraArgs), { printCommand: true, stdio: 'inherit' });
} else {
return superspawn.spawn('ios-deploy', ['--justlaunch', '--no-wifi', '-d', '-b', appPath].concat(extraArgs), { printCommand: true, stdio: 'inherit' });
return spawn('ios-deploy', ['--justlaunch', '--no-wifi', '-d', '-b', appPath].concat(extraArgs), { printCommand: true, stdio: 'inherit' });
}
}

Expand Down Expand Up @@ -198,18 +199,18 @@ function startSim (appPath, target) {
}

function iossimLaunch (appPath, devicetypeid, log, exit) {
const f = path.resolve(path.dirname(require.resolve('ios-sim')), 'bin', 'ios-sim');
const params = ['launch', appPath, '--devicetypeid', devicetypeid, '--log', log, exit];

return superspawn.spawn(f, params, { cwd: projectPath, printCommand: true })
.progress(stdio => {
if (stdio.stderr) {
events.emit('error', `[ios-sim] ${stdio.stderr}`);
}
if (stdio.stdout) {
events.emit('log', `[ios-sim] ${stdio.stdout.trim()}`);
}
})
return spawn(
require.resolve('ios-sim/bin/ios-sim'),
['launch', appPath, '--devicetypeid', devicetypeid, '--log', log, exit],
{ cwd: projectPath, printCommand: true }
).progress(stdio => {
if (stdio.stderr) {
events.emit('error', `[ios-sim] ${stdio.stderr}`);
}
if (stdio.stdout) {
events.emit('log', `[ios-sim] ${stdio.stdout.trim()}`);
}
})
.then(result => {
events.emit('log', 'Simulator successfully started via `ios-sim`.');
});
Expand Down
51 changes: 0 additions & 51 deletions bin/templates/scripts/cordova/lib/spawn.js

This file was deleted.

Loading

0 comments on commit c26d8b1

Please sign in to comment.