@tarve/core 0.4.0 → 0.5.0
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/CHANGELOG.md +80 -1
- package/README.md +67 -10
- package/dist/npm/build.js +823 -83
- package/dist/npm/cli.js +1087 -116
- package/dist/npm/embedded-assets.js +15 -1
- package/dist/npm/index.js +2468 -181
- package/dist/npm/native-runtime.json +3 -3
- package/dist/npm/node-build.js +865 -0
- package/dist/npm/protocol.js +1 -1
- package/dist/npm/runtime.js +5 -5
- package/dist/types/core/app-metadata.d.ts +49 -0
- package/dist/types/core/build.d.ts +21 -0
- package/dist/types/core/cli.d.ts +1 -1
- package/dist/types/core/esbuild-file-imports.d.ts +8 -0
- package/dist/types/core/node-build.d.ts +7 -0
- package/dist/types/core/node-dev.d.ts +4 -0
- package/dist/types/core/node-target.d.ts +10 -0
- package/dist/types/core/runtime-select.d.ts +8 -0
- package/dist/types/core/src/app-metadata.d.ts +16 -0
- package/dist/types/core/src/app.d.ts +87 -2
- package/dist/types/core/src/assets.d.ts +4 -0
- package/dist/types/core/src/bridge/binding.d.ts +10 -34
- package/dist/types/core/src/canvas.d.ts +161 -0
- package/dist/types/core/src/components/image.d.ts +7 -3
- package/dist/types/core/src/components/layout.d.ts +4 -3
- package/dist/types/core/src/components/pressable.d.ts +2 -2
- package/dist/types/core/src/extra-controls.d.ts +1 -1
- package/dist/types/core/src/html-elements.d.ts +26 -0
- package/dist/types/core/src/index.d.ts +4 -1
- package/dist/types/core/src/inline-text.d.ts +21 -0
- package/dist/types/core/src/jsx-runtime.d.ts +175 -4
- package/dist/types/core/src/media-element.d.ts +71 -0
- package/dist/types/core/src/memo.d.ts +18 -0
- package/dist/types/core/src/png.d.ts +29 -0
- package/dist/types/core/src/popups.d.ts +3 -2
- package/dist/types/core/src/reconciler.d.ts +27 -3
- package/dist/types/core/src/test-renderer.d.ts +2 -0
- package/dist/types/core/src/theme.d.ts +2 -1
- package/dist/types/core/src/virtual-list.d.ts +5 -1
- package/dist/types/core/windows-executable.d.ts +16 -0
- package/dist/types/protocol/src/index.d.ts +187 -5
- package/native/linux-x64/libtarve_native.so +0 -0
- package/native/linux-x64/runtime.json +4 -4
- package/native/win32-x64/runtime.json +4 -4
- package/native/win32-x64/tarve_native.dll +0 -0
- package/package.json +11 -2
package/dist/npm/cli.js
CHANGED
|
@@ -1,36 +1,404 @@
|
|
|
1
|
-
#!/usr/bin/env
|
|
2
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __returnValue = (v) => v;
|
|
4
|
+
function __exportSetter(name, newValue) {
|
|
5
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
6
|
+
}
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, {
|
|
10
|
+
get: all[name],
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
set: __exportSetter.bind(all, name)
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
var __esm = (fn, res, err) => () => {
|
|
17
|
+
if (fn)
|
|
18
|
+
try {
|
|
19
|
+
res = fn(fn = 0);
|
|
20
|
+
} catch (e) {
|
|
21
|
+
err = [e];
|
|
22
|
+
}
|
|
23
|
+
if (err)
|
|
24
|
+
throw err[0];
|
|
25
|
+
return res;
|
|
26
|
+
};
|
|
3
27
|
|
|
4
|
-
// packages/core/
|
|
5
|
-
import {
|
|
28
|
+
// packages/core/src/png.ts
|
|
29
|
+
import { deflateSync, inflateSync } from "node:zlib";
|
|
30
|
+
function isPng(bytes) {
|
|
31
|
+
return SIGNATURE.every((value, index) => bytes[index] === value);
|
|
32
|
+
}
|
|
33
|
+
function decodePng(bytes) {
|
|
34
|
+
if (!isPng(bytes))
|
|
35
|
+
throw new Error("not a PNG file");
|
|
36
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
37
|
+
let offset = 8;
|
|
38
|
+
let width = 0, height = 0, depth = 0, colour = 0, interlace = 0;
|
|
39
|
+
let palette;
|
|
40
|
+
let transparency;
|
|
41
|
+
const idat = [];
|
|
42
|
+
while (offset + 12 <= bytes.length) {
|
|
43
|
+
const length = view.getUint32(offset);
|
|
44
|
+
const type = String.fromCharCode(...bytes.subarray(offset + 4, offset + 8));
|
|
45
|
+
const data = bytes.subarray(offset + 8, offset + 8 + length);
|
|
46
|
+
if (offset + 12 + length > bytes.length)
|
|
47
|
+
throw new Error("PNG chunk runs past the end of the file");
|
|
48
|
+
if (type === "IHDR") {
|
|
49
|
+
width = view.getUint32(offset + 8);
|
|
50
|
+
height = view.getUint32(offset + 12);
|
|
51
|
+
depth = data[8];
|
|
52
|
+
colour = data[9];
|
|
53
|
+
interlace = data[12];
|
|
54
|
+
} else if (type === "PLTE")
|
|
55
|
+
palette = data;
|
|
56
|
+
else if (type === "tRNS")
|
|
57
|
+
transparency = data;
|
|
58
|
+
else if (type === "IDAT")
|
|
59
|
+
idat.push(data);
|
|
60
|
+
else if (type === "IEND")
|
|
61
|
+
break;
|
|
62
|
+
offset += 12 + length;
|
|
63
|
+
}
|
|
64
|
+
if (width <= 0 || height <= 0 || idat.length === 0)
|
|
65
|
+
throw new Error("PNG has no image data");
|
|
66
|
+
if (width * height > 16384 * 16384)
|
|
67
|
+
throw new Error("PNG is too large");
|
|
68
|
+
if (interlace !== 0)
|
|
69
|
+
throw new Error("interlaced PNGs are not supported; save it without interlacing");
|
|
70
|
+
const channels = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 }[colour];
|
|
71
|
+
if (channels === undefined || ![1, 2, 4, 8, 16].includes(depth) || depth < 8 && colour !== 0 && colour !== 3 || depth === 16 && colour === 3) {
|
|
72
|
+
throw new Error(`unsupported PNG format (colour type ${colour}, bit depth ${depth})`);
|
|
73
|
+
}
|
|
74
|
+
const bitsPerPixel = channels * depth;
|
|
75
|
+
const bytesPerPixel = Math.max(1, bitsPerPixel >> 3);
|
|
76
|
+
const stride = Math.ceil(width * bitsPerPixel / 8);
|
|
77
|
+
const raw = inflateSync(Buffer.concat(idat.map((chunk) => Buffer.from(chunk))));
|
|
78
|
+
if (raw.length < height * (stride + 1))
|
|
79
|
+
throw new Error("PNG image data is truncated");
|
|
80
|
+
const lines = new Uint8Array(height * stride);
|
|
81
|
+
for (let y = 0;y < height; y++) {
|
|
82
|
+
const filter = raw[y * (stride + 1)];
|
|
83
|
+
const source = y * (stride + 1) + 1;
|
|
84
|
+
const target = y * stride;
|
|
85
|
+
for (let x = 0;x < stride; x++) {
|
|
86
|
+
const encoded = raw[source + x];
|
|
87
|
+
const left = x >= bytesPerPixel ? lines[target + x - bytesPerPixel] : 0;
|
|
88
|
+
const up = y > 0 ? lines[target - stride + x] : 0;
|
|
89
|
+
const upLeft = y > 0 && x >= bytesPerPixel ? lines[target - stride + x - bytesPerPixel] : 0;
|
|
90
|
+
let predictor = 0;
|
|
91
|
+
if (filter === 1)
|
|
92
|
+
predictor = left;
|
|
93
|
+
else if (filter === 2)
|
|
94
|
+
predictor = up;
|
|
95
|
+
else if (filter === 3)
|
|
96
|
+
predictor = left + up >> 1;
|
|
97
|
+
else if (filter === 4) {
|
|
98
|
+
const p = left + up - upLeft;
|
|
99
|
+
const pa = Math.abs(p - left), pb = Math.abs(p - up), pc = Math.abs(p - upLeft);
|
|
100
|
+
predictor = pa <= pb && pa <= pc ? left : pb <= pc ? up : upLeft;
|
|
101
|
+
} else if (filter !== 0)
|
|
102
|
+
throw new Error(`unsupported PNG filter ${filter}`);
|
|
103
|
+
lines[target + x] = encoded + predictor & 255;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const sample = (y, index) => {
|
|
107
|
+
const row = y * stride;
|
|
108
|
+
if (depth === 8)
|
|
109
|
+
return lines[row + index];
|
|
110
|
+
if (depth === 16)
|
|
111
|
+
return lines[row + index * 2];
|
|
112
|
+
const bit = index * depth;
|
|
113
|
+
return lines[row + (bit >> 3)] >> 8 - depth - (bit & 7) & (1 << depth) - 1;
|
|
114
|
+
};
|
|
115
|
+
const scale = depth < 8 && colour === 0 ? 255 / ((1 << depth) - 1) : 1;
|
|
116
|
+
const rgba = new Uint8Array(width * height * 4);
|
|
117
|
+
for (let y = 0;y < height; y++) {
|
|
118
|
+
for (let x = 0;x < width; x++) {
|
|
119
|
+
const out = (y * width + x) * 4;
|
|
120
|
+
const base = x * channels;
|
|
121
|
+
if (colour === 3) {
|
|
122
|
+
const index = sample(y, base);
|
|
123
|
+
rgba[out] = palette?.[index * 3] ?? 0;
|
|
124
|
+
rgba[out + 1] = palette?.[index * 3 + 1] ?? 0;
|
|
125
|
+
rgba[out + 2] = palette?.[index * 3 + 2] ?? 0;
|
|
126
|
+
rgba[out + 3] = transparency?.[index] ?? 255;
|
|
127
|
+
} else if (colour === 0 || colour === 4) {
|
|
128
|
+
const grey = Math.round(sample(y, base) * scale);
|
|
129
|
+
rgba[out] = rgba[out + 1] = rgba[out + 2] = grey;
|
|
130
|
+
rgba[out + 3] = colour === 4 ? sample(y, base + 1) : 255;
|
|
131
|
+
} else {
|
|
132
|
+
rgba[out] = sample(y, base);
|
|
133
|
+
rgba[out + 1] = sample(y, base + 1);
|
|
134
|
+
rgba[out + 2] = sample(y, base + 2);
|
|
135
|
+
rgba[out + 3] = colour === 6 ? sample(y, base + 3) : 255;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return { width, height, rgba };
|
|
140
|
+
}
|
|
141
|
+
function crc32(bytes) {
|
|
142
|
+
let crc = 4294967295;
|
|
143
|
+
for (const byte of bytes)
|
|
144
|
+
crc = CRC_TABLE[(crc ^ byte) & 255] ^ crc >>> 8;
|
|
145
|
+
return (crc ^ 4294967295) >>> 0;
|
|
146
|
+
}
|
|
147
|
+
function chunk(type, data) {
|
|
148
|
+
const out = Buffer.alloc(12 + data.length);
|
|
149
|
+
out.writeUInt32BE(data.length, 0);
|
|
150
|
+
out.write(type, 4, "ascii");
|
|
151
|
+
out.set(data, 8);
|
|
152
|
+
out.writeUInt32BE(crc32(out.subarray(4, 8 + data.length)), 8 + data.length);
|
|
153
|
+
return out;
|
|
154
|
+
}
|
|
155
|
+
function encodePng(rgba, width, height) {
|
|
156
|
+
const stride = width * 4;
|
|
157
|
+
const raw = Buffer.alloc((stride + 1) * height);
|
|
158
|
+
for (let y = 0;y < height; y++)
|
|
159
|
+
raw.set(rgba.subarray(y * stride, (y + 1) * stride), y * (stride + 1) + 1);
|
|
160
|
+
const header = Buffer.alloc(13);
|
|
161
|
+
header.writeUInt32BE(width, 0);
|
|
162
|
+
header.writeUInt32BE(height, 4);
|
|
163
|
+
header[8] = 8;
|
|
164
|
+
header[9] = 6;
|
|
165
|
+
return new Uint8Array(Buffer.concat([
|
|
166
|
+
Buffer.from(SIGNATURE),
|
|
167
|
+
chunk("IHDR", header),
|
|
168
|
+
chunk("IDAT", deflateSync(raw, { level: 9 })),
|
|
169
|
+
chunk("IEND", new Uint8Array)
|
|
170
|
+
]));
|
|
171
|
+
}
|
|
172
|
+
function resizeSquare(image, size) {
|
|
173
|
+
const side = Math.max(image.width, image.height);
|
|
174
|
+
const offsetX = (side - image.width) / 2, offsetY = (side - image.height) / 2;
|
|
175
|
+
const out = new Uint8Array(size * size * 4);
|
|
176
|
+
const ratio = side / size;
|
|
177
|
+
for (let y = 0;y < size; y++) {
|
|
178
|
+
const y0 = y * ratio - offsetY, y1 = (y + 1) * ratio - offsetY;
|
|
179
|
+
for (let x = 0;x < size; x++) {
|
|
180
|
+
const x0 = x * ratio - offsetX, x1 = (x + 1) * ratio - offsetX;
|
|
181
|
+
let r = 0, g = 0, b = 0, a = 0;
|
|
182
|
+
for (let sy = Math.max(0, Math.floor(y0));sy < Math.min(image.height, Math.ceil(y1)); sy++) {
|
|
183
|
+
const wy = Math.min(y1, sy + 1) - Math.max(y0, sy);
|
|
184
|
+
for (let sx = Math.max(0, Math.floor(x0));sx < Math.min(image.width, Math.ceil(x1)); sx++) {
|
|
185
|
+
const w = wy * (Math.min(x1, sx + 1) - Math.max(x0, sx));
|
|
186
|
+
const i = (sy * image.width + sx) * 4;
|
|
187
|
+
const alpha = image.rgba[i + 3] * w;
|
|
188
|
+
r += image.rgba[i] * alpha;
|
|
189
|
+
g += image.rgba[i + 1] * alpha;
|
|
190
|
+
b += image.rgba[i + 2] * alpha;
|
|
191
|
+
a += alpha;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
const area = ratio * ratio;
|
|
195
|
+
const o = (y * size + x) * 4;
|
|
196
|
+
if (a > 0) {
|
|
197
|
+
out[o] = Math.round(r / a);
|
|
198
|
+
out[o + 1] = Math.round(g / a);
|
|
199
|
+
out[o + 2] = Math.round(b / a);
|
|
200
|
+
}
|
|
201
|
+
out[o + 3] = Math.round(a / area);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return out;
|
|
205
|
+
}
|
|
206
|
+
function encodeIco(entries) {
|
|
207
|
+
const header = Buffer.alloc(6 + entries.length * 16);
|
|
208
|
+
header.writeUInt16LE(0, 0);
|
|
209
|
+
header.writeUInt16LE(1, 2);
|
|
210
|
+
header.writeUInt16LE(entries.length, 4);
|
|
211
|
+
let offset = header.length;
|
|
212
|
+
entries.forEach(({ size, png }, index) => {
|
|
213
|
+
const at = 6 + index * 16;
|
|
214
|
+
header[at] = size >= 256 ? 0 : size;
|
|
215
|
+
header[at + 1] = size >= 256 ? 0 : size;
|
|
216
|
+
header.writeUInt16LE(1, at + 4);
|
|
217
|
+
header.writeUInt16LE(32, at + 6);
|
|
218
|
+
header.writeUInt32LE(png.length, at + 8);
|
|
219
|
+
header.writeUInt32LE(offset, at + 12);
|
|
220
|
+
offset += png.length;
|
|
221
|
+
});
|
|
222
|
+
return new Uint8Array(Buffer.concat([header, ...entries.map((entry) => Buffer.from(entry.png))]));
|
|
223
|
+
}
|
|
224
|
+
function isIco(bytes) {
|
|
225
|
+
return bytes.length >= 6 && bytes[0] === 0 && bytes[1] === 0 && bytes[2] === 1 && bytes[3] === 0;
|
|
226
|
+
}
|
|
227
|
+
function largestIcoPng(bytes) {
|
|
228
|
+
if (!isIco(bytes))
|
|
229
|
+
return;
|
|
230
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
231
|
+
let best;
|
|
232
|
+
let bestSize = 0;
|
|
233
|
+
for (let index = 0;index < view.getUint16(4, true); index++) {
|
|
234
|
+
const at = 6 + index * 16;
|
|
235
|
+
if (at + 16 > bytes.length)
|
|
236
|
+
break;
|
|
237
|
+
const size = bytes[at] || 256;
|
|
238
|
+
const length = view.getUint32(at + 8, true), start = view.getUint32(at + 12, true);
|
|
239
|
+
const data = bytes.subarray(start, start + length);
|
|
240
|
+
if (isPng(data) && size > bestSize) {
|
|
241
|
+
best = data;
|
|
242
|
+
bestSize = size;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return best;
|
|
246
|
+
}
|
|
247
|
+
var SIGNATURE, CRC_TABLE;
|
|
248
|
+
var init_png = __esm(() => {
|
|
249
|
+
SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10];
|
|
250
|
+
CRC_TABLE = new Uint32Array(256).map((_, n) => {
|
|
251
|
+
let c = n;
|
|
252
|
+
for (let k = 0;k < 8; k++)
|
|
253
|
+
c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
254
|
+
return c >>> 0;
|
|
255
|
+
});
|
|
256
|
+
});
|
|
6
257
|
|
|
7
|
-
// packages/core/
|
|
8
|
-
import { existsSync
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
258
|
+
// packages/core/app-metadata.ts
|
|
259
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
260
|
+
import { basename, dirname, extname, join, relative, resolve } from "node:path";
|
|
261
|
+
function fields(value, where) {
|
|
262
|
+
if (value === undefined)
|
|
263
|
+
return {};
|
|
264
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
265
|
+
throw new TypeError(`${where} must be an object`);
|
|
266
|
+
const out = {};
|
|
267
|
+
for (const key of FIELDS) {
|
|
268
|
+
const field = value[key];
|
|
269
|
+
if (field === undefined)
|
|
270
|
+
continue;
|
|
271
|
+
if (typeof field !== "string" || !field.trim())
|
|
272
|
+
throw new TypeError(`${where}.${key} must be a non-empty string`);
|
|
273
|
+
out[key] = field.trim();
|
|
274
|
+
}
|
|
275
|
+
return out;
|
|
276
|
+
}
|
|
277
|
+
function author(value) {
|
|
278
|
+
if (typeof value === "string")
|
|
279
|
+
return value.replace(/\s*[<(].*$/, "").trim() || undefined;
|
|
280
|
+
if (value && typeof value === "object" && typeof value.name === "string")
|
|
281
|
+
return value.name;
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
function validAppId(id) {
|
|
285
|
+
return id.length > 0 && id.length <= 128 && !/[\s\\]/.test(id);
|
|
286
|
+
}
|
|
287
|
+
function resolveAppMetadata(entrypoint, overrides = {}) {
|
|
288
|
+
const entry = resolve(entrypoint);
|
|
289
|
+
let directory = dirname(entry);
|
|
290
|
+
let manifest;
|
|
291
|
+
let root = directory;
|
|
292
|
+
for (;; ) {
|
|
293
|
+
const path = join(directory, "package.json");
|
|
294
|
+
if (existsSync(path)) {
|
|
295
|
+
manifest = JSON.parse(readFileSync(path, "utf8"));
|
|
296
|
+
root = directory;
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
const parent = dirname(directory);
|
|
300
|
+
if (parent === directory)
|
|
301
|
+
break;
|
|
302
|
+
directory = parent;
|
|
303
|
+
}
|
|
304
|
+
const tarve = manifest?.tarve;
|
|
305
|
+
const shared = fields(tarve === undefined ? undefined : Object.fromEntries(Object.entries(tarve).filter(([key]) => key !== "apps")), "package.json tarve");
|
|
306
|
+
const apps = tarve?.apps;
|
|
307
|
+
const key = relative(root, entry).replaceAll("\\", "/");
|
|
308
|
+
const own = fields(apps?.[key] ?? apps?.["./" + key], `package.json tarve.apps["${key}"]`);
|
|
309
|
+
const fromPackage = tarve === undefined ? {} : {
|
|
310
|
+
...typeof manifest?.version === "string" ? { version: manifest.version } : {},
|
|
311
|
+
...typeof manifest?.description === "string" ? { description: manifest.description } : {},
|
|
312
|
+
...author(manifest?.author) ? { publisher: author(manifest?.author) } : {}
|
|
313
|
+
};
|
|
314
|
+
const configured = { ...fromPackage, ...shared, ...own };
|
|
315
|
+
if (configured.icon)
|
|
316
|
+
configured.icon = resolve(root, configured.icon);
|
|
317
|
+
const flags = fields(Object.fromEntries(Object.entries(overrides).filter(([, value]) => value !== undefined)), "option");
|
|
318
|
+
if (flags.icon)
|
|
319
|
+
flags.icon = resolve(flags.icon);
|
|
320
|
+
const merged = { ...configured, ...flags };
|
|
321
|
+
const metadata = { name: merged.name ?? basename(entry).replace(/\.[^.]+$/, "") };
|
|
322
|
+
for (const field of FIELDS)
|
|
323
|
+
if (field !== "name" && merged[field] !== undefined)
|
|
324
|
+
metadata[field] = merged[field];
|
|
325
|
+
if (metadata.appId !== undefined && !validAppId(metadata.appId)) {
|
|
326
|
+
throw new TypeError(`appId must be 1-128 characters without spaces or backslashes: ${metadata.appId}`);
|
|
327
|
+
}
|
|
328
|
+
if (metadata.icon !== undefined) {
|
|
329
|
+
if (!existsSync(metadata.icon))
|
|
330
|
+
throw new Error(`App icon not found: ${metadata.icon}`);
|
|
331
|
+
if (![".png", ".ico"].includes(extname(metadata.icon).toLowerCase()))
|
|
332
|
+
throw new TypeError(`App icon must be a .png or .ico file: ${metadata.icon}`);
|
|
333
|
+
}
|
|
334
|
+
return metadata;
|
|
335
|
+
}
|
|
336
|
+
function appIconImages(path) {
|
|
337
|
+
const bytes = new Uint8Array(readFileSync(path));
|
|
338
|
+
if (isIco(bytes)) {
|
|
339
|
+
const png = largestIcoPng(bytes);
|
|
340
|
+
if (!png)
|
|
341
|
+
throw new Error(`${path} has no PNG-compressed image; Linux builds and the window icon need one. Use a .png icon instead.`);
|
|
342
|
+
return { ico: bytes, png };
|
|
343
|
+
}
|
|
344
|
+
if (!isPng(bytes))
|
|
345
|
+
throw new TypeError(`${path} is not a PNG or ICO file`);
|
|
346
|
+
let image;
|
|
347
|
+
try {
|
|
348
|
+
image = decodePng(bytes);
|
|
349
|
+
} catch (error) {
|
|
350
|
+
throw new Error(`App icon ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
351
|
+
}
|
|
352
|
+
const side = Math.max(image.width, image.height);
|
|
353
|
+
const sizes = ICO_SIZES.filter((size) => size <= side);
|
|
354
|
+
if (!sizes.length)
|
|
355
|
+
sizes.push(16);
|
|
356
|
+
const entries = sizes.map((size) => ({ size, png: encodePng(resizeSquare(image, size), size, size) }));
|
|
357
|
+
return { ico: encodeIco(entries), png: entries.at(-1).png };
|
|
358
|
+
}
|
|
359
|
+
function buildMetadata(entrypoint, overrides) {
|
|
360
|
+
const metadata = resolveAppMetadata(entrypoint, overrides);
|
|
361
|
+
const images = metadata.icon ? appIconImages(metadata.icon) : undefined;
|
|
362
|
+
const icon = images ? `data:image/png;base64,${Buffer.from(images.png).toString("base64")}` : undefined;
|
|
363
|
+
return { metadata, images, define: { "process.env.TARVE_APP_METADATA": JSON.stringify(runtimeMetadata(metadata, icon)) } };
|
|
364
|
+
}
|
|
365
|
+
function runtimeMetadata(metadata, icon) {
|
|
366
|
+
return JSON.stringify({
|
|
367
|
+
name: metadata.name,
|
|
368
|
+
...metadata.appId ? { appId: metadata.appId } : {},
|
|
369
|
+
...icon ? { icon } : {}
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
function writeLinuxDesktopEntry(outfile, metadata, png) {
|
|
373
|
+
const id = metadata.appId ?? basename(outfile);
|
|
374
|
+
const escape = (value) => value.replace(/[\\\n\r\t]/g, (match) => ({ "\\": "\\\\", "\n": "\\n", "\r": "\\r", "\t": "\\t" })[match]);
|
|
375
|
+
const lines = [
|
|
376
|
+
"[Desktop Entry]",
|
|
377
|
+
"Type=Application",
|
|
378
|
+
`Name=${escape(metadata.name)}`,
|
|
379
|
+
...metadata.description ? [`Comment=${escape(metadata.description)}`] : [],
|
|
380
|
+
`Exec="${outfile.replaceAll('"', "\\\"")}" %U`,
|
|
381
|
+
...png ? [`Icon=${id}`] : [],
|
|
382
|
+
`StartupWMClass=${id}`,
|
|
383
|
+
"Terminal=false",
|
|
384
|
+
""
|
|
385
|
+
];
|
|
386
|
+
mkdirSync(dirname(outfile), { recursive: true });
|
|
387
|
+
const desktop = join(dirname(outfile), `${id}.desktop`);
|
|
388
|
+
writeFileSync(desktop, lines.join(`
|
|
389
|
+
`));
|
|
390
|
+
if (png)
|
|
391
|
+
writeFileSync(join(dirname(outfile), `${id}.png`), png);
|
|
392
|
+
return desktop;
|
|
393
|
+
}
|
|
394
|
+
var FIELDS, ICO_SIZES;
|
|
395
|
+
var init_app_metadata = __esm(() => {
|
|
396
|
+
init_png();
|
|
397
|
+
FIELDS = ["name", "version", "description", "publisher", "copyright", "icon", "appId"];
|
|
398
|
+
ICO_SIZES = [16, 20, 24, 32, 40, 48, 64, 96, 128, 256];
|
|
399
|
+
});
|
|
13
400
|
|
|
14
401
|
// packages/core/targets.ts
|
|
15
|
-
var BUILD_TARGETS = ["windows-x64", "linux-x64"];
|
|
16
|
-
var TARGET_CONFIGS = {
|
|
17
|
-
"windows-x64": {
|
|
18
|
-
platform: "win32",
|
|
19
|
-
arch: "x64",
|
|
20
|
-
bunTarget: "bun-windows-x64",
|
|
21
|
-
nativeDirectory: "win32-x64",
|
|
22
|
-
nativeName: "tarve_native.dll",
|
|
23
|
-
executableSuffix: ".exe"
|
|
24
|
-
},
|
|
25
|
-
"linux-x64": {
|
|
26
|
-
platform: "linux",
|
|
27
|
-
arch: "x64",
|
|
28
|
-
bunTarget: "bun-linux-x64",
|
|
29
|
-
nativeDirectory: "linux-x64",
|
|
30
|
-
nativeName: "libtarve_native.so",
|
|
31
|
-
executableSuffix: ""
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
402
|
function isBuildTarget(value) {
|
|
35
403
|
return BUILD_TARGETS.includes(value);
|
|
36
404
|
}
|
|
@@ -43,34 +411,50 @@ function hostBuildTarget(platform = process.platform, arch = process.arch) {
|
|
|
43
411
|
return config.platform === platform && config.arch === arch;
|
|
44
412
|
});
|
|
45
413
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
414
|
+
var BUILD_TARGETS, TARGET_CONFIGS;
|
|
415
|
+
var init_targets = __esm(() => {
|
|
416
|
+
BUILD_TARGETS = ["windows-x64", "linux-x64"];
|
|
417
|
+
TARGET_CONFIGS = {
|
|
418
|
+
"windows-x64": {
|
|
419
|
+
platform: "win32",
|
|
420
|
+
arch: "x64",
|
|
421
|
+
bunTarget: "bun-windows-x64",
|
|
422
|
+
nativeDirectory: "win32-x64",
|
|
423
|
+
nativeName: "tarve_native.dll",
|
|
424
|
+
executableSuffix: ".exe"
|
|
425
|
+
},
|
|
426
|
+
"linux-x64": {
|
|
427
|
+
platform: "linux",
|
|
428
|
+
arch: "x64",
|
|
429
|
+
bunTarget: "bun-linux-x64",
|
|
430
|
+
nativeDirectory: "linux-x64",
|
|
431
|
+
nativeName: "libtarve_native.so",
|
|
432
|
+
executableSuffix: ""
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
});
|
|
52
436
|
|
|
53
437
|
// packages/protocol/src/index.ts
|
|
54
|
-
var NATIVE_ABI_VERSION = 5;
|
|
55
|
-
var PROTOCOL_VERSION = 48;
|
|
438
|
+
var NATIVE_ABI_VERSION = 5, PROTOCOL_VERSION = 58;
|
|
56
439
|
|
|
57
440
|
// packages/core/native-runtime.ts
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
441
|
+
import { createHash } from "node:crypto";
|
|
442
|
+
import { existsSync as existsSync2 } from "node:fs";
|
|
443
|
+
import { readFile, writeFile, glob } from "node:fs/promises";
|
|
444
|
+
import { basename as basename2, dirname as dirname2, join as join2 } from "node:path";
|
|
61
445
|
async function sha256File(path) {
|
|
62
446
|
return createHash("sha256").update(await readFile(path)).digest("hex");
|
|
63
447
|
}
|
|
64
448
|
async function addGlobFiles(files, root, prefix, pattern) {
|
|
65
|
-
const directory =
|
|
66
|
-
if (!
|
|
449
|
+
const directory = join2(root, "native", prefix);
|
|
450
|
+
if (!existsSync2(directory))
|
|
67
451
|
return;
|
|
68
|
-
for await (const relative of
|
|
69
|
-
files.push(
|
|
452
|
+
for await (const relative of glob(pattern, { cwd: directory })) {
|
|
453
|
+
files.push(join2(prefix, relative).replaceAll("\\", "/"));
|
|
70
454
|
}
|
|
71
455
|
}
|
|
72
456
|
async function nativeSourceFingerprint(root) {
|
|
73
|
-
const native =
|
|
457
|
+
const native = join2(root, "native");
|
|
74
458
|
const files = ["Cargo.toml", "Cargo.lock"];
|
|
75
459
|
await addGlobFiles(files, root, "src", "**/*.rs");
|
|
76
460
|
await addGlobFiles(files, root, "vendor", "**/*.rs");
|
|
@@ -78,8 +462,8 @@ async function nativeSourceFingerprint(root) {
|
|
|
78
462
|
files.sort();
|
|
79
463
|
const hash = createHash("sha256");
|
|
80
464
|
for (const relative of files) {
|
|
81
|
-
const path =
|
|
82
|
-
if (!
|
|
465
|
+
const path = join2(native, ...relative.split("/"));
|
|
466
|
+
if (!existsSync2(path))
|
|
83
467
|
throw new Error(`Native source input is missing: ${path}`);
|
|
84
468
|
hash.update(relative);
|
|
85
469
|
hash.update("\x00");
|
|
@@ -91,8 +475,8 @@ async function nativeSourceFingerprint(root) {
|
|
|
91
475
|
}
|
|
92
476
|
async function createNativeCompatibilityManifest(root, packageVersion) {
|
|
93
477
|
const [rustProtocol, rustBridge] = await Promise.all([
|
|
94
|
-
readFile(
|
|
95
|
-
readFile(
|
|
478
|
+
readFile(join2(root, "native", "src", "protocol.rs"), "utf8"),
|
|
479
|
+
readFile(join2(root, "native", "src", "bridge.rs"), "utf8")
|
|
96
480
|
]);
|
|
97
481
|
const rustProtocolVersion = Number(rustProtocol.match(/pub const VERSION: u32 = (\d+);/)?.[1]);
|
|
98
482
|
const rustAbiVersion = Number(rustBridge.match(/pub const ABI_VERSION: u32 = (\d+);/)?.[1]);
|
|
@@ -119,7 +503,7 @@ function validateCompatibilityShape(value, path) {
|
|
|
119
503
|
}
|
|
120
504
|
}
|
|
121
505
|
async function readNativeCompatibilityManifest(path) {
|
|
122
|
-
if (!
|
|
506
|
+
if (!existsSync2(path))
|
|
123
507
|
throw new Error(`Tarve native compatibility metadata is missing: ${path}`);
|
|
124
508
|
let value;
|
|
125
509
|
try {
|
|
@@ -131,8 +515,8 @@ async function readNativeCompatibilityManifest(path) {
|
|
|
131
515
|
return value;
|
|
132
516
|
}
|
|
133
517
|
async function assertNativeRuntimeCompatible(library, target, expected) {
|
|
134
|
-
const path =
|
|
135
|
-
if (!
|
|
518
|
+
const path = join2(dirname2(library), NATIVE_RUNTIME_MANIFEST_NAME);
|
|
519
|
+
if (!existsSync2(path)) {
|
|
136
520
|
throw new Error(`Tarve native runtime metadata missing for ${target}: ${path}. ` + "Rebuild/stage this target with `bun run package:native` on its native OS before cross-compiling.");
|
|
137
521
|
}
|
|
138
522
|
let value;
|
|
@@ -157,12 +541,327 @@ async function assertNativeRuntimeCompatible(library, target, expected) {
|
|
|
157
541
|
}
|
|
158
542
|
return manifest;
|
|
159
543
|
}
|
|
544
|
+
var NATIVE_RUNTIME_MANIFEST_NAME = "runtime.json", NATIVE_COMPATIBILITY_MANIFEST_NAME = "native-runtime.json", NATIVE_RUNTIME_MANIFEST_SCHEMA = 1;
|
|
545
|
+
var init_native_runtime = __esm(() => {
|
|
546
|
+
init_targets();
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
// packages/core/esbuild-file-imports.ts
|
|
550
|
+
import { readFileSync as readFileSync2 } from "node:fs";
|
|
551
|
+
import { dirname as dirname3, resolve as resolve2 } from "node:path";
|
|
552
|
+
var ASSET, fileImportAttributes;
|
|
553
|
+
var init_esbuild_file_imports = __esm(() => {
|
|
554
|
+
ASSET = /\.(?:png|jpe?g|webp|gif|svg)$/i;
|
|
555
|
+
fileImportAttributes = {
|
|
556
|
+
name: "tarve-file-import-attributes",
|
|
557
|
+
setup(build) {
|
|
558
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
|
559
|
+
if (args.namespace === "tarve-file") {
|
|
560
|
+
return args.path.startsWith("node:") ? { path: args.path, external: true } : { path: args.path, namespace: "tarve-file-copy" };
|
|
561
|
+
}
|
|
562
|
+
const type = args.with?.type ?? (ASSET.test(args.path) && args.kind === "import-statement" ? "file" : undefined);
|
|
563
|
+
if (type !== "file" && type !== "text")
|
|
564
|
+
return;
|
|
565
|
+
return { path: resolve2(dirname3(args.importer), args.path), namespace: `tarve-${type}` };
|
|
566
|
+
});
|
|
567
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-text" }, (args) => ({ contents: readFileSync2(args.path, "utf8"), loader: "text" }));
|
|
568
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-file" }, (args) => ({
|
|
569
|
+
contents: [
|
|
570
|
+
`import relative from ${JSON.stringify(args.path)};`,
|
|
571
|
+
`import { join } from "node:path";`,
|
|
572
|
+
`export default join(import.meta.dirname, relative);`
|
|
573
|
+
].join(`
|
|
574
|
+
`),
|
|
575
|
+
loader: "js",
|
|
576
|
+
resolveDir: dirname3(args.path)
|
|
577
|
+
}));
|
|
578
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-file-copy" }, (args) => ({
|
|
579
|
+
contents: readFileSync2(args.path),
|
|
580
|
+
loader: "file"
|
|
581
|
+
}));
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
// packages/core/node-target.ts
|
|
587
|
+
import { createHash as createHash2, randomUUID } from "node:crypto";
|
|
588
|
+
import { chmodSync, existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync3, renameSync, rmSync, writeFileSync as writeFileSync2 } from "node:fs";
|
|
589
|
+
import { homedir } from "node:os";
|
|
590
|
+
import { join as join3 } from "node:path";
|
|
591
|
+
import { gunzipSync } from "node:zlib";
|
|
592
|
+
function distribution(target, version) {
|
|
593
|
+
return target === "windows-x64" ? { file: "win-x64/node.exe", executable: "node.exe" } : { file: `node-v${version}-linux-x64.tar.gz`, member: `node-v${version}-linux-x64/bin/node`, executable: "node" };
|
|
594
|
+
}
|
|
595
|
+
function cacheRoot() {
|
|
596
|
+
if (process.env.TARVE_CACHE_DIR)
|
|
597
|
+
return process.env.TARVE_CACHE_DIR;
|
|
598
|
+
if (process.platform === "win32")
|
|
599
|
+
return join3(process.env.LOCALAPPDATA ?? join3(homedir(), "AppData", "Local"), "tarve", "cache");
|
|
600
|
+
return join3(process.env.XDG_CACHE_HOME ?? join3(homedir(), ".cache"), "tarve");
|
|
601
|
+
}
|
|
602
|
+
function mirror() {
|
|
603
|
+
return (process.env.NODEJS_ORG_MIRROR ?? "https://nodejs.org/dist").replace(/\/+$/, "");
|
|
604
|
+
}
|
|
605
|
+
async function download(url) {
|
|
606
|
+
const response = await fetch(url);
|
|
607
|
+
if (!response.ok)
|
|
608
|
+
throw new Error(`Could not download ${url}: HTTP ${response.status}`);
|
|
609
|
+
return Buffer.from(await response.arrayBuffer());
|
|
610
|
+
}
|
|
611
|
+
function extractTarMember(archive, member) {
|
|
612
|
+
let offset = 0;
|
|
613
|
+
let longName;
|
|
614
|
+
while (offset + 512 <= archive.length) {
|
|
615
|
+
const header = archive.subarray(offset, offset + 512);
|
|
616
|
+
if (header.every((byte) => byte === 0))
|
|
617
|
+
break;
|
|
618
|
+
const field = (start, length) => header.toString("utf8", start, start + length).replace(/\0.*$/s, "");
|
|
619
|
+
const size = Number.parseInt(field(124, 12).trim() || "0", 8);
|
|
620
|
+
const type = field(156, 1) || "0";
|
|
621
|
+
const prefix = field(345, 155);
|
|
622
|
+
const name = longName ?? (prefix ? `${prefix}/${field(0, 100)}` : field(0, 100));
|
|
623
|
+
const body = archive.subarray(offset + 512, offset + 512 + size);
|
|
624
|
+
longName = undefined;
|
|
625
|
+
if (type === "L")
|
|
626
|
+
longName = body.toString("utf8").replace(/\0.*$/s, "");
|
|
627
|
+
else if (type === "x")
|
|
628
|
+
longName = /\d+ path=([^\n]*)\n/.exec(body.toString("utf8"))?.[1];
|
|
629
|
+
else if ((type === "0" || type === "\x00") && name.replace(/^\.\//, "") === member)
|
|
630
|
+
return Buffer.from(body);
|
|
631
|
+
offset += 512 + Math.ceil(size / 512) * 512;
|
|
632
|
+
}
|
|
633
|
+
throw new Error(`${member} not found in the Node.js archive`);
|
|
634
|
+
}
|
|
635
|
+
function assertNodeVersion(executable, version) {
|
|
636
|
+
if (!readFileSync3(executable).includes(`node-v${version}`)) {
|
|
637
|
+
throw new Error(`${executable} is not Node.js v${version}; the target executable must match the Node.js that builds the app`);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
async function targetNodeExecutable(target, version) {
|
|
641
|
+
const { file, member, executable } = distribution(target, version);
|
|
642
|
+
const directory = join3(cacheRoot(), "node", `v${version}`, target);
|
|
643
|
+
const path = join3(directory, executable);
|
|
644
|
+
if (existsSync3(path))
|
|
645
|
+
return path;
|
|
646
|
+
const base = `${mirror()}/v${version}`;
|
|
647
|
+
const sums = (await download(`${base}/SHASUMS256.txt`)).toString("utf8");
|
|
648
|
+
const expected = sums.split(/\r?\n/).find((line) => line.endsWith(` ${file}`))?.split(/\s+/)[0];
|
|
649
|
+
if (!expected)
|
|
650
|
+
throw new Error(`${file} is not listed in the Node.js v${version} SHASUMS256.txt`);
|
|
651
|
+
const payload = await download(`${base}/${file}`);
|
|
652
|
+
const actual = createHash2("sha256").update(payload).digest("hex");
|
|
653
|
+
if (actual !== expected)
|
|
654
|
+
throw new Error(`Checksum mismatch for ${file}: expected ${expected}, got ${actual}`);
|
|
655
|
+
const binary = member ? extractTarMember(gunzipSync(payload), member) : payload;
|
|
656
|
+
mkdirSync2(directory, { recursive: true });
|
|
657
|
+
const temporary = join3(directory, `${executable}.${randomUUID()}.tmp`);
|
|
658
|
+
try {
|
|
659
|
+
writeFileSync2(temporary, binary, { mode: 493 });
|
|
660
|
+
try {
|
|
661
|
+
renameSync(temporary, path);
|
|
662
|
+
} catch (error) {
|
|
663
|
+
if (!existsSync3(path))
|
|
664
|
+
throw error;
|
|
665
|
+
}
|
|
666
|
+
} finally {
|
|
667
|
+
rmSync(temporary, { force: true });
|
|
668
|
+
}
|
|
669
|
+
if (process.platform !== "win32")
|
|
670
|
+
chmodSync(path, 493);
|
|
671
|
+
return path;
|
|
672
|
+
}
|
|
673
|
+
var init_node_target = () => {};
|
|
674
|
+
|
|
675
|
+
// packages/core/windows-executable.ts
|
|
676
|
+
var exports_windows_executable = {};
|
|
677
|
+
__export(exports_windows_executable, {
|
|
678
|
+
brandWindowsExecutable: () => brandWindowsExecutable
|
|
679
|
+
});
|
|
680
|
+
import { readFileSync as readFileSync4, writeFileSync as writeFileSync3 } from "node:fs";
|
|
681
|
+
import { Data, NtExecutable, NtExecutableResource, Resource } from "resedit";
|
|
682
|
+
function numericVersion(version) {
|
|
683
|
+
const parts = version.split(/[.+-]/).slice(0, 4).map((part) => Number.parseInt(part, 10));
|
|
684
|
+
return [0, 1, 2, 3].map((index) => {
|
|
685
|
+
const value = parts[index];
|
|
686
|
+
return Number.isInteger(value) && value >= 0 && value <= 65535 ? value : 0;
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
function brandWindowsExecutable(path, info) {
|
|
690
|
+
const executable = NtExecutable.from(readFileSync4(path), { ignoreCert: true });
|
|
691
|
+
const resources = NtExecutableResource.from(executable);
|
|
692
|
+
const versionInfo = Resource.VersionInfo.fromEntries(resources.entries)[0] ?? Resource.VersionInfo.createEmpty();
|
|
693
|
+
const languages = versionInfo.getAllLanguagesForStringValues();
|
|
694
|
+
if (languages.length === 0)
|
|
695
|
+
languages.push({ lang: 1033, codepage: 1200 });
|
|
696
|
+
const [major, minor, patch, revision] = numericVersion(info.version);
|
|
697
|
+
const text = `${major}.${minor}.${patch}.${revision}`;
|
|
698
|
+
for (const language of languages) {
|
|
699
|
+
for (const key of ["CompanyName", "LegalCopyright", "LegalTrademarks", "Comments", "PrivateBuild", "SpecialBuild"]) {
|
|
700
|
+
versionInfo.removeStringValue(language, key);
|
|
701
|
+
}
|
|
702
|
+
versionInfo.setStringValues(language, {
|
|
703
|
+
FileDescription: info.description ?? `${info.name} — native Tarve application`,
|
|
704
|
+
...info.publisher ? { CompanyName: info.publisher } : {},
|
|
705
|
+
...info.copyright ? { LegalCopyright: info.copyright } : {},
|
|
706
|
+
ProductName: info.name,
|
|
707
|
+
InternalName: info.name,
|
|
708
|
+
OriginalFilename: info.filename,
|
|
709
|
+
FileVersion: text,
|
|
710
|
+
ProductVersion: text
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
versionInfo.setFileVersion(major, minor, patch, revision);
|
|
714
|
+
versionInfo.setProductVersion(major, minor, patch, revision);
|
|
715
|
+
versionInfo.outputToResourceEntries(resources.entries);
|
|
716
|
+
if (info.ico) {
|
|
717
|
+
const groups = Resource.IconGroupEntry.fromEntries(resources.entries);
|
|
718
|
+
const group = groups[0];
|
|
719
|
+
const icons = Data.IconFile.from(info.ico).icons.map((icon) => icon.data);
|
|
720
|
+
Resource.IconGroupEntry.replaceIconsForResource(resources.entries, group?.id ?? 1, group?.lang ?? 1033, icons);
|
|
721
|
+
}
|
|
722
|
+
resources.outputResource(executable);
|
|
723
|
+
const output = Buffer.from(executable.generate());
|
|
724
|
+
output.writeUInt16LE(IMAGE_SUBSYSTEM_WINDOWS_GUI, output.readUInt32LE(60) + 24 + 68);
|
|
725
|
+
writeFileSync3(path, output);
|
|
726
|
+
}
|
|
727
|
+
var IMAGE_SUBSYSTEM_WINDOWS_GUI = 2;
|
|
728
|
+
var init_windows_executable = () => {};
|
|
729
|
+
|
|
730
|
+
// packages/core/node-build.ts
|
|
731
|
+
import { execFileSync } from "node:child_process";
|
|
732
|
+
import { existsSync as existsSync4, mkdtempSync, writeFileSync as writeFileSync4, readFileSync as readFileSync5 } from "node:fs";
|
|
733
|
+
import { mkdir, rm } from "node:fs/promises";
|
|
734
|
+
import { tmpdir } from "node:os";
|
|
735
|
+
import { basename as basename3, dirname as dirname4, join as join4, relative as relative2, resolve as resolve3 } from "node:path";
|
|
736
|
+
import { nativePath } from "#tarve/runtime";
|
|
737
|
+
async function buildNode(options) {
|
|
738
|
+
const target = options.target ?? hostBuildTarget();
|
|
739
|
+
if (!target)
|
|
740
|
+
throw new Error(`Unsupported Node.js build host: ${process.platform}-${process.arch}`);
|
|
741
|
+
const config = targetConfig(target);
|
|
742
|
+
const node = options.nodeExecutable ?? (process.versions.bun ? Bun.which("node") ?? "node" : process.execPath);
|
|
743
|
+
const version = execFileSync(node, ["--version"], { encoding: "utf8" }).trim();
|
|
744
|
+
const match = /^v(\d+)\.(\d+)\./.exec(version);
|
|
745
|
+
if (!match || Number(match[1]) < 26 || Number(match[1]) === 26 && Number(match[2]) < 10) {
|
|
746
|
+
throw new Error(`Node.js 26.10+ is required for node:ffi inside a SEA (found ${version})`);
|
|
747
|
+
}
|
|
748
|
+
const { metadata, images, define } = buildMetadata(options.entrypoint, metadataOverrides(options));
|
|
749
|
+
const outfile = resolve3(options.outfile ?? `dist/${metadata.name}${config.executableSuffix}`);
|
|
750
|
+
let executable;
|
|
751
|
+
if (options.targetExecutable) {
|
|
752
|
+
executable = resolve3(options.targetExecutable);
|
|
753
|
+
if (!existsSync4(executable))
|
|
754
|
+
throw new Error(`Target Node.js executable is missing: ${executable}`);
|
|
755
|
+
assertNodeVersion(executable, version.slice(1));
|
|
756
|
+
}
|
|
757
|
+
const library = resolve3(options.nativeLibrary ?? (target === hostBuildTarget() ? nativePath() : join4(import.meta.dirname, "../../native", config.nativeDirectory, config.nativeName)));
|
|
758
|
+
if (!existsSync4(library))
|
|
759
|
+
throw new Error(`Tarve native library missing: ${library}`);
|
|
760
|
+
const temporary = mkdtempSync(join4(tmpdir(), "tarve-node-build-"));
|
|
761
|
+
try {
|
|
762
|
+
const root = import.meta.dirname;
|
|
763
|
+
const source = existsSync4(join4(root, "src/index.ts"));
|
|
764
|
+
if (!options.nativeLibrary && (!source || target !== hostBuildTarget())) {
|
|
765
|
+
const expected = source ? await createNativeCompatibilityManifest(resolve3(root, "../.."), JSON.parse(readFileSync5(resolve3(root, "../../package.json"), "utf8")).version) : await readNativeCompatibilityManifest(join4(root, NATIVE_COMPATIBILITY_MANIFEST_NAME));
|
|
766
|
+
await assertNativeRuntimeCompatible(library, target, expected);
|
|
767
|
+
}
|
|
768
|
+
if (!executable && target !== hostBuildTarget()) {
|
|
769
|
+
executable = await targetNodeExecutable(target, version.slice(1));
|
|
770
|
+
}
|
|
771
|
+
const { build: bundle } = await import("esbuild");
|
|
772
|
+
const result = await bundle({
|
|
773
|
+
entryPoints: [resolve3(options.entrypoint)],
|
|
774
|
+
outfile: join4(temporary, "app.mjs"),
|
|
775
|
+
bundle: true,
|
|
776
|
+
platform: "node",
|
|
777
|
+
format: "esm",
|
|
778
|
+
target: "node26",
|
|
779
|
+
jsx: "automatic",
|
|
780
|
+
jsxImportSource: "@tarve/core",
|
|
781
|
+
loader: { ".png": "file", ".jpg": "file", ".jpeg": "file", ".webp": "file", ".gif": "file", ".svg": "file" },
|
|
782
|
+
metafile: true,
|
|
783
|
+
define,
|
|
784
|
+
external: ["esbuild"],
|
|
785
|
+
plugins: [fileImportAttributes, {
|
|
786
|
+
name: "tarve-node-runtime",
|
|
787
|
+
setup(build) {
|
|
788
|
+
if (source) {
|
|
789
|
+
build.onResolve({ filter: /^@tarve\/core(?:\/(?:jsx-runtime|jsx-dev-runtime))?$/ }, (args) => {
|
|
790
|
+
const entry = args.path.slice("@tarve/core".length) || "/index";
|
|
791
|
+
return { path: join4(root, "src", `${entry.slice(1)}.ts`) };
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
build.onResolve({ filter: /^#tarve\/runtime$/ }, () => ({ path: "native-runtime", namespace: "tarve-node" }));
|
|
795
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-node" }, () => ({
|
|
796
|
+
contents: `import { join } from "node:path"; export function nativePath() { return join(import.meta.dirname, ${JSON.stringify(config.nativeName)}); }`,
|
|
797
|
+
loader: "js"
|
|
798
|
+
}));
|
|
799
|
+
}
|
|
800
|
+
}]
|
|
801
|
+
});
|
|
802
|
+
const assets = { [config.nativeName]: seaPath(library) };
|
|
803
|
+
for (const [path, output] of Object.entries(result.metafile.outputs)) {
|
|
804
|
+
if (!output.entryPoint)
|
|
805
|
+
assets[relative2(temporary, resolve3(path)).replaceAll("\\", "/")] = seaPath(resolve3(path));
|
|
806
|
+
}
|
|
807
|
+
const sea = join4(temporary, "sea.json");
|
|
808
|
+
writeFileSync4(sea, JSON.stringify({
|
|
809
|
+
main: seaPath(join4(temporary, "app.mjs")),
|
|
810
|
+
mainFormat: "module",
|
|
811
|
+
output: seaPath(outfile),
|
|
812
|
+
...executable ? { executable: seaPath(executable) } : {},
|
|
813
|
+
useVfs: true,
|
|
814
|
+
useCodeCache: false,
|
|
815
|
+
useSnapshot: false,
|
|
816
|
+
assets,
|
|
817
|
+
execArgv: ["--disable-warning=ExperimentalWarning"],
|
|
818
|
+
execArgvExtension: "env"
|
|
819
|
+
}));
|
|
820
|
+
await mkdir(dirname4(outfile), { recursive: true });
|
|
821
|
+
await rm(outfile, { force: true });
|
|
822
|
+
execFileSync(node, ["--build-sea", sea], { stdio: "pipe" });
|
|
823
|
+
if (config.platform === "win32") {
|
|
824
|
+
const tarveVersion = JSON.parse(readFileSync5(resolve3(root, "../../package.json"), "utf8")).version;
|
|
825
|
+
(await Promise.resolve().then(() => (init_windows_executable(), exports_windows_executable))).brandWindowsExecutable(outfile, {
|
|
826
|
+
name: metadata.name,
|
|
827
|
+
version: metadata.version ?? tarveVersion,
|
|
828
|
+
filename: basename3(outfile),
|
|
829
|
+
description: metadata.description,
|
|
830
|
+
publisher: metadata.publisher,
|
|
831
|
+
copyright: metadata.copyright,
|
|
832
|
+
ico: images?.ico
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
if (config.platform === "linux" && (metadata.icon || metadata.appId))
|
|
836
|
+
writeLinuxDesktopEntry(outfile, metadata, images?.png);
|
|
837
|
+
return outfile;
|
|
838
|
+
} finally {
|
|
839
|
+
await rm(temporary, { recursive: true, force: true });
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
function seaPath(path) {
|
|
843
|
+
return path.replaceAll("\\", "/");
|
|
844
|
+
}
|
|
845
|
+
var init_node_build = __esm(() => {
|
|
846
|
+
init_esbuild_file_imports();
|
|
847
|
+
init_targets();
|
|
848
|
+
init_build();
|
|
849
|
+
init_app_metadata();
|
|
850
|
+
init_native_runtime();
|
|
851
|
+
init_node_target();
|
|
852
|
+
});
|
|
160
853
|
|
|
161
854
|
// packages/core/build.ts
|
|
855
|
+
import { existsSync as existsSync5, mkdtempSync as mkdtempSync2, writeFileSync as writeFileSync5 } from "node:fs";
|
|
856
|
+
import { mkdir as mkdir2, rm as rm2 } from "node:fs/promises";
|
|
857
|
+
import { tmpdir as tmpdir2 } from "node:os";
|
|
858
|
+
import { dirname as dirname5, join as join5, resolve as resolve4 } from "node:path";
|
|
859
|
+
import { fileURLToPath } from "node:url";
|
|
860
|
+
import { nativePath as nativePath2 } from "#tarve/runtime";
|
|
162
861
|
function resolveNativeLibrary(target, override) {
|
|
163
862
|
if (override) {
|
|
164
|
-
const path =
|
|
165
|
-
if (!
|
|
863
|
+
const path = resolve4(override);
|
|
864
|
+
if (!existsSync5(path))
|
|
166
865
|
throw new Error(`Tarve native library does not exist: ${path}`);
|
|
167
866
|
return path;
|
|
168
867
|
}
|
|
@@ -170,37 +869,42 @@ function resolveNativeLibrary(target, override) {
|
|
|
170
869
|
const hostTarget = hostBuildTarget();
|
|
171
870
|
if (hostTarget === target) {
|
|
172
871
|
try {
|
|
173
|
-
return
|
|
872
|
+
return nativePath2();
|
|
174
873
|
} catch {}
|
|
175
874
|
}
|
|
176
875
|
const candidates = [
|
|
177
|
-
|
|
178
|
-
|
|
876
|
+
join5(import.meta.dir, config.nativeName),
|
|
877
|
+
resolve4(import.meta.dir, "../../native", config.nativeDirectory, config.nativeName)
|
|
179
878
|
];
|
|
180
|
-
const found = candidates.find(
|
|
879
|
+
const found = candidates.find(existsSync5);
|
|
181
880
|
if (found)
|
|
182
881
|
return found;
|
|
183
882
|
throw new Error(`Tarve native library missing for ${target}. Expected ${config.nativeName} in native/${config.nativeDirectory}. ` + "Use a Tarve package containing that target runtime or pass nativeLibrary explicitly.");
|
|
184
883
|
}
|
|
185
884
|
async function build(options) {
|
|
885
|
+
if (options.runtime === "node") {
|
|
886
|
+
await Promise.resolve().then(() => init_node_build());
|
|
887
|
+
return buildNode(options);
|
|
888
|
+
}
|
|
186
889
|
const target = options.target ?? hostBuildTarget();
|
|
187
890
|
if (!target)
|
|
188
891
|
throw new Error(`Tarve executable distribution is not supported for ${process.platform}-${process.arch}. Pass an explicit supported target when cross-compiling.`);
|
|
189
892
|
if (!isBuildTarget(target))
|
|
190
893
|
throw new TypeError(`Unsupported Tarve build target: ${String(target)}. Expected ${BUILD_TARGETS.join(" or ")}.`);
|
|
191
894
|
const config = targetConfig(target);
|
|
192
|
-
const entrypoint =
|
|
193
|
-
const packageMetadata = await Bun.file(
|
|
895
|
+
const entrypoint = resolve4(options.entrypoint);
|
|
896
|
+
const packageMetadata = await Bun.file(resolve4(import.meta.dir, "../../package.json")).json();
|
|
194
897
|
if (typeof packageMetadata.version !== "string" || packageMetadata.version.length === 0) {
|
|
195
898
|
throw new Error("Tarve package version is missing.");
|
|
196
899
|
}
|
|
197
|
-
const
|
|
198
|
-
const
|
|
900
|
+
const { metadata, images, define } = buildMetadata(entrypoint, metadataOverrides(options));
|
|
901
|
+
const name = metadata.name;
|
|
902
|
+
const outfile = resolve4(options.outfile ?? `dist/${name}${config.executableSuffix}`);
|
|
199
903
|
const library = resolveNativeLibrary(target, options.nativeLibrary);
|
|
200
904
|
const assetsModule = fileURLToPath(import.meta.resolve("#tarve/assets"));
|
|
201
|
-
const sourceDirectory =
|
|
202
|
-
const repositorySource =
|
|
203
|
-
const expectedCompatibility = repositorySource ? await createNativeCompatibilityManifest(
|
|
905
|
+
const sourceDirectory = join5(import.meta.dir, "src");
|
|
906
|
+
const repositorySource = existsSync5(join5(sourceDirectory, "index.ts"));
|
|
907
|
+
const expectedCompatibility = repositorySource ? await createNativeCompatibilityManifest(resolve4(import.meta.dir, "../.."), packageMetadata.version) : await readNativeCompatibilityManifest(join5(import.meta.dir, NATIVE_COMPATIBILITY_MANIFEST_NAME));
|
|
204
908
|
const requireRuntimeManifest = target !== hostBuildTarget() || !options.nativeLibrary && !repositorySource;
|
|
205
909
|
if (requireRuntimeManifest) {
|
|
206
910
|
await assertNativeRuntimeCompatible(library, target, expectedCompatibility);
|
|
@@ -214,9 +918,9 @@ async function build(options) {
|
|
|
214
918
|
name: "tarve-native-runtime",
|
|
215
919
|
setup(builder) {
|
|
216
920
|
if (repositorySource) {
|
|
217
|
-
builder.onResolve({ filter: /^tarve$/ }, () => ({ path:
|
|
218
|
-
builder.onResolve({ filter: /^tarve\/jsx-runtime$/ }, () => ({ path:
|
|
219
|
-
builder.onResolve({ filter: /^tarve\/jsx-dev-runtime$/ }, () => ({ path:
|
|
921
|
+
builder.onResolve({ filter: /^tarve$/ }, () => ({ path: join5(sourceDirectory, "index.ts") }));
|
|
922
|
+
builder.onResolve({ filter: /^tarve\/jsx-runtime$/ }, () => ({ path: join5(sourceDirectory, "jsx-runtime.ts") }));
|
|
923
|
+
builder.onResolve({ filter: /^tarve\/jsx-dev-runtime$/ }, () => ({ path: join5(sourceDirectory, "jsx-dev-runtime.ts") }));
|
|
220
924
|
}
|
|
221
925
|
builder.onResolve({ filter: /^#tarve\/runtime$/ }, () => ({ path: "runtime", namespace: "tarve" }));
|
|
222
926
|
builder.onLoad({ filter: /^runtime$/, namespace: "tarve" }, () => ({
|
|
@@ -231,61 +935,328 @@ async function build(options) {
|
|
|
231
935
|
}));
|
|
232
936
|
}
|
|
233
937
|
};
|
|
234
|
-
await
|
|
235
|
-
const
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
target:
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
938
|
+
await mkdir2(dirname5(outfile), { recursive: true });
|
|
939
|
+
const iconDirectory = images && config.platform === "win32" ? mkdtempSync2(join5(tmpdir2(), "tarve-icon-")) : undefined;
|
|
940
|
+
const icoPath = iconDirectory ? join5(iconDirectory, "app.ico") : undefined;
|
|
941
|
+
if (icoPath)
|
|
942
|
+
writeFileSync5(icoPath, images.ico);
|
|
943
|
+
try {
|
|
944
|
+
const result = await Bun.build({
|
|
945
|
+
entrypoints: [entrypoint],
|
|
946
|
+
target: "bun",
|
|
947
|
+
plugins: [runtime],
|
|
948
|
+
define: { "process.env.NODE_ENV": JSON.stringify("production"), ...define },
|
|
949
|
+
minify: true,
|
|
950
|
+
compile: {
|
|
951
|
+
target: config.bunTarget,
|
|
952
|
+
outfile,
|
|
953
|
+
autoloadDotenv: false,
|
|
954
|
+
autoloadBunfig: false,
|
|
955
|
+
...config.platform === "win32" ? {
|
|
956
|
+
windows: {
|
|
957
|
+
hideConsole: true,
|
|
958
|
+
title: name,
|
|
959
|
+
version: metadata.version ?? packageMetadata.version,
|
|
960
|
+
description: metadata.description ?? `${name} — native Tarve application`,
|
|
961
|
+
...metadata.publisher ? { publisher: metadata.publisher } : {},
|
|
962
|
+
...metadata.copyright ? { copyright: metadata.copyright } : {},
|
|
963
|
+
...icoPath ? { icon: icoPath } : {}
|
|
964
|
+
}
|
|
965
|
+
} : {}
|
|
966
|
+
}
|
|
967
|
+
});
|
|
968
|
+
if (!result.success)
|
|
969
|
+
throw new AggregateError(result.logs, "Could not compile the Tarve application");
|
|
970
|
+
} finally {
|
|
971
|
+
if (iconDirectory)
|
|
972
|
+
await rm2(iconDirectory, { recursive: true, force: true });
|
|
973
|
+
}
|
|
974
|
+
if (config.platform === "linux" && (metadata.icon || metadata.appId))
|
|
975
|
+
writeLinuxDesktopEntry(outfile, metadata, images?.png);
|
|
253
976
|
return outfile;
|
|
254
977
|
}
|
|
978
|
+
function metadataOverrides(options) {
|
|
979
|
+
return { name: options.name, version: options.version, description: options.description, publisher: options.publisher, copyright: options.copyright, icon: options.icon, appId: options.appId };
|
|
980
|
+
}
|
|
981
|
+
var init_build = __esm(() => {
|
|
982
|
+
init_app_metadata();
|
|
983
|
+
init_targets();
|
|
984
|
+
init_native_runtime();
|
|
985
|
+
init_targets();
|
|
986
|
+
});
|
|
255
987
|
|
|
256
988
|
// packages/core/cli.ts
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
989
|
+
init_app_metadata();
|
|
990
|
+
init_build();
|
|
991
|
+
import { spawn } from "node:child_process";
|
|
992
|
+
import { resolve as resolve6 } from "node:path";
|
|
993
|
+
import { parseArgs } from "node:util";
|
|
994
|
+
|
|
995
|
+
// packages/core/node-dev.ts
|
|
996
|
+
init_esbuild_file_imports();
|
|
997
|
+
import { fork, spawnSync } from "node:child_process";
|
|
998
|
+
import { mkdtempSync as mkdtempSync3, rmSync as rmSync2, writeFileSync as writeFileSync6 } from "node:fs";
|
|
999
|
+
import { tmpdir as tmpdir3 } from "node:os";
|
|
1000
|
+
import { join as join6, resolve as resolve5 } from "node:path";
|
|
1001
|
+
import { context, build as build2 } from "esbuild";
|
|
1002
|
+
import { nativePath as nativePath3 } from "#tarve/runtime";
|
|
1003
|
+
var installedBuildApi = {
|
|
1004
|
+
name: "tarve-installed-build-api",
|
|
1005
|
+
setup(build) {
|
|
1006
|
+
build.onResolve({ filter: /^@tarve\/core\/build$/ }, () => ({ path: import.meta.resolve("@tarve/core/build"), external: true }));
|
|
1007
|
+
}
|
|
1008
|
+
};
|
|
1009
|
+
async function devNode(entrypoint) {
|
|
1010
|
+
const directory = mkdtempSync3(join6(tmpdir3(), "tarve-node-dev-"));
|
|
1011
|
+
const app = join6(directory, "app.mjs");
|
|
1012
|
+
const bootstrap = join6(directory, "bootstrap.mjs");
|
|
1013
|
+
writeFileSync6(bootstrap, `
|
|
1014
|
+
import { pathToFileURL } from "node:url";
|
|
1015
|
+
const entry = pathToFileURL(process.argv[2]).href;
|
|
1016
|
+
let version = 0;
|
|
1017
|
+
async function load() {
|
|
1018
|
+
await import(entry + "?v=" + version++);
|
|
1019
|
+
process.send?.({ type: "loaded", version });
|
|
1020
|
+
}
|
|
1021
|
+
function showError(error) {
|
|
1022
|
+
console.error(error);
|
|
1023
|
+
globalThis[Symbol.for("tarve.devApp")]?.remount(() => { throw error; });
|
|
1024
|
+
}
|
|
1025
|
+
let queue = Promise.resolve().then(load).catch(error => { console.error(error); process.exit(1); });
|
|
1026
|
+
process.on("message", message => {
|
|
1027
|
+
if (message === "reload") queue = queue.then(load).catch(showError);
|
|
1028
|
+
if (message?.type === "build-error") showError(new Error(message.error));
|
|
1029
|
+
});
|
|
1030
|
+
process.on("disconnect", () => process.exit(0));
|
|
1031
|
+
`);
|
|
1032
|
+
let child;
|
|
1033
|
+
let readyToReload = false;
|
|
1034
|
+
let builds = 0;
|
|
1035
|
+
const compiler = await context({
|
|
1036
|
+
entryPoints: [resolve5(entrypoint)],
|
|
1037
|
+
outfile: app,
|
|
1038
|
+
bundle: true,
|
|
1039
|
+
platform: "node",
|
|
1040
|
+
format: "esm",
|
|
1041
|
+
target: "node26",
|
|
1042
|
+
jsx: "automatic",
|
|
1043
|
+
jsxImportSource: "@tarve/core",
|
|
1044
|
+
loader: { ".png": "file", ".jpg": "file", ".jpeg": "file", ".webp": "file", ".gif": "file", ".svg": "file" },
|
|
1045
|
+
plugins: [installedBuildApi, fileImportAttributes, {
|
|
1046
|
+
name: "tarve-node-hot",
|
|
1047
|
+
setup(build) {
|
|
1048
|
+
build.onEnd((result) => {
|
|
1049
|
+
if (++builds <= 2 || !readyToReload || !child?.connected)
|
|
1050
|
+
return;
|
|
1051
|
+
if (result.errors.length) {
|
|
1052
|
+
child.send({ type: "build-error", error: result.errors.map((error) => error.text).join(`
|
|
1053
|
+
`) });
|
|
1054
|
+
} else {
|
|
1055
|
+
child.send("reload");
|
|
1056
|
+
}
|
|
1057
|
+
});
|
|
268
1058
|
}
|
|
1059
|
+
}]
|
|
1060
|
+
});
|
|
1061
|
+
try {
|
|
1062
|
+
await compiler.rebuild();
|
|
1063
|
+
await compiler.watch();
|
|
1064
|
+
child = fork(bootstrap, [app], {
|
|
1065
|
+
execPath: nodeExecutable(),
|
|
1066
|
+
stdio: ["inherit", "inherit", "inherit", "ipc"],
|
|
1067
|
+
execArgv: ["--disable-warning=ExperimentalWarning"],
|
|
1068
|
+
env: { ...process.env, TARVE_DEV: "1", TARVE_HOT: "1", TARVE_NATIVE: nativePath3(), TARVE_ENTRY: resolve5(entrypoint) }
|
|
269
1069
|
});
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
if (values.target !== undefined && !isBuildTarget(values.target)) {
|
|
276
|
-
throw new Error(`Unsupported target: ${values.target}. Expected ${BUILD_TARGETS.join(" or ")}.`);
|
|
1070
|
+
child.on("message", (message) => {
|
|
1071
|
+
if (typeof message === "object" && message && "version" in message) {
|
|
1072
|
+
if (Number(message.version) === 1)
|
|
1073
|
+
readyToReload = true;
|
|
1074
|
+
console.log(Number(message.version) === 1 ? "tarve dev: ready" : "tarve dev: remounted");
|
|
277
1075
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
1076
|
+
});
|
|
1077
|
+
const running = child;
|
|
1078
|
+
let stopping = false;
|
|
1079
|
+
const stop = () => {
|
|
1080
|
+
stopping = true;
|
|
1081
|
+
running.kill();
|
|
1082
|
+
};
|
|
1083
|
+
process.on("SIGINT", stop);
|
|
1084
|
+
process.on("SIGTERM", stop);
|
|
1085
|
+
try {
|
|
1086
|
+
const code = await new Promise((resolve, reject) => {
|
|
1087
|
+
running.once("error", reject);
|
|
1088
|
+
running.once("exit", (code, signal) => resolve(code ?? (signal ? 1 : 0)));
|
|
1089
|
+
});
|
|
1090
|
+
if (code !== 0 && !stopping)
|
|
1091
|
+
throw new Error(`Node application exited with code ${code}`);
|
|
1092
|
+
} finally {
|
|
1093
|
+
process.off("SIGINT", stop);
|
|
1094
|
+
process.off("SIGTERM", stop);
|
|
285
1095
|
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
|
|
1096
|
+
} finally {
|
|
1097
|
+
if (child && !child.killed && child.exitCode === null)
|
|
1098
|
+
child.kill();
|
|
1099
|
+
await compiler.dispose();
|
|
1100
|
+
rmSync2(directory, { recursive: true, force: true });
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
function nodeExecutable() {
|
|
1104
|
+
return process.versions.bun ? Bun.which("node") ?? "node" : process.execPath;
|
|
1105
|
+
}
|
|
1106
|
+
async function runNode(entrypoint) {
|
|
1107
|
+
const directory = mkdtempSync3(join6(tmpdir3(), "tarve-node-run-"));
|
|
1108
|
+
try {
|
|
1109
|
+
const app = join6(directory, "app.mjs");
|
|
1110
|
+
await build2({
|
|
1111
|
+
entryPoints: [resolve5(entrypoint)],
|
|
1112
|
+
outfile: app,
|
|
1113
|
+
bundle: true,
|
|
1114
|
+
platform: "node",
|
|
1115
|
+
format: "esm",
|
|
1116
|
+
target: "node26",
|
|
1117
|
+
jsx: "automatic",
|
|
1118
|
+
jsxImportSource: "@tarve/core",
|
|
1119
|
+
loader: { ".png": "file", ".jpg": "file", ".jpeg": "file", ".webp": "file", ".gif": "file", ".svg": "file" },
|
|
1120
|
+
plugins: [installedBuildApi, fileImportAttributes]
|
|
1121
|
+
});
|
|
1122
|
+
const result = spawnSync(nodeExecutable(), ["--disable-warning=ExperimentalWarning", app], {
|
|
1123
|
+
stdio: "inherit",
|
|
1124
|
+
env: { ...process.env, TARVE_NATIVE: nativePath3(), TARVE_ENTRY: resolve5(entrypoint) }
|
|
1125
|
+
});
|
|
1126
|
+
if (result.error)
|
|
1127
|
+
throw result.error;
|
|
1128
|
+
return result.status ?? 1;
|
|
1129
|
+
} finally {
|
|
1130
|
+
rmSync2(directory, { recursive: true, force: true });
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
// packages/core/runtime-select.ts
|
|
1135
|
+
function selectRuntime(flag, versions = process.versions, userAgent = process.env.npm_config_user_agent) {
|
|
1136
|
+
if (flag !== undefined) {
|
|
1137
|
+
if (flag !== "bun" && flag !== "node")
|
|
1138
|
+
throw new Error(`Unsupported runtime: ${flag}. Expected bun or node.`);
|
|
1139
|
+
return flag;
|
|
289
1140
|
}
|
|
1141
|
+
const launcher = /^(?:npm|pnpm|yarn)\/.*?\bnode\/v(\S+)/.exec(userAgent ?? "");
|
|
1142
|
+
if (launcher && supportsNodeRuntime(launcher[1]))
|
|
1143
|
+
return "node";
|
|
1144
|
+
if (versions.bun || /^bun\//.test(userAgent ?? ""))
|
|
1145
|
+
return "bun";
|
|
1146
|
+
return supportsNodeRuntime(versions.node) ? "node" : "bun";
|
|
1147
|
+
}
|
|
1148
|
+
function supportsNodeRuntime(version) {
|
|
1149
|
+
const [major = 0, minor = 0] = (version ?? "").split(".").map(Number);
|
|
1150
|
+
return major > 26 || major === 26 && minor >= 10;
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
// packages/core/cli.ts
|
|
1154
|
+
init_targets();
|
|
1155
|
+
var COMMANDS = ["run", "dev", "build"];
|
|
1156
|
+
function usage(target) {
|
|
1157
|
+
const output = `dist/App${target ? targetConfig(target).executableSuffix : ""}`;
|
|
1158
|
+
return [
|
|
1159
|
+
"Usage: tarve <run|dev|build> <app.tsx> [options]",
|
|
1160
|
+
"",
|
|
1161
|
+
" run run the app",
|
|
1162
|
+
" dev run with hot reload into the same window",
|
|
1163
|
+
" build compile a standalone executable",
|
|
1164
|
+
"",
|
|
1165
|
+
"Options:",
|
|
1166
|
+
" --runtime bun|node defaults to the runtime that launched tarve",
|
|
1167
|
+
` --target ${BUILD_TARGETS.join("|")} build: target OS (cross-compiles)`,
|
|
1168
|
+
` --outfile ${output} build: output path`,
|
|
1169
|
+
" --name App, --version 1.0.0 app metadata (executable, window, app identity)",
|
|
1170
|
+
" --icon icon.png app icon: .png (square, 256 px+) or .ico",
|
|
1171
|
+
" --app-id Acme.App Windows AppUserModelID / Linux app id",
|
|
1172
|
+
" --description, --publisher, --copyright build: executable details",
|
|
1173
|
+
' each defaults to the "tarve" field of package.json',
|
|
1174
|
+
" --target-executable path build (node): target OS Node binary for offline cross-builds"
|
|
1175
|
+
].join(`
|
|
1176
|
+
`);
|
|
1177
|
+
}
|
|
1178
|
+
function forward(command, args, env = process.env) {
|
|
1179
|
+
return new Promise((resolveCode, reject) => {
|
|
1180
|
+
const child = spawn(command, args, { stdio: "inherit", env });
|
|
1181
|
+
const stop = () => child.kill();
|
|
1182
|
+
process.on("SIGINT", stop);
|
|
1183
|
+
process.on("SIGTERM", stop);
|
|
1184
|
+
child.once("error", (error) => {
|
|
1185
|
+
reject(error.code === "ENOENT" ? new Error(command === "bun" ? "Bun was not found. Install Bun, or run with Node.js 26.10+ and --runtime node." : `${command} was not found. Install it or choose the other runtime with --runtime.`) : error);
|
|
1186
|
+
});
|
|
1187
|
+
child.once("exit", (code, signal) => {
|
|
1188
|
+
process.off("SIGINT", stop);
|
|
1189
|
+
process.off("SIGTERM", stop);
|
|
1190
|
+
resolveCode(code ?? (signal ? 1 : 0));
|
|
1191
|
+
});
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
async function main() {
|
|
1195
|
+
const { values, positionals } = parseArgs({
|
|
1196
|
+
args: process.argv.slice(2),
|
|
1197
|
+
allowPositionals: true,
|
|
1198
|
+
options: {
|
|
1199
|
+
outfile: { type: "string" },
|
|
1200
|
+
name: { type: "string" },
|
|
1201
|
+
version: { type: "string" },
|
|
1202
|
+
icon: { type: "string" },
|
|
1203
|
+
"app-id": { type: "string" },
|
|
1204
|
+
description: { type: "string" },
|
|
1205
|
+
publisher: { type: "string" },
|
|
1206
|
+
copyright: { type: "string" },
|
|
1207
|
+
target: { type: "string" },
|
|
1208
|
+
runtime: { type: "string" },
|
|
1209
|
+
"target-executable": { type: "string" },
|
|
1210
|
+
help: { type: "boolean", short: "h" }
|
|
1211
|
+
}
|
|
1212
|
+
});
|
|
1213
|
+
const [command, entrypoint] = positionals;
|
|
1214
|
+
if (values.help || positionals.length === 0) {
|
|
1215
|
+
console.log(usage(values.target && isBuildTarget(values.target) ? values.target : hostBuildTarget()));
|
|
1216
|
+
return 0;
|
|
1217
|
+
}
|
|
1218
|
+
if (!COMMANDS.includes(command) || !entrypoint || positionals.length !== 2) {
|
|
1219
|
+
throw new Error("Expected: tarve <run|dev|build> <app.tsx>. Use --help for options.");
|
|
1220
|
+
}
|
|
1221
|
+
if (values.target !== undefined && !isBuildTarget(values.target)) {
|
|
1222
|
+
throw new Error(`Unsupported target: ${values.target}. Expected ${BUILD_TARGETS.join(" or ")}.`);
|
|
1223
|
+
}
|
|
1224
|
+
const runtime = selectRuntime(values.runtime);
|
|
1225
|
+
if (runtime === "bun" && !process.versions.bun) {
|
|
1226
|
+
return forward("bun", [process.argv[1], ...process.argv.slice(2), "--runtime", "bun"]);
|
|
1227
|
+
}
|
|
1228
|
+
if (command === "build") {
|
|
1229
|
+
console.log(await build({
|
|
1230
|
+
entrypoint,
|
|
1231
|
+
outfile: values.outfile,
|
|
1232
|
+
name: values.name,
|
|
1233
|
+
version: values.version,
|
|
1234
|
+
icon: values.icon,
|
|
1235
|
+
appId: values["app-id"],
|
|
1236
|
+
description: values.description,
|
|
1237
|
+
publisher: values.publisher,
|
|
1238
|
+
copyright: values.copyright,
|
|
1239
|
+
target: values.target,
|
|
1240
|
+
runtime,
|
|
1241
|
+
targetExecutable: values["target-executable"]
|
|
1242
|
+
}));
|
|
1243
|
+
return 0;
|
|
1244
|
+
}
|
|
1245
|
+
const metadata = resolveAppMetadata(entrypoint, { name: values.name, icon: values.icon, appId: values["app-id"] });
|
|
1246
|
+
process.env.TARVE_APP_METADATA = runtimeMetadata(metadata, metadata.icon);
|
|
1247
|
+
if (runtime === "node") {
|
|
1248
|
+
if (command === "dev") {
|
|
1249
|
+
await devNode(entrypoint);
|
|
1250
|
+
return 0;
|
|
1251
|
+
}
|
|
1252
|
+
return runNode(entrypoint);
|
|
1253
|
+
}
|
|
1254
|
+
const app = resolve6(entrypoint);
|
|
1255
|
+
return command === "dev" ? forward(process.execPath, ["--hot", app], { ...process.env, TARVE_DEV: process.env.TARVE_DEV ?? "1" }) : forward(process.execPath, [app]);
|
|
1256
|
+
}
|
|
1257
|
+
try {
|
|
1258
|
+
process.exitCode = await main();
|
|
1259
|
+
} catch (error) {
|
|
1260
|
+
console.error(`error: ${error instanceof Error ? error.message : String(error)}`);
|
|
1261
|
+
process.exitCode = 1;
|
|
290
1262
|
}
|
|
291
|
-
await main();
|