Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJaredWilcurt authored Dec 14, 2023
2 parents 19a7a98 + b880dcd commit 33c3e61
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 56 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# EditorConfig is awesome: http://EditorConfig.org

# Top-most EditorConfig file
root = true

# defaults for all files
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

# Markdown files uses two trailing spaces to indicate a <br>
[*.{md,snap}]
trim_trailing_whitespace = false
31 changes: 7 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
# base-volta-off-of-nwjs

WIP: Tool to update the Volta config in package.json so Node match's your NW.js version
Updates the Volta config in your `package.json` so your Node.js version will match the version of Node.js built in to NW.js.

This script needs adapted to work as a library:

* https://github.com/nwutils/nw-selenium-javascript-example/blob/main/base-volta-off-of-nw.mjs
## Usage

1. `npm pkg set scripts.postinstall="npx base-volta-off-of-nwjs"`

## API ideas
This will add a command to your npm scripts that will automatically run after every time you do an `npm install`. It will update the Volta object in your `package.json` so the `node` value will match

I'm wondering if it could be as easy as this?

```json
{
"name": "my-app",
"main": "index.html",
"scripts": {
"start": "nw .",
"postinstall": "base-volta-off-of-nwjs"
}
"devDependencies": {
"nw": "0.80.0-sdk",
"base-volta-off-of-nwjs": "^1.0.0"
}
}
```
## Requirements

Alternatively we could allow passing in the path to `node_modules` or the path to the `package.json`?

```json
"postinstall": "base-volta-off-of-nwjs ./node_modules ./package.json"
```
1. You must have `nw` listed in `dependencies` or `devDependencies`
1. `nw` must be installed in the `node_modules` folder
126 changes: 94 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { promises as fs } from 'fs';
#!/usr/bin/env node

import { promises as fs } from 'node:fs';
import path from 'node:path';
import * as url from 'url';
import * as url from 'node:url';

let https;
try {
Expand All @@ -11,6 +13,9 @@ try {

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

let originalManifestIndentation = 2;
let originalManifestEOL = '\n';

function fileExists (file) {
return fs.access(file, fs.constants.F_OK)
.then(() => {
Expand Down Expand Up @@ -47,21 +52,31 @@ function getVersions () {
});
}

function getLocalNwManifest () {
function getLocalNwManifestPath () {
return new Promise(async (resolve, reject) => {
const nwManifest = path.join(__dirname, 'node_modules', 'nw', 'package.json');
const nwManifestExists = await fileExists(nwManifest);

if (nwManifestExists) {
try {
let data = await fs.readFile(nwManifest, 'binary');
data = JSON.parse(data);
resolve(data);
} catch (error) {
reject(error);
}
const nwManifestRelativeToHere = path.resolve(__dirname, '..', 'nw', 'package.json');
const nwManifestRelativeToCwd = path.resolve(process.cwd(), 'node_modules', 'nw', 'package.json');
const hereExists = await fileExists(nwManifestRelativeToHere);
const cwdExists = await fileExists(nwManifestRelativeToCwd);
if (hereExists) {
resolve(nwManifestRelativeToHere);
} else if (cwdExists) {
resolve(nwManifestRelativeToCwd);
} else {
reject(new Error('NW.js manifest file does not seem to exist'));
reject(new Error('Could not locate nw node module manifest.'));
}
});
}

function getLocalNwManifest () {
return new Promise(async (resolve, reject) => {
try {
const nwManifest = await getLocalNwManifestPath();
let data = await fs.readFile(nwManifest, 'binary');
data = JSON.parse(data);
resolve(data);
} catch (error) {
reject(error);
}
});
}
Expand Down Expand Up @@ -91,26 +106,68 @@ function getCorrectNodeVersion () {
});
}

function getManifest () {
function determineOriginalManifestIndentation (data) {
data = data.trim();
data = data.replaceAll('\r\n', '\n');

if (data[0] !== '{' || data[1] !== '\n') {
return;
}

let first = data[2];
let second = data[3];
let third = data[4];
let fourth = data[5];

if (first === '\t') {
originalManifestIndentation = '\t';
} else if (first + second + third + fourth === ' ') {
originalManifestIndentation = 4;
} else {
originalManifestIndentation = 2;
}
}

function determinOriginalEOL (data) {
if (data.includes('\r\n')) {
originalManifestEOL = '\r\n';
} else {
originalManifestEOL = '\n';
}
}

function getManifestPath () {
return new Promise(async (resolve, reject) => {
const manifest = path.join(__dirname, 'package.json');
const manifestExists = await fileExists(manifest);

if (manifestExists) {
try {
let data = await fs.readFile(manifest, 'binary');
data = JSON.parse(data);
resolve(data);
} catch (error) {
reject(error);
}
const manifestRelativeToHere = path.resolve(__dirname, '..', '..', 'package.json');
const manifestRelativeToCwd = path.resolve(process.cwd(), 'package.json');
const hereExists = await fileExists(manifestRelativeToHere);
const cwdExists = await fileExists(manifestRelativeToCwd);
if (hereExists) {
resolve(manifestRelativeToHere);
} else if (cwdExists) {
resolve(manifestRelativeToCwd);
} else {
reject(new Error('Cannot locate package.json'));
reject(new Error('Could not locate your manifest.'));
}
});
}

function updateVoltaObjectInManifest () {
function getManifest () {
return new Promise(async (resolve, reject) => {
try {
const manifest = await getManifestPath();
let data = await fs.readFile(manifest, 'binary');
determineOriginalManifestIndentation(String(data));
determinOriginalEOL(String(data));
data = JSON.parse(data);
resolve(data);
} catch (error) {
reject(error);
}
});
}

function getManifestWithUpdatedVoltaObject () {
return new Promise(async (resolve, reject) => {
try {
const manifest = await getManifest();
Expand All @@ -126,9 +183,14 @@ function updateVoltaObjectInManifest () {

async function run () {
try {
const mutatedManifest = await updateVoltaObjectInManifest();
const manifest = path.join(__dirname, 'package.json');
await fs.writeFile(manifest, JSON.stringify(mutatedManifest, null, 2) + '\n');
const manifestPath = await getManifestPath();

let mutatedManifest = await getManifestWithUpdatedVoltaObject();
mutatedManifest = JSON.stringify(mutatedManifest, null, originalManifestIndentation);
mutatedManifest = mutatedManifest.replaceAll('\r\n', '\n').replaceAll('\n', originalManifestEOL);
mutatedManifest = mutatedManifest + originalManifestEOL;

await fs.writeFile(manifestPath, mutatedManifest);
} catch (error) {
console.error(error);
}
Expand Down
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "base-volta-off-of-nwjs",
"version": "0.0.1",
"main": "index.js",
"bin": "./index.js",
"license": "MIT",
"description": "Updates Volta config in package.json so Node match's what your NW.js version uses",
"scripts": {
},
"repository": {
"type": "git",
"url": "git+https://github.com/nwutils/base-volta-off-of-nwjs.git"
},
"keywords": [
"nw.js",
"volta",
"node.js",
"version",
"management",
"automated"
],
"author": "TheJaredWilcurt",
"bugs": {
"url": "https://github.com/nwutils/base-volta-off-of-nwjs/issues"
},
"homepage": "https://github.com/nwutils/base-volta-off-of-nwjs#readme",
"volta": {
"node": "21.4.0",
"npm": "10.2.4"
}
}

0 comments on commit 33c3e61

Please sign in to comment.