Skip to content

Commit

Permalink
chore: convert to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushmanchhabra committed Mar 9, 2024
1 parent 39f98b3 commit 334c086
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
directory: "."
schedule:
interval: "daily"
- package-ecosystem: "github-actions"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: ci

on:
pull_request:
branches: [ main ]
branches: ["*"]

concurrency:
group: ${{ github.ref }}
Expand Down
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @ayushmanchhabra
20 changes: 10 additions & 10 deletions lib/app_assets.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
var fs = require('fs');
var path = require('path');
var argv = require('yargs').argv;
import { readFileSync, writeFileSync } from 'fs';
import { join, basename } from 'path';
import { argv } from 'yargs';

// Helper to simulate the shell's "cp" command
function copyFileSync(srcFile, destFile) {
var content = fs.readFileSync(srcFile);
fs.writeFileSync(destFile, content);
var content = readFileSync(srcFile);
writeFileSync(destFile, content);
}

// Copy asset files (specified via process.argv) over to the app binary's folder
exports.copyAssets = function copyAssets(platform, binPath) {
export function copyAssets(platform, binPath) {
// OS X: Save a custom plist file to Contents/Info.plist in the
// app bundle. This lets you customize things like the app's menubar
// name and icon (see argv.mac_icon below)
if (argv.mac_plist && platform === 'darwin') {
var plistPath = path.join(binPath, '..', '..', 'Info.plist');
var plistPath = join(binPath, '..', '..', 'Info.plist');
copyFileSync(argv.mac_plist, plistPath);
}

// OS X: Save icon files to the Resources dir in the app bundle.
// Note that for the icon to work properly you need to point to
// it with a custom plist file.
if (argv.mac_icon && platform === 'darwin') {
var iconName = path.basename(argv.mac_icon); // Preserve the file's name
var iconPath = path.join(binPath, '..', '..', 'Resources', iconName);
var iconName = basename(argv.mac_icon); // Preserve the file's name
var iconPath = join(binPath, '..', '..', 'Resources', iconName);
copyFileSync(argv.mac_icon, iconPath);
}
};
}
45 changes: 22 additions & 23 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
#!/usr/bin/env node

const { spawn } = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');
const process = require('node:process');

const { copyAssets } = require('./app_assets.js');
const findpath = require('./findpath.js').findpath;

