From 58da438c4f7b216bcef4c60c7c4a09f19740c177 Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Thu, 31 Oct 2024 20:12:13 +0530 Subject: [PATCH 1/9] chore(test): comment out options.app.* from demo --- tests/fixtures/demo.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/fixtures/demo.js b/tests/fixtures/demo.js index 1dd5455f..df73d1bc 100644 --- a/tests/fixtures/demo.js +++ b/tests/fixtures/demo.js @@ -8,16 +8,16 @@ await nwbuild({ cacheDir: './node_modules/nw', outDir: './tests/fixtures/out', glob: false, - app: { - name: 'nwapp', - // MacOS options - LSApplicationCategoryType: 'public.app-category.utilities', - CFBundleIdentifier: 'io.nwutils.demo', - CFBundleName: 'Demo', - CFBundleDisplayName: 'Demo', - CFBundleSpokenName: 'Demo', - CFBundleVersion: '0.0.0', - CFBundleShortVersionString: '0.0.0', - NSHumanReadableCopyright: 'Copyright (c) 2024 NW.js Utilities' - } + // app: { + // name: 'nwapp', + // // MacOS options + // LSApplicationCategoryType: 'public.app-category.utilities', + // CFBundleIdentifier: 'io.nwutils.demo', + // CFBundleName: 'Demo', + // CFBundleDisplayName: 'Demo', + // CFBundleSpokenName: 'Demo', + // CFBundleVersion: '0.0.0', + // CFBundleShortVersionString: '0.0.0', + // NSHumanReadableCopyright: 'Copyright (c) 2024 NW.js Utilities' + // } }); From cd6be4d9f5fa95e10cef35122523dfdc02de0f57 Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Thu, 31 Oct 2024 20:17:31 +0530 Subject: [PATCH 2/9] chore(util): console.log pkg --- src/util.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/util.js b/src/util.js index 370d30ae..ce79e17d 100644 --- a/src/util.js +++ b/src/util.js @@ -219,6 +219,7 @@ export const parse = async (options, pkg) => { options.nativeAddon = str2Bool(options.nativeAddon) ?? false; options.app = options.app ?? {}; + console.log(pkg); options.app.name = options.app.name ?? pkg.name; /* Remove special and control characters from app.name to mitigate potential path traversal. */ options.app.name = options.app.name.replace(/[<>:"/\\|?*\u0000-\u001F]/g, ''); From 76de55e47381d365199f196d0f1cdec6620f794e Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Thu, 31 Oct 2024 22:49:41 +0530 Subject: [PATCH 3/9] chore(util): handle options.app.name being undefined on first parse function call --- src/util.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/util.js b/src/util.js index ce79e17d..145e0082 100644 --- a/src/util.js +++ b/src/util.js @@ -219,10 +219,12 @@ export const parse = async (options, pkg) => { options.nativeAddon = str2Bool(options.nativeAddon) ?? false; options.app = options.app ?? {}; - console.log(pkg); options.app.name = options.app.name ?? pkg.name; - /* Remove special and control characters from app.name to mitigate potential path traversal. */ - options.app.name = options.app.name.replace(/[<>:"/\\|?*\u0000-\u001F]/g, ''); + /* Since the `parse` function is called twice, the first time `pkg` is `{}` and `options.app.name` is `undefined`. */ + if (options.app.name) { + /* Remove special and control characters from app.name to mitigate potential path traversal. */ + options.app.name = options.app.name.replace(/[<>:"/\\|?*\u0000-\u001F]/g, ''); + } options.app.icon = options.app.icon ?? undefined; // TODO(#737): move this out From 0104a7f291fcaae4d9581bfc060b6476ea3628e0 Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Thu, 31 Oct 2024 23:04:59 +0530 Subject: [PATCH 4/9] revert --- tests/fixtures/demo.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/fixtures/demo.js b/tests/fixtures/demo.js index df73d1bc..ae29dfee 100644 --- a/tests/fixtures/demo.js +++ b/tests/fixtures/demo.js @@ -8,16 +8,16 @@ await nwbuild({ cacheDir: './node_modules/nw', outDir: './tests/fixtures/out', glob: false, - // app: { - // name: 'nwapp', - // // MacOS options - // LSApplicationCategoryType: 'public.app-category.utilities', - // CFBundleIdentifier: 'io.nwutils.demo', - // CFBundleName: 'Demo', - // CFBundleDisplayName: 'Demo', - // CFBundleSpokenName: 'Demo', - // CFBundleVersion: '0.0.0', - // CFBundleShortVersionString: '0.0.0', - // NSHumanReadableCopyright: 'Copyright (c) 2024 NW.js Utilities' - // } + app: { + name: 'Demo', + // MacOS options + LSApplicationCategoryType: 'public.app-category.utilities', + CFBundleIdentifier: 'io.nwutils.demo', + CFBundleName: 'Demo', + CFBundleDisplayName: 'Demo', + CFBundleSpokenName: 'Demo', + CFBundleVersion: '0.0.0', + CFBundleShortVersionString: '0.0.0', + NSHumanReadableCopyright: 'Copyright (c) 2024 NW.js Utilities' + } }); From 9390685d2f2775d04293a1d317635e3d35a2b4de Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Fri, 1 Nov 2024 00:00:06 +0530 Subject: [PATCH 5/9] chore(test): update demo exe path --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 46bdb9de..3995a4fa 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "test": "vitest run --coverage", "test:cov": "vitest --coverage.enabled true", "demo:bld": "node ./tests/fixtures/demo.js", - "demo:exe": "./tests/fixtures/out/nwapp.app/Contents/MacOS/nwapp", + "demo:exe": "./tests/fixtures/out/Demo.app/Contents/MacOS/Demo", "demo:cli": "nwbuild --mode=run --version=0.92.0 --flavor=sdk --glob=false --cacheDir=./node_modules/nw ./tests/fixtures/app" }, "devDependencies": { From 2ef96a743a438946e455827694fead289e9c4ecb Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Fri, 1 Nov 2024 00:03:10 +0530 Subject: [PATCH 6/9] chore(docs): add item to roadmap --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a60e1753..1a1f9576 100644 --- a/README.md +++ b/README.md @@ -385,6 +385,7 @@ nwbuild({ ### Chores +- chore: add Linux, MacOS and Windows fixtures - chore(docs): don't store JSDoc definitions in `typedef`s - get's hard to understand during development. - chore: annotate file paths as `fs.PathLike` instead of `string`. - chore(bld): factor out core build step From c4dbc537c4b1db59e538376cc26a5fe4d4ad555d Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Fri, 1 Nov 2024 12:17:02 +0530 Subject: [PATCH 7/9] feat(bld): set product_string in manifest for MacOS builds --- package.json | 2 +- src/bld.js | 10 ++++++++-- src/index.js | 11 +++++++---- src/util.js | 15 ++++++++++----- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 3995a4fa..bb699cd4 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "test:cov": "vitest --coverage.enabled true", "demo:bld": "node ./tests/fixtures/demo.js", "demo:exe": "./tests/fixtures/out/Demo.app/Contents/MacOS/Demo", - "demo:cli": "nwbuild --mode=run --version=0.92.0 --flavor=sdk --glob=false --cacheDir=./node_modules/nw ./tests/fixtures/app" + "demo:cli": "nwbuild --mode=run --flavor=sdk --glob=false --cacheDir=./node_modules/nw ./tests/fixtures/app" }, "devDependencies": { "@eslint/js": "^9.12.0", diff --git a/src/bld.js b/src/bld.js index d2895854..665fadd3 100644 --- a/src/bld.js +++ b/src/bld.js @@ -133,7 +133,13 @@ async function bld({ await fs.promises.cp(nwDir, outDir, { recursive: true, verbatimSymlinks: true }); const files = await util.globFiles({ srcDir, glob }); - const manifest = await util.getNodeManifest({ srcDir, glob }); + let manifest = await util.getNodeManifest({ srcDir, glob }); + + /* Set `product_string` in manifest for MacOS. This is used in renaming the Helper apps. */ + if (options.platform === 'osx') { + manifest.json.product_string = app.name; + await fs.promises.writeFile(manifest.path, manifest.json); + } if (glob) { for (let file of files) { @@ -169,7 +175,7 @@ async function bld({ typeof managedManifest === 'object' || typeof managedManifest === 'string' ) { - await manageManifest({ nwPkg: manifest, managedManifest, outDir, platform }); + await manageManifest({ nwPkg: manifest.json, managedManifest, outDir, platform }); } if (platform === 'linux') { diff --git a/src/index.js b/src/index.js index 995a3227..b77986d4 100644 --- a/src/index.js +++ b/src/index.js @@ -39,18 +39,21 @@ import util from './util.js'; async function nwbuild(options) { let built; let releaseInfo = {}; - let manifest = {}; + let manifest = { + path: '', + json: undefined, + }; try { // Parse options options = await util.parse(options, manifest); manifest = await util.getNodeManifest({ srcDir: options.srcDir, glob: options.glob }); - if (typeof manifest?.nwbuild === 'object') { - options = manifest.nwbuild; + if (typeof manifest.json?.nwbuild === 'object') { + options = manifest.json.nwbuild; } - options = await util.parse(options, manifest); + options = await util.parse(options, manifest.json); //TODO: impl logging diff --git a/src/util.js b/src/util.js index 145e0082..6ff94c64 100644 --- a/src/util.js +++ b/src/util.js @@ -136,25 +136,30 @@ async function globFiles({ * @param {object} options - node manifest options * @param {string | string []} options.srcDir - src dir * @param {boolean} options.glob - glob flag - * @returns {object} - Node manifest + * @returns {Promise.<{path: string, json: object}>} - Node manifest */ async function getNodeManifest({ srcDir, glob }) { - let manifest; + let manifest = { + path: '', + json: undefined, + }; let files; if (glob) { files = await globFiles({ srcDir, glob }); for (const file of files) { if (path.basename(file) === 'package.json' && manifest === undefined) { - manifest = JSON.parse(await fs.promises.readFile(file)); + manifest.path = file; + manifest.json = JSON.parse(await fs.promises.readFile(file)); } } } else { - manifest = JSON.parse(await fs.promises.readFile(path.resolve(srcDir, 'package.json'))); + manifest.path = path.resolve(srcDir, 'package.json'); + manifest.json = JSON.parse(await fs.promises.readFile(path.resolve(srcDir, 'package.json'))); } - if (manifest === undefined) { + if (manifest.json === undefined) { throw new Error('package.json not found in srcDir file glob patterns.'); } From 9de5b194338bfe232af5edcc2a0cbb9bdf0488ad Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Fri, 1 Nov 2024 12:26:05 +0530 Subject: [PATCH 8/9] fix: linting error --- src/bld.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bld.js b/src/bld.js index 665fadd3..2df43124 100644 --- a/src/bld.js +++ b/src/bld.js @@ -136,7 +136,7 @@ async function bld({ let manifest = await util.getNodeManifest({ srcDir, glob }); /* Set `product_string` in manifest for MacOS. This is used in renaming the Helper apps. */ - if (options.platform === 'osx') { + if (platform === 'osx') { manifest.json.product_string = app.name; await fs.promises.writeFile(manifest.path, manifest.json); } From a4dcbe44e7baeb493bfa6491c6e5e734dc15b25c Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Fri, 1 Nov 2024 12:35:39 +0530 Subject: [PATCH 9/9] fix(bld): JSON.stringify the updated manifest.json before writing to file --- src/bld.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bld.js b/src/bld.js index 2df43124..9c11bfb2 100644 --- a/src/bld.js +++ b/src/bld.js @@ -138,7 +138,7 @@ async function bld({ /* Set `product_string` in manifest for MacOS. This is used in renaming the Helper apps. */ if (platform === 'osx') { manifest.json.product_string = app.name; - await fs.promises.writeFile(manifest.path, manifest.json); + await fs.promises.writeFile(manifest.path, JSON.stringify(manifest.json)); } if (glob) {