Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor install script #114

Merged
merged 38 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
ae2a3ed
build(node): bump latest to `v20.8.0`
ayushmanchhabra Sep 29, 2023
4e4a642
refactor: sort imports
ayushmanchhabra Sep 29, 2023
bec0756
docs: add license
ayushmanchhabra Sep 30, 2023
914c7b5
docs: remove duplicate license
ayushmanchhabra Sep 30, 2023
84b76cb
refactor: install script
ayushmanchhabra Sep 30, 2023
11ce6d5
build(dependabot): batch updates
ayushmanchhabra Sep 30, 2023
e423a77
build: convert to esm
ayushmanchhabra Sep 30, 2023
27334e7
chore: move under `/lib` dir
ayushmanchhabra Oct 3, 2023
a8de567
build(npm): remove `engines` property since we get the latest Node ve…
ayushmanchhabra Oct 3, 2023
dbf112e
Convert double quotes to single quotes
TheJaredWilcurt Oct 5, 2023
53e31b0
refactor: use optional chaining for prerelease check
ayushmanchhabra Oct 5, 2023
bf92233
fix: exit on incompatible version/(platform/arch)
ayushmanchhabra Oct 5, 2023
c8b4cab
chore: remove additional `console.log` function
ayushmanchhabra Oct 5, 2023
ed681cd
refactor: use `Array.prototype.join` instead of `$` operator
ayushmanchhabra Oct 5, 2023
72ae57a
refactor: use `Array.prototype.join` instead of `$` operator
ayushmanchhabra Oct 6, 2023
67d2e39
chore: revert out of scope change
ayushmanchhabra Oct 6, 2023
566d755
fix: decompress binary from file path
ayushmanchhabra Oct 6, 2023
cc467f8
fix(npm): update lock file
ayushmanchhabra Oct 6, 2023
f103f4a
fix: use `join()`
ayushmanchhabra Oct 6, 2023
6bdf09d
fix: fail if file path is invalid
ayushmanchhabra Oct 6, 2023
2c97298
build(npm): remove `packageManager` property
ayushmanchhabra Oct 6, 2023
e51cf7b
refactor: make impl DRY and improve naming
ayushmanchhabra Oct 6, 2023
f775d13
Merge branch 'refactor/install' of github.com:ayushmanchhabra/nw-npm …
ayushmanchhabra Oct 6, 2023
cdc38ee
fix: out of scope function
ayushmanchhabra Oct 6, 2023
cd38c64
fix: async await call
ayushmanchhabra Oct 6, 2023
e39dd23
fix: out of scope function
ayushmanchhabra Oct 6, 2023
35d15af
fix: await
ayushmanchhabra Oct 6, 2023
1ab0406
fix: use correct dirs
ayushmanchhabra Oct 6, 2023
d759d38
fix
ayushmanchhabra Oct 6, 2023
1f55f3f
refactor: use `String.prototype.endsWith`
ayushmanchhabra Oct 7, 2023
0e9505c
refactor: use `String.prototype.endsWith`
ayushmanchhabra Oct 7, 2023
93b7bbd
refactor: use `!` operator
ayushmanchhabra Oct 7, 2023
dcae229
docs: indent JSDoc comments
ayushmanchhabra Oct 7, 2023
20a0f22
refactor: use `String.prototype.endsWith`
ayushmanchhabra Oct 7, 2023
7c0213f
refactor: use `String.prototype.endsWith`
ayushmanchhabra Oct 7, 2023
b9a6794
fix: destructure and append prelease array to versionString
ayushmanchhabra Oct 7, 2023
bb2178d
refactor: decompression calls
ayushmanchhabra Oct 7, 2023
f033872
docs: improve function description
ayushmanchhabra Oct 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
directory: "."
schedule:
interval: "daily"
interval: "weekly"
day: "saturday"
versioning-strategy: increase
groups:
npm:
patterns:
- "*"
- package-ecosystem: "github-actions"
directory: ".github/workflows"
schedule:
interval: "daily"
interval: "weekly"
day: "saturday"
groups:
gha:
patterns:
- "*"
ayushmanchhabra marked this conversation as resolved.
Show resolved Hide resolved
132 changes: 132 additions & 0 deletions lib/install.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { createWriteStream, existsSync, rmSync, renameSync } from 'node:fs';
ayushmanchhabra marked this conversation as resolved.
Show resolved Hide resolved
import { get } from 'node:https';
import { dirname, resolve } from 'node:path';
import { arch, env, platform, exit } from 'node:process';
import { fileURLToPath, URL } from 'node:url';

