Skip to content

Commit

Permalink
refactor: download function (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushmanchhabra authored Sep 20, 2022
1 parent 3530ada commit 4e44894
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 2,012 deletions.
12 changes: 12 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [3.8.4] - 2022-09-20

### Changed

- Refactor download function

## [3.8.3-beta.1]

### Changed

- Check for first instance of `package.json`

## [3.8.3] - 2022-08-26

### Changed
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ Using pnpm:
pnpm add nw-builder
```

## Usage

Check out the the demo under `/test/demo` directory for examples of module usage. For CLI, type in `nwbuild --help` in your terminal.

## API Reference

> Stay up to date via the [Changelog](https://github.com/nwjs-community/nw-builder/blob/master/.github/CHANGELOG.md).
Expand Down
176 changes: 0 additions & 176 deletions lib/downloader.cjs

This file was deleted.

105 changes: 27 additions & 78 deletions lib/index.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { existsSync } = require("node:fs");
var _ = require("lodash");
var inherits = require("inherits");
var EventEmitter = require("events").EventEmitter;
Expand All @@ -11,6 +12,7 @@ var spawn = require("child_process").spawn;
var semver = require("semver");
var platformOverrides = require("./platformOverrides.cjs");
var deprecate = require("deprecate");
const { install } = require("nw-install");

const {
checkCache,
Expand All @@ -23,20 +25,6 @@ const {
var NwVersions = require("./versions.cjs");
var Version = require("./Version.cjs");
var Utils = require("./utils.cjs");
var Downloader = require("./downloader.cjs");
// var platforms = require("./platforms.cjs");

var pkg = require("../package.json");

(async () => {
let updateNotifier = undefined;
try {
updateNotifier = await import("update-notifier");
updateNotifier.default({ pkg }).notify();
} catch (e) {
console.error(e);
}
})();

// We inherit from EventEmitter for logging
inherits(NwBuilder, EventEmitter);
Expand Down Expand Up @@ -325,74 +313,35 @@ NwBuilder.prototype.platformFilesForVersion = function () {
return Promise.resolve();
};

NwBuilder.prototype.downloadNwjs = function () {
var self = this,
downloads = [];

this._forEachPlatform(function (name, platform) {
self.setPlatformCacheDirectory(
name,
platform,
self._version.version,
self._version.flavor,
);
platform.url = self._version.platforms[name + "-" + self._version.flavor];

// Ensure that there is a cache folder
if (self.options.forceDownload) {
fs.removeSync(platform.cache);
}

fs.mkdirpSync(platform.cache);
self.emit(
"log",
"Create cache folder in " +
path.resolve(
self.options.cacheDir,
self._version.version + "-" + self._version.flavor,
),
);

NwBuilder.prototype.downloadNwjs = async function () {
let options = this.options;
for await (let osName of options.platforms) {
let plat = osName.slice(0, osName.length - 2);
let arch =
osName.slice(osName.length - 2, osName.length) === "32" ? "ia32" : "x64";
this._platforms[osName].url =
this._version.platforms[osName + "-" + this._version.flavor];
this._platforms[
osName
].cache = `${this.options.cacheDir}/${this.options.version}-${this.options.flavor}/${osName}/nwjs-${this.options.flavor}-v${this.options.version}-${plat}-${arch}`;
if (
!self.isPlatformCached(
name,
platform,
self._version.version,
self._version.flavor,
)
options.forceDownload === true ||
existsSync(
`${this.options.cacheDir}/${this.options.version}-${this.options.flavor}/${osName}/nwjs-${this.options.flavor}-v${this.options.version}-${plat}-${arch}`,
) === false
) {
downloads.push(
Downloader.downloadAndUnpack(platform.cache, platform.url).catch(
function (err) {
if (err.statusCode === 404) {
self.emit(
"log",
"ERROR: The version " +
self._version.version +
" (" +
self._version.flavor +
") does not have a corresponding build posted at " +
self.options.downloadUrl +
". Please choose a version from that list.",
);
} else {
self.emit("log", err.msg);
}

return Promise.reject("Unable to download NWjs.");
},
),
await install(
this.options.version,
this.options.flavor,
plat,
arch,
"https://nwjs.io",
"https://dl.nwjs.io",
`${this.options.cacheDir}/${this.options.version}-${this.options.flavor}/${osName}`,
`nwjs-${this.options.flavor}-v${this.options.version}-${plat}-${arch}`,
);
self.emit("log", "Downloading: " + platform.url);
} else {
self.emit("log", "Using cache for: " + name);
}
});

return Promise.all(downloads).then(function (data) {
Downloader.clearProgressbar();
return data;
});
}
};

NwBuilder.prototype.buildGypModules = function () {
Expand Down
Loading

0 comments on commit 4e44894

Please sign in to comment.