From 7a108af2ca4af743f7a87bfac9224b5d4e8798fa Mon Sep 17 00:00:00 2001 From: gautamkrishnar Date: Sat, 11 Jul 2020 03:18:06 +0530 Subject: [PATCH] fixed bugs in package script --- package.js | 62 ++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/package.js b/package.js index 20a7c6f..07fab50 100644 --- a/package.js +++ b/package.js @@ -1,35 +1,31 @@ const path = require('path'); const fs = require('fs'); const archiver = require('archiver'); -const filePath = path.resolve(__dirname, 'dist', 'manifest.json'); +const distDir = path.resolve(__dirname, 'dist'); +const filePath = path.resolve(distDir, 'manifest.json'); const packagePath = path.resolve(__dirname, 'packaged'); const file = require(filePath); let args = process.argv.slice(2); let settings = file['browser_specific_settings']; -function archive(folder) { - let output = fs.createWriteStream(folder); - let archived = archiver('zip'); - output.on('close', function () { - console.log(archived.pointer() + ' total bytes'); - console.log('archiver has been finalized and the output file descriptor has closed.'); - }); - output.on('end', function () { - console.log('Data has been drained'); - }); - archived.on('warning', function (err) { - if (err.code === 'ENOENT') { - console.log(err); - } else { - throw err; - } - }); - archived.on('error', function (err) { - throw err; +/** + * Promisified implementation of Archiver + * @param {string} source - directory to zip + * @param {string} out - output zip file path + * @returns {Promise} + */ +function zipUtil(source, out) { + const archive = archiver("zip", { zlib: { level: 9 } }); + const stream = fs.createWriteStream(out); + + return new Promise((resolve, reject) => { + archive + .directory(source, false, {}) + .on("error", (err) => reject(err)) + .pipe(stream); + stream.on("close", () => resolve(out)); + archive.finalize(); }); - archived.pipe(output); - archived.directory('dist/', false, null); - archived.finalize(); } args.forEach(function (val) { @@ -52,16 +48,18 @@ args.forEach(function (val) { } }); console.log('Creating ' + zip + ' ...'); - archive(zipPath); - console.log('Restoring Browser Specific Settings in the manifest ...'); - file['browser_specific_settings'] = settings; - fs.writeFileSync(filePath, JSON.stringify(file), function writeJSON(err) { - if (err) { - return console.error(err); - } - }) + zipUtil(distDir,zipPath).then(()=> { + console.log('Created ', zipPath); + console.log('Restoring Browser Specific Settings in the manifest ...'); + file['browser_specific_settings'] = settings; + fs.writeFileSync(filePath, JSON.stringify(file), function writeJSON(err) { + if (err) { + return console.error(err); + } + }); + }).catch((err)=> console.log(err)); } else { console.log('Creating ' + zip + ' ...'); - archive(zipPath); + zipUtil(distDir, zipPath).then(() => console.log('Created ', zipPath)); } });