import compressing from 'compressing';
import progress from 'cli-progress';
import semver from 'semver';

import nodeManifest from "../package.json" assert { type: "json" };

/**
* NW.js build flavor
*
* @type {'sdk' | 'normal'}
*/
let buildType = env.npm_config_nwjs_build_type || env.NWJS_BUILD_TYPE || 'normal';

// Parse Node manifest version.
let parsedVersion = semver.parse(nodeManifest.version);
let versionString = `${parsedVersion.major}.${parsedVersion.minor}.${parsedVersion.patch}`;
ayushmanchhabra marked this conversation as resolved.
Show resolved Hide resolved

// Check if version is a prelease.
if (parsedVersion.prerelease !== undefined && typeof parsedVersion.prerelease[0] === 'string') {
ayushmanchhabra marked this conversation as resolved.
Show resolved Hide resolved
let prerelease = parsedVersion.prerelease[0].split('-');
if (prerelease.length > 1) {
prerelease = prerelease.slice(0, -1);
}
versionString = `${versionString}-${prerelease}`;
ayushmanchhabra marked this conversation as resolved.
Show resolved Hide resolved
}

// Check build flavor
if (versionString.slice(-4) === '-sdk') {
ayushmanchhabra marked this conversation as resolved.
Show resolved Hide resolved
versionString = versionString.slice(0, -4);
buildType = 'sdk';
} else if (versionString.slice(-3) === 'sdk') {
ayushmanchhabra marked this conversation as resolved.
Show resolved Hide resolved
versionString = versionString.slice(0, -3);
buildType = 'sdk';
}

let url = '';
let hostArch = env.npm_config_nwjs_process_arch || arch;
let urlBase = env.npm_config_nwjs_urlbase || env.NWJS_URLBASE || 'https://dl.nwjs.io/v';
let buildTypeSuffix = buildType === 'normal' ? '' : `-${buildType}`;
let hostOs = env.npm_config_nwjs_platform || env.NWJS_PLATFORM || platform;

// Determine download url
switch (hostOs) {
case 'win32':
url = `${urlBase}${versionString}/nwjs${buildTypeSuffix}-v${versionString}-win-${hostArch}.zip`;
break;
case 'darwin':
url = `${urlBase}${versionString}/nwjs${buildTypeSuffix}-v${versionString}-osx-${hostArch}.zip`;
break;
case 'linux':
url = `${urlBase}${versionString}/nwjs${buildTypeSuffix}-v${versionString}-linux-${hostArch}.tar.gz`;
break;
}
ayushmanchhabra marked this conversation as resolved.
Show resolved Hide resolved

if (url === '') {
console.error('[ERROR] Could not find a compatible version of nw.js to download for your platform.');
ayushmanchhabra marked this conversation as resolved.
Show resolved Hide resolved
}

const __dirname = dirname(fileURLToPath(import.meta.url));
let dest = resolve(__dirname, '..', 'nwjs');

if (existsSync(dest) === true) {
console.info("[ INFO ] The NW.js binary already exists.");
ayushmanchhabra marked this conversation as resolved.
Show resolved Hide resolved
exit(0);
}

let parsedUrl = new URL(url);
let filePath = '';
if (parsedUrl.protocol === 'file:') {
filePath = resolve(
decodeURIComponent(
url.slice('file://'.length)
)
);

if (existsSync(filePath) === false) {
ayushmanchhabra marked this conversation as resolved.
Show resolved Hide resolved
console.error('[ERROR] Could not find ' + filePath);
}

} else {

const bar = new progress.SingleBar({}, progress.Presets.rect);

const stream = createWriteStream(dest);

get(url, (response) => {

let chunks = 0;
bar.start(Number(response.headers["content-length"]), 0);
response.on("data", async (chunk) => {
chunks += chunk.length;
bar.increment();
bar.update(chunks);
});

response.on("error", () => {
rmSync(dest, {
recursive: true,
force: true,
});
});

response.on("end", () => {
if (platform === "linux") {
compressing.tgz.uncompress(dest, ".")
.then(() => rmSync(dest, {
ayushmanchhabra marked this conversation as resolved.
Show resolved Hide resolved
recursive: true,
force: true,
}))
.then(() => {
renameSync(`nwjs-v${versionString}-${platform}-${arch}`, "nwjs");
});
} else {
compressing.zip.uncompress(dest, ".")
.then(() => rmSync(dest, {
recursive: true,
force: true,
}))
.then(() => {
renameSync(`nwjs-v${versionString}-${platform}-${arch}`, "nwjs");
});
}
});
response.pipe(stream);
});
}
Loading