Skip to content

Commit

Permalink
fix(get): wrap unzipper in Promise (#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
jssuttles authored Dec 21, 2023
1 parent c154fc8 commit 735d228
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 46 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"scripts": {
"lint": "eslint src/get.js",
"docs": "jsdoc -d docs ./src/get.js ./src/run.js ./src/bld.js",
"test": "node test/get.pre.js && node --test test/get.test.js",
"test": "node --test test/get.test.js",
"demo": "cd test/fixture && node demo.js"
},
"devDependencies": {
Expand Down
55 changes: 29 additions & 26 deletions src/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ import util from "./util.js";

/**
* @typedef {object} GetOptions
* @property {string | "latest" | "stable" | "lts"} [options.version = "latest"] Runtime version
* @property {"normal" | "sdk"} [options.flavor = "normal"] Build flavor
* @property {"linux" | "osx" | "win"} [options.platform] Target platform
* @property {"ia32" | "x64" | "arm64"} [options.arch] Target arch
* @property {string} [options.downloadUrl = "https://dl.nwjs.io"] Download server
* @property {string} [options.cacheDir = "./cache"] Cache directory
* @property {boolean} [options.cache = true] If false, remove cache and redownload.
* @property {boolean} [options.ffmpeg = false] If true, ffmpeg is not downloaded.
* @property {false | "gyp"} [options.nativeAddon = false] Rebuild native modules
* @property {string | "latest" | "stable" | "lts"} [version = "latest"] Runtime version
* @property {"normal" | "sdk"} [flavor = "normal"] Build flavor
* @property {"linux" | "osx" | "win"} [platform] Target platform
* @property {"ia32" | "x64" | "arm64"} [arch] Target arch
* @property {string} [downloadUrl = "https://dl.nwjs.io"] Download server
* @property {string} [cacheDir = "./cache"] Cache directory
* @property {boolean} [cache = true] If false, remove cache and redownload.
* @property {boolean} [ffmpeg = false] If true, ffmpeg is not downloaded.
* @property {false | "gyp"} [nativeAddon = false] Rebuild native modules
*/

/**
* Get binaries.
*
*
* @async
* @function
* @param {GetOptions} options Get mode options
Expand All @@ -37,11 +37,11 @@ import util from "./util.js";
* nwbuild({
* mode: "get",
* });
*
*
* // Use with nw module
* nwbuild({
* mode: "get",
* cacheDir: "./node_modules/nw"
* cacheDir: "./node_modules/nw"
* });
*
* @example
Expand Down Expand Up @@ -125,13 +125,14 @@ const getNwjs = async (options) => {
C: options.cacheDir
});
} else {
fs.createReadStream(out)
.pipe(unzipper.Extract({ path: options.cacheDir }))
.on("finish", async () => {
if (options.platform === "osx") {
await createSymlinks(options);
}
});
await new Promise((res) => {
fs.createReadStream(out)
.pipe(unzipper.Extract({ path: options.cacheDir }))
.on("finish", res);
});
if (options.platform === "osx") {
await createSymlinks(options);
}
}
return;
}
Expand Down Expand Up @@ -201,13 +202,15 @@ const getNwjs = async (options) => {
C: options.cacheDir
});
} else {
fs.createReadStream(out)
.pipe(unzipper.Extract({ path: options.cacheDir }))
.on("finish", async () => {
if (options.platform === "osx") {
await createSymlinks(options);
}
});
await new Promise((res) => {
fs.createReadStream(out)
.pipe(unzipper.Extract({ path: options.cacheDir }))
.on("finish", res);
});
if (options.platform === "osx") {
await createSymlinks(options);
}

}
}

Expand Down
17 changes: 0 additions & 17 deletions test/get.pre.js

This file was deleted.

7 changes: 5 additions & 2 deletions test/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import fs from "node:fs";
import fsm from "node:fs/promises";
import path from "node:path";
import process from "node:process";
import { describe, it } from "node:test";
import { describe, it, before } from "node:test";
import get from '../src/get.js';

describe("get", async () => {

const options = {
version: "0.82.0",
flavor: "sdk",
Expand All @@ -18,6 +18,9 @@ describe("get", async () => {
ffmpeg: false,
nativeAddon: false,
};
before(async () => {
await get(options);
});

it("downloads macos binary", async function () {
assert.strictEqual(fs.existsSync(path.resolve(process.cwd(), options.cacheDir, `nwjs${options.flavor === "sdk" ? "-sdk" : ""}-v${options.version}-${options.platform}-${options.arch}`, "nwjs.app")), true);
Expand Down

0 comments on commit 735d228

Please sign in to comment.