1- import fs from 'node:fs' ;
21import path from 'node:path' ;
32import type { BuildOptions , BuildResultV3 } from '@vercel/build-utils' ;
43import {
54 FileFsRef ,
65 debug ,
76 download ,
87 glob ,
9- runShellScript ,
108 createLambda ,
119} from '@vercel/build-utils' ;
1210import execa from 'execa' ;
1311import { installRustToolchain } from './lib/rust-toolchain' ;
1412import type { Runtime } from './lib/runtime' ;
15- import { getCargoMetadata , findBinaryName , findCargoWorkspace } from './lib/cargo' ;
13+ import {
14+ getCargoMetadata ,
15+ findBinaryName ,
16+ findCargoWorkspace ,
17+ } from './lib/cargo' ;
18+ import {
19+ assertEnv ,
20+ getExecutableName ,
21+ gatherExtraFiles ,
22+ runUserScripts ,
23+ } from './lib/utils' ;
1624
1725type RustEnv = Record < 'RUSTFLAGS' | 'PATH' , string > ;
1826
19- function assertEnv ( name : string ) : string {
20- if ( ! process . env [ name ] ) {
21- throw new Error ( `Missing ENV variable process.env.${ name } ` ) ;
22- }
23-
24- return process . env [ name ] as unknown as string ;
25- }
26-
27- async function runUserScripts ( dir : string ) : Promise < void > {
28- const buildScriptPath = path . join ( dir , 'build.sh' ) ;
29- const buildScriptExists = fs . existsSync ( buildScriptPath ) ;
30-
31- if ( buildScriptExists ) {
32- debug ( 'Running `build.sh`' ) ;
33- await runShellScript ( buildScriptPath ) ;
34- }
35- }
36-
37- async function gatherExtraFiles (
38- globMatcher : string | string [ ] | undefined ,
39- workPath : string ,
40- ) : Promise < Record < string , FileFsRef > > {
41- if ( ! globMatcher ) return { } ;
42-
43- debug (
44- `Gathering extra files for glob \`${ JSON . stringify (
45- globMatcher ,
46- ) } \` in ${ workPath } `,
47- ) ;
48-
49- if ( Array . isArray ( globMatcher ) ) {
50- const allMatches = await Promise . all (
51- globMatcher . map ( ( pattern ) => glob ( pattern , workPath ) ) ,
52- ) ;
53-
54- return allMatches . reduce ( ( acc , matches ) => ( { ...acc , ...matches } ) , { } ) ;
55- }
56-
57- return glob ( globMatcher , workPath ) ;
58- }
59-
6027async function buildHandler ( options : BuildOptions ) : Promise < BuildResultV3 > {
6128 const BUILDER_DEBUG = Boolean ( process . env . VERCEL_BUILDER_DEBUG ?? false ) ;
6229 const { files, entrypoint, workPath, config, meta } = options ;
@@ -103,11 +70,7 @@ async function buildHandler(options: BuildOptions): Promise<BuildResultV3> {
10370 throw err ;
10471 }
10572
106- debug ( `Building \`${ binaryName } \` completed` ) ;
107-
108- // The compiled binary in Windows has the `.exe` extension
109- const binExtension = process . platform === 'win32' ? '.exe' : '' ;
110- const bootstrap = `bootstrap${ binExtension } ` ;
73+ debug ( `Building \`${ binaryName } \` for \`${ process . platform } \` completed` ) ;
11174
11275 const { target_directory : targetDirectory } = await getCargoMetadata ( {
11376 cwd : process . cwd ( ) ,
@@ -117,9 +80,10 @@ async function buildHandler(options: BuildOptions): Promise<BuildResultV3> {
11780 const bin = path . join (
11881 targetDirectory ,
11982 BUILDER_DEBUG ? 'debug' : 'release' ,
120- binaryName ,
83+ getExecutableName ( binaryName ) ,
12184 ) ;
12285
86+ const bootstrap = getExecutableName ( 'bootstrap' ) ;
12387 const lambda = await createLambda ( {
12488 files : {
12589 ...extraFiles ,
@@ -141,7 +105,6 @@ const runtime: Runtime = {
141105 prepareCache : async ( { workPath } ) => {
142106 debug ( `Caching \`${ workPath } \`` ) ;
143107 const cacheFiles = await glob ( 'target/**' , workPath ) ;
144-
145108 // Convert this into a reduce
146109 for ( const f of Object . keys ( cacheFiles ) ) {
147110 const accept =
@@ -155,7 +118,6 @@ const runtime: Runtime = {
155118 delete cacheFiles [ f ] ;
156119 }
157120 }
158-
159121 return cacheFiles ;
160122 } ,
161123 shouldServe : async ( options ) : Promise < boolean > => {
0 commit comments