function run() {
// Rename nw.js's own package.json as workaround for https://github.com/nwjs/nw.js/issues/1503
var packagejson = path.resolve(__dirname, '..', 'package.json');
var packagejsonBackup = path.resolve(__dirname, '..', 'package_backup.json');
if (!fs.existsSync(packagejsonBackup)) {
import child_process from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import { copyAssets } from './app_assets.js';
import { findpath } from './findpath.js';

async function run() {
/* Rename nw.js's own package.json as workaround for https://github.com/nwjs/nw.js/issues/1503 */
const packagejson = path.resolve(__dirname, '..', 'package.json');
const packagejsonBackup = path.resolve(__dirname, '..', 'package_backup.json');
if (fs.existsSync(packagejsonBackup) === false) {
try {
fs.renameSync(packagejson, packagejsonBackup);
} catch (err) {}
}

// copy over any asset files (icons, etc) specified via CLI args:
/* Copy over any asset files (icons, etc) specified via CLI args */
copyAssets(process.platform, findpath());

// Normalize cli args
var args = process.argv.slice(2);
var cwd = (args.length < 1) ? '.' : args[0];
if (!fs.existsSync(path.resolve(cwd))) {
/* Normalize cli args */
const args = process.argv.slice(2);
const cwd = (args.length < 1) ? '.' : args[0];
if (fs.existsSync(path.resolve(cwd)) === false) {
args = ['.'].concat(args);
} else {
args = [cwd].concat(args.slice(1));
}

// Spawn node-webkit
var nw = spawn(findpath(), args, { stdio: 'inherit' });
const nw = child_process.spawn(findpath(), args, { stdio: 'inherit' });
nw.on('close', function() {
process.nextTick(function() {
process.exit(0);
});
});

// Restore package.json shortly after nw is spawned
/* Restore package.json shortly after nw is spawned */
setTimeout(function() {
try {
if (fs.existsSync(packagejsonBackup)) {
Expand All @@ -48,9 +47,9 @@ function run() {
}, 1000);
}

if (!fs.existsSync(findpath())) {
console.log('nw.js appears to have failed to download and extract. Attempting to download and extract again...');
var child = spawn(process.execPath, [path.resolve(__dirname, '..', 'scripts', 'install.js')], { stdio: 'inherit' });
if (fs.existsSync(findpath()) === false) {
console.log('NW.js failed to download and extract. Attempting to download and extract again...');
const child = child_process.spawn(process.execPath, [path.resolve(__dirname, '..', 'scripts', 'install.js')], { stdio: 'inherit' });
child.on('close', run);
} else {
run();
Expand Down
18 changes: 9 additions & 9 deletions lib/findpath.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { env, platform } = require('node:process');
const { join, resolve } = require('node:path');
import process from 'node:process';
import path from 'node:path';

/**
* Get the platform dependant path of the NW.js or ChromeDriver binary.
Expand All @@ -8,7 +8,7 @@ const { join, resolve } = require('node:path');
* @return {string}
*/
function findpath(executable = 'nwjs') {
const nwDir = resolve(__dirname, '..', 'nwjs');
const nwDir = path.resolve(__dirname, '..', 'nwjs');
/**
* File path to executable.
*
Expand All @@ -21,26 +21,26 @@ function findpath(executable = 'nwjs') {
*
* @type {NodeJS.Platform}
*/
let hostOs = env.npm_config_nwjs_platform || env.NWJS_PLATFORM || platform;
let hostOs = process.env.npm_config_nwjs_platform || process.env.NWJS_PLATFORM || process.platform;

/**
* Get the platform dependant path of the NW.js binary.
*/
function findNwjs() {
if (hostOs === 'darwin') {
binPath = join(nwDir, 'nwjs.app', 'Contents', 'MacOS', 'nwjs');
binPath = path.join(nwDir, 'nwjs.app', 'Contents', 'MacOS', 'nwjs');
} else if (hostOs === 'win32') {
binPath = join(nwDir, 'nw.exe');
binPath = path.join(nwDir, 'nw.exe');
} else {
binPath = join(nwDir, 'nw');
binPath = path.join(nwDir, 'nw');
}
}

/**
* Get the platform dependant path of the ChromeDriver binary.
*/
function findChromeDriver() {
binPath = resolve(nwDir, `chromedriver${platform === "win32" ? ".exe" : ""}`);
binPath = path.resolve(nwDir, `chromedriver${process.platform === "win32" ? ".exe" : ""}`);
}

if (executable === 'nwjs') {
Expand All @@ -54,4 +54,4 @@ function findpath(executable = 'nwjs') {
return binPath;
}

module.exports = { findpath };
export default { findpath };
24 changes: 12 additions & 12 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const { createWriteStream, existsSync } = require('node:fs');
const { rename, rm, symlink } = require('node:fs/promises');
const { get } = require('node:https');
const { resolve } = require('node:path');
const { arch, env, platform, exit } = require('node:process');
const { URL } = require('node:url');
import { createWriteStream, existsSync } from 'node:fs';
import { rename, rm, symlink } from 'node:fs/promises';
import { get } from 'node:https';
import { resolve } from 'node:path';
import { arch, env, platform, exit } from 'node:process';
import { URL } from 'node:url';

const compressing = require('compressing');
const progress = require('cli-progress');
const semver = require('semver');
import compressing from 'compressing';
import { SingleBar, Presets } from 'cli-progress';
import { parse } from 'semver';

const nodeManifest = require('../package.json');
import { version } from '../package.json';

install()
.catch((error) => {
Expand Down Expand Up @@ -56,7 +56,7 @@ async function install() {
/**
* Parsed string version to Semver version object
*/
let parsedVersion = semver.parse(nodeManifest.version);
let parsedVersion = parse(version);

/**
* Version string of the format `X.Y.Z-pre`.
Expand Down Expand Up @@ -180,7 +180,7 @@ async function install() {
}

if (existsSync(nwCache) === false) {
const bar = new progress.SingleBar({}, progress.Presets.rect);
const bar = new SingleBar({}, Presets.rect);

const stream = createWriteStream(nwCache);
request = new Promise((res, rej) => {
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"name": "nw",
"version": "0.85.0",
"description": "A installer for nw.js",
"repository": {
"type": "git",
"url": "git://github.com/nwjs/npm-installer.git"
},
"description": "An installer for NW.js",
"type": "module",
"main": "lib/findpath.js",
"bin": {
"nw": "lib/cli.js"
},
"repository": {
"type": "git",
"url": "git://github.com/nwjs/npm-installer.git"
},
"scripts": {
"postinstall": "node lib/install.js",
"test": "node --test-reporter=spec --test test/index.js",
Expand Down
2 changes: 1 addition & 1 deletion test/app/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "nwapp",
"name": "demo",
"main": "index.html"
}
12 changes: 6 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const { strictEqual } = require('node:assert');
const { existsSync } = require('node:fs');
const test = require('node:test');
import assert from 'node:assert';
import fs from 'node:fs';
import test from 'node:test';

const findpath = require('../lib/findpath.js').findpath;
import { findpath } from '../lib/findpath.js';

test('nwjs has downloaded and been extracted', function() {
strictEqual(existsSync(findpath()), true);
assert.strictEqual(fs.existsSync(findpath()), true);
});

test('chromedriver does not exist in normal build', function() {
strictEqual(existsSync(findpath('chromedriver')), false);
assert.strictEqual(fs.existsSync(findpath('chromedriver')), false);
});

0 comments on commit 334c086

Please sign in to comment.