Skip to content

Commit

Permalink
fix (node): require failure with shebang interpreter (apache#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
erisu authored and NiklasMerz committed Feb 24, 2020
1 parent 6638068 commit 08e4d4a
Show file tree
Hide file tree
Showing 16 changed files with 266 additions and 197 deletions.
2 changes: 0 additions & 2 deletions bin/lib/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env node

/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
Expand Down
15 changes: 14 additions & 1 deletion bin/templates/scripts/cordova/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@

/* jslint node: true */

/**
* @todo update coho to update this line.
* @todo use `package.json` instead but first
* figure out how this fit in with the platform-centered workflow structure.
* This workflow would not have the `package.json` file.
*/
// Coho updates this line
const VERSION = '6.0.0-dev';

const fs = require('fs');
const path = require('path');
const unorm = require('unorm');
Expand Down Expand Up @@ -189,7 +198,7 @@ Api.prototype.getPlatformInfo = function () {
result.locations = this.locations;
result.root = this.root;
result.name = this.platform;
result.version = require('./version');
result.version = Api.version();
result.projectConfig = new ConfigParser(this.locations.configXml);

return result;
Expand Down Expand Up @@ -685,4 +694,8 @@ Api.prototype.requirements = function () {
return check_reqs.check_all();
};

Api.version = function () {
return VERSION;
};

module.exports = Api;
6 changes: 3 additions & 3 deletions bin/templates/scripts/cordova/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function getBundleIdentifier (projectObject) {
* @return {Promise}
*/
function getDefaultSimulatorTarget () {
return require('./list-emulator-build-targets').run()
return require('./listEmulatorBuildTargets').run()
.then(emulators => {
let targetEmulator;
if (emulators.length > 0) {
Expand Down Expand Up @@ -137,7 +137,7 @@ module.exports.run = buildOpts => {
}
}

return require('./list-devices').run()
return require('./listDevices').run()
.then(devices => {
if (devices.length > 0 && !(buildOpts.emulator)) {
// we also explicitly set device flag in options as we pass
Expand All @@ -155,7 +155,7 @@ module.exports.run = buildOpts => {
newTarget = newTarget.split(',')[0];
}
// a target was given to us, find the matching Xcode destination name
const promise = require('./list-emulator-build-targets').targetForSimIdentifier(newTarget);
const promise = require('./listEmulatorBuildTargets').targetForSimIdentifier(newTarget);
return promise.then(theTarget => {
if (!theTarget) {
return getDefaultSimulatorTarget().then(defaultTarget => {
Expand Down
48 changes: 5 additions & 43 deletions bin/templates/scripts/cordova/lib/list-devices
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,10 @@
under the License.
*/

var Q = require('q');
var exec = require('child_process').exec;
const { run } = require('./listDevices');

/**
* 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 accumulator;
});
}

exports.run = listDevices;

// Check if module is started as separate script.
// If so, then invoke main method and print out results.
if (!module.parent) {
listDevices().then(function (devices) {
devices.forEach(function (device) {
console.log(device);
});
run().then(devices => {
devices.forEach(device => {
console.log(device);
});
}
});
90 changes: 4 additions & 86 deletions bin/templates/scripts/cordova/lib/list-emulator-build-targets
Original file line number Diff line number Diff line change
Expand Up @@ -19,90 +19,8 @@
under the License.
*/

var Q = require('q');
var exec = require('child_process').exec;
const { run } = require('./listEmulatorBuildTargets');

/**
* Returns a list of available simulator build targets of the form
*
* [
* { name: <xcode-destination-name>,
* identifier: <simctl-identifier>,
* simIdentifier: <cordova emulate target>
* }
* ]
*
*/
function listEmulatorBuildTargets () {
return Q.nfcall(exec, 'xcrun simctl list --json')
.then(function (stdio) {
return JSON.parse(stdio[0]);
})
.then(function (simInfo) {
var devices = simInfo.devices;
var deviceTypes = simInfo.devicetypes;
return deviceTypes.reduce(function (typeAcc, deviceType) {
if (!deviceType.name.match(/^[iPad|iPhone]/)) {
// ignore targets we don't support (like Apple Watch or Apple TV)
return typeAcc;
}
var availableDevices = Object.keys(devices).reduce(function (availAcc, deviceCategory) {
var availableDevicesInCategory = devices[deviceCategory];
availableDevicesInCategory.forEach(function (device) {
if (device.name === deviceType.name || device.name === deviceType.name.replace(/-inch/g, ' inch')) {
// Check new flag isAvailable (XCode 10.1+) or legacy string availability (XCode 10 and lower)
if (device.isAvailable || (device.availability && device.availability.toLowerCase().indexOf('unavailable') < 0)) {
availAcc.push(device);
}
}
});
return availAcc;
}, []);
// we only want device types that have at least one available device
// (regardless of OS); this filters things out like iPhone 4s, which
// is present in deviceTypes, but probably not available on the user's
// system.
if (availableDevices.length > 0) {
typeAcc.push(deviceType);
}
return typeAcc;
}, []);
})
.then(function (filteredTargets) {
// the simIdentifier, or cordova emulate target name, is the very last part
// of identifier.
return filteredTargets.map(function (target) {
var identifierPieces = target.identifier.split('.');
target.simIdentifier = identifierPieces[identifierPieces.length - 1];
return target;
});
});
}

exports.run = listEmulatorBuildTargets;

/**
* Given a simIdentifier, return the matching target.
*
* @param {string} simIdentifier a target, like "iPhone-SE"
* @return {Object} the matching target, or undefined if no match
*/
exports.targetForSimIdentifier = function (simIdentifier) {
return listEmulatorBuildTargets()
.then(function (targets) {
return targets.reduce(function (acc, target) {
if (!acc && target.simIdentifier.toLowerCase() === simIdentifier.toLowerCase()) {
acc = target;
}
return acc;
}, undefined);
});
};

// Check if module is started as separate script.
// If so, then invoke main method and print out results.
if (!module.parent) {
listEmulatorBuildTargets().then(function (targets) {
console.log(JSON.stringify(targets, null, 2));
});
}
run().then(targets => {
console.log(JSON.stringify(targets, null, 2));
});
25 changes: 5 additions & 20 deletions bin/templates/scripts/cordova/lib/list-emulator-images
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,10 @@
under the License.
*/

var Q = require('q');
var iossim = require('ios-sim');
const { run } = require('./listEmulatorImages');

/**
* Gets list of iOS devices available for simulation
* @return {Promise} Promise fulfilled with list of devices available for simulation
*/
function listEmulatorImages () {
return Q.resolve(iossim.getdevicetypes());
}

exports.run = listEmulatorImages;

// Check if module is started as separate script.
// If so, then invoke main method and print out results.
if (!module.parent) {
listEmulatorImages().then(function (names) {
names.forEach(function (name) {
console.log(name);
});
run().then(names => {
names.forEach(name => {
console.log(name);
});
}
});
31 changes: 5 additions & 26 deletions bin/templates/scripts/cordova/lib/list-started-emulators
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,10 @@
under the License.
*/

var Q = require('q');
var exec = require('child_process').exec;
const { run } = require('./listStartedEmulators');

/**
* Gets list of running iOS simulators
* @return {Promise} Promise fulfilled with list of running iOS simulators
*/
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');
});
}

exports.run = listStartedEmulators;

// Check if module is started as separate script.
// If so, then invoke main method and print out results.
if (!module.parent) {
listStartedEmulators().then(function (emulators) {
emulators.forEach(function (emulator) {
console.log(emulator);
});
run().then(emulators => {
emulators.forEach(emulator => {
console.log(emulator);
});
}
});
54 changes: 54 additions & 0 deletions bin/templates/scripts/cordova/lib/listDevices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

var Q = require('q');
var exec = require('child_process').exec;

/**
* 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 accumulator;
});
}

exports.run = listDevices;
Loading

0 comments on commit 08e4d4a

Please sign in to comment.