From 334c08646f0045cddd62bf1ffe93eba8e558b9f2 Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Sat, 9 Mar 2024 14:34:43 +0530 Subject: [PATCH] chore: convert to esm --- .github/dependabot.yml | 2 +- .github/workflows/ci.yml | 2 +- CODEOWNERS | 1 + lib/app_assets.js | 20 +++++++++--------- lib/cli.js | 45 ++++++++++++++++++++-------------------- lib/findpath.js | 18 ++++++++-------- lib/install.js | 24 ++++++++++----------- package.json | 11 +++++----- test/app/package.json | 2 +- test/index.js | 12 +++++------ 10 files changed, 69 insertions(+), 68 deletions(-) create mode 100644 CODEOWNERS diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 06f9a85..74af2b6 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,7 +1,7 @@ version: 2 updates: - package-ecosystem: "npm" - directory: "/" + directory: "." schedule: interval: "daily" - package-ecosystem: "github-actions" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9ccdd3c..90403dc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,7 +2,7 @@ name: ci on: pull_request: - branches: [ main ] + branches: ["*"] concurrency: group: ${{ github.ref }} diff --git a/CODEOWNERS b/CODEOWNERS new file mode 100644 index 0000000..d607742 --- /dev/null +++ b/CODEOWNERS @@ -0,0 +1 @@ +* @ayushmanchhabra diff --git a/lib/app_assets.js b/lib/app_assets.js index 232e4a4..a71a277 100644 --- a/lib/app_assets.js +++ b/lib/app_assets.js @@ -1,20 +1,20 @@ -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); } @@ -22,8 +22,8 @@ exports.copyAssets = function copyAssets(platform, binPath) { // 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); } -}; +} diff --git a/lib/cli.js b/lib/cli.js index fe43ed0..b758746 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -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)) { @@ -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(); diff --git a/lib/findpath.js b/lib/findpath.js index 87836ac..e23cdc3 100644 --- a/lib/findpath.js +++ b/lib/findpath.js @@ -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. @@ -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. * @@ -21,18 +21,18 @@ 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'); } } @@ -40,7 +40,7 @@ function findpath(executable = 'nwjs') { * 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') { @@ -54,4 +54,4 @@ function findpath(executable = 'nwjs') { return binPath; } -module.exports = { findpath }; +export default { findpath }; diff --git a/lib/install.js b/lib/install.js index 0a5ba30..9abc8aa 100755 --- a/lib/install.js +++ b/lib/install.js @@ -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) => { @@ -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`. @@ -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) => { diff --git a/package.json b/package.json index a7997ee..03de621 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/app/package.json b/test/app/package.json index 87bb133..78e42d1 100644 --- a/test/app/package.json +++ b/test/app/package.json @@ -1,4 +1,4 @@ { - "name": "nwapp", + "name": "demo", "main": "index.html" } diff --git a/test/index.js b/test/index.js index 2de6d85..cf81614 100644 --- a/test/index.js +++ b/test/index.js @@ -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); });