@lakutata/builder 3.1.3 → 3.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +24 -1
  2. package/README.zh-CN.md +7 -1
  3. package/dist/esm/index.js +5 -0
  4. package/dist/esm/lib/Archive.js +3 -2
  5. package/dist/esm/lib/Bundle.js +2 -1
  6. package/dist/esm/lib/Directories.js +18 -0
  7. package/dist/esm/lib/Executable.js +27 -6
  8. package/dist/esm/lib/Installer.js +28 -14
  9. package/dist/esm/lib/NativeRuntime.js +27 -3
  10. package/dist/esm/lib/Processes.js +3 -2
  11. package/dist/esm/lib/Tools.js +98 -19
  12. package/dist/esm/lib/linux/Linux.js +60 -10
  13. package/dist/esm/lib/mac/Mac.js +39 -11
  14. package/dist/esm/lib/mobile/Addons.js +675 -0
  15. package/dist/esm/lib/mobile/Node.js +190 -0
  16. package/dist/esm/lib/mobile/Stage.js +180 -0
  17. package/dist/esm/lib/nwjs/Addons.js +310 -0
  18. package/dist/esm/lib/nwjs/Package.js +149 -0
  19. package/dist/esm/lib/nwjs/Runtime.js +188 -0
  20. package/dist/esm/lib/nwjs/Stage.js +92 -0
  21. package/dist/esm/lib/win/Windows.js +68 -45
  22. package/dist/types/index.d.ts +7 -0
  23. package/dist/types/lib/Bundle.d.ts +5 -0
  24. package/dist/types/lib/Directories.d.ts +8 -0
  25. package/dist/types/lib/Executable.d.ts +7 -0
  26. package/dist/types/lib/Installer.d.ts +37 -4
  27. package/dist/types/lib/Processes.d.ts +2 -1
  28. package/dist/types/lib/Tools.d.ts +47 -1
  29. package/dist/types/lib/linux/Linux.d.ts +2 -1
  30. package/dist/types/lib/mac/Mac.d.ts +1 -1
  31. package/dist/types/lib/mobile/Addons.d.ts +154 -0
  32. package/dist/types/lib/mobile/Node.d.ts +89 -0
  33. package/dist/types/lib/mobile/Stage.d.ts +111 -0
  34. package/dist/types/lib/nwjs/Addons.d.ts +69 -0
  35. package/dist/types/lib/nwjs/Package.d.ts +56 -0
  36. package/dist/types/lib/nwjs/Runtime.d.ts +120 -0
  37. package/dist/types/lib/nwjs/Stage.d.ts +104 -0
  38. package/dist/types/lib/win/Windows.d.ts +30 -1
  39. package/package.json +2 -2
package/README.md CHANGED
@@ -6,7 +6,7 @@ The builder for lakutata applications. The `package bundle`, `package exe` and `
6
6
  `@lakutata/cli` use it, and a project's scripts can call it directly:
7
7
 
8
8
  ```js
9
- import {buildExecutable, buildInstaller, bundleProject} from '@lakutata/builder'
9
+ import {buildExecutable, buildInstaller, buildNwjsApp, bundleProject} from '@lakutata/builder'
10
10
 
11
11
  //dist/<name>.cjs, run by node, with its native addons and its resources next to it
12
12
  await bundleProject({project: process.cwd()})
