-
Notifications
You must be signed in to change notification settings - Fork 302
/
index.js
139 lines (123 loc) · 5.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import console from 'node:console';
import fs from 'node:fs';
import bld from './bld.js';
import get from './get/index.js';
import run from './run.js';
import util from './util.js';
/**
* @typedef {object} Options Configuration options
* @property {"get" | "run" | "build"} [mode="build"] Choose between get, run or build mode
* @property {"latest" | "stable" | string} [version="latest"] Runtime version
* @property {"normal" | "sdk"} [flavor="normal"] Runtime flavor
* @property {"linux" | "osx" | "win"} platform Host platform
* @property {"ia32" | "x64" | "arm64"} arch Host architecture
* @property {"https://dl.nwjs.io" | string} [downloadUrl="https://dl.nwjs.io"] Download server
* @property {"https://nwjs.io/versions" | string} [manifestUrl="https://nwjs.io/versions"] Versions manifest
* @property {"./cache" | string} [cacheDir="./cache"] Directory to cache NW binaries
* @property {"./" | string} [srcDir="./"] File paths to application code
* @property {"./out" | string} [outDir="./out"] Directory to store build artifacts
* @property {object} app Refer to Linux/Windows Specific Options under Getting Started in the docs
* @property {boolean} [cache=true] If true the existing cache is used. Otherwise it removes and redownloads it.
* @property {boolean} [ffmpeg=false] If true the chromium ffmpeg is replaced by community version
* @property {boolean} [glob=true] If true file globbing is enabled when parsing srcDir.
* @property {"error" | "warn" | "info" | "debug"} [logLevel="info"] Specify level of logging.
* @property {boolean | "zip" | "tar" | "tgz"} [zip=false] If true, "zip", "tar" or "tgz" the outDir directory is compressed.
* @property {boolean | string | object} [managedManifest = false] Managed manifest mode
* @property {false | "gyp"} [nodeAddon = false] Rebuild Node native addons
* @property {boolean} [cli=false] If true the CLI is used to parse options. This option is used internally.
*/
/**
* Main module exported.
* @async
* @function
* @param {Options} options Options
* @returns {Promise<void>}
*/
async function nwbuild(options) {
let built;
let releaseInfo = {};
let manifest = {
path: '',
json: undefined,
};
try {
// Parse options
options = await util.parse(options, manifest);
manifest = await util.getNodeManifest({ srcDir: options.srcDir, glob: options.glob });
if (typeof manifest.json?.nwbuild === 'object') {
options = manifest.json.nwbuild;
}
options = await util.parse(options, manifest.json);
//TODO: impl logging
built = fs.existsSync(options.cacheDir);
if (built === false) {
await fs.promises.mkdir(options.cacheDir, { recursive: true });
}
if (options.mode === 'build') {
built = fs.existsSync(options.outDir);
if (built === false) {
await fs.promises.mkdir(options.outDir, { recursive: true });
}
}
// Validate options.version to get the version specific release info
releaseInfo = await util.getReleaseInfo(
options.version,
options.platform,
options.arch,
options.cacheDir,
options.manifestUrl,
);
await util.validate(options, releaseInfo);
// Remove leading "v" from version string
options.version = releaseInfo.version.slice(1);
// Download binaries
await get({
version: options.version,
flavor: options.flavor,
platform: options.platform,
arch: options.arch,
downloadUrl: options.downloadUrl,
cacheDir: options.cacheDir,
cache: options.cache,
ffmpeg: options.ffmpeg,
nativeAddon: options.nativeAddon,
});
if (options.mode === 'get') {
// Do nothing else since we have already downloaded the binaries.
return;
}
if (options.mode === 'run') {
await run({
version: options.version,
flavor: options.flavor,
platform: options.platform,
arch: options.arch,
srcDir: options.srcDir,
cacheDir: options.cacheDir,
glob: options.glob,
argv: options.argv,
});
} else if (options.mode === 'build') {
await bld({
version: options.version,
flavor: options.flavor,
platform: options.platform,
arch: options.arch,
manifestUrl: options.manifestUrl,
srcDir: options.srcDir,
cacheDir: options.cacheDir,
outDir: options.outDir,
app: options.app,
glob: options.glob,
managedManifest: options.managedManifest,
nativeAddon: options.nativeAddon,
zip: options.zip,
releaseInfo: releaseInfo,
});
}
} catch (error) {
console.error(error);
throw error;
}
}
export default nwbuild;