1
1
#!/usr/bin/env node
2
2
3
3
import { esbuildDecorators } from "@anatine/esbuild-decorators" ;
4
- import { Command , Option } from "commander" ;
5
4
import { build } from "esbuild" ;
6
5
import { readFileSync } from "node:fs" ;
7
6
import { mkdir , writeFile } from "node:fs/promises" ;
8
- import { join , posix } from "node:path" ;
7
+ import { basename , join , posix , resolve } from "node:path" ;
9
8
import invariant from "tiny-invariant" ;
10
- import { z } from "zod" ;
11
- import { fromZodError } from "zod-validation-error" ;
12
9
13
10
import {
14
11
bundleDependenciesPlugin ,
15
12
mockServerOnlyPlugin ,
16
13
workerSetupImportConfigPlugin ,
17
14
} from "../src/utilities/build.js" ;
18
- import { readConfig } from "../src/utilities/configFiles.js" ;
15
+ import { ReadConfigResult } from "../src/utilities/configFiles.js" ;
19
16
import { writeJSONFile } from "../src/utilities/fileSystem.js" ;
20
17
import { logger } from "../src/utilities/logger.js" ;
21
- import { cliRootPath } from "../src/utilities/resolveInternalFilePath.js" ;
22
18
import { createTaskFileImports , gatherTaskFiles } from "../src/utilities/taskFiles.js" ;
23
- import { escapeImportPath , spinner } from "../src/utilities/windows.js" ;
24
-
25
- const CompileCommandOptionsSchema = z . object ( {
26
- logLevel : z . enum ( [ "debug" , "info" , "log" , "warn" , "error" , "none" ] ) . default ( "log" ) ,
27
- skipTypecheck : z . boolean ( ) . default ( false ) ,
28
- outputMetafile : z . string ( ) . optional ( ) ,
29
- } ) ;
30
-
31
- export type CompileCommandOptions = z . infer < typeof CompileCommandOptionsSchema > ;
32
-
33
- export function configureCompileCommand ( program : Command ) {
34
- program
35
- . command ( "deploy-compile" )
36
- . argument (
37
- "[dir]" ,
38
- "The project root directory. Usually where the top level package.json is located."
39
- )
40
- . option (
41
- "-l, --log-level <level>" ,
42
- "The CLI log level to use (debug, info, log, warn, error, none). This does not effect the log level of your trigger.dev tasks." ,
43
- "log"
44
- )
45
- . option ( "--skip-typecheck" , "Whether to skip the pre-build typecheck" )
46
- . addOption (
47
- new Option (
48
- "--output-metafile <path>" ,
49
- "If provided, will save the esbuild metafile for the build to the specified path"
50
- ) . hideHelp ( )
51
- )
52
- . action ( compile ) ;
53
- }
54
-
55
- async function compile ( dir : string , options : CompileCommandOptions ) {
56
- const parsedOptions = CompileCommandOptionsSchema . safeParse ( options ) ;
57
- if ( ! parsedOptions . success ) {
58
- throw new Error ( fromZodError ( parsedOptions . error ) . toString ( ) ) ;
59
- }
60
- logger . loggerLevel = parsedOptions . data . logLevel ;
19
+ import { escapeImportPath } from "../src/utilities/windows.js" ;
61
20
62
- const resolvedConfig = await readConfig ( dir ) ;
21
+ type CompileOptions = {
22
+ outputMetafile ?: string ;
23
+ resolvedConfig: ReadConfigResult ;
24
+ } ;
63
25
64
- if ( resolvedConfig . status === "error" ) {
65
- throw new Error ( `cannot resolve config in directory ${ dir } ` ) ;
26
+ export async function compile ( options : CompileOptions ) {
27
+ if ( options . resolvedConfig . status === "error" ) {
28
+ throw new Error ( "cannot resolve config" ) ;
66
29
}
67
-
68
- const { config } = resolvedConfig ;
69
- const configPath = resolvedConfig . status === "file" ? resolvedConfig . path : undefined ;
30
+ const { config } = options . resolvedConfig ;
31
+ const configPath =
32
+ options . resolvedConfig . status === "file" ? options . resolvedConfig . path : undefined ;
70
33
71
34
// COPIED FROM compileProject()
72
35
// const compileSpinner = spinner();
73
36
// compileSpinner.start(`Building project in ${config.projectDir}`);
74
37
75
38
const taskFiles = await gatherTaskFiles ( config ) ;
76
39
const workerFacade = readFileSync (
77
- join ( cliRootPath ( ) , "workers" , "prod" , "worker-facade.js" ) ,
40
+ resolve ( "./dist/workers/prod/worker-facade.js" ) ,
41
+ // join(cliRootPath(), "workers", "prod", "worker-facade.js"),
78
42
"utf-8"
79
43
) ;
80
44
81
- const workerSetupPath = join ( cliRootPath ( ) , "workers" , "prod" , "worker-setup.js" ) ;
45
+ // const workerSetupPath = join(cliRootPath(), "workers", "prod", "worker-setup.js");
46
+ const workerSetupPath = resolve ( "./dist/workers/prod/worker-setup.js" ) ;
82
47
83
48
let workerContents = workerFacade
84
49
. replace ( "__TASKS__" , createTaskFileImports ( taskFiles ) )
@@ -106,7 +71,8 @@ async function compile(dir: string, options: CompileCommandOptions) {
106
71
const result = await build ( {
107
72
stdin : {
108
73
contents : workerContents ,
109
- resolveDir : process . cwd ( ) ,
74
+ // resolveDir: process.cwd(),
75
+ resolveDir : config . projectDir ,
110
76
sourcefile : "__entryPoint.ts" ,
111
77
} ,
112
78
bundle : true ,
@@ -118,7 +84,8 @@ async function compile(dir: string, options: CompileCommandOptions) {
118
84
platform : "node" ,
119
85
format : "cjs" , // This is needed to support opentelemetry instrumentation that uses module patching
120
86
target : [ "node18" , "es2020" ] ,
121
- outdir : "out" ,
87
+ // outdir: "out",
88
+ outdir : resolve ( config . projectDir , "out" ) ,
122
89
banner : {
123
90
js : `process.on("uncaughtException", function(error, origin) { if (error instanceof Error) { process.send && process.send({ type: "EVENT", message: { type: "UNCAUGHT_EXCEPTION", payload: { error: { name: error.name, message: error.message, stack: error.stack }, origin }, version: "v1" } }); } else { process.send && process.send({ type: "EVENT", message: { type: "UNCAUGHT_EXCEPTION", payload: { error: { name: "Error", message: typeof error === "string" ? error : JSON.stringify(error) }, origin }, version: "v1" } }); } });` ,
124
91
} ,
@@ -155,14 +122,16 @@ async function compile(dir: string, options: CompileCommandOptions) {
155
122
}
156
123
157
124
const entryPointContents = readFileSync (
158
- join ( cliRootPath ( ) , "workers" , "prod" , "entry-point.js" ) ,
125
+ resolve ( "./dist/workers/prod/entry-point.js" ) ,
126
+ // join(cliRootPath(), "workers", "prod", "entry-point.js"),
159
127
"utf-8"
160
128
) ;
161
129
162
130
const entryPointResult = await build ( {
163
131
stdin : {
164
132
contents : entryPointContents ,
165
- resolveDir : process . cwd ( ) ,
133
+ // resolveDir: process.cwd(),
134
+ resolveDir : config . projectDir ,
166
135
sourcefile : "index.ts" ,
167
136
} ,
168
137
bundle : true ,
@@ -175,7 +144,8 @@ async function compile(dir: string, options: CompileCommandOptions) {
175
144
packages : "external" ,
176
145
format : "cjs" , // This is needed to support opentelemetry instrumentation that uses module patching
177
146
target : [ "node18" , "es2020" ] ,
178
- outdir : "out" ,
147
+ // outdir: "out",
148
+ outdir : resolve ( config . projectDir , "out" ) ,
179
149
define : {
180
150
__PROJECT_CONFIG__ : JSON . stringify ( config ) ,
181
151
} ,
@@ -210,12 +180,21 @@ async function compile(dir: string, options: CompileCommandOptions) {
210
180
logger . debug ( `Writing compiled files to ${ tempDir } ` ) ;
211
181
212
182
// Get the metaOutput for the result build
213
- const metaOutput = result . metafile ! . outputs [ posix . join ( "out" , "stdin.js" ) ] ;
183
+ // const metaOutput = result.metafile!.outputs[posix.join("out", "stdin.js")];
184
+ const metaOutput =
185
+ result . metafile ! . outputs [
186
+ posix . join ( "e2e" , "fixtures" , basename ( config . projectDir ) , "out" , "stdin.js" )
187
+ ] ;
214
188
215
189
invariant ( metaOutput , "Meta output for the result build is missing" ) ;
216
190
217
191
// Get the metaOutput for the entryPoint build
218
- const entryPointMetaOutput = entryPointResult . metafile ! . outputs [ posix . join ( "out" , "stdin.js" ) ] ;
192
+ // const entryPointMetaOutput =
193
+ // entryPointResult.metafile!.outputs[posix.join("out", "stdin.js")];
194
+ const entryPointMetaOutput =
195
+ entryPointResult . metafile ! . outputs [
196
+ posix . join ( "e2e" , "fixtures" , basename ( config . projectDir ) , "out" , "stdin.js" )
197
+ ] ;
219
198
220
199
invariant ( entryPointMetaOutput , "Meta output for the entryPoint build is missing" ) ;
221
200
@@ -249,4 +228,6 @@ async function compile(dir: string, options: CompileCommandOptions) {
249
228
await writeFile ( join ( tempDir ! , "worker.js.map" ) , workerSourcemapFile . text ) ;
250
229
// Save the entryPoint outputFile to /tmp/dir/index.js
251
230
await writeFile ( join ( tempDir ! , "index.js" ) , entryPointOutputFile . text ) ;
231
+
232
+ return { metaOutput, entryPointMetaOutput } ;
252
233
}
0 commit comments