Skip to content

Commit b6d5612

Browse files
chore: extract options finder logic
1 parent d39a6d3 commit b6d5612

File tree

2 files changed

+56
-46
lines changed

2 files changed

+56
-46
lines changed

src/nwbuild.js

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import { mkdir, readFile, rm } from "node:fs/promises";
2-
import { basename } from "node:path";
3-
4-
import glob from "glob-promise";
1+
import { mkdir, rm } from "node:fs/promises";
52

63
import { decompress } from "./get/decompress.js";
74
import { download } from "./get/download.js";
@@ -11,6 +8,7 @@ import { packager } from "./bld/package.js";
118
import { develop } from "./run/develop.js";
129
import { isCached } from "./util/cache.js";
1310
import { notify } from "./util/notify.js";
11+
import { getOptions } from "./util/options.js";
1412
import { parse } from "./util/parse.js";
1513
import { validate } from "./util/validate.js";
1614

@@ -81,54 +79,16 @@ import { log } from "./log.js";
8179
*/
8280
const nwbuild = async (options) => {
8381
let nwDir = "";
84-
let nwPkg = undefined;
8582
let cached;
8683
let built;
8784
let releaseInfo = {};
88-
notify();
89-
try {
90-
let files = [];
91-
let patterns = options.srcDir.split(" ");
92-
93-
for (const pattern of patterns) {
94-
let contents = await glob(pattern);
95-
files.push(...contents);
96-
// Try to find the first instance of the package.json
97-
for (const content of contents) {
98-
if (basename(content) === "package.json" && nwPkg === undefined) {
99-
nwPkg = JSON.parse(await readFile(content));
100-
}
101-
}
102-
103-
if (nwPkg === undefined) {
104-
throw new Error("package.json not found in srcDir file glob patterns.");
105-
}
106-
}
107-
108-
if (files.length === 0) {
109-
throw new Error(`The globbing pattern ${options.srcDir} is invalid.`);
110-
}
11185

112-
// The name property is required for NW.js applications
113-
if (nwPkg.name === undefined) {
114-
throw new Error(`name property is missing from package.json`);
115-
}
86+
notify();
11687

117-
// The main property is required for NW.js applications
118-
if (nwPkg.main === undefined) {
119-
throw new Error(`main property is missing from package.json`);
120-
}
88+
try {
12189

122-
// If the nwbuild property exists in srcDir/package.json, then they take precedence
123-
if (typeof nwPkg.nwbuild === "object") {
124-
options = { ...nwPkg.nwbuild };
125-
} else if (typeof nwPkg.nwbuild === "undefined") {
126-
log.debug(`nwbuild property is not defined in package.json`);
127-
} else {
128-
throw new Error(
129-
`nwbuild property in the package.json is of type ${typeof nwPkg.nwbuild}. Expected type object.`,
130-
);
131-
}
90+
const { opts, files, nwPkg } = await getOptions(options);
91+
options = opts;
13292

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

src/util/options.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { readFile } from "node:fs/promises";
2+
import { basename } from "node:path";
3+
4+
import glob from "glob-promise";
5+
6+
import { log } from "../log.js";
7+
8+
/**
9+
* Get options object from package.json, otherwise from module/cli
10+
*
11+
* @param {import("../nwbuild").Options} opts The options object
12+
* @return {Promise<object>} The options object
13+
*/
14+
export const getOptions = async (opts) => {
15+
let files = [];
16+
let nwPkg;
17+
const patterns = opts.srcDir.split(" ");
18+
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+
}
27+
}
28+
29+
if (nwPkg === undefined) {
30+
throw new Error("package.json not found in srcDir file glob patterns.");
31+
}
32+
}
33+
34+
if (files.length === 0) {
35+
throw new Error(`The globbing pattern ${opts.srcDir} is invalid.`);
36+
}
37+
38+
// If the nwbuild property exists in srcDir/package.json, then they take precedence
39+
if (typeof nwPkg.nwbuild === "object") {
40+
opts = { ...nwPkg.nwbuild };
41+
} else if (typeof nwPkg.nwbuild === "undefined") {
42+
log.debug(`nwbuild property is not defined in package.json`);
43+
} else {
44+
throw new Error(
45+
`nwbuild property in the package.json is of type ${typeof nwPkg.nwbuild}. Expected type object.`,
46+
);
47+
}
48+
49+
return { opts, files, nwPkg };
50+
};

0 commit comments

Comments
 (0)