@@ -23,13 +23,17 @@ import {
23
23
import { loadConfig } from "../config.js" ;
24
24
import { CLOUD_API_URL } from "../consts.js" ;
25
25
import { cliLink , prettyError } from "../utilities/cliOutput.js" ;
26
- import { createFileFromTemplate } from "../utilities/createFileFromTemplate.js" ;
26
+ import {
27
+ createFileFromTemplate ,
28
+ generateTemplateUrl ,
29
+ } from "../utilities/createFileFromTemplate.js" ;
27
30
import { createFile , pathExists , readFile } from "../utilities/fileSystem.js" ;
28
31
import { printStandloneInitialBanner } from "../utilities/initialBanner.js" ;
29
32
import { logger } from "../utilities/logger.js" ;
30
33
import { cliRootPath } from "../utilities/resolveInternalFilePath.js" ;
31
34
import { spinner } from "../utilities/windows.js" ;
32
35
import { login } from "./login.js" ;
36
+ import { resolveTSConfig } from "pkg-types" ;
33
37
34
38
const InitCommandOptions = CommonCommandOptions . extend ( {
35
39
projectRef : z . string ( ) . optional ( ) ,
@@ -54,7 +58,7 @@ export function configureInitCommand(program: Command) {
54
58
. option (
55
59
"-t, --tag <package tag>" ,
56
60
"The version of the @trigger.dev/sdk package to install" ,
57
- "latest "
61
+ "beta "
58
62
)
59
63
. option ( "--skip-package-install" , "Skip installing the @trigger.dev/sdk package" )
60
64
. option ( "--override-config" , "Override the existing config file if it exists" )
@@ -81,8 +85,7 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
81
85
82
86
intro ( "Initializing project" ) ;
83
87
84
- // Detect tsconfig.json and exit if not found
85
- await detectTsConfig ( dir , options ) ;
88
+ const cwd = resolve ( process . cwd ( ) , dir ) ;
86
89
87
90
const authorization = await login ( {
88
91
embedded : true ,
@@ -107,18 +110,22 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
107
110
"cli.config.profile" : authorization . profile ,
108
111
} ) ;
109
112
113
+ const tsconfigPath = await tryResolveTsConfig ( cwd ) ;
114
+
110
115
if ( ! options . overrideConfig ) {
111
116
try {
112
117
// check to see if there is an existing trigger.dev config file in the project directory
113
- const result = await loadConfig ( { cwd : dir } ) ;
118
+ const result = await loadConfig ( { cwd } ) ;
114
119
115
- outro (
116
- result . configFile
117
- ? `Project already initialized: Found config file at ${ result . configFile } . Pass --override-config to override`
118
- : "Project already initialized"
119
- ) ;
120
+ if ( result . configFile && result . configFile !== "trigger.config" ) {
121
+ outro (
122
+ result . configFile
123
+ ? `Project already initialized: Found config file at ${ result . configFile } . Pass --override-config to override`
124
+ : "Project already initialized"
125
+ ) ;
120
126
121
- return ;
127
+ return ;
128
+ }
122
129
} catch ( e ) {
123
130
// continue
124
131
}
@@ -154,7 +161,9 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
154
161
await writeConfigFile ( dir , selectedProject , options , triggerDir ) ;
155
162
156
163
// Add trigger.config.ts to tsconfig.json
157
- await addConfigFileToTsConfig ( dir , options ) ;
164
+ if ( tsconfigPath ) {
165
+ await addConfigFileToTsConfig ( tsconfigPath , options ) ;
166
+ }
158
167
159
168
// Ignore .trigger dir
160
169
await gitIgnoreDotTriggerDir ( dir , options ) ;
@@ -175,7 +184,7 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
175
184
) ;
176
185
log . info ( ` 2. Visit your ${ projectDashboard } to view your newly created tasks.` ) ;
177
186
log . info (
178
- ` 3. Head over to our ${ cliLink ( "v3 docs" , "https://trigger.dev/docs/v3 " ) } to learn more.`
187
+ ` 3. Head over to our ${ cliLink ( "v3 docs" , "https://trigger.dev/docs" ) } to learn more.`
179
188
) ;
180
189
log . info (
181
190
` 4. Need help? Join our ${ cliLink (
@@ -246,11 +255,11 @@ async function createTriggerDir(dir: string, options: InitCommandOptions) {
246
255
return { location, isCustomValue : location !== defaultValue } ;
247
256
}
248
257
249
- const templatePath = join ( cliRootPath ( ) , "templates" , "examples" , ` ${ example } .ts.template `) ;
258
+ const templateUrl = generateTemplateUrl ( `examples/ ${ example } .ts`) ;
250
259
const outputPath = join ( triggerDir , "example.ts" ) ;
251
260
252
261
await createFileFromTemplate ( {
253
- templatePath ,
262
+ templateUrl ,
254
263
outputPath,
255
264
replacements : { } ,
256
265
} ) ;
@@ -324,52 +333,10 @@ async function gitIgnoreDotTriggerDir(dir: string, options: InitCommandOptions)
324
333
} ) ;
325
334
}
326
335
327
- async function detectTsConfig ( dir : string , options : InitCommandOptions ) {
328
- return await tracer . startActiveSpan ( "detectTsConfig" , async ( span ) => {
329
- try {
330
- const projectDir = resolve ( process . cwd ( ) , dir ) ;
331
- const tsconfigPath = join ( projectDir , "tsconfig.json" ) ;
332
-
333
- span . setAttributes ( {
334
- "cli.projectDir" : projectDir ,
335
- "cli.tsconfigPath" : tsconfigPath ,
336
- } ) ;
337
-
338
- const tsconfigExists = await pathExists ( tsconfigPath ) ;
339
-
340
- if ( ! tsconfigExists ) {
341
- prettyError (
342
- "No tsconfig.json found" ,
343
- `The init command needs to be run in a TypeScript project. You can create one like this:` ,
344
- `npm install typescript --save-dev\nnpx tsc --init\n`
345
- ) ;
346
-
347
- throw new Error ( "TypeScript required" ) ;
348
- }
349
-
350
- logger . debug ( "tsconfig.json exists" , { tsconfigPath } ) ;
351
-
352
- span . end ( ) ;
353
- } catch ( e ) {
354
- if ( ! ( e instanceof SkipCommandError ) ) {
355
- recordSpanException ( span , e ) ;
356
- }
357
-
358
- span . end ( ) ;
359
-
360
- throw e ;
361
- }
362
- } ) ;
363
- }
364
-
365
- async function addConfigFileToTsConfig ( dir : string , options : InitCommandOptions ) {
336
+ async function addConfigFileToTsConfig ( tsconfigPath : string , options : InitCommandOptions ) {
366
337
return await tracer . startActiveSpan ( "addConfigFileToTsConfig" , async ( span ) => {
367
338
try {
368
- const projectDir = resolve ( process . cwd ( ) , dir ) ;
369
- const tsconfigPath = join ( projectDir , "tsconfig.json" ) ;
370
-
371
339
span . setAttributes ( {
372
- "cli.projectDir" : projectDir ,
373
340
"cli.tsconfigPath" : tsconfigPath ,
374
341
} ) ;
375
342
@@ -481,21 +448,21 @@ async function writeConfigFile(
481
448
spnnr . start ( "Creating config file" ) ;
482
449
483
450
const projectDir = resolve ( process . cwd ( ) , dir ) ;
484
- const templatePath = join ( cliRootPath ( ) , "templates" , "trigger.config.ts.template" ) ;
485
451
const outputPath = join ( projectDir , "trigger.config.ts" ) ;
452
+ const templateUrl = generateTemplateUrl ( "trigger.config.ts" ) ;
486
453
487
454
span . setAttributes ( {
488
455
"cli.projectDir" : projectDir ,
489
- "cli.templatePath" : templatePath ,
456
+ "cli.templatePath" : templateUrl ,
490
457
"cli.outputPath" : outputPath ,
491
458
} ) ;
492
459
493
460
const result = await createFileFromTemplate ( {
494
- templatePath ,
461
+ templateUrl ,
495
462
replacements : {
496
463
projectRef : project . externalRef ,
497
464
triggerDirectoriesOption : triggerDir . isCustomValue
498
- ? `\n triggerDirectories : ["${ triggerDir . location } "],`
465
+ ? `\n dirs : ["${ triggerDir . location } "],`
499
466
: "" ,
500
467
} ,
501
468
outputPath,
@@ -608,3 +575,12 @@ async function selectProject(apiClient: CliApiClient, dashboardUrl: string, proj
608
575
}
609
576
} ) ;
610
577
}
578
+
579
+ async function tryResolveTsConfig ( cwd : string ) {
580
+ try {
581
+ const tsconfigPath = await resolveTSConfig ( cwd ) ;
582
+ return tsconfigPath ;
583
+ } catch ( e ) {
584
+ return ;
585
+ }
586
+ }
0 commit comments