@tsdown/exe 0.21.0-beta.2 → 0.21.0-beta.4

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.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025-present VoidZero Inc. & Contributors
4
+ Copyright (c) 2024 Kevin Deng (https://github.com/sxzz)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # @tsdown/exe
2
+
3
+ [![npm version][npmx-version-src]][npmx-href]
4
+ [![npm downloads][npmx-downloads-src]][npmx-href]
5
+
6
+ Cross-platform executable building for [tsdown](https://tsdown.dev), powered by Node.js [Single Executable Applications (SEA)](https://nodejs.org/api/single-executable-applications.html).
7
+
8
+ ## Documentation
9
+
10
+ See the [Executable documentation](https://tsdown.dev/options/exe) for usage and configuration details.
11
+
12
+ ## License
13
+
14
+ [MIT](../../LICENSE)
15
+
16
+ <!-- Badges -->
17
+
18
+ [npmx-version-src]: https://npmx.dev/api/registry/badge/version/@tsdown/exe
19
+ [npmx-downloads-src]: https://npmx.dev/api/registry/badge/downloads-month/@tsdown/exe
20
+ [npmx-href]: https://npmx.dev/@tsdown/exe
@@ -0,0 +1,42 @@
1
+ //#region src/platform.d.ts
2
+ type ExePlatform = "win" | "darwin" | "linux";
3
+ type ExeArch = "x64" | "arm64";
4
+ interface ExeTarget {
5
+ platform: ExePlatform;
6
+ arch: ExeArch;
7
+ /**
8
+ * Node.js version to use for the executable. Should be a valid Node.js version string (e.g., "25.7.0").
9
+ * The minimum required version is 25.7.0, which is when SEA support was added to Node.js.
10
+ */
11
+ nodeVersion: string;
12
+ }
13
+ interface ExeExtensionOptions {
14
+ /**
15
+ * Cross-platform targets for building executables.
16
+ * Requires `@tsdown/exe` to be installed.
17
+ * When specified, builds an executable for each target platform/arch combination.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * targets: [
22
+ * { platform: 'linux', arch: 'x64', nodeVersion: '25.7.0' },
23
+ * { platform: 'darwin', arch: 'arm64', nodeVersion: '25.7.0' },
24
+ * { platform: 'win', arch: 'x64', nodeVersion: '25.7.0' },
25
+ * ]
26
+ * ```
27
+ */
28
+ targets?: ExeTarget[];
29
+ }
30
+ declare function getTargetSuffix(target: ExeTarget): string;
31
+ //#endregion
32
+ //#region src/cache.d.ts
33
+ declare function getCacheDir(): string;
34
+ declare function getCachedBinaryPath(target: ExeTarget): string;
35
+ //#endregion
36
+ //#region src/download.d.ts
37
+ interface MinimalLogger {
38
+ info: (...args: any[]) => void;
39
+ }
40
+ declare function resolveNodeBinary(target: ExeTarget, logger?: MinimalLogger): Promise<string>;
41
+ //#endregion
42
+ export { type ExeArch, type ExeExtensionOptions, type ExePlatform, type ExeTarget, getCacheDir, getCachedBinaryPath, getTargetSuffix, resolveNodeBinary };
package/dist/index.mjs ADDED
@@ -0,0 +1,134 @@
1
+ import os from "node:os";
2
+ import path from "node:path";
3
+ import process from "node:process";
4
+ import { Buffer } from "node:buffer";
5
+ import { access, chmod, mkdir, rename, rm, writeFile } from "node:fs/promises";
6
+ import { createDebug } from "obug";
7
+ import { x } from "tinyexec";
8
+ import semver from "semver";
9
+ import satisfies from "semver/functions/satisfies.js";
10
+
11
+ //#region src/cache.ts
12
+ function getCacheDir() {
13
+ const home = os.homedir();
14
+ if (process.platform === "win32") {
15
+ const localAppData = process.env.LOCALAPPDATA || path.join(home, "AppData/Local");
16
+ return path.join(localAppData, "tsdown/Caches");
17
+ }
18
+ if (process.platform === "darwin") return path.join(home, "Library/Caches/tsdown");
19
+ const xdgCache = process.env.XDG_CACHE_HOME || path.join(home, ".cache");
20
+ return path.join(xdgCache, "tsdown");
21
+ }
22
+ function getCachedBinaryPath(target) {
23
+ const cacheDir = getCacheDir();
24
+ const binName = target.platform === "win" ? "node.exe" : "node";
25
+ return path.join(cacheDir, "node", `v${target.nodeVersion}`, `${target.platform}-${target.arch}`, binName);
26
+ }
27
+
28
+ //#endregion
29
+ //#region ../../src/utils/fs.ts
30
+ function fsExists(path) {
31
+ return access(path).then(() => true, () => false);
32
+ }
33
+ function fsRemove(path) {
34
+ return rm(path, {
35
+ force: true,
36
+ recursive: true
37
+ }).catch(() => {});
38
+ }
39
+
40
+ //#endregion
41
+ //#region src/platform.ts
42
+ const SEA_VERSION_RANGE = ">=25.7.0";
43
+ function getArchiveExtension(platform) {
44
+ if (platform === "win") return "zip";
45
+ if (platform === "linux") return "tar.xz";
46
+ return "tar.gz";
47
+ }
48
+ function getDownloadUrl(target) {
49
+ const { platform, arch, nodeVersion } = target;
50
+ return `https://nodejs.org/dist/v${nodeVersion}/node-v${nodeVersion}-${platform}-${arch}.${getArchiveExtension(platform)}`;
51
+ }
52
+ function getBinaryPathInArchive(target) {
53
+ const { platform, arch, nodeVersion } = target;
54
+ const dirName = `node-v${nodeVersion}-${platform}-${arch}`;
55
+ if (platform === "win") return `${dirName}/node.exe`;
56
+ return `${dirName}/bin/node`;
57
+ }
58
+ function normalizeNodeVersion(target) {
59
+ const version = semver.valid(target.nodeVersion);
60
+ if (!version) throw new Error(`Invalid Node.js version: ${target.nodeVersion}. Please provide a valid version string (e.g., "25.7.0").`);
61
+ if (!satisfies(version, SEA_VERSION_RANGE)) throw new Error(`Node.js ${version} does not support SEA (Single Executable Applications). Required: ${SEA_VERSION_RANGE}`);
62
+ return version;
63
+ }
64
+ function getTargetSuffix(target) {
65
+ return `-${target.platform}-${target.arch}`;
66
+ }
67
+
68
+ //#endregion
69
+ //#region src/download.ts
70
+ const debug = createDebug("tsdown:exe:download");
71
+ async function resolveNodeBinary(target, logger) {
72
+ debug("Resolving Node.js binary for target: %O", target);
73
+ target.nodeVersion = normalizeNodeVersion(target);
74
+ const cachedPath = getCachedBinaryPath(target);
75
+ debug("Cache path: %s", cachedPath);
76
+ if (await fsExists(cachedPath)) {
77
+ debug("Cache hit: %s", cachedPath);
78
+ logger?.info(`Using cached Node.js ${target.nodeVersion} for ${target.platform}-${target.arch}`);
79
+ return cachedPath;
80
+ }
81
+ const url = getDownloadUrl(target);
82
+ debug("Cache miss, downloading from: %s", url);
83
+ logger?.info(`Downloading Node.js ${target.nodeVersion} for ${target.platform}-${target.arch}...`);
84
+ logger?.info(` ${url}`);
85
+ await mkdir(path.dirname(cachedPath), { recursive: true });
86
+ const response = await fetch(url);
87
+ if (!response.ok) throw new Error(`Failed to download Node.js binary: HTTP ${response.status} from ${url}`);
88
+ const archivePath = `${cachedPath}.download.${getArchiveExtension(target.platform)}`;
89
+ const buffer = Buffer.from(await response.arrayBuffer());
90
+ debug("Downloaded %d bytes, writing to: %s", buffer.length, archivePath);
91
+ await writeFile(archivePath, buffer);
92
+ try {
93
+ await extractBinary(archivePath, cachedPath, target);
94
+ if (target.platform !== "win") await chmod(cachedPath, 493);
95
+ debug("Binary cached at: %s", cachedPath);
96
+ logger?.info(`Cached Node.js binary at: ${cachedPath}`);
97
+ } finally {
98
+ await fsRemove(archivePath);
99
+ }
100
+ return cachedPath;
101
+ }
102
+ async function extractBinary(archivePath, targetBinaryPath, target) {
103
+ const binaryInArchive = getBinaryPathInArchive(target);
104
+ const outDir = path.dirname(targetBinaryPath);
105
+ debug("Extracting %s from archive to %s", binaryInArchive, outDir);
106
+ if (target.platform === "win") await x("tar", [
107
+ "-xf",
108
+ archivePath,
109
+ "-C",
110
+ outDir,
111
+ "--strip-components=1",
112
+ binaryInArchive
113
+ ], {
114
+ nodeOptions: { stdio: "inherit" },
115
+ throwOnError: true
116
+ });
117
+ else await x("tar", [
118
+ `-x${archivePath.endsWith(".tar.xz") ? "J" : "z"}f`,
119
+ archivePath,
120
+ "-C",
121
+ outDir,
122
+ "--strip-components=2",
123
+ binaryInArchive
124
+ ], {
125
+ nodeOptions: { stdio: "inherit" },
126
+ throwOnError: true
127
+ });
128
+ const extractedName = target.platform === "win" ? "node.exe" : "node";
129
+ const extractedPath = path.join(outDir, extractedName);
130
+ if (extractedPath !== targetBinaryPath) await rename(extractedPath, targetBinaryPath);
131
+ }
132
+
133
+ //#endregion
134
+ export { getCacheDir, getCachedBinaryPath, getTargetSuffix, resolveNodeBinary };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tsdown/exe",
3
3
  "type": "module",
4
- "version": "0.21.0-beta.2",
4
+ "version": "0.21.0-beta.4",
5
5
  "description": "Cross-platform executable building for tsdown",
6
6
  "author": "Kevin Deng <sxzz@sxzz.moe>",
7
7
  "license": "MIT",
@@ -16,10 +16,7 @@
16
16
  "url": "https://github.com/rolldown/tsdown/issues"
17
17
  },
18
18
  "exports": {
19
- ".": {
20
- "dev": "./src/index.ts",
21
- "default": "./dist/index.mjs"
22
- },
19
+ ".": "./dist/index.mjs",
23
20
  "./package.json": "./package.json"
24
21
  },
25
22
  "types": "./dist/index.d.mts",
@@ -35,21 +32,17 @@
35
32
  "dist"
36
33
  ],
37
34
  "publishConfig": {
38
- "access": "public",
39
- "exports": {
40
- ".": "./dist/index.mjs",
41
- "./package.json": "./package.json"
42
- }
35
+ "access": "public"
43
36
  },
44
37
  "engines": {
45
38
  "node": ">=20.19.0"
46
39
  },
40
+ "dependencies": {
41
+ "obug": "^2.1.1",
42
+ "semver": "^7.7.4",
43
+ "tinyexec": "^1.0.2"
44
+ },
47
45
  "scripts": {
48
46
  "build": "unrun ../../src/run.ts -c ../../tsdown.config.ts -F ."
49
- },
50
- "dependencies": {
51
- "obug": "catalog:prod",
52
- "semver": "catalog:prod",
53
- "tinyexec": "catalog:prod"
54
47
  }
55
- }
48
+ }