Skip to content

Commit 829d2ed

Browse files
authored
Download import.sh script inside Builder (#23)
Rather than downloading `import.sh` per-build, it'll be downloaded at Builder build-time and published in the npm package. This will make the Builder faster and more reliable.
1 parent e1fd24d commit 829d2ed

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

build.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
rm -rf dist
5+
tsc --project src/tsconfig.json
6+
cp src/bootstrap src/build.sh src/runtime.sh dist
7+
curl -sfLS "https://import.sh" > dist/import.sh
8+
chmod +x dist/import.sh

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
"dist"
1616
],
1717
"scripts": {
18-
"prebuild": "rm -rf dist",
19-
"build": "tsc --project src/tsconfig.json",
20-
"postbuild": "cp src/bootstrap src/build.sh src/runtime.sh dist",
18+
"build": "./build.sh",
2119
"now-build": "next build",
2220
"prepublishOnly": "npm run build"
2321
},

src/build.sh

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,9 @@ set -euo pipefail
44
# `import` debug logs are always enabled during build
55
export IMPORT_DEBUG=1
66

7-
# Install `import`
8-
IMPORT_BIN="$IMPORT_CACHE/bin/import"
9-
echo "Installing \`import\` to \"$IMPORT_BIN\""
10-
mkdir -p "$(dirname "$IMPORT_BIN")"
11-
curl -sfLS "https://import.sh" > "$IMPORT_BIN"
12-
chmod +x "$IMPORT_BIN"
13-
echo "Done installing \`import\`"
14-
157
# Cache runtime and user dependencies
168
echo "Caching imports in \"$ENTRYPOINT\""
17-
. "$IMPORT_BIN"
9+
. "$BUILDER_DIST/import.sh"
1810
. "$WORK_PATH/$ENTRYPOINT"
1911
echo "Done caching imports"
2012

src/index.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const TMP = tmpdir();
2323
// `chmod()` is required for usage with `vercel-dev-runtime`
2424
// since file mode is not preserved in Vercel CLI deployments.
2525
fs.chmodSync(join(__dirname, 'build.sh'), 0o755);
26+
fs.chmodSync(join(__dirname, 'import.sh'), 0o755);
2627
fs.chmodSync(join(__dirname, 'bootstrap'), 0o755);
2728

2829
const bootstrapPromise = FileFsRef.fromFsPath({
@@ -31,6 +32,9 @@ const bootstrapPromise = FileFsRef.fromFsPath({
3132
const runtimePromise = FileFsRef.fromFsPath({
3233
fsPath: join(__dirname, 'runtime.sh'),
3334
});
35+
const importPromise = FileFsRef.fromFsPath({
36+
fsPath: join(__dirname, 'import.sh'),
37+
});
3438
const curlPromise = fetch(
3539
'https://github.com/dtschan/curl-static/releases/download/v7.63.0/curl'
3640
).then(async (res) => {
@@ -100,6 +104,7 @@ export const build: BuildV3 = async ({
100104
IMPORT_TRACE,
101105
WORK_PATH: workPath,
102106
ENTRYPOINT: entrypoint,
107+
BUILDER_DIST: __dirname,
103108
};
104109

105110
const buildDir = await getWriteableDirectory();
@@ -123,6 +128,7 @@ export const build: BuildV3 = async ({
123128
)),
124129
bootstrap: await bootstrapPromise,
125130
'.import-cache/runtime.sh': await runtimePromise,
131+
'.import-cache/bin/import': await importPromise,
126132
'.import-cache/bin/curl': await curlPromise,
127133
'.import-cache/bin/jq': await jqPromise,
128134
// For now only the entrypoint file is copied into the lambda
@@ -157,19 +163,24 @@ export const build: BuildV3 = async ({
157163
// - if symlink, then include if it points to a traced files
158164
// - if not symlink, then always include in output
159165
const binDir = join(importCacheDir, 'bin');
160-
const bins = await fs.readdir(binDir);
166+
let bins: string[] = [];
167+
try {
168+
bins = await fs.readdir(binDir);
169+
} catch (err: any) {
170+
if (err.code !== 'ENOENT') throw err;
171+
}
161172
for (const bin of bins) {
162173
const binPath = join(binDir, bin);
163174
const target = await fs.readlink(binPath).catch((err) => {
164-
if (err.code !== "EINVAL") throw err;
175+
if (err.code !== 'EINVAL') throw err;
165176
});
166177
if (target) {
167178
const rel = relative(importCacheDir, join(binDir, target));
168-
if (!lambdaFiles[join(".import-cache", rel)]) {
179+
if (!lambdaFiles[join('.import-cache', rel)]) {
169180
continue;
170181
}
171182
}
172-
lambdaFiles[join(".import-cache/bin", bin)] =
183+
lambdaFiles[join('.import-cache/bin', bin)] =
173184
await FileFsRef.fromFsPath({
174185
fsPath: binPath,
175186
});

0 commit comments

Comments
 (0)