-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathbinding-platform.ts
More file actions
161 lines (145 loc) · 4.9 KB
/
binding-platform.ts
File metadata and controls
161 lines (145 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import spawn from 'cross-spawn';
import { copyFile, writeFile } from 'fs/promises';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { ensureDir, type BuildConfig } from './util';
export async function buildPlatformBinding(config: BuildConfig) {
await new Promise((resolve, reject) => {
try {
ensureDir(config.distQwikPkgDir);
ensureDir(config.distBindingsDir);
const cmd = `napi`;
const args = [
`build`,
`--cargo-name`,
'qwik_napi',
`--platform`,
`--config=packages/qwik/src/napi/napi.config.json`,
config.distBindingsDir,
];
if (config.platformTarget) {
args.push(`--target`, config.platformTarget);
}
if (!config.dev) {
args.push(`--release`);
args.push(`--strip`);
}
const napiCwd = join(config.rootDir);
const child = spawn(cmd, args, { stdio: 'inherit', cwd: napiCwd });
child.on('error', reject);
child.on('close', (code) => {
if (code === 0) {
resolve(child.stdout);
} else {
reject(`napi exited with code ${code}`);
}
});
} catch (e) {
reject(e);
}
});
console.log('🐯 native binding');
}
// TODO only download current target and wasm
export async function copyPlatformBindingWasm(config: BuildConfig) {
ensureDir(config.distQwikPkgDir);
ensureDir(config.distBindingsDir);
const cacheDir = join(config.tmpDir, `cached-bindings`);
let version = config.distVersion;
const isDev = version.includes('-dev');
let cdnUrl = 'https://cdn.jsdelivr.net/npm/';
let packageName: string;
if (isDev) {
cdnUrl = `https://pkg.pr.new/QwikDev/qwik/`;
version = version.split('-dev')[0];
}
if (version.startsWith('2')) {
// 6903 is the PR that builds v2
packageName = `@qwik.dev/core@${isDev ? '6903' : version}`;
} else {
packageName = `@builder.io/qwik@${isDev ? 'main' : version}`;
}
let cacheVersionDir: string;
if (isDev) {
// We fetch from pkg.pr.new which is a CDN for the CI builds
// It redirects to the latest version
cdnUrl = `${cdnUrl}${packageName}`;
// First request the URL, this will redirect to the latest version
const rsp = await fetch(cdnUrl);
if (!rsp.ok) {
throw new Error(`Unable to find Qwik package from ${cdnUrl}`);
}
const url = rsp.url;
// get the package name from the url
const realPackageName = url.split('/').pop()!;
// now check if we already have this package in the cache
const cachedPath = join(cacheDir, realPackageName);
if (!existsSync(cachedPath)) {
// download the package
console.log(`🦉 downloading CI build from ${url}`);
const pkgRsp = await fetch(url);
if (!pkgRsp.ok) {
console.error(pkgRsp);
throw new Error(`Unable to fetch Qwik package from ${pkgRsp.url}`);
}
await writeFile(cachedPath, pkgRsp.body as any);
}
// now unpack the package using tar, into the cache directory
const unpackedPath = join(cacheDir, `${realPackageName}-unpacked`);
ensureDir(unpackedPath);
await new Promise((resolve, reject) => {
const child = spawn('tar', ['-xvf', cachedPath, '-C', unpackedPath]);
child.on('error', (e) => {
console.error(e);
reject(e);
});
child.on('close', (code) => {
if (code === 0) {
resolve(child.stdout);
} else {
console.error(child.stdout);
reject(`tar exited with code ${code}`);
}
});
});
// now we need to find the bindings in the package
cacheVersionDir = join(unpackedPath, 'package', 'bindings');
} else {
cdnUrl = `${cdnUrl}${packageName}/bindings/`;
cacheVersionDir = join(cacheDir, version);
ensureDir(cacheVersionDir);
}
try {
const bindingFilenames = [
'qwik.darwin-arm64.node',
'qwik.darwin-x64.node',
'qwik.linux-x64-gnu.node',
'qwik.wasm.cjs',
'qwik.wasm.mjs',
'qwik.win32-x64-msvc.node',
'qwik_wasm_bg.wasm',
];
await Promise.all(
bindingFilenames.map(async (bindingFilename) => {
const cachedPath = join(cacheVersionDir, bindingFilename);
const distPath = join(config.distBindingsDir, bindingFilename);
if (!existsSync(cachedPath)) {
if (isDev) {
throw new Error(`Unable to find Qwik binding from ${cachedPath}`);
}
const url = `${cdnUrl}${bindingFilename}`;
console.log(`🦉 native binding / wasm (downloading from ${url})`);
const rsp = (await fetch(url)) as any;
if (!rsp.ok) {
throw new Error(`Unable to fetch Qwik binding from ${rsp.url}`);
}
await writeFile(cachedPath, rsp.body);
}
await copyFile(cachedPath, distPath);
})
);
console.log(`🦉 native binding / wasm (copied from npm v${version})`);
} catch (e) {
console.warn(`😱 ${e}`);
}
}