Skip to content

Commit

Permalink
refactor: use async await
Browse files Browse the repository at this point in the history
  • Loading branch information
alenakhineika committed Jul 19, 2024
1 parent d831e38 commit 4102a86
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 41 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.

18 changes: 10 additions & 8 deletions packages/hadron-build/commands/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,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 Down Expand Up @@ -557,19 +557,21 @@ exports.run = (argv, done) => {
task('store build configuration as json', writeConfigToJson)
].filter(Boolean));

return tasks.reduce((promise, task) => {
return promise.then(task);
}, Promise.resolve()).then(() => {
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);
}
});
done(null, target);
}).catch((err) => {
done(err);
});
}
};

exports.handler = (argv) => {
Expand Down
56 changes: 25 additions & 31 deletions packages/hadron-build/lib/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const { execFileSync } = require('child_process');
const fs = require('fs-extra');
const path = require('path');
const zipFolder = require('zip-folder');
const { promisify } = require('util');

function zip(_opts, done) {
async function zip(_opts, done) {
const opts = Object.assign({}, _opts);
opts.dir = path.resolve(opts.dir);
opts.out = path.resolve(opts.out);
Expand All @@ -18,50 +19,43 @@ function zip(_opts, done) {
opts.outPath = path.resolve(opts.out, path.basename(opts.dir, '.app')) + '.zip';
}

const runZip = () => {
return new Promise((resolve, reject) => {
if (opts.platform !== 'darwin') {
zipFolder(opts.dir, opts.outPath, (err) => {
if (err) return reject(err);
return resolve(opts.outPath);
});
} else {
try {
const args = ['-r', '--symlinks', opts.outPath, './'];
execFileSync('zip', args, {
env: process.env,
cwd: path.join(opts.dir, '..'),
stdio: 'inherit'
});
return resolve(opts.outPath);
} catch (err) {
return reject(err);
}
}
});
const runZip = async () => {
if (opts.platform !== 'darwin') {
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'
});
}
}

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'));
}
return await fs.unlink(opts.outPath);
await fs.unlink(opts.outPath);
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
}

debug('creating zip', opts);

removeZipIfExists()
.then(() => fs.mkdirs(opts.out))
.then(() => runZip())
.then(result => done(null, result))
.catch(err => done(err));

try {
await removeZipIfExists();
await fs.mkdirs(opts.out);
await runZip();
done(null, opts.outPath);
} catch (err) {
done(err);
}
}

/**
Expand Down

0 comments on commit 4102a86

Please sign in to comment.