Skip to content

Commit

Permalink
docs: add example usage with nw module (#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushmanchhabra authored Dec 12, 2023
1 parent 2078b29 commit 593483d
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 808 deletions.
110 changes: 53 additions & 57 deletions docs/bld.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,17 @@ <h1 class="page-title">Source: bld.js</h1>

<section>
<article>
<pre class="prettyprint source linenums"><code>import { exec } from "node:child_process";
<pre class="prettyprint source linenums"><code>import child_process from "node:child_process";
import console from "node:console";
import { resolve } from "node:path";
import { arch as ARCH, platform as PLATFORM, chdir } from "node:process";
import {
cp,
copyFile,
rename,
readFile,
rm,
writeFile,
} from "node:fs/promises";
import path from "node:path";
import fsm from "node:fs/promises";
import process from "node:process";

import compressing from "compressing";
import rcedit from "rcedit";
import plist from "plist";

import { ARCH_KV, PLATFORM_KV, getManifest, getReleaseInfo, globFiles } from "./util.js"
import util from "./util.js"

/**
* References:
Expand Down Expand Up @@ -176,11 +169,11 @@ <h1 class="page-title">Source: bld.js</h1>
* sudo xattr -r -d com.apple.quarantine /path/to/nwjs.app
*
*/
export async function bld({
async function bld({
version = "latest",
flavor = "normal",
platform = PLATFORM_KV[PLATFORM],
arch = ARCH_KV[ARCH],
platform = util.PLATFORM_KV[process.platform],
arch = util.ARCH_KV[process.arch],
manifestUrl = "https://nwjs.io/versions",
srcDir = "./src",
cacheDir = "./cache",
Expand All @@ -191,23 +184,23 @@ <h1 class="page-title">Source: bld.js</h1>
nativeAddon = false,
zip = false,
}) {
const nwDir = resolve(
const nwDir = path.resolve(
cacheDir,
`nwjs${flavor === "sdk" ? "-sdk" : ""}-v${version}-${platform
}-${arch}`,
);

await rm(outDir, { force: true, recursive: true });
await cp(nwDir, outDir, { recursive: true, verbatimSymlinks: true });
await fsm.rm(outDir, { force: true, recursive: true });
await fsm.cp(nwDir, outDir, { recursive: true, verbatimSymlinks: true });

const files = await globFiles({ srcDir, glob });
const manifest = await getManifest({ srcDir, glob });
const files = await util.globFiles({ srcDir, glob });
const manifest = await util.getNodeManifest({ srcDir, glob });

if (glob) {
for (let file of files) {
await cp(
await fsm.cp(
file,
resolve(
path.resolve(
outDir,
platform !== "osx"
? "package.nw"
Expand All @@ -218,9 +211,9 @@ <h1 class="page-title">Source: bld.js</h1>
);
}
} else {
await cp(
await fsm.cp(
files,
resolve(
path.resolve(
outDir,
platform !== "osx"
? "package.nw"
Expand All @@ -230,7 +223,7 @@ <h1 class="page-title">Source: bld.js</h1>
);
}

const releaseInfo = await getReleaseInfo(
const releaseInfo = await util.getReleaseInfo(
version,
platform,
arch,
Expand Down Expand Up @@ -276,16 +269,16 @@ <h1 class="page-title">Source: bld.js</h1>
}

if (typeof managedManifest === "string") {
manifest = JSON.parse(await readFile(managedManifest));
manifest = JSON.parse(await fsm.readFile(managedManifest));
}

if (manifest.devDependencies) {
manifest.devDependencies = undefined;
}
manifest.packageManager = manifest.packageManager ?? "npm@*";

await writeFile(
resolve(
await fsm.writeFile(
path.resolve(
outDir,
platform !== "osx"
? "package.nw"
Expand All @@ -296,8 +289,8 @@ <h1 class="page-title">Source: bld.js</h1>
"utf8",
);

chdir(
resolve(
process.chdir(
path.resolve(
outDir,
platform !== "osx"
? "package.nw"
Expand All @@ -306,16 +299,16 @@ <h1 class="page-title">Source: bld.js</h1>
);

if (manifest.packageManager.startsWith("npm")) {
exec(`npm install`);
child_process.exec(`npm install`);
} else if (manifest.packageManager.startsWith("yarn")) {
exec(`yarn install`);
child_process.exec(`yarn install`);
} else if (manifest.packageManager.startsWith("pnpm")) {
exec(`pnpm install`);
child_process.exec(`pnpm install`);
}
};

const setLinuxConfig = async ({ app, outDir }) => {
if (PLATFORM === "win32") {
if (process.platform === "win32") {
console.warn(
"Linux apps built on Windows platform do not preserve all file permissions. See #716",
);
Expand Down Expand Up @@ -347,7 +340,7 @@ <h1 class="page-title">Source: bld.js</h1>
SingleMainWindow: app.singleMainWindow,
};

await rename(`${outDir}/nw`, `${outDir}/${app.name}`);
await fsm.rename(`${outDir}/nw`, `${outDir}/${app.name}`);

let fileContent = `[Desktop Entry]\n`;
Object.keys(desktopEntryFile).forEach((key) => {
Expand All @@ -356,7 +349,7 @@ <h1 class="page-title">Source: bld.js</h1>
}
});
let filePath = `${outDir}/${app.name}.desktop`;
await writeFile(filePath, fileContent);
await fsm.writeFile(filePath, fileContent);
};

const setWinConfig = async ({ app, outDir }) => {
Expand Down Expand Up @@ -392,8 +385,8 @@ <h1 class="page-title">Source: bld.js</h1>
}

try {
const outDirAppExe = resolve(outDir, `${app.name}.exe`);
await rename(resolve(outDir, "nw.exe"), outDirAppExe);
const outDirAppExe = path.resolve(outDir, `${app.name}.exe`);
await fsm.rename(path.resolve(outDir, "nw.exe"), outDirAppExe);
await rcedit(outDirAppExe, rcEditOptions);
} catch (error) {
console.warn(
Expand All @@ -404,33 +397,33 @@ <h1 class="page-title">Source: bld.js</h1>
};

const setOsxConfig = async ({ outDir, app }) => {
if (PLATFORM === "win32") {
if (process.platform === "win32") {
console.warn(
"MacOS apps built on Windows platform do not preserve all file permissions. See #716",
);
}

try {
const outApp = resolve(outDir, `${app.name}.app`);
await rename(resolve(outDir, "nwjs.app"), outApp);
const outApp = path.resolve(outDir, `${app.name}.app`);
await fsm.rename(path.resolve(outDir, "nwjs.app"), outApp);
if (app.icon !== undefined) {
await copyFile(
resolve(app.icon),
resolve(outApp, "Contents", "Resources", "app.icns"),
await fsm.copyFile(
path.resolve(app.icon),
path.resolve(outApp, "Contents", "Resources", "app.icns"),
);
}

const infoPlistPath = resolve(outApp, "Contents", "Info.plist");
const infoPlistJson = plist.parse(await readFile(infoPlistPath, "utf-8"));
const infoPlistPath = path.resolve(outApp, "Contents", "Info.plist");
const infoPlistJson = plist.parse(await fsm.readFile(infoPlistPath, "utf-8"));

const infoPlistStringsPath = resolve(
const infoPlistStringsPath = path.resolve(
outApp,
"Contents",
"Resources",
"en.lproj",
"InfoPlist.strings",
);
const infoPlistStringsData = await readFile(
const infoPlistStringsData = await fsm.readFile(
infoPlistStringsPath,
"utf-8",
);
Expand Down Expand Up @@ -458,8 +451,8 @@ <h1 class="page-title">Source: bld.js</h1>
}
});

await writeFile(infoPlistPath, plist.build(infoPlistJson));
await writeFile(
await fsm.writeFile(infoPlistPath, plist.build(infoPlistJson));
await fsm.writeFile(
infoPlistStringsPath,
infoPlistStringsDataArray.toString().replace(/,/g, "\n"),
);
Expand All @@ -469,17 +462,17 @@ <h1 class="page-title">Source: bld.js</h1>
};

const buildNativeAddon = ({ cacheDir, version, platform, arch, outDir, nodeVersion }) => {
let nodePath = resolve(cacheDir, `node-v${version}-${platform}-${arch}`);
chdir(
resolve(
let nodePath = path.resolve(cacheDir, `node-v${version}-${platform}-${arch}`);
process.chdir(
path.resolve(
outDir,
platform !== "osx"
? "package.nw"
: "nwjs.app/Contents/Resources/app.nw",
),
);

exec(
child_process.exec(
`node-gyp rebuild --target=${nodeVersion} --nodedir=${nodePath}`,
(error) => {
if (error !== null) {
Expand All @@ -501,8 +494,11 @@ <h1 class="page-title">Source: bld.js</h1>
await compressing.tgz.compressDir(outDir, `${outDir}.tgz`);
}

await rm(outDir, { recursive: true, force: true });
};</code></pre>
await fsm.rm(outDir, { recursive: true, force: true });
};

export default bld;
</code></pre>
</article>
</section>

Expand All @@ -518,7 +514,7 @@ <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.htm
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Sat Dec 02 2023 16:20:36 GMT-0500 (Eastern Standard Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Tue Dec 12 2023 11:22:57 GMT-0500 (Eastern Standard Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
Loading

0 comments on commit 593483d

Please sign in to comment.