Skip to content

Commit

Permalink
chore: convert from cjs to esm (#134)
Browse files Browse the repository at this point in the history
* chore: convert from cjs to esm

* chore(test): convert from cjs to esm

* chore(cd): convert sdk script from cjs to esm

* fix: assert json type

* fix: export findpath

* fix: __dirname workaround
  • Loading branch information
ayushmanchhabra authored Mar 14, 2024
1 parent d24e926 commit 0f0453e
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 65 deletions.
6 changes: 3 additions & 3 deletions .github/sdk.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('node:fs');
import { writeFileSync } from 'node:fs';

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

nodeManifest.version = nodeManifest.version + '-sdk';

fs.writeFileSync('./package.json', JSON.stringify(nodeManifest, null, 2));
writeFileSync('./package.json', JSON.stringify(nodeManifest, null, 2));
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

## [0.85.0]

## Changed

- Handle error when trying to create symlink on Windows as non-Administrator [aca09ab](https://github.com/nwjs/npm-installer/commit/aca09abbd315fc87c6c4f813748aa4a5898bbda7)
Expand Down
41 changes: 22 additions & 19 deletions bin/nw.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
#!/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');
import { spawn } from 'node:child_process';
import { existsSync, renameSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { platform, argv, nextTick, exit, execPath } from 'node:process';
import { fileURLToPath } from 'node:url';

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

const __dirname = dirname(fileURLToPath(import.meta.url));

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)) {
var packagejson = resolve(__dirname, '..', 'package.json');
var packagejsonBackup = resolve(__dirname, '..', 'package_backup.json');
if (!existsSync(packagejsonBackup)) {
try {
fs.renameSync(packagejson, packagejsonBackup);
renameSync(packagejson, packagejsonBackup);
} catch (err) {}
}

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

// Normalize cli args
var args = process.argv.slice(2);
var args = argv.slice(2);
var cwd = (args.length < 1) ? '.' : args[0];
if (!fs.existsSync(path.resolve(cwd))) {
if (!existsSync(resolve(cwd))) {
args = ['.'].concat(args);
} else {
args = [cwd].concat(args.slice(1));
Expand All @@ -33,24 +36,24 @@ function run() {
// Spawn node-webkit
var nw = spawn(findpath(), args, { stdio: 'inherit' });
nw.on('close', function() {
process.nextTick(function() {
process.exit(0);
nextTick(function() {
exit(0);
});
});

// Restore package.json shortly after nw is spawned
setTimeout(function() {
try {
if (fs.existsSync(packagejsonBackup)) {
fs.renameSync(packagejsonBackup, packagejson);
if (existsSync(packagejsonBackup)) {
renameSync(packagejsonBackup, packagejson);
}
} catch (err) {}
}, 1000);
}

if (!fs.existsSync(findpath())) {
if (!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' });
var child = spawn(execPath, [resolve(__dirname, '..', 'scripts', 'install.js')], { stdio: 'inherit' });
child.on('close', run);
} else {
run();
Expand Down
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);
}
};
}
11 changes: 6 additions & 5 deletions lib/findpath.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const { env, platform } = require('node:process');
const { join, resolve } = require('node:path');
import { env, platform } from 'node:process';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = dirname(fileURLToPath(import.meta.url));

/**
* Get the platform dependant path of the NW.js or ChromeDriver binary.
*
* @param {'nwjs' | 'chromedriver'} executable Path to NW.js or Chromedriver executable.
* @return {string}
*/
function findpath(executable = 'nwjs') {
export function findpath(executable = 'nwjs') {
const nwDir = resolve(__dirname, '..', 'nwjs');
/**
* File path to executable.
Expand Down Expand Up @@ -53,5 +56,3 @@ function findpath(executable = 'nwjs') {

return binPath;
}

module.exports = { findpath };
28 changes: 15 additions & 13 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
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 { dirname, resolve } from 'node:path';
import { arch, env, platform, exit } from 'node:process';
import { URL, fileURLToPath } 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 nodeManifest from '../package.json' assert { type: "json" };

install()
const __dirname = dirname(fileURLToPath(import.meta.url));

await install()
.catch((error) => {
if (error.code === 'EPERM') {
console.error('Unable to create symlink since user did not run as Administrator.');
Expand Down Expand Up @@ -56,7 +58,7 @@ async function install() {
/**
* Parsed string version to Semver version object
*/
let parsedVersion = semver.parse(nodeManifest.version);
let parsedVersion = parse(nodeManifest.version);

/**
* Version string of the format `X.Y.Z-pre`.
Expand Down Expand Up @@ -180,7 +182,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
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "nw",
"version": "0.85.0",
"description": "A installer for nw.js",
"description": "An installer for nw.js",
"repository": {
"type": "git",
"url": "git://github.com/nwjs/npm-installer.git"
},
"type": "module",
"main": "lib/findpath.js",
"bin": {
"nw": "bin/nw.js"
Expand Down
8 changes: 4 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { strictEqual } = require('node:assert');
const { existsSync } = require('node:fs');
const test = require('node:test');
import { strictEqual } from 'node:assert';
import { existsSync } 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);
Expand Down

0 comments on commit 0f0453e

Please sign in to comment.