From c926aa8c5713393c74fb1cddaedebb9918ae7479 Mon Sep 17 00:00:00 2001 From: Anders Evenrud Date: Sat, 11 Apr 2020 22:35:17 +0200 Subject: [PATCH] Updated package installation stream handling (#28) --- src/packages.js | 9 +++++---- src/utils/packages.js | 12 ++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/packages.js b/src/packages.js index e2870c4..07ef235 100644 --- a/src/packages.js +++ b/src/packages.js @@ -98,11 +98,11 @@ class Packages { * Loads all packages * @return {Promise} */ - createLoader() { + async createLoader() { let result = []; const {discoveredFile, manifestFile} = this.options; - const discovered = readOrDefault(discoveredFile); - const manifest = readOrDefault(manifestFile); + const discovered = await readOrDefault(discoveredFile); + const manifest = await readOrDefault(manifestFile); const sources = discovered.map(d => path.join(d, 'metadata.json')); logger.info('Using package discovery file', relative(discoveredFile)); @@ -204,6 +204,7 @@ class Packages { const target = await realpath(`${userRoot}/${name}`, user); if (await fs.exists(target)) { + // FIXME: Secure this await fs.remove(target); await this.writeUserManifest(userRoot, user); } else { @@ -226,7 +227,7 @@ class Packages { // TODO: Check conflicts ? const root = await realpath(userRoot, user); const manifest = await realpath(`${userRoot}/metadata.json`, user); - const filenames = await fg(root + '/*/metadata.json'); // FIXME: Windows! + const filenames = await fg(root.replace(/\\/g, '/') + '/*/metadata.json'); const metadatas = await Promise.all(filenames.map(f => fs.readJson(f))); await fs.writeJson(manifest, metadatas); diff --git a/src/utils/packages.js b/src/utils/packages.js index 0a88355..5c4f677 100644 --- a/src/utils/packages.js +++ b/src/utils/packages.js @@ -44,14 +44,14 @@ const fetchSteam = (url, options) => bent()(url, null, { headers: options.headers || {} }); -const readOrDefault = filename => fs.existsSync(filename) - ? fs.readJsonSync(filename) - : []; +const readOrDefault = async (filename) => await fs.exists(filename) + ? fs.readJson(filename) + : Promise.resolve([]); const extract = (stream, target) => new Promise((resolve, reject) => { - stream.once('end', () => resolve()); - stream.once('error', error => reject(error)); - stream.pipe(tar.extract({C: target})); + const s = stream.pipe(tar.extract({C: target})); + s.once('end', () => resolve()); + s.once('error', error => reject(error)); }); module.exports = {