Skip to content

Commit

Permalink
fix: file globing (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushmanchhabra authored Feb 5, 2023
1 parent ef37e56 commit 38dc4a6
Show file tree
Hide file tree
Showing 13 changed files with 685 additions and 97 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [4.0.10] - 2023-02-05

### Added

- `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.

### Changed

- Copy subdirectories of `options.srDir` in the correct location.

## [4.0.9] - 2023-02-03

### Added
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ nwbuild({
CLI usage

```shell
nwbuild ./nwapp --mode=run --version=latest --flavor=sdk
nwbuild ./nwapp/**/* --mode=run --version=latest --flavor=sdk
```

package.json usage
Expand Down Expand Up @@ -83,7 +83,7 @@ nwbuild({
CLI usage

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

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

await nwbuild({
srcDir: "./nwapp",
srcDir: "./nwapp/**/*",
mode: "build",
version: "latest",
flavor: "normal",
Expand Down
28 changes: 23 additions & 5 deletions docs/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ <h5 class="subsection-title">Properties:</h5>
><ul class="dummy"
><li>
<a href="nwbuild.js.html">nwbuild.js</a>,
<a href="nwbuild.js.html#line18">line 18</a>
<a href="nwbuild.js.html#line17">line 17</a>
</li></ul
></dd
>
Expand Down Expand Up @@ -641,7 +641,8 @@ <h5 class="subsection-title">Properties:</h5>
<td class="default"> "./" </td>

<td class="description last"
>String of glob patterns which correspond to NW app code</td
>String of space separated glob patterns which correspond to
NW app code</td
>
</tr>

Expand Down Expand Up @@ -849,6 +850,23 @@ <h5 class="subsection-title">Properties:</h5>
>If true the outDir directory is zipped</td
>
</tr>

<tr>
<td class="name"><code>cli</code></td>

<td class="type">
<span class="param-type">boolean</span>
</td>

<td class="attributes"> &lt;optional><br /> </td>

<td class="default"> false </td>

<td class="description last"
>If true the CLI is used to glob srcDir and parse other
options</td
>
</tr>
</tbody>
</table>

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

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

<script>
Expand Down
4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ <h2><a href="index.html">Home</a></h2

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

<script>
Expand Down
92 changes: 28 additions & 64 deletions docs/nwbuild.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ <h1 class="page-title">Source: nwbuild.js</h1>
<article>
<pre
class="prettyprint source linenums"
><code>import { mkdir, readFile, rm } from "node:fs/promises";
import { basename } from "node:path";

import glob from "glob-promise";
><code>import { mkdir, rm } from "node:fs/promises";

import { decompress } from "./get/decompress.js";
import { download } from "./get/download.js";
Expand All @@ -41,6 +38,8 @@ <h1 class="page-title">Source: nwbuild.js</h1>
import { packager } from "./bld/package.js";
import { develop } from "./run/develop.js";
import { isCached } from "./util/cache.js";
import { notify } from "./util/notify.js";
import { getOptions } from "./util/options.js";
import { parse } from "./util/parse.js";
import { validate } from "./util/validate.js";

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

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

/**
Expand All @@ -111,67 +111,24 @@ <h1 class="page-title">Source: nwbuild.js</h1>
*/
const nwbuild = async (options) => {
let nwDir = "";
let nwPkg = undefined;
let cached;
let nwCached;
let built;
let releaseInfo = {};
try {
let files = [];
let patterns = options.srcDir.split(" ");

for (const pattern of patterns) {
let contents = await glob(pattern);
files.push(...contents);
// Try to find the first instance of the package.json
for (const content of contents) {
if (basename(content) === "package.json" &amp;&amp; nwPkg === undefined) {
nwPkg = JSON.parse(await readFile(content));
}
}

if (nwPkg === undefined) {
throw new Error("package.json not found in srcDir file glob patterns.");
}
}
notify();

if (files.length === 0) {
throw new Error(`The globbing pattern ${options.srcDir} is invalid.`);
}

// The name property is required for NW.js applications
if (nwPkg.name === undefined) {
throw new Error(`name property is missing from package.json`);
}

// The main property is required for NW.js applications
if (nwPkg.main === undefined) {
throw new Error(`main property is missing from package.json`);
}

// If the nwbuild property exists in srcDir/package.json, then they take precedence
if (typeof nwPkg.nwbuild === "object") {
options = { ...nwPkg.nwbuild };
}
if (typeof nwPkg.nwbuild === "undefined") {
log.debug(`nwbuild property is not defined in package.json`);
} else {
throw new Error(
`nwbuild property in the package.json is of type ${typeof nwPkg.nwbuild}. Expected type object.`,
);
}
try {
const { opts, files, nwPkg } = await getOptions(options);
options = opts;

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

// Variable to store nwDir file path
nwDir = `${options.cacheDir}/nwjs${
options.flavor === "sdk" ? "-sdk" : ""
}-v${options.version}-${options.platform}-${options.arch}`;

// Create cacheDir if it does not exist
cached = await isCached(nwDir);
cached = await isCached(options.cacheDir);
if (cached === false) {
await mkdir(nwDir, { recursive: true });
await mkdir(options.cacheDir, { recursive: true });
}

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

// Validate options.version here
// We need to do this to get the version specific release info
// Validate options.version to get the version specific release info
releaseInfo = await getReleaseInfo(
options.version,
options.cacheDir,
options.manifestUrl,
);
// Remove leading "v" from version string
options.version = releaseInfo.version.slice(1);

validate(options, releaseInfo);
await validate(options, releaseInfo);

// Variable to store nwDir file path
nwDir = `${options.cacheDir}/nwjs${
options.flavor === "sdk" ? "-sdk" : ""
}-v${options.version}-${options.platform}-${options.arch}`;

nwCached = await isCached(nwDir);
// Remove cached NW binary
if (options.cache === false &amp;&amp; cached === true) {
if (options.cache === false &amp;&amp; nwCached === true) {
log.debug("Remove cached NW binary");
await rm(nwDir, { force: true, recursive: true });
}
// Download relevant NW.js binaries
if (cached === false) {
if (nwCached === false) {
log.debug("Download relevant NW.js binaries");
await download(
options.version,
Expand Down Expand Up @@ -226,7 +190,7 @@ <h1 class="page-title">Source: nwbuild.js</h1>
}
} catch (error) {
log.error(error);
return error;
throw error;
}
};

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

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

<script>
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nw-builder",
"version": "4.0.9",
"version": "4.0.10",
"description": "Build NW.js desktop applications for MacOS, Windows and Linux.",
"keywords": [
"NW.js",
Expand All @@ -21,7 +21,6 @@
"files": [
"./src",
"./LICENSE"

],
"homepage": "https://github.com/nwutils/nw-builder",
"repository": {
Expand All @@ -34,8 +33,8 @@
"docs": "jsdoc ./src/nwbuild.js -d docs",
"test:unit": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test:e2e": "cd ./test/e2e && node bld.js && node run.js",
"demo:cli": "cd ./test/e2e && node cli.js",
"demo:nix": "cd ./test/e2e && node nix.js",
"demo:cli": "cd ./test/e2e && nwbuild ./nwapp/* ./nwapp/**/* --mode=build --version=latest --flavor=normal --platform=linux --arch=x64 --outDir=./build/cli",
"demo:nix": "cd test/e2e && node nix.js",
"demo:osx": "cd ./test/e2e && node osx.js",
"demo:win": "cd ./test/e2e && node win.js"
},
Expand Down
6 changes: 3 additions & 3 deletions src/bld/package.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sep } from "node:path";
import { cp, rm } from "node:fs/promises";
import { basename } from "node:path";

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

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

for (const file of files) {
for (let file of files) {
log.debug(`Copy ${file} file to ${outDir} directory`);
await cp(
file,
`${outDir}/${
platform !== "osx" ? "package.nw" : "nwjs.app/Contents/Resources/app.nw"
}/${basename(file)}`,
}/${file.split(sep).splice(2).join(sep)}`,
{
recursive: true,
},
Expand Down
1 change: 1 addition & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ const cli = yargs(hideBin(process.argv))
nwbuild({
...cli,
srcDir: cli._.join(" "),
cli: true,
});
1 change: 1 addition & 0 deletions src/nwbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import { log } from "./log.js";
* @property {App} app Multi platform configuration options
* @property {boolean} [cache=true] If true the existing cache is used. Otherwise it removes and redownloads it.
* @property {boolean} [zip=false] If true the outDir directory is zipped
* @property {boolean} [cli=false] If true the CLI is used to glob srcDir and parse other options
*/

/**
Expand Down
26 changes: 16 additions & 10 deletions src/util/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,27 @@ export const getOptions = async (opts) => {
let nwPkg;
const patterns = opts.srcDir.split(" ");

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

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

if (nwPkg === undefined) {
throw new Error("package.json not found in srcDir file glob patterns.");
}

if (files.length === 0) {
throw new Error(`The globbing pattern ${opts.srcDir} is invalid.`);
}
Expand Down
5 changes: 0 additions & 5 deletions test/e2e/cli.js

This file was deleted.

Loading

0 comments on commit 38dc4a6

Please sign in to comment.