Skip to content

Commit 38dc4a6

Browse files
fix: file globing (#785)
1 parent ef37e56 commit 38dc4a6

File tree

13 files changed

+685
-97
lines changed

13 files changed

+685
-97
lines changed

CHANGELOG-4.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
## [4.0.10] - 2023-02-05
11+
12+
### Added
13+
14+
- `options.cli` flag to prevent `node-glob` from globbing already globbed files and erroring out with a `package.json not found in srcDir file glob patterns` message.
15+
16+
### Changed
17+
18+
- Copy subdirectories of `options.srDir` in the correct location.
19+
1020
## [4.0.9] - 2023-02-03
1121

1222
### Added

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ nwbuild({
4242
CLI usage
4343

4444
```shell
45-
nwbuild ./nwapp --mode=run --version=latest --flavor=sdk
45+
nwbuild ./nwapp/**/* --mode=run --version=latest --flavor=sdk
4646
```
4747

4848
package.json usage
@@ -83,7 +83,7 @@ nwbuild({
8383
CLI usage
8484

8585
```shell
86-
nwbuild ./nwapp --mode=build --version=latest --flavor=normal --platform=linux --arch=x64 --outDir=./out
86+
nwbuild ./nwapp/**/* --mode=build --version=latest --flavor=normal --platform=linux --arch=x64 --outDir=./out
8787
```
8888

8989
package.json usage
@@ -301,7 +301,7 @@ The final code should look like this.
301301
const { nwbuild } = require("nw-builder");
302302

303303
await nwbuild({
304-
srcDir: "./nwapp",
304+
srcDir: "./nwapp/**/*",
305305
mode: "build",
306306
version: "latest",
307307
flavor: "normal",

docs/global.html

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ <h5 class="subsection-title">Properties:</h5>
592592
><ul class="dummy"
593593
><li>
594594
<a href="nwbuild.js.html">nwbuild.js</a>,
595-
<a href="nwbuild.js.html#line18">line 18</a>
595+
<a href="nwbuild.js.html#line17">line 17</a>
596596
</li></ul
597597
></dd
598598
>
@@ -641,7 +641,8 @@ <h5 class="subsection-title">Properties:</h5>
641641
<td class="default"> "./" </td>
642642

643643
<td class="description last"
644-
>String of glob patterns which correspond to NW app code</td
644+
>String of space separated glob patterns which correspond to
645+
NW app code</td
645646
>
646647
</tr>
647648

@@ -849,6 +850,23 @@ <h5 class="subsection-title">Properties:</h5>
849850
>If true the outDir directory is zipped</td
850851
>
851852
</tr>
853+
854+
<tr>
855+
<td class="name"><code>cli</code></td>
856+
857+
<td class="type">
858+
<span class="param-type">boolean</span>
859+
</td>
860+
861+
<td class="attributes"> &lt;optional><br /> </td>
862+
863+
<td class="default"> false </td>
864+
865+
<td class="description last"
866+
>If true the CLI is used to glob srcDir and parse other
867+
options</td
868+
>
869+
</tr>
852870
</tbody>
853871
</table>
854872

@@ -858,7 +876,7 @@ <h5 class="subsection-title">Properties:</h5>
858876
><ul class="dummy"
859877
><li>
860878
<a href="nwbuild.js.html">nwbuild.js</a>,
861-
<a href="nwbuild.js.html#line58">line 58</a>
879+
<a href="nwbuild.js.html#line57">line 57</a>
862880
</li></ul
863881
></dd
864882
>
@@ -879,8 +897,8 @@ <h2><a href="index.html">Home</a></h2
879897

880898
<footer>
881899
Documentation generated by
882-
<a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.0</a> on Fri Jan 06
883-
2023 17:15:41 GMT-0500 (Eastern Standard Time)
900+
<a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.0</a> on Sun Feb 05
901+
2023 17:00:04 GMT-0500 (Eastern Standard Time)
884902
</footer>
885903

886904
<script>

docs/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ <h2><a href="index.html">Home</a></h2
4040

4141
<footer>
4242
Documentation generated by
43-
<a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.0</a> on Fri Jan 06
44-
2023 17:15:41 GMT-0500 (Eastern Standard Time)
43+
<a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.0</a> on Sun Feb 05
44+
2023 17:00:04 GMT-0500 (Eastern Standard Time)
4545
</footer>
4646

4747
<script>

docs/nwbuild.js.html

Lines changed: 28 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ <h1 class="page-title">Source: nwbuild.js</h1>
2929
<article>
3030
<pre
3131
class="prettyprint source linenums"
32-
><code>import { mkdir, readFile, rm } from "node:fs/promises";
33-
import { basename } from "node:path";
34-
35-
import glob from "glob-promise";
32+
><code>import { mkdir, rm } from "node:fs/promises";
3633

3734
import { decompress } from "./get/decompress.js";
3835
import { download } from "./get/download.js";
@@ -41,6 +38,8 @@ <h1 class="page-title">Source: nwbuild.js</h1>
4138
import { packager } from "./bld/package.js";
4239
import { develop } from "./run/develop.js";
4340
import { isCached } from "./util/cache.js";
41+
import { notify } from "./util/notify.js";
42+
import { getOptions } from "./util/options.js";
4443
import { parse } from "./util/parse.js";
4544
import { validate } from "./util/validate.js";
4645

@@ -88,7 +87,7 @@ <h1 class="page-title">Source: nwbuild.js</h1>
8887

8988
/**
9089
* @typedef {object} Options
91-
* @property {string} [srcDir="./"] String of glob patterns which correspond to NW app code
90+
* @property {string} [srcDir="./"] String of space separated glob patterns which correspond to NW app code
9291
* @property {"run" | "build"} [mode="build"] Run or build application
9392
* @property {"latest" | "stable" | string} [version="latest"] NW runtime version
9493
* @property {"normal" | "sdk"} [flavor="normal"] NW runtime build flavor
@@ -101,6 +100,7 @@ <h1 class="page-title">Source: nwbuild.js</h1>
101100
* @property {App} app Multi platform configuration options
102101
* @property {boolean} [cache=true] If true the existing cache is used. Otherwise it removes and redownloads it.
103102
* @property {boolean} [zip=false] If true the outDir directory is zipped
103+
* @property {boolean} [cli=false] If true the CLI is used to glob srcDir and parse other options
104104
*/
105105

106106
/**
@@ -111,67 +111,24 @@ <h1 class="page-title">Source: nwbuild.js</h1>
111111
*/
112112
const nwbuild = async (options) => {
113113
let nwDir = "";
114-
let nwPkg = undefined;
115114
let cached;
115+
let nwCached;
116116
let built;
117117
let releaseInfo = {};
118-
try {
119-
let files = [];
120-
let patterns = options.srcDir.split(" ");
121118

122-
for (const pattern of patterns) {
123-
let contents = await glob(pattern);
124-
files.push(...contents);
125-
// Try to find the first instance of the package.json
126-
for (const content of contents) {
127-
if (basename(content) === "package.json" &amp;&amp; nwPkg === undefined) {
128-
nwPkg = JSON.parse(await readFile(content));
129-
}
130-
}
131-
132-
if (nwPkg === undefined) {
133-
throw new Error("package.json not found in srcDir file glob patterns.");
134-
}
135-
}
119+
notify();
136120

137-
if (files.length === 0) {
138-
throw new Error(`The globbing pattern ${options.srcDir} is invalid.`);
139-
}
140-
141-
// The name property is required for NW.js applications
142-
if (nwPkg.name === undefined) {
143-
throw new Error(`name property is missing from package.json`);
144-
}
145-
146-
// The main property is required for NW.js applications
147-
if (nwPkg.main === undefined) {
148-
throw new Error(`main property is missing from package.json`);
149-
}
150-
151-
// If the nwbuild property exists in srcDir/package.json, then they take precedence
152-
if (typeof nwPkg.nwbuild === "object") {
153-
options = { ...nwPkg.nwbuild };
154-
}
155-
if (typeof nwPkg.nwbuild === "undefined") {
156-
log.debug(`nwbuild property is not defined in package.json`);
157-
} else {
158-
throw new Error(
159-
`nwbuild property in the package.json is of type ${typeof nwPkg.nwbuild}. Expected type object.`,
160-
);
161-
}
121+
try {
122+
const { opts, files, nwPkg } = await getOptions(options);
123+
options = opts;
162124

163-
// Parse options, set required values to undefined and flags with default values unless specified by user
125+
// Parse options
164126
options = await parse(options, nwPkg);
165127

166-
// Variable to store nwDir file path
167-
nwDir = `${options.cacheDir}/nwjs${
168-
options.flavor === "sdk" ? "-sdk" : ""
169-
}-v${options.version}-${options.platform}-${options.arch}`;
170-
171128
// Create cacheDir if it does not exist
172-
cached = await isCached(nwDir);
129+
cached = await isCached(options.cacheDir);
173130
if (cached === false) {
174-
await mkdir(nwDir, { recursive: true });
131+
await mkdir(options.cacheDir, { recursive: true });
175132
}
176133

177134
// Create outDir if it does not exist
@@ -180,23 +137,30 @@ <h1 class="page-title">Source: nwbuild.js</h1>
180137
await mkdir(options.outDir, { recursive: true });
181138
}
182139

183-
// Validate options.version here
184-
// We need to do this to get the version specific release info
140+
// Validate options.version to get the version specific release info
185141
releaseInfo = await getReleaseInfo(
186142
options.version,
187143
options.cacheDir,
188144
options.manifestUrl,
189145
);
146+
// Remove leading "v" from version string
147+
options.version = releaseInfo.version.slice(1);
190148

191-
validate(options, releaseInfo);
149+
await validate(options, releaseInfo);
192150

151+
// Variable to store nwDir file path
152+
nwDir = `${options.cacheDir}/nwjs${
153+
options.flavor === "sdk" ? "-sdk" : ""
154+
}-v${options.version}-${options.platform}-${options.arch}`;
155+
156+
nwCached = await isCached(nwDir);
193157
// Remove cached NW binary
194-
if (options.cache === false &amp;&amp; cached === true) {
158+
if (options.cache === false &amp;&amp; nwCached === true) {
195159
log.debug("Remove cached NW binary");
196160
await rm(nwDir, { force: true, recursive: true });
197161
}
198162
// Download relevant NW.js binaries
199-
if (cached === false) {
163+
if (nwCached === false) {
200164
log.debug("Download relevant NW.js binaries");
201165
await download(
202166
options.version,
@@ -226,7 +190,7 @@ <h1 class="page-title">Source: nwbuild.js</h1>
226190
}
227191
} catch (error) {
228192
log.error(error);
229-
return error;
193+
throw error;
230194
}
231195
};
232196

@@ -248,8 +212,8 @@ <h2><a href="index.html">Home</a></h2
248212

249213
<footer>
250214
Documentation generated by
251-
<a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.0</a> on Fri Jan 06
252-
2023 17:15:41 GMT-0500 (Eastern Standard Time)
215+
<a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.0</a> on Sun Feb 05
216+
2023 17:00:04 GMT-0500 (Eastern Standard Time)
253217
</footer>
254218

255219
<script>

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nw-builder",
3-
"version": "4.0.9",
3+
"version": "4.0.10",
44
"description": "Build NW.js desktop applications for MacOS, Windows and Linux.",
55
"keywords": [
66
"NW.js",
@@ -21,7 +21,6 @@
2121
"files": [
2222
"./src",
2323
"./LICENSE"
24-
2524
],
2625
"homepage": "https://github.com/nwutils/nw-builder",
2726
"repository": {
@@ -34,8 +33,8 @@
3433
"docs": "jsdoc ./src/nwbuild.js -d docs",
3534
"test:unit": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
3635
"test:e2e": "cd ./test/e2e && node bld.js && node run.js",
37-
"demo:cli": "cd ./test/e2e && node cli.js",
38-
"demo:nix": "cd ./test/e2e && node nix.js",
36+
"demo:cli": "cd ./test/e2e && nwbuild ./nwapp/* ./nwapp/**/* --mode=build --version=latest --flavor=normal --platform=linux --arch=x64 --outDir=./build/cli",
37+
"demo:nix": "cd test/e2e && node nix.js",
3938
"demo:osx": "cd ./test/e2e && node osx.js",
4039
"demo:win": "cd ./test/e2e && node win.js"
4140
},

src/bld/package.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { sep } from "node:path";
12
import { cp, rm } from "node:fs/promises";
2-
import { basename } from "node:path";
33

44
import { log } from "../log.js";
55

@@ -34,13 +34,13 @@ const packager = async (
3434
log.debug(`Copy ${nwDir} files to ${outDir} directory`);
3535
await cp(nwDir, outDir, { recursive: true });
3636

37-
for (const file of files) {
37+
for (let file of files) {
3838
log.debug(`Copy ${file} file to ${outDir} directory`);
3939
await cp(
4040
file,
4141
`${outDir}/${
4242
platform !== "osx" ? "package.nw" : "nwjs.app/Contents/Resources/app.nw"
43-
}/${basename(file)}`,
43+
}/${file.split(sep).splice(2).join(sep)}`,
4444
{
4545
recursive: true,
4646
},

src/cli.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ const cli = yargs(hideBin(process.argv))
3737
nwbuild({
3838
...cli,
3939
srcDir: cli._.join(" "),
40+
cli: true,
4041
});

src/nwbuild.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import { log } from "./log.js";
6969
* @property {App} app Multi platform configuration options
7070
* @property {boolean} [cache=true] If true the existing cache is used. Otherwise it removes and redownloads it.
7171
* @property {boolean} [zip=false] If true the outDir directory is zipped
72+
* @property {boolean} [cli=false] If true the CLI is used to glob srcDir and parse other options
7273
*/
7374

7475
/**

src/util/options.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,27 @@ export const getOptions = async (opts) => {
1616
let nwPkg;
1717
const patterns = opts.srcDir.split(" ");
1818

19-
for (const pattern of patterns) {
20-
let contents = await glob(pattern);
21-
files.push(...contents);
22-
// Try to find the first instance of the package.json
23-
for (const content of contents) {
24-
if (basename(content) === "package.json" && nwPkg === undefined) {
25-
nwPkg = JSON.parse(await readFile(content));
26-
}
19+
// If the cli option is not true, then the srcDir glob patterns have not been parsed
20+
if (opts.cli !== true) {
21+
for (const pattern of patterns) {
22+
let contents = await glob(pattern);
23+
files.push(...contents);
2724
}
25+
} else {
26+
files = [...patterns];
27+
}
2828

29-
if (nwPkg === undefined) {
30-
throw new Error("package.json not found in srcDir file glob patterns.");
29+
// Try to find the first instance of the package.json
30+
for (const file of files) {
31+
if (basename(file) === "package.json" && nwPkg === undefined) {
32+
nwPkg = JSON.parse(await readFile(file));
3133
}
3234
}
3335

36+
if (nwPkg === undefined) {
37+
throw new Error("package.json not found in srcDir file glob patterns.");
38+
}
39+
3440
if (files.length === 0) {
3541
throw new Error(`The globbing pattern ${opts.srcDir} is invalid.`);
3642
}

0 commit comments

Comments
 (0)