From f2ca4fb03a00ce61b77d9f45fdcb92f5d5b61a61 Mon Sep 17 00:00:00 2001 From: Alena Khineika Date: Fri, 19 Jul 2024 13:55:19 +0200 Subject: [PATCH] refactor(hadron-build): remove usage of "async" package and its tasks COMPASS-5051 (#6044) * refactor(hadron-build): remove usage of "async" package and its tasks COMPASS-5051 * refactor: use async await --- package-lock.json | 2 - packages/hadron-build/commands/release.js | 49 +++++++------- packages/hadron-build/lib/zip.js | 78 +++++++++++------------ packages/hadron-build/package.json | 1 - 4 files changed, 58 insertions(+), 72 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8ee50e5dc83..61581dd338e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48348,7 +48348,6 @@ "@npmcli/arborist": "^6.2.0", "@octokit/rest": "^18.6.2", "asar": "^3.0.3", - "async": "^3.2.2", "chalk": "^4.1.2", "cli-table": "^0.3.1", "debug": "^4.3.4", @@ -74210,7 +74209,6 @@ "@npmcli/arborist": "^6.2.0", "@octokit/rest": "^18.6.2", "asar": "^3.0.3", - "async": "^3.2.2", "chai": "^4.2.0", "chalk": "^4.1.2", "cli-table": "^0.3.1", diff --git a/packages/hadron-build/commands/release.js b/packages/hadron-build/commands/release.js index 4f02af27fd1..b09aefb461d 100644 --- a/packages/hadron-build/commands/release.js +++ b/packages/hadron-build/commands/release.js @@ -23,7 +23,6 @@ const path = require('path'); const del = require('del'); const fs = require('fs-extra'); const _ = require('lodash'); -const async = require('async'); const asar = require('asar'); const packager = require('electron-packager'); const createApplicationZip = require('../lib/zip'); @@ -454,7 +453,7 @@ const createApplicationAsar = (CONFIG, done) => { }, done); }).catch((err) => { if (err) { - console.error(err); + cli.error(err); } done(); }); @@ -503,7 +502,7 @@ _.assign(exports.builder, verify.builder); * @param {Function} done Callback * @returns {any} */ -exports.run = (argv, done) => { +exports.run = async (argv, done) => { cli.argv = argv; verifyDistro(argv); @@ -513,17 +512,17 @@ exports.run = (argv, done) => { cli.debug(`Building distribution: ${target.distribution}`); const task = (name, fn) => { - return function(cb) { + return () => new Promise((resolve, reject) => { cli.debug(`start: ${name}`); - fn(target, function(err) { + fn(target, (err) => { if (err) { cli.error(err); - return cb(err); + return reject(err); } cli.debug(`completed: ${name}`); - cb(); + return resolve(); }); - }; + }); }; const skipInstaller = @@ -532,11 +531,7 @@ exports.run = (argv, done) => { const noAsar = process.env.NO_ASAR === 'true' || argv.no_asar; const tasks = _.flatten([ - function(cb) { - verify.tasks(argv) - .then(() => cb()) - .catch(cb); - }, + () => verify.tasks(argv), task('copy npmrc from root', ({ dir }, done) => { fs.cp( path.resolve(dir, '..', '..', '.npmrc'), @@ -562,21 +557,21 @@ exports.run = (argv, done) => { task('store build configuration as json', writeConfigToJson) ].filter(Boolean)); - return async.series(tasks, (_err) => { - try { - if (_err) { - return done(_err); - } - done(null, target); - } finally { - // clean up copied npmrc - fs.rm(path.resolve(target.dir, '.npmrc'), (err) => { - if (err) { - cli.warn(err.message); - } - }); + try { + for (const task of tasks) { + await task(); } - }); + done(null, target); + } catch (err) { + done(err); + } finally { + // clean up copied npmrc + fs.rm(path.resolve(target.dir, '.npmrc'), (err) => { + if (err) { + cli.warn(err.message); + } + }); + } }; exports.handler = (argv) => { diff --git a/packages/hadron-build/lib/zip.js b/packages/hadron-build/lib/zip.js index 1677c4e25f7..6dd42d8c0b5 100644 --- a/packages/hadron-build/lib/zip.js +++ b/packages/hadron-build/lib/zip.js @@ -1,17 +1,17 @@ 'use strict'; const debug = require('debug')('hadron-build:zip'); - const { execFileSync } = require('child_process'); -var fs = require('fs-extra'); -var path = require('path'); -var zipFolder = require('zip-folder'); -var series = require('async').series; +const fs = require('fs-extra'); +const path = require('path'); +const zipFolder = require('zip-folder'); +const { promisify } = require('util'); -function zip(_opts, done) { - var opts = Object.assign({}, _opts); +async function zip(_opts, done) { + const opts = Object.assign({}, _opts); opts.dir = path.resolve(opts.dir); opts.out = path.resolve(opts.out); opts.platform = opts.platform || process.platform; + if (path.extname(opts.out).toLowerCase() === '.zip') { opts.outPath = opts.out; opts.out = path.dirname(opts.out); @@ -19,49 +19,43 @@ function zip(_opts, done) { opts.outPath = path.resolve(opts.out, path.basename(opts.dir, '.app')) + '.zip'; } - function runZip(cb) { + const runZip = async () => { if (opts.platform !== 'darwin') { - zipFolder(opts.dir, opts.outPath, cb); - return; + await promisify(zipFolder)(opts.dir, opts.outPath); + } else { + const args = ['-r', '--symlinks', opts.outPath, './']; + execFileSync('zip', args, { + env: process.env, + cwd: path.join(opts.dir, '..'), + stdio: 'inherit' + }); } + } - var args = [ - '-r', - '--symlinks', - opts.outPath, - './' - ]; - - execFileSync('zip', args, { - env: process.env, - cwd: path.join(opts.dir, '..'), - stdio: 'inherit' - }); - - cb(null, opts.outPath); + const removeZipIfExists = async () => { + try { + const stats = await fs.stat(opts.outPath); + if (!stats.isFile()) { + throw new Error('Refusing to wipe path "' + opts.outPath + '" as it is ' + (stats.isDirectory() ? 'a directory' : 'not a file')); + } + await fs.unlink(opts.outPath); + } catch (err) { + if (err.code !== 'ENOENT') { + throw err; + } + } } debug('creating zip', opts); - series([ - function removeZipIfExists(cb) { - fs.stat(opts.outPath, function(err, stats) { - if (err) return cb(null); - - if (!stats.isFile()) { - return cb(new Error('Refusing to wipe path "' + opts.outPath + '" as it is ' + (stats.isDirectory() ? 'a directory' : 'not a file'))); - } - return fs.unlink(opts.outPath, cb); - }); - }, - fs.mkdirs.bind(null, opts.out), - runZip - ], function(err) { - if (err) { - return done(err); - } + try { + await removeZipIfExists(); + await fs.mkdirs(opts.out); + await runZip(); done(null, opts.outPath); - }); + } catch (err) { + done(err); + } } /** diff --git a/packages/hadron-build/package.json b/packages/hadron-build/package.json index b76376804e3..f89f4c176fb 100644 --- a/packages/hadron-build/package.json +++ b/packages/hadron-build/package.json @@ -27,7 +27,6 @@ "@npmcli/arborist": "^6.2.0", "@octokit/rest": "^18.6.2", "asar": "^3.0.3", - "async": "^3.2.2", "chalk": "^4.1.2", "cli-table": "^0.3.1", "debug": "^4.3.4",