-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli.ts
More file actions
125 lines (110 loc) · 3.1 KB
/
Copy pathcli.ts
File metadata and controls
125 lines (110 loc) · 3.1 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
import 'zx/globals';
import { TEST_DATABASE_FILES } from './tests/fixtures.ts';
const WASM_BINDGEN_LOCK_PATTERN = /name = "wasm-bindgen"\nversion = "([^"]+)"/;
const detectCiEnvs = () => {
const ciEnvs = [
'CI',
'GITHUB_ACTIONS',
'GITHUB_RUN_ID',
'GITHUB_RUN_NUMBER',
'GITHUB_RUN_ATTEMPT',
];
for (const env of ciEnvs) {
if (process.env[env]) {
return true;
}
}
return false;
};
const readWasmBindgenVersion = (): string => {
const lockfile = fs.readFileSync('Cargo.lock', 'utf8');
const match = lockfile.match(WASM_BINDGEN_LOCK_PATTERN);
if (!match) {
throw new Error('Could not read wasm-bindgen version from Cargo.lock');
}
return match[1];
};
const readInstalledWasmBindgenVersion = async (): Promise<string | null> => {
const binPath = which('wasm-bindgen');
if (!binPath) {
return null;
}
const output = await $`wasm-bindgen --version`.nothrow().quiet();
if (output.exitCode !== 0) {
return null;
}
return String(output.stdout).trim().split(/\s+/).pop() ?? null;
};
const cliArgs = minimist(process.argv.slice(2), {
string: [
'target',
'profile',
'features',
],
boolean: [
'install-bindgen',
'build-rs',
'build-js',
'fetch-test-artifacts',
'install-playwright-browser',
],
alias: {
i: 'install-bindgen',
b: 'build-rs',
j: 'build-js',
t: 'target',
p: 'profile',
f: 'fetch-test-artifacts',
bp: 'install-playwright-browser',
},
default: {
target: 'node',
profile: 'release',
},
});
if (cliArgs['install-bindgen'] && !detectCiEnvs()) {
const wanted = readWasmBindgenVersion();
const current = await readInstalledWasmBindgenVersion();
if (current !== wanted) {
await $`cargo install -f wasm-bindgen-cli --version ${wanted}`;
}
}
const profile = cliArgs.profile || 'release';
if (cliArgs['build-rs']) {
const featureArgs = cliArgs.features ? ['--features', String(cliArgs.features)] : [];
await $`cargo build --lib --${profile} --target wasm32-unknown-unknown ${featureArgs}`;
}
if (cliArgs['build-js']) {
const target: string = cliArgs.target || 'node';
const targetToDir = {
nodejs: 'node',
web: 'browser',
'experimental-nodejs-module': 'node-module',
bundler: 'bundler',
'no-modules': 'no-module',
}[target] || 'node';
fs.removeSync(`./${targetToDir}`);
await $`wasm-bindgen \
--target ${target} ./target/wasm32-unknown-unknown/${profile}/index.wasm \
--out-dir ./${targetToDir}`;
if (target === 'bundler') {
const indexJs = await fs.readFile(`./${targetToDir}/index.js`, 'utf-8')
const indexJsJSRTypePath = `/* @ts-self-types="./index.d.ts" */
${indexJs}`
await fs.writeFile(`./${targetToDir}/index.js`, indexJsJSRTypePath)
}
}
if (cliArgs['fetch-test-artifacts']) {
const workspaceRoot = path.resolve();
for (const database of TEST_DATABASE_FILES) {
const dbFilePath = path.join(workspaceRoot, 'tests', `.${database}`);
if (fs.existsSync(dbFilePath)) {
console.log('DB File', database, 'already exists');
continue;
}
const dbFile = await fetch(`https://github.com/maxmind/MaxMind-DB/raw/main/test-data/${database}`)
.then(v => v.arrayBuffer())
.then(v => new Uint8Array(v));
await fs.writeFile(dbFilePath, dbFile);
}
}