From 4e92b6383b36b46d856ddc71d1e7392eb5fd66d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20von=20der=20Gr=C3=BCn?= Date: Sat, 23 Oct 2021 14:09:46 +0200 Subject: [PATCH] refactor!: drop q (#115) --- .eslintrc.yml | 4 ++ bin/apple_osx_version | 2 +- bin/apple_xcode_version | 2 +- bin/check_reqs | 2 +- bin/create | 5 ++- bin/lib/check_reqs.js | 15 +++---- bin/lib/create.js | 9 ++-- bin/lib/versions.js | 43 ++++++++----------- bin/templates/scripts/cordova/build | 2 +- bin/templates/scripts/cordova/clean | 2 +- bin/templates/scripts/cordova/lib/build.js | 14 +++--- bin/templates/scripts/cordova/lib/clean.js | 3 +- .../scripts/cordova/lib/plugman/Plugman.js | 5 +-- bin/templates/scripts/cordova/lib/prepare.js | 11 +++-- bin/templates/scripts/cordova/lib/run.js | 5 +-- bin/templates/scripts/cordova/lib/spawn.js | 31 +++++++------ bin/templates/scripts/cordova/run | 2 +- bin/update | 5 ++- package.json | 1 - 19 files changed, 79 insertions(+), 84 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index c35fe3e5..992c2019 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -17,6 +17,10 @@ extends: '@cordova/eslint-config/node' +# FIXME avoid rule exceptions +rules: + prefer-promise-reject-errors: off + overrides: - files: [tests/spec/**/*.js] extends: '@cordova/eslint-config/node-tests' diff --git a/bin/apple_osx_version b/bin/apple_osx_version index 5b4a8b85..b9c14e0d 100755 --- a/bin/apple_osx_version +++ b/bin/apple_osx_version @@ -21,7 +21,7 @@ var versions = require('./lib/versions.js'); -versions.get_apple_osx_version().done(null, function (err) { +versions.get_apple_osx_version().catch(function (err) { console.log(err); process.exit(2); }); diff --git a/bin/apple_xcode_version b/bin/apple_xcode_version index 29633291..76478285 100755 --- a/bin/apple_xcode_version +++ b/bin/apple_xcode_version @@ -21,7 +21,7 @@ var versions = require('./lib/versions.js'); -versions.get_apple_xcode_version().done(function (version) { +versions.get_apple_xcode_version().then(function (version) { console.log(version); }, function (err) { console.error(err); diff --git a/bin/check_reqs b/bin/check_reqs index 8f1d5ca5..0ac8466c 100755 --- a/bin/check_reqs +++ b/bin/check_reqs @@ -25,7 +25,7 @@ var check_reqs = require('./lib/check_reqs'); if (['--help', '/?', '-h', 'help', '-help', '/help'].indexOf(process.argv[2]) > -1) { console.log('Usage: check_reqs or node check_reqs'); } else { - check_reqs.run().done(null, function (err) { + check_reqs.run().catch(function (err) { console.error('Failed to check requirements due to ' + err); process.exit(2); }); diff --git a/bin/create b/bin/create index ad2ff0bf..ec6c4e4c 100755 --- a/bin/create +++ b/bin/create @@ -62,4 +62,7 @@ var options = { customTemplate: argv.argv.remain[3] }; -Api.createPlatform(projectPath, config, options).done(); +Api.createPlatform(projectPath, config, options).catch(err => { + console.error(err); + process.exit(2); +}); diff --git a/bin/lib/check_reqs.js b/bin/lib/check_reqs.js index 0c2ed34a..7830e61e 100644 --- a/bin/lib/check_reqs.js +++ b/bin/lib/check_reqs.js @@ -17,7 +17,6 @@ under the License. */ -var Q = require('q'); var shell = require('shelljs'); var versions = require('./versions'); @@ -36,8 +35,8 @@ module.exports.run = module.exports.check_xcodebuild = function () { module.exports.check_os = function () { // Build OSX apps available for OSX platform only, so we reject on others platforms return process.platform === 'darwin' - ? Q.resolve(process.platform) - : Q.reject('Cordova tooling for OSX requires Apple OS X'); + ? Promise.resolve(process.platform) + : Promise.reject('Cordova tooling for OSX requires Apple OS X'); }; /** @@ -51,14 +50,14 @@ function checkTool (tool, minVersion, message) { // Check whether tool command is available at all var tool_command = shell.which(tool); if (!tool_command) { - return Q.reject(tool + ' was not found. ' + (message || '')); + return Promise.reject(tool + ' was not found. ' + (message || '')); } // check if tool version is greater than specified one return versions.get_tool_version(tool).then(function (version) { version = version.trim(); return versions.compareVersions(version, minVersion) >= 0 - ? Q.resolve(version) - : Q.reject('Cordova needs ' + tool + ' version ' + minVersion + + ? Promise.resolve(version) + : Promise.reject('Cordova needs ' + tool + ' version ' + minVersion + ' or greater, you have version ' + version + '. ' + (message || '')); }); } @@ -103,7 +102,7 @@ module.exports.check_all = function () { return promise.then(function () { // If fatal requirement is failed, // we don't need to check others - if (fatalIsHit) return Q(); + if (fatalIsHit) return; var requirement = requirements[idx]; return checkFn() @@ -117,7 +116,7 @@ module.exports.check_all = function () { result.push(requirement); }); }); - }, Q()) + }, Promise.resolve()) .then(function () { // When chain is completed, return requirements array to upstream API return result; diff --git a/bin/lib/create.js b/bin/lib/create.js index 55c6d2c6..7c67e7af 100755 --- a/bin/lib/create.js +++ b/bin/lib/create.js @@ -20,7 +20,6 @@ */ var shell = require('shelljs'); -var Q = require('q'); var path = require('path'); var fs = require('fs'); var ROOT = path.join(__dirname, '..', '..'); @@ -153,12 +152,12 @@ exports.createProject = function (project_path, package_name, project_name, opts // check that project path doesn't exist if (fs.existsSync(project_path)) { - return Q.reject('Project already exists'); + return Promise.reject('Project already exists'); } // check that parent directory does exist so cp -r will not fail if (!fs.existsSync(project_parent)) { - return Q.reject(project_parent + ' does not exist. Please specify an existing parent folder'); + return Promise.reject(project_parent + ' does not exist. Please specify an existing parent folder'); } // create the project directory and copy over files @@ -193,7 +192,7 @@ exports.createProject = function (project_path, package_name, project_name, opts copyScripts(project_path); events.emit('log', generateDoneMessage('create', use_shared)); - return Q.resolve(); + return Promise.resolve(); }; exports.updateProject = function (projectPath, opts, events) { @@ -203,7 +202,7 @@ exports.updateProject = function (projectPath, opts, events) { copyScripts(projectPath); events.emit('log', generateDoneMessage('update', opts.link)); }); - return Q.resolve(); + return Promise.resolve(); }; function generateDoneMessage (type, link) { diff --git a/bin/lib/versions.js b/bin/lib/versions.js index f5a71c47..9462d56a 100755 --- a/bin/lib/versions.js +++ b/bin/lib/versions.js @@ -20,19 +20,17 @@ */ var child_process = require('child_process'); -var Q = require('q'); exports.get_apple_osx_version = function () { - var d = Q.defer(); - child_process.exec('xcodebuild -showsdks', function (error, stdout, stderr) { - if (error) { - d.reject(stderr); - } else { - d.resolve(stdout); - } - }); - - return d.promise.then(function (output) { + return new Promise((resolve, reject) => { + child_process.exec('xcodebuild -showsdks', function (error, stdout, stderr) { + if (error) { + reject(stderr); + } else { + resolve(stdout); + } + }); + }).then(function (output) { var regex = /[0-9]*\.[0-9]*/; var versions = []; var regexOSX = /^OS X \d+/; @@ -44,23 +42,20 @@ exports.get_apple_osx_version = function () { } versions.sort(); console.log(versions[0]); - return Q(); - }, function (stderr) { - return Q.reject(stderr); }); }; exports.get_apple_xcode_version = function () { - var d = Q.defer(); - child_process.exec('xcodebuild -version', function (error, stdout, stderr) { - var versionMatch = /Xcode (.*)/.exec(stdout); - if (error || !versionMatch) { - d.reject(stderr); - } else { - d.resolve(versionMatch[1]); - } + return new Promise((resolve, reject) => { + child_process.exec('xcodebuild -version', function (error, stdout, stderr) { + var versionMatch = /Xcode (.*)/.exec(stdout); + if (error || !versionMatch) { + reject(stderr); + } else { + resolve(versionMatch[1]); + } + }); }); - return d.promise; }; /** @@ -72,7 +67,7 @@ exports.get_apple_xcode_version = function () { exports.get_tool_version = function (toolName) { switch (toolName) { case 'xcodebuild': return exports.get_apple_xcode_version(); - default: return Q.reject(toolName + ' is not valid tool name. Valid names are: \'xcodebuild\''); + default: return Promise.reject(toolName + ' is not valid tool name. Valid names are: \'xcodebuild\''); } }; diff --git a/bin/templates/scripts/cordova/build b/bin/templates/scripts/cordova/build index ca4f22cf..a6bfef59 100755 --- a/bin/templates/scripts/cordova/build +++ b/bin/templates/scripts/cordova/build @@ -45,7 +45,7 @@ var buildOpts = nopt({ // Make buildOptions compatible with PlatformApi build method spec buildOpts.argv = buildOpts.argv.remain; -new Api().build(buildOpts).done(function () { +new Api().build(buildOpts).then(function () { console.log('** BUILD SUCCEEDED **'); }, function (err) { var errorMessage = (err && err.stack) ? err.stack : err; diff --git a/bin/templates/scripts/cordova/clean b/bin/templates/scripts/cordova/clean index a5f52469..5d711c43 100755 --- a/bin/templates/scripts/cordova/clean +++ b/bin/templates/scripts/cordova/clean @@ -26,7 +26,7 @@ if (['--help', '/?', '-h', 'help', '-help', '/help'].indexOf(process.argv[2]) >= process.exit(0); } -new Api().clean({ argv: process.argv.slice(2) }).done(function () { +new Api().clean({ argv: process.argv.slice(2) }).then(function () { console.log('** CLEAN SUCCEEDED **'); }, function (err) { console.error(err); diff --git a/bin/templates/scripts/cordova/lib/build.js b/bin/templates/scripts/cordova/lib/build.js index 26b6de89..577b6cdd 100644 --- a/bin/templates/scripts/cordova/lib/build.js +++ b/bin/templates/scripts/cordova/lib/build.js @@ -17,7 +17,7 @@ * under the License. */ -var Q = require('q'); +const { promisify } = require('util'); var path = require('path'); var shell = require('shelljs'); var spawn = require('./spawn'); @@ -33,16 +33,16 @@ module.exports.run = function (buildOpts) { buildOpts = buildOpts || {}; if (buildOpts.debug && buildOpts.release) { - return Q.reject('Only one of "debug"/"release" options should be specified'); + return Promise.reject('Only one of "debug"/"release" options should be specified'); } if (buildOpts.device && buildOpts.emulator) { - return Q.reject('Only one of "device"/"emulator" options should be specified'); + return Promise.reject('Only one of "device"/"emulator" options should be specified'); } if (buildOpts.buildConfig) { if (!fs.existsSync(buildOpts.buildConfig)) { - return Q.reject('Build config file does not exist:' + buildOpts.buildConfig); + return Promise.reject('Build config file does not exist:' + buildOpts.buildConfig); } events.emit('log', 'Reading build config file:', path.resolve(buildOpts.buildConfig)); var buildConfig = JSON.parse(fs.readFileSync(buildOpts.buildConfig, 'utf-8')); @@ -72,7 +72,7 @@ module.exports.run = function (buildOpts) { if (buildOpts.provisioningProfile) { extraConfig += 'PROVISIONING_PROFILE = ' + buildOpts.provisioningProfile + '\n'; } - return Q.nfcall(fs.writeFile, path.join(__dirname, '..', 'build-extras.xcconfig'), extraConfig, 'utf-8'); + return promisify(fs.writeFile)(path.join(__dirname, '..', 'build-extras.xcconfig'), extraConfig, 'utf-8'); }).then(function () { var configuration = buildOpts.release ? 'Release' : 'Debug'; @@ -115,7 +115,7 @@ function findXCodeProjectIn (projectPath) { }); if (xcodeProjFiles.length === 0) { - return Q.reject('No Xcode project found in ' + projectPath); + return Promise.reject('No Xcode project found in ' + projectPath); } if (xcodeProjFiles.length > 1) { events.emit('warn', 'Found multiple .xcodeproj directories in \n' + @@ -123,7 +123,7 @@ function findXCodeProjectIn (projectPath) { } var projectName = path.basename(xcodeProjFiles[0], '.xcodeproj'); - return Q.resolve(projectName); + return Promise.resolve(projectName); } module.exports.findXCodeProjectIn = findXCodeProjectIn; diff --git a/bin/templates/scripts/cordova/lib/clean.js b/bin/templates/scripts/cordova/lib/clean.js index 20e8ac66..391e8ce6 100644 --- a/bin/templates/scripts/cordova/lib/clean.js +++ b/bin/templates/scripts/cordova/lib/clean.js @@ -17,7 +17,6 @@ * under the License. */ -var Q = require('q'); var path = require('path'); var shell = require('shelljs'); var spawn = require('./spawn'); @@ -30,7 +29,7 @@ module.exports.run = function () { })[0]; if (!projectName) { - return Q.reject('No Xcode project found in ' + projectPath); + return Promise.reject('No Xcode project found in ' + projectPath); } return spawn('xcodebuild', ['-project', projectName, '-configuration', 'Debug', '-alltargets', 'clean'], projectPath) diff --git a/bin/templates/scripts/cordova/lib/plugman/Plugman.js b/bin/templates/scripts/cordova/lib/plugman/Plugman.js index ea5e0700..e3dfc78f 100644 --- a/bin/templates/scripts/cordova/lib/plugman/Plugman.js +++ b/bin/templates/scripts/cordova/lib/plugman/Plugman.js @@ -17,7 +17,6 @@ under the License. */ -var Q = require('q'); var path = require('path'); var fs = require('fs'); var shell = require('shelljs'); @@ -53,7 +52,7 @@ Plugman.get = function (locations, events) { module.exports = Plugman; Plugman.prototype.addPlugin = function (plugin, installOptions) { - if (!plugin || plugin.constructor.name !== 'PluginInfo') { return Q.reject(new CordovaError('The parameter is incorrect. The first parameter to addPlugin should be a PluginInfo instance')); } + if (!plugin || plugin.constructor.name !== 'PluginInfo') { return Promise.reject(new CordovaError('The parameter is incorrect. The first parameter to addPlugin should be a PluginInfo instance')); } installOptions = installOptions || {}; installOptions.variables = installOptions.variables || {}; @@ -96,7 +95,7 @@ Plugman.prototype.addPlugin = function (plugin, installOptions) { }; Plugman.prototype.removePlugin = function (plugin, uninstallOptions) { - if (!plugin || plugin.constructor.name !== 'PluginInfo') { return Q.reject(new CordovaError('The parameter is incorrect. The first parameter to addPlugin should be a PluginInfo instance')); } + if (!plugin || plugin.constructor.name !== 'PluginInfo') { return Promise.reject(new CordovaError('The parameter is incorrect. The first parameter to addPlugin should be a PluginInfo instance')); } var self = this; var actions = new ActionStack(); diff --git a/bin/templates/scripts/cordova/lib/prepare.js b/bin/templates/scripts/cordova/lib/prepare.js index 85ae0e42..2844172e 100644 --- a/bin/templates/scripts/cordova/lib/prepare.js +++ b/bin/templates/scripts/cordova/lib/prepare.js @@ -17,7 +17,6 @@ under the License. */ -var Q = require('q'); var fs = require('fs'); var path = require('path'); var shell = require('shelljs'); @@ -40,7 +39,7 @@ module.exports.prepare = function (cordovaProject) { configMunger.get(this.locations.root), this.locations); // Update own www dir with project's www assets and plugins' assets and js-files - return Q.when(updateWww(cordovaProject, this.locations)).then(function () { + return Promise.resolve(updateWww(cordovaProject, this.locations)).then(function () { // update project according to config.xml changes. return updateProject(self._config, self.locations); }).then(function () { @@ -161,7 +160,7 @@ function updateProject (platformConfig, locations) { return handleBuildSettings(platformConfig, locations).then(function () { if (name === originalName) { events.emit('verbose', 'OSX Product Name has not changed (still "' + originalName + '")'); - return Q(); + return Promise.resolve(); } // Update product name inside pbxproj file @@ -169,7 +168,7 @@ function updateProject (platformConfig, locations) { try { proj.parseSync(); } catch (err) { - return Q.reject(new CordovaError('An error occurred during parsing of project.pbxproj. Start weeping. Output: ' + err)); + return Promise.reject(new CordovaError('An error occurred during parsing of project.pbxproj. Start weeping. Output: ' + err)); } proj.updateProductName(name); @@ -194,13 +193,13 @@ function updateProject (platformConfig, locations) { fs.writeFileSync(locations.pbxproj, pbx_contents, 'utf-8'); events.emit('verbose', 'Wrote out OSX Product Name and updated XCode project file names from "' + originalName + '" to "' + name + '".'); // in case of updated paths we return them back to - return Q(); + return Promise.resolve(); }); } function handleBuildSettings (platformConfig, locations) { // nothing to do - return Q(); + return Promise.resolve(); } function handleIcons (projectConfig, platformRoot) { diff --git a/bin/templates/scripts/cordova/lib/run.js b/bin/templates/scripts/cordova/lib/run.js index 8a427f1f..db9a6bb4 100644 --- a/bin/templates/scripts/cordova/lib/run.js +++ b/bin/templates/scripts/cordova/lib/run.js @@ -19,7 +19,6 @@ /* jshint node: true */ -var Q = require('q'); var path = require('path'); var build = require('./build'); var spawn = require('./spawn'); @@ -28,11 +27,9 @@ var events = require('cordova-common').events; var projectPath = path.join(__dirname, '..', '..'); module.exports.run = function (runOptions) { - return Q.resolve().then(function () { + return Promise.resolve().then(function () { if (!runOptions.nobuild) { return build.run(runOptions); - } else { - return Q.resolve(); } }).then(function () { return build.findXCodeProjectIn(projectPath); diff --git a/bin/templates/scripts/cordova/lib/spawn.js b/bin/templates/scripts/cordova/lib/spawn.js index 60f53bbe..030188fc 100644 --- a/bin/templates/scripts/cordova/lib/spawn.js +++ b/bin/templates/scripts/cordova/lib/spawn.js @@ -17,7 +17,6 @@ under the License. */ -var Q = require('q'); var proc = require('child_process'); /** @@ -29,20 +28,20 @@ var proc = require('child_process'); * @return {Promise} Promise either fullfilled or rejected with error code */ module.exports = function (cmd, args, opt_cwd) { - var d = Q.defer(); - try { - var child = proc.spawn(cmd, args, { cwd: opt_cwd, stdio: 'inherit' }); + return new Promise((resolve, reject) => { + try { + var child = proc.spawn(cmd, args, { cwd: opt_cwd, stdio: 'inherit' }); - child.on('exit', function (code) { - if (code) { - d.reject('Error code ' + code + ' for command: ' + cmd + ' with args: ' + args); - } else { - d.resolve(); - } - }); - } catch (e) { - console.error('error caught: ' + e); - d.reject(e); - } - return d.promise; + child.on('exit', function (code) { + if (code) { + reject('Error code ' + code + ' for command: ' + cmd + ' with args: ' + args); + } else { + resolve(); + } + }); + } catch (e) { + console.error('error caught: ' + e); + reject(e); + } + }); }; diff --git a/bin/templates/scripts/cordova/run b/bin/templates/scripts/cordova/run index 28f86478..6e2ca236 100755 --- a/bin/templates/scripts/cordova/run +++ b/bin/templates/scripts/cordova/run @@ -51,7 +51,7 @@ var opts = nopt({ // Make options compatible with PlatformApi build method spec opts.argv = opts.argv.remain; -new Api().run(opts).done(function () { +new Api().run(opts).then(function () { console.log('** RUN SUCCEEDED **'); }, function (err) { var errorMessage = (err && err.stack) ? err.stack : err; diff --git a/bin/update b/bin/update index 2f512766..8f2bb6ec 100755 --- a/bin/update +++ b/bin/update @@ -32,4 +32,7 @@ if (args.help || args.argv.remain.length === 0) { process.exit(0); } -Api.updatePlatform(args.argv.remain[0], { link: (args.link || args.shared) }).done(); +Api.updatePlatform(args.argv.remain[0], { link: (args.link || args.shared) }).catch(err => { + console.log(err); + process.exit(2); +}); diff --git a/package.json b/package.json index eaeede9f..b25780c1 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,6 @@ "cordova-common": "^4.0.0", "nopt": "^4.0.1", "plist": "^3.0.1", - "q": "^1.4.1", "shelljs": "^0.5.3", "underscore": "^1.9.1", "unorm": "^1.4.1",