-
Notifications
You must be signed in to change notification settings - Fork 300
/
index.js
211 lines (181 loc) · 6.51 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import fs from "node:fs";
import path from "node:path";
import decompress from "./decompress.js";
import ffmpeg from "./ffmpeg.js";
import node from "./node.js";
import nw from "./nw.js";
import util from "../util.js";
/**
* @typedef {object} GetOptions
* @property {string | "latest" | "stable" | "lts"} [version = "latest"] Runtime version
* @property {"normal" | "sdk"} [flavor = "normal"] Build flavor
* @property {"linux" | "osx" | "win"} [platform] Target platform
* @property {"ia32" | "x64" | "arm64"} [arch] Target arch
* @property {string} [downloadUrl = "https://dl.nwjs.io"] Download server
* @property {string} [cacheDir = "./cache"] Cache directory
* @property {boolean} [cache = true] If false, remove cache and redownload.
* @property {boolean} [ffmpeg = false] If true, ffmpeg is not downloaded.
* @property {false | "gyp"} [nativeAddon = false] Rebuild native modules
*/
/**
* Get binaries.
*
* @async
* @function
* @param {GetOptions} options Get mode options
* @return {Promise<void>}
*/
async function get(options) {
/**
* If `options.cacheDir` exists, then `true`. Otherwise, it is `false`.
*
* @type {boolean}
*/
const cacheDirExists = await util.fileExists(options.cacheDir);
if (cacheDirExists === false) {
await fs.promises.mkdir(options.cacheDir, { recursive: true });
}
/**
* File path to compressed binary.
*
* @type {string}
*/
let nwFilePath = path.resolve(
options.cacheDir,
`nwjs${options.flavor === "sdk" ? "-sdk" : ""}-v${options.version}-${options.platform}-${options.arch}.${options.platform === "linux" ? "tar.gz" : "zip"
}`,
);
/**
* File path to directory which contain NW.js and related binaries.
*
* @type {string}
*/
let nwDirPath = path.resolve(
options.cacheDir,
`nwjs${options.flavor === "sdk" ? "-sdk" : ""}-v${options.version}-${options.platform}-${options.arch}`,
);
// If `options.cache` is false, then remove the compressed binary.
if (options.cache === false) {
await fs.promises.rm(nwFilePath, {
recursive: true,
force: true,
});
}
// We remove the nwDir to prevent the edge case where you download with ffmpeg flag enabled
// but want a subsequent build with ffmpeg flag disabled. By removing the directory and
// decompressing it again, we prevent the community ffmpeg files from being left over.
// This is important since the community ffmpeg builds have specific licensing constraints.
await fs.promises.rm(nwDirPath, { recursive: true, force: true });
/**
* If the compressed binary exists, then `true`. Otherwise, it is `false`.
*
* @type {boolean}
*/
const nwFilePathExists = await util.fileExists(nwFilePath);
if (nwFilePathExists === false) {
nwFilePath = await nw(options.downloadUrl, options.version, options.flavor, options.platform, options.arch, options.cacheDir);
}
await decompress(nwFilePath, options.cacheDir);
if (options.ffmpeg === true) {
/**
* File path to compressed binary which contains community FFmpeg binary.
*
* @type {string}
*/
let ffmpegFilePath = path.resolve(
options.cacheDir,
`ffmpeg-${options.version}-${options.platform}-${options.arch}.zip`,
);
// If `options.cache` is false, then remove the compressed binary.
if (options.cache === false) {
await fs.promises.rm(ffmpegFilePath, {
recursive: true,
force: true,
});
}
/**
* If the compressed binary exists, then `true`. Otherwise, it is `false`.
*
* @type {boolean}
*/
const ffmpegFilePathExists = await util.fileExists(ffmpegFilePath);
if (ffmpegFilePathExists === false) {
// Do not update the options.downloadUrl with the ffmpeg URL here. Doing so would lead to error when options.ffmpeg and options.nativeAddon are both enabled.
const downloadUrl =
"https://github.com/nwjs-ffmpeg-prebuilt/nwjs-ffmpeg-prebuilt/releases/download";
ffmpegFilePath = await ffmpeg(downloadUrl, options.version, options.platform, options.arch, options.cacheDir);
}
await decompress(ffmpegFilePath, options.cacheDir);
/**
* Platform dependant file name of FFmpeg binary.
*
* @type {string}
*/
let ffmpegFileName = "";
if (options.platform === "linux") {
ffmpegFileName = "libffmpeg.so";
} else if (options.platform === "win") {
ffmpegFileName = "ffmpeg.dll";
} else if (options.platform === "osx") {
ffmpegFileName = "libffmpeg.dylib";
}
/**
* File path to platform specific FFmpeg file.
*
* @type {string}
*/
let ffmpegBinaryPath = path.resolve(options.cacheDir, ffmpegFileName);
/**
* File path of where FFmpeg will be copied to.
*
* @type {string}
*/
let ffmpegBinaryDest = "";
if (options.platform === "linux") {
ffmpegBinaryDest = path.resolve(nwDirPath, "lib", ffmpegFileName);
} else if (options.platform === "win") {
ffmpegBinaryDest = path.resolve(nwDirPath, ffmpegFileName);
} else if (options.platform === "osx") {
ffmpegBinaryDest = path.resolve(
nwDirPath,
"nwjs.app",
"Contents",
"Frameworks",
"nwjs Framework.framework",
"Versions",
"Current",
ffmpegFileName,
);
}
await fs.promises.copyFile(ffmpegBinaryPath, ffmpegBinaryDest);
}
if (options.nativeAddon === "gyp") {
/**
* File path to NW'js Node headers tarball.
*
* @type {string}
*/
let nodeFilePath = path.resolve(
options.cacheDir,
`headers-v${options.version}.tar.gz`,
);
// If `options.cache` is false, then remove the compressed binary.
if (options.cache === false) {
await fs.promises.rm(nodeFilePath, {
recursive: true,
force: true,
});
}
/**
* If the compressed binary exists, then `true`. Otherwise, it is `false`.
*
* @type {boolean}
*/
const nodeFilePathExists = await util.fileExists(nodeFilePath);
if (nodeFilePathExists === false) {
nodeFilePath = await node(options.downloadUrl, options.version, options.cacheDir);
}
await decompress(nodeFilePath, options.cacheDir);
}
}
export default get;