@@ -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
3734import { decompress } from "./get/decompress.js";
3835import { download } from "./get/download.js";
@@ -41,6 +38,8 @@ <h1 class="page-title">Source: nwbuild.js</h1>
4138import { packager } from "./bld/package.js";
4239import { develop } from "./run/develop.js";
4340import { isCached } from "./util/cache.js";
41+ import { notify } from "./util/notify.js";
42+ import { getOptions } from "./util/options.js";
4443import { parse } from "./util/parse.js";
4544import { 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 */
112112const 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" && 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 && cached === true) {
158+ if (options.cache === false && 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 >
0 commit comments