Skip to content

Commit

Permalink
refactor(hadron-build): remove usage of "async" package and its tasks C…
Browse files Browse the repository at this point in the history
…OMPASS-5051 (#6044)

* refactor(hadron-build): remove usage of "async" package and its tasks COMPASS-5051

* refactor: use async await
  • Loading branch information
alenakhineika authored Jul 19, 2024
1 parent e61a7c2 commit f2ca4fb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 72 deletions.
2 changes: 0 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 22 additions & 27 deletions packages/hadron-build/commands/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -454,7 +453,7 @@ const createApplicationAsar = (CONFIG, done) => {
}, done);
}).catch((err) => {
if (err) {
console.error(err);
cli.error(err);
}
done();
});
Expand Down Expand Up @@ -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);
Expand All @@ -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 =
Expand All @@ -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'),
Expand All @@ -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) => {
Expand Down
78 changes: 36 additions & 42 deletions packages/hadron-build/lib/zip.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,61 @@
'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);
} else {
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);
}
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/hadron-build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f2ca4fb

Please sign in to comment.