@@ -16,6 +16,8 @@ await buildExecutable({project: process.cwd(), runtime: 'node', layout: 'directo
16
16
  //dist/installer: the installers for the current platform (deb, rpm, apk, archlinux, tar.gz on Linux; pkg, tar.gz on
17
17
  //macOS; nsis, msi, zip on Windows), which also register the application's service when it declares one
18
18
  await buildInstaller({project: process.cwd()})
19
+ //An NW.js application (its version in lakutata.nwjs): dist/nwjs/<name>-<build>, the runtime of NW.js named after it
20
+ await buildNwjsApp({project: process.cwd()})
19
21
  ```
20
22
 
21
23
  The builder works on the compiled application: build the project with `tsc` first, since `tsc` emits the decorator
@@ -29,6 +31,27 @@ result. At run time, `resourcesPath()` from lakutata locates the application's r
29
31
  `lakutata package bundle`, `lakutata package exe` and `lakutata package installer` in the `@lakutata/cli`
30
32
  documentation.
31
33
 
34
+ The NW.js applications run in the version of NW.js of their `lakutata.nwjs`. `nwjsReleases()` lists the releases of
35
+ NW.js, `downloadNwjs()` downloads a runtime into the cache (checked against the checksums of its release), and
36
+ `stageNwjsApp()` makes the application for NW.js: its bundle for the Node.js of that NW.js, its native addons made to
37
+ load in it (built again for its architecture, or with its headers when they use the V8 API; on Windows, given the
38
+ Node.js of NW.js), its resources and the manifest of NW.js. `buildNwjsApp()` puts it into the normal flavor of NW.js
39
+ named after the application, and `buildInstaller()` makes its installers with `"runtime": "nwjs"` (the installers of a
40
+ desktop application: dmg on macOS, nsis on Windows, deb on Linux by default).
41
+
42
+ The mobile applications of React Native (Android, iOS, HarmonyOS) run in a libnode built from the sources of Node.js by
43
+ lakutata/libnode. `downloadMobileNode()` downloads the libnode of a system from its releases once
44
+ (`LAKUTATA_LIBNODE_MIRROR`: a mirror of them), checks it with their checksums and keeps it in the cache
45
+ (`LAKUTATA_MOBILE_NODE_DIR`: a libnode of your own instead). `stageMobileApp()` makes the Node.js project of the
46
+ application: the bundle for the Node.js of the libnode (Node.js 24.20.0, `MOBILE_NODE_VERSION`, without a libnode), the
47
+ resources, the manifest and an entry that puts away `fetch()` without WebAssembly (HarmonyOS). It builds the native
48
+ addons for the system from their sources, against the headers and the libraries of the libnode: a native tree per
49
+ architecture of Android (node-gyp and the NDK), for iOS a framework per addon for each SDK (Xcode) in the directory the
50
+ `frameworks` option gives, and for HarmonyOS a library per addon (the native SDK of DevEco Studio) in the libraries of
51
+ the HAP the `libraries` option gives. The HAP of HarmonyOS leaves out of its resources the files whose names start with
52
+ a dot: on HarmonyOS the native tree is packed into the project (`native.gz`, `native.json`) and extracted at its first
53
+ run into the directory `LAKUTATA_NATIVE_DIR` gives.
54
+
32
55
  The Linux installers are tested in containers (Docker): `npm run test:installer` builds them on Debian and Alpine, then
33
56
  installs, upgrades and removes them on Debian and Fedora (systemd) and on Alpine (OpenRC). The macOS installers are
34
57
  installed in a macOS virtual machine (`npm run test:installer:mac`, with tart), and the Windows installers on Windows,
package/README.zh-CN.md CHANGED
@@ -5,7 +5,7 @@
5
5
  lakutata 应用的打包工具。`@lakutata/cli` 的 `package bundle`、`package exe` 和 `package installer` 命令都靠它完成工作,项目的脚本也可以直接调用它:
6
6
 
7
7
  ```js
8
- import {buildExecutable, buildInstaller, bundleProject} from '@lakutata/builder'
8
+ import {buildExecutable, buildInstaller, buildNwjsApp, bundleProject} from '@lakutata/builder'
9
9
 
10
10
  //dist/<name>.cjs,由 node 运行,原生插件和资源放在它旁边
11
11
  await bundleProject({project: process.cwd()})
@@ -15,10 +15,16 @@ await buildExecutable({project: process.cwd(), runtime: 'node', layout: 'directo
15
15
  //dist/installer:当前平台的安装包(Linux 上是 deb、rpm、apk、archlinux、tar.gz;macOS 上是 pkg、tar.gz;
16
16
  //Windows 上是 nsis、msi、zip),应用声明了服务时,安装包还会注册这个服务
17
17
  await buildInstaller({project: process.cwd()})
18
+ //NW.js 应用(版本在 lakutata.nwjs 中):dist/nwjs/<name>-<build>,以应用命名的 NW.js 运行时
19
+ await buildNwjsApp({project: process.cwd()})
18
20
  ```
19
21
 
20
22
  打包工具处理的是编译后的应用:请先用 `tsc` 构建项目,因为装饰器的元数据是由 `tsc` 生成的。单文件包是一个 CommonJS 文件,由 esbuild 生成。原生插件不能打进单文件包,所以它们连同运行时加载的内容会一起收集到一个 native 目录树中。打包器跟踪不到应用按路径读取的文件,这些文件也放在这个目录树中:单独运行的脚本(worker、worker 池、`fork()`、`require(path.join(__dirname, 'x.js'))`)会各自打包,数据文件和目录则直接复制。打包工具会找出由模块位置构造的路径,并改写它们;无法一起发布的内容会列在结果的 `warnings` 中。运行时,lakutata 的 `resourcesPath()` 负责找到应用的资源,这些资源在应用 `package.json` 的 `lakutata.resources` 中声明。安装包则由同一个文件中的 `lakutata.installer` 配置。详见 `@lakutata/cli` 文档中的 `lakutata package bundle`、`lakutata package exe` 和 `lakutata package installer`。
21
23
 
24
+ NW.js 应用运行在它 `lakutata.nwjs` 中指定的 NW.js 版本里。`nwjsReleases()` 列出 NW.js 的发布版本,`downloadNwjs()` 把一个运行时下载到缓存目录(按其发布版本的校验和核对),`stageNwjsApp()` 为 NW.js 准备应用:为这个 NW.js 的 Node.js 打包的代码、处理成能在其中加载的原生插件(为它的架构重新构建,使用 V8 API 的插件用它的头文件重新构建;在 Windows 上让插件加载 NW.js 的 Node.js)、资源和 NW.js 的清单。`buildNwjsApp()` 把应用放进以应用命名的 NW.js normal 版本中,`buildInstaller()` 在 `"runtime": "nwjs"` 时制作它的安装包(桌面应用的安装包:默认 macOS 上是 dmg,Windows 上是 nsis,Linux 上是 deb)。
25
+
26
+ React Native 手机应用(Android、iOS、鸿蒙)运行在一个 libnode 中,它由 lakutata/libnode 从 Node.js 源码编译而成。`downloadMobileNode()` 从它的 Release 下载一个系统的 libnode,只下载一次(`LAKUTATA_LIBNODE_MIRROR` 指定镜像),用 Release 的校验和验证后保存在缓存中(设置 `LAKUTATA_MOBILE_NODE_DIR` 时改用自己编译的 libnode)。`stageMobileApp()` 生成应用的 Node.js 项目:面向 libnode 中的 Node.js 的单文件包(没有 libnode 时面向 Node.js 24.20.0,`MOBILE_NODE_VERSION`)、资源、清单,以及一个入口:没有 WebAssembly 时(鸿蒙)收起 `fetch()`。它还会用 libnode 的头文件和库,从源码为目标系统编译原生插件:Android 每种架构一棵原生树(node-gyp 和 NDK);iOS 每个插件为每个 SDK 编成一个 framework(Xcode),放在 `frameworks` 指定的目录中;鸿蒙每个插件编成一个库(DevEco Studio 的原生 SDK),放在 `libraries` 指定的 HAP 库目录中。鸿蒙的 HAP 会把以点开头的文件从资源中去掉,所以鸿蒙上的原生树打包进项目(`native.gz`、`native.json`),第一次运行时解压到 `LAKUTATA_NATIVE_DIR` 指定的目录。
27
+
22
28
  Linux 安装包在容器(Docker)中测试:`npm run test:installer` 先在 Debian 和 Alpine 上制作安装包,再在 Debian、Fedora(systemd)和 Alpine(OpenRC)上安装、升级和卸载它们。macOS 安装包在 macOS 虚拟机中测试安装(`npm run test:installer:mac`,使用 tart),Windows 安装包则在 Windows 上以管理员身份测试安装(`LAKUTATA_WINDOWS_INSTALL=1 npm run test:installer:windows`)。
23
29
 
24
30
  ## 另见
package/dist/esm/index.js CHANGED
@@ -11,3 +11,8 @@ export { INSTALLER_HOOKS, NATIVE_HOOKS } from './lib/Scripts.js';
11
11
  export { detectLibc } from './lib/Platform.js';
12
12
  export { WINDOWS_TARGETS } from './lib/win/Windows.js';
13
13
  export { MAC_TARGETS } from './lib/mac/Mac.js';
14
+ export { downloadNwjs, nwjsPlatform, nwjsRelease, nwjsReleases } from './lib/nwjs/Runtime.js';
15
+ export { stageNwjsApp } from './lib/nwjs/Stage.js';
16
+ export { buildNwjsApp } from './lib/nwjs/Package.js';
17
+ export { stageMobileApp } from './lib/mobile/Stage.js';
18
+ export { downloadMobileNode, MOBILE_NODE_RELEASE, MOBILE_NODE_VERSION } from './lib/mobile/Node.js';
@@ -1,5 +1,6 @@
1
- import { cp, rm, stat } from 'node:fs/promises';
1
+ import { rm, stat } from 'node:fs/promises';
2
2
  import path from 'node:path';
3
+ import { copyDirectory } from './Directories.js';
3
4
  import { runCommand } from './Processes.js';
4
5
  /**
5
6
  * The tar.gz archive of the directory of the executable (its native addons and its resources next to it), named after
@@ -12,7 +13,7 @@ import { runCommand } from './Processes.js';
12
13
  export async function archiveDirectory(plan, arch, work, outdir) {
13
14
  const base = `${plan.name}-${plan.version}-${process.platform}-${arch}`;
14
15
  await rm(path.join(work, base), { recursive: true, force: true });
15
- await cp(plan.directory, path.join(work, base), { recursive: true, verbatimSymlinks: true });
16
+ await copyDirectory(plan.directory, path.join(work, base));
16
17
  const file = path.join(outdir, `${base}.tar.gz`);
17
18
  const archived = await runCommand('tar', ['-czf', file, base], work);
18
19
  if (archived.code !== 0)
@@ -25,6 +25,7 @@ const IMPORT_META_URL = '__lakutataImportMetaUrl';
25
25
  * arguments of the CommonJS modules: its file is the executable (as in the executables of Node.js), its require loads
26
26
  * the modules of node and the ones of a node_modules next to the executable (the ones left out of the bundle)
27
27
  */
28
+ //node-compat: process.getBuiltinModule (Bun only)
28
29
  const BUN_PRELUDE = 'var __filename=process.execPath,__dirname=process.getBuiltinModule(\'node:path\').dirname(__filename),require=typeof require===\'function\'?require:process.getBuiltinModule(\'node:module\').createRequire(__filename);';
29
30
  /**
30
31
  * The files of the resources of an application, declared by the "lakutata.resources" of its package.json: their paths in
@@ -111,7 +112,7 @@ export async function bundleProject(options) {
111
112
  metafile: true,
112
113
  bundle: true,
113
114
  platform: 'node',
114
- target: `node${process.versions.node.split('.')[0]}`,
115
+ target: `node${options.node?.replace(/^v/, '') ?? process.versions.node.split('.')[0]}`,
115
116
  format: 'cjs',
116
117
  minify: options.minify ?? true,
117
118
  //The names of the classes are the ones of the objects of the container and of the errors
@@ -0,0 +1,18 @@
1
+ import { cp, mkdir } from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { runCommand } from './Processes.js';
4
+ /**
5
+ * Copy a directory into another one, its links kept as they are: cp -a on macOS and Linux (the copy of Node.js turns
6
+ * the relative links into absolute ones before Node.js 18.17 and 20.2: its option verbatimSymlinks), the copy of Node.js
7
+ * on Windows (its directories have no links)
8
+ * @param source
9
+ * @param target
10
+ */
11
+ export async function copyDirectory(source, target) {
12
+ if (process.platform === 'win32')
13
+ return await cp(source, target, { recursive: true });
14
+ await mkdir(target, { recursive: true });
15
+ const copied = await runCommand('cp', ['-a', `${source}/.`, target], path.dirname(target));
16
+ if (copied.code !== 0)
17
+ throw new Error(`${source} cannot be copied: ${copied.output.trim()}`);
18
+ }
@@ -76,14 +76,31 @@ async function writeIcon(work, icon) {
76
76
  return file;
77
77
  }
78
78
  /**
79
- * Embed a bundle in the Node.js binary running the tool (the single executable applications of Node.js)
79
+ * The node program embedded in an executable and its version: the one given, else the one running the tool. It must
80
+ * have the assets of the single executable applications (Node.js 20.12, 21.7 or later): node:sea and its getAsset.
81
+ * @param program
82
+ */
83
+ async function nodeOfExecutable(program) {
84
+ const node = program ? path.resolve(program) : process.execPath;
85
+ //node-compat: node:sea (the probe, run by the node program, loads it in a try)
86
+ const probe = await runCommand(node, ['-e', "let assets=false;try{assets=typeof require('node:sea').getAsset==='function'}catch{}process.stdout.write(process.version+' '+assets)"], process.cwd()).catch((error) => ({ code: 1, output: error.message }));
87
+ const [version, assets] = probe.output.trim().split(' ');
88
+ if (probe.code !== 0 || !/^v\d+\.\d+\.\d+$/.test(version))
89
+ throw new Error(`${node} cannot be run as the node program of the executable: ${probe.output.trim()}`);
90
+ if (assets !== 'true')
91
+ throw new Error(`The executables of Node.js need the assets of its single executable applications (Node.js 20.12, 21.7 or later): ${node} is Node.js ${version}. Give a newer node program (nodeProgram, --node-program). Or make the executable with Bun (--runtime bun)`);
92
+ return { path: node, version: version };
93
+ }
94
+ /**
95
+ * Embed a bundle in a Node.js binary (the single executable applications of Node.js)
80
96
  * @param bundle
81
97
  * @param work
82
98
  * @param file
83
99
  * @param warnings
100
+ * @param node the node program embedded
84
101
  * @param icon the icon of the application (ico, Windows)
85
102
  */
86
- async function embedInNode(bundle, work, file, warnings, icon) {
103
+ async function embedInNode(bundle, work, file, warnings, node, icon) {
87
104
  const assets = {};
88
105
  if (bundle.native) {
89
106
  //The files of the native packages, extracted by the executable at its first run
@@ -104,9 +121,9 @@ async function embedInNode(bundle, work, file, warnings, icon) {
104
121
  useSnapshot: false,
105
122
  assets: assets
106
123
  }));
107
- await must(process.execPath, ['--experimental-sea-config', 'sea-config.json'], work);
124
+ await must(node.path, ['--experimental-sea-config', 'sea-config.json'], work);
108
125
  const { inject } = await import('postject');
109
- await copyFile(process.execPath, file);
126
+ await copyFile(node.path, file);
110
127
  //The icon of the application replaces the one of node.exe, before the bundle is embedded
111
128
  if (icon)
112
129
  await setExecutableIcon(file, icon);
@@ -119,7 +136,7 @@ async function embedInNode(bundle, work, file, warnings, icon) {
119
136
  //The Authenticode signature of node.exe does not match the executable anymore
120
137
  if (process.platform === 'win32')
121
138
  warnings.push(`${path.basename(file)} has the signature of Node.js, which is not valid anymore: sign it (signtool) before distributing it`);
122
- return `Node.js ${process.version}`;
139
+ return `Node.js ${node.version}`;
123
140
  }
124
141
  /**
125
142
  * Compile a bundle into an executable of Bun, in bytecode: the files of the native tree are embedded beside it, the
@@ -189,6 +206,8 @@ export async function buildExecutable(options) {
189
206
  const outdir = path.resolve(project, options.outdir ?? 'dist');
190
207
  //Bun is found before the application is bundled
191
208
  const bun = options.runtime === 'bun' ? await findBun(project) : undefined;
209
+ //The node program embedded, checked before the application is bundled
210
+ const node = bun ? undefined : await nodeOfExecutable(options.nodeProgram);
192
211
  const work = await mkdtemp(path.join(os.tmpdir(), 'lakutata-executable-'));
193
212
  try {
194
213
  const directory = options.layout === 'directory';
@@ -200,6 +219,8 @@ export async function buildExecutable(options) {
200
219
  await rm(target, { recursive: true, force: true });
201
220
  await mkdir(target, { recursive: true });
202
221
  const bundle = await bundleProject({
222
+ //The syntax of the node embedded
223
+ ...node ? { node: node.version } : {},
203
224
  ...options,
204
225
  outfile: path.join(work, 'main.cjs'),
205
226
  native: directory ? 'directory' : 'embedded',
@@ -225,7 +246,7 @@ export async function buildExecutable(options) {
225
246
  //The icon of the application: the one of the executables of Windows (the ones of macOS and Linux have none)
226
247
  const application = applicationIcon(project, manifest);
227
248
  const icon = application && process.platform === 'win32' ? icoFile(application.image) : undefined;
228
- const runtime = bun ? await compileWithBun(bun, embedded, work, made, icon) : await embedInNode(embedded, work, made, warnings, icon);
249
+ const runtime = bun ? await compileWithBun(bun, embedded, work, made, icon) : await embedInNode(embedded, work, made, warnings, node, icon);
229
250
  await copyFile(made, file);
230
251
  await chmod(file, 0o755);
231
252
  return {
@@ -3,6 +3,7 @@ import os from 'node:os';
3
3
  import path from 'node:path';
4
4
  import { programName } from './Bundle.js';
5
5
  import { buildElectronInstaller } from './electron/Electron.js';
6
+ import { buildNwjsApp } from './nwjs/Package.js';
6
7
  import { buildExecutable } from './Executable.js';
7
8
  import { applicationIcon, icoFile } from './Icons.js';
8
9
  import { detectLibc } from './Platform.js';
@@ -51,10 +52,12 @@ export async function buildInstaller(options) {
51
52
  if (process.platform !== 'linux' && !mac && !windows)
52
53
  throw new Error(`The installers of ${process.platform} are not supported: the ones of Linux are made on Linux, the ones of macOS on macOS, the ones of Windows on Windows`);
53
54
  const libc = detectLibc();
55
+ //A desktop application (NW.js): its installers by default the ones of the desktop applications (as electron-builder)
56
+ const desktop = config.runtime === 'nwjs';
54
57
  const targets = options.targets?.length ? options.targets
55
- : mac ? config.mac?.targets ?? ['pkg', 'tar.gz']
56
- : windows ? config.win?.targets ?? ['nsis', 'msi', 'zip']
57
- : config.linux?.targets ?? (libc === 'glibc' ? ['deb', 'rpm', 'tar.gz'] : ['apk', 'tar.gz']);
58
+ : mac ? config.mac?.targets ?? (desktop ? ['dmg'] : ['pkg', 'tar.gz'])
59
+ : windows ? config.win?.targets ?? (desktop ? ['nsis'] : ['nsis', 'msi', 'zip'])
60
+ : config.linux?.targets ?? (libc === 'glibc' ? (desktop ? ['deb'] : ['deb', 'rpm', 'tar.gz']) : desktop ? ['tar.gz'] : ['apk', 'tar.gz']);
58
61
  for (const target of targets) {
59
62
  if (mac) {
60
63
  if (!MAC_TARGETS.includes(target))
@@ -91,6 +94,8 @@ export async function buildInstaller(options) {
91
94
  if (!/^[A-Za-z0-9][A-Za-z0-9.-]*$/.test(appId))
92
95
  throw new Error(`The identifier of the application "${appId}" must be letters, digits, "." and "-" (lakutata.installer.appId)`);
93
96
  const service = config.service === true ? {} : config.service || undefined;
97
+ if (desktop && service)
98
+ throw new Error('A desktop application (lakutata.installer.runtime nwjs) has no service (lakutata.installer.service)');
94
99
  if (service?.name !== undefined && !/^[A-Za-z0-9_.@-]+$/.test(service.name))
95
100
  throw new Error(`The name of the service "${service.name}" must be letters, digits, "_", ".", "@" and "-"`);
96
101
  //A user of the system (Linux, macOS), an account of Windows (DOMAIN\user, NT SERVICE\name, a managed account ending in $)
@@ -108,35 +113,42 @@ export async function buildInstaller(options) {
108
113
  icon = path.join(work, 'icon.ico');
109
114
  await writeFile(icon, icoFile(application.image));
110
115
  }
111
- const executable = await buildExecutable({
116
+ //The executable and what it loads; or the application of NW.js (its runtime, its executable named after it)
117
+ const nwjs = desktop ? await buildNwjsApp({ project: project, entry: options.entry, minify: options.minify, external: options.external, arch: options.arch, outdir: path.join(work, 'stage'), progress: options.progress }) : undefined;
118
+ const executable = nwjs ? undefined : await buildExecutable({
112
119
  project: project,
113
120
  entry: options.entry,
114
121
  minify: options.minify,
115
122
  external: options.external,
116
123
  runtime: config.runtime,
124
+ nodeProgram: options.nodeProgram,
117
125
  layout: 'directory',
118
126
  outdir: path.join(work, 'stage')
119
127
  });
120
- warnings.push(...executable.warnings);
128
+ warnings.push(...executable?.warnings ?? nwjs.stage.bundle.warnings);
129
+ //The directory installed: the one of the bundle of macOS (/Applications/<product>.app), the one of the runtime of NW.js
130
+ const installed = nwjs ? (mac ? path.dirname(nwjs.directory) : nwjs.directory) : executable.directory;
131
+ const launcher = nwjs && path.basename(mac ? nwjs.directory : nwjs.executable);
121
132
  const plan = {
122
133
  name: name,
123
134
  appId: appId,
124
135
  productName: config.productName ?? (typeof manifest.productName === 'string' ? manifest.productName : name),
125
136
  program: program,
126
137
  version: version,
127
- description: config.description ?? manifest.description ?? name,
138
+ //An empty description is none (the one of a project created without it): Windows Installer refuses it
139
+ description: config.description || manifest.description || name,
128
140
  maintainer: maintainer,
129
141
  homepage: config.homepage ?? (typeof manifest.homepage === 'string' ? manifest.homepage : undefined),
130
142
  license: config.license ?? (typeof manifest.license === 'string' ? manifest.license : undefined),
131
143
  vendor: config.vendor,
132
- addToPath: config.addToPath ?? true,
144
+ addToPath: config.addToPath ?? !desktop,
133
145
  depends: config.linux?.depends ?? [],
134
146
  recommends: config.linux?.recommends ?? [],
135
147
  section: config.linux?.section,
136
148
  priority: config.linux?.priority,
137
149
  service: service && {
138
150
  name: service.name ?? name,
139
- description: service.description ?? config.description ?? manifest.description ?? name,
151
+ description: service.description || config.description || manifest.description || name,
140
152
  args: service.args ?? [],
141
153
  env: { MODE: 'production', ...service.env },
142
154
  //The users of the services of macOS are hidden by their names (_www), the ones of Windows are virtual
@@ -144,17 +156,19 @@ export async function buildInstaller(options) {
144
156
  restart: service.restart ?? 'on-failure',
145
157
  autoStart: service.autoStart ?? true
146
158
  },
147
- directory: executable.directory,
148
- prefix: mac ? `/usr/local/lib/${name}` : windows ? `C:\\Program Files\\${name}` : `/opt/${name}`,
159
+ directory: installed,
160
+ prefix: mac ? (launcher ? `/Applications/${launcher}` : `/usr/local/lib/${name}`) : windows ? `C:\\Program Files\\${name}` : `/opt/${name}`,
149
161
  scripts: scripts,
150
- icon: icon
162
+ icon: icon,
163
+ arch: nwjs ? nwjs.runtime.platform.slice(nwjs.runtime.platform.indexOf('-') + 1) : process.arch,
164
+ ...launcher ? { desktop: { executable: launcher, icon: application?.file } } : {}
151
165
  };
152
166
  const outdir = path.resolve(project, options.outdir ?? path.join('dist', 'installer'));
153
167
  await mkdir(outdir, { recursive: true });
154
168
  const files = mac ? await buildMacPackages(plan, targets, path.join(work, 'mac'), outdir)
155
- : windows ? await buildWindowsPackages(plan, targets, path.join(work, 'win'), outdir)
156
- : await buildLinuxPackages(plan, targets, path.join(work, 'linux'), outdir);
157
- return { name: name, version: version, files: files, executable: executable, warnings: warnings };
169
+ : windows ? await buildWindowsPackages(plan, targets, path.join(work, 'win'), outdir, options.progress)
170
+ : await buildLinuxPackages(plan, targets, path.join(work, 'linux'), outdir, options.progress);
171
+ return { name: name, version: version, files: files, ...executable ? { executable: executable } : {}, ...nwjs ? { nwjs: nwjs } : {}, warnings: warnings };
158
172
  }
159
173
  finally {
160
174
  await rm(work, { recursive: true, force: true });
@@ -30,8 +30,10 @@ export function lakutataNativeRequire(nodeRequire, config) {
30
30
  const fs = nodeRequire('node:fs');
31
31
  const os = nodeRequire('node:os');
32
32
  const { createRequire, isBuiltin } = nodeRequire('node:module');
33
+ //node-compat: node:sea (a type here; loaded below only where it exists)
33
34
  let sea;
34
35
  try {
36
+ //node-compat: node:sea
35
37
  sea = nodeRequire('node:sea');
36
38
  }
37
39
  catch {
@@ -80,13 +82,33 @@ export function lakutataNativeRequire(nodeRequire, config) {
80
82
  fs.symlinkSync(path.relative(path.dirname(at), local(temporary, link.target)), at, 'dir');
81
83
  }
82
84
  fs.writeFileSync(path.join(temporary, '.complete'), '');
85
+ const complete = () => fs.existsSync(path.join(directory, '.complete'));
83
86
  try {
84
87
  fs.renameSync(temporary, directory);
85
88
  }
86
89
  catch (error) {
87
90
  //Another process extracted it
88
- if (!fs.existsSync(path.join(directory, '.complete')))
91
+ if (complete())
92
+ return directory;
93
+ if (!fs.existsSync(directory))
89
94
  throw error;
95
+ //A tree left incomplete (a process stopped, a cleaning of the temporary files that keeps the directories):
96
+ //moved aside, removed, replaced
97
+ const stale = `${directory}.stale.${process.pid}.${Date.now()}`;
98
+ try {
99
+ fs.renameSync(directory, stale);
100
+ }
101
+ catch {
102
+ //Moved by another process
103
+ }
104
+ fs.rmSync(stale, { recursive: true, force: true });
105
+ try {
106
+ fs.renameSync(temporary, directory);
107
+ }
108
+ catch (retry) {
109
+ if (!complete())
110
+ throw retry;
111
+ }
90
112
  }
91
113
  }
92
114
  finally {
@@ -112,10 +134,12 @@ export function lakutataNativeRequire(nodeRequire, config) {
112
134
  }
113
135
  }
114
136
  else {
115
- //Next to the bundle, or in the resources of a packaged Electron application (outside of its archive)
137
+ //Next to the bundle, the tree of the architecture first (<directory>-arm64: a mobile application of Android holds
138
+ //one tree per architecture), or in the resources of a packaged Electron application (outside of its archive)
116
139
  const resources = process.resourcesPath;
117
140
  const local = path.join(__dirname, config.directory);
118
- root = fs.existsSync(local) || !resources ? local : path.join(resources, config.directory);
141
+ const own = `${local}-${process.arch}`;
142
+ root = fs.existsSync(own) ? own : fs.existsSync(local) || !resources ? local : path.join(resources, config.directory);
119
143
  }
120
144
  return root;
121
145
  };
@@ -27,11 +27,12 @@ export function spawnCommand(command, args, options) {
27
27
  * @param command
28
28
  * @param args
29
29
  * @param cwd
30
+ * @param env its environment, the one of the process by default
30
31
  * @return the exit code and the output
31
32
  */
32
- export function runCommand(command, args, cwd) {
33
+ export function runCommand(command, args, cwd, env) {
33
34
  return new Promise((resolve, reject) => {
34
- const child = spawnCommand(command, args, { cwd: cwd });
35
+ const child = spawnCommand(command, args, { cwd: cwd, env: env });
35
36
  let output = '';
36
37
  //Piped (the default stdio)
37
38
  child.stdout.on('data', (chunk) => output += chunk.toString());
@@ -1,6 +1,6 @@
1
1
  import { createHash } from 'node:crypto';
2
2
  import { existsSync } from 'node:fs';
3
- import { chmod, mkdir, mkdtemp, rename, rm, writeFile } from 'node:fs/promises';
3
+ import { chmod, mkdir, mkdtemp, open, rename, rm } from 'node:fs/promises';
4
4
  import os from 'node:os';
5
5
  import path from 'node:path';
6
6
  import { runCommand } from './Processes.js';
@@ -54,13 +54,100 @@ const TOOLS = {
54
54
  export function cacheDirectory() {
55
55
  return process.env.LAKUTATA_CACHE_DIR ?? path.join(process.env.XDG_CACHE_HOME ?? path.join(os.homedir(), '.cache'), 'lakutata');
56
56
  }
57
+ /**
58
+ * Download a file, its sha256 computed while it is written. A connection receiving too little (one of the releases of
59
+ * GitHub can stay at some kilobytes a second while another one downloads the file at once) or broken is left for
60
+ * another one, which downloads the file again from its start
61
+ * @param url
62
+ * @param file
63
+ * @param options
64
+ */
65
+ export async function downloadFile(url, file, options = {}) {
66
+ const interval = options.interval ?? 30000;
67
+ const minimum = options.minimum ?? 100 * 1024;
68
+ const attempts = options.attempts ?? 3;
69
+ const slow = `under ${minimum % 1024 === 0 ? `${minimum / 1024} KB` : `${minimum} bytes`} received in ${interval / 1000} s`;
70
+ for (let attempt = 1;; attempt++) {
71
+ const controller = new AbortController();
72
+ let received = 0;
73
+ let checked = 0;
74
+ //From the request: a connection waiting for its response receives nothing too
75
+ const timer = setInterval(() => {
76
+ if (received - checked < minimum)
77
+ controller.abort();
78
+ checked = received;
79
+ }, interval);
80
+ try {
81
+ const response = await fetch(url, { headers: options.headers, signal: controller.signal });
82
+ if (!response.ok || !response.body)
83
+ throw new Error(`${url} cannot be downloaded (${response.status})`);
84
+ const hash = createHash('sha256');
85
+ const handle = await open(file, 'w');
86
+ try {
87
+ for await (const chunk of response.body) {
88
+ received += chunk.length;
89
+ hash.update(chunk);
90
+ await handle.write(chunk);
91
+ }
92
+ }
93
+ finally {
94
+ await handle.close();
95
+ }
96
+ return hash.digest('hex');
97
+ }
98
+ catch (error) {
99
+ //The connections broken fail with a TypeError (fetch failed, terminated); a file refused by the server, or
100
+ //which cannot be written, is not downloaded again
101
+ const cause = error.cause?.code;
102
+ const reason = controller.signal.aborted ? slow : error instanceof TypeError ? `${error.message}${cause ? ` (${cause})` : ''}` : undefined;
103
+ if (reason === undefined)
104
+ throw error;
105
+ if (attempt === attempts)
106
+ throw new Error(`${url} cannot be downloaded, ${attempts} connections tried: ${reason}`, { cause: error });
107
+ options.progress?.(`${path.basename(file)} again (${attempt + 1}/${attempts}): ${reason}`);
108
+ }
109
+ finally {
110
+ clearInterval(timer);
111
+ }
112
+ }
113
+ }
114
+ /**
115
+ * Move a directory written aside into its place in the cache. The one already there is kept when it is complete (put
116
+ * there by another build running at the same time), and replaced when it is not: left by a build that was stopped, or
117
+ * emptied by a cleaning of the temporary files that removes the files and keeps the directories (macOS cleans its
118
+ * temporary directory so)
119
+ * @param temporary The directory written, removed by the caller.
120
+ * @param target Its place in the cache.
121
+ * @param complete Whether the directory in its place is complete.
122
+ */
123
+ export async function moveIntoCache(temporary, target, complete) {
124
+ try {
125
+ await rename(temporary, target);
126
+ }
127
+ catch (error) {
128
+ if (complete())
129
+ return;
130
+ if (!existsSync(target))
131
+ throw error;
132
+ //Moved aside before being removed: its place is free at once for this build and the others
133
+ const stale = `${target}.stale-${process.pid}-${Date.now()}`;
134
+ await rename(target, stale).catch(() => undefined);
135
+ await rm(stale, { recursive: true, force: true });
136
+ await rename(temporary, target).catch((retry) => {
137
+ //Put there by another build meanwhile
138
+ if (!complete())
139
+ throw retry;
140
+ });
141
+ }
142
+ }
57
143
  /**
58
144
  * A tool of the builder: the one given by its environment variable (LAKUTATA_NFPM, LAKUTATA_WINSW, LAKUTATA_NSIS:
59
145
  * makensis.exe, LAKUTATA_WIX: candle.exe, next to light.exe), else the version of the builder, downloaded once into the
60
146
  * cache and checked with its sha256
61
147
  * @param name
148
+ * @param progress Called with what is being downloaded
62
149
  */
63
- export async function ensureTool(name) {
150
+ export async function ensureTool(name, progress) {
64
151
  const given = process.env[`LAKUTATA_${name.toUpperCase()}`];
65
152
  if (given)
66
153
  return given;
@@ -73,21 +160,17 @@ export async function ensureTool(name) {
73
160
  const executable = path.join(directory, ...tool.executable.split('/'));
74
161
  if (existsSync(executable))
75
162
  return executable;
76
- //SourceForge answers the browsers with its page, the tools with the file
77
- const response = await fetch(tool.url(release.archive), { headers: { 'user-agent': 'Wget' } }).catch((error) => {
78
- throw new Error(`${name} cannot be downloaded (${error.message}): give its executable (LAKUTATA_${name.toUpperCase()})`);
79
- });
80
- if (!response.ok)
81
- throw new Error(`${name} cannot be downloaded (${tool.url(release.archive)}: ${response.status}): give its executable (LAKUTATA_${name.toUpperCase()})`);
82
- const archive = Buffer.from(await response.arrayBuffer());
83
- const sha256 = createHash('sha256').update(archive).digest('hex');
84
- if (sha256 !== release.sha256)
85
- throw new Error(`The archive of ${name} ${tool.version} downloaded is not the one of its release (sha256 ${sha256})`);
86
- //Extracted aside, then renamed: the builds running together do not see it partly written
163
+ progress?.(`${release.archive} (${name} ${tool.version})`);
164
+ //Downloaded and extracted aside, then renamed: the builds running together do not see it partly written
87
165
  await mkdir(path.dirname(directory), { recursive: true });
88
166
  const temporary = await mkdtemp(`${directory}.`);
89
167
  try {
90
- await writeFile(path.join(temporary, release.archive), archive);
168
+ //SourceForge answers the browsers with its page, the tools with the file
169
+ const sha256 = await downloadFile(tool.url(release.archive), path.join(temporary, release.archive), { headers: { 'user-agent': 'Wget' }, progress: progress }).catch((error) => {
170
+ throw new Error(`${error.message}: give the executable of ${name} (LAKUTATA_${name.toUpperCase()})`);
171
+ });
172
+ if (sha256 !== release.sha256)
173
+ throw new Error(`The archive of ${name} ${tool.version} downloaded is not the one of its release (sha256 ${sha256})`);
91
174
  //An archive (tar.gz, zip: the tar of Windows reads them), or the executable itself
92
175
  if (/\.(tar\.gz|zip)$/.test(release.archive)) {
93
176
  const extracted = await runCommand('tar', ['-xf', release.archive], temporary);
@@ -96,11 +179,7 @@ export async function ensureTool(name) {
96
179
  await rm(path.join(temporary, release.archive));
97
180
  }
98
181
  await chmod(path.join(temporary, ...tool.executable.split('/')), 0o755);
99
- await rename(temporary, directory).catch((error) => {
100
- //Extracted by another build
101
- if (!existsSync(executable))
102
- throw error;
103
- });
182
+ await moveIntoCache(temporary, directory, () => existsSync(executable));
104
183
  }
105
184
  finally {
106
185
  await rm(temporary, { recursive: true, force: true });