8
8
import chalk from "chalk" ;
9
9
import { Command } from "commander" ;
10
10
import { execa } from "execa" ;
11
+ import { applyEdits , modify } from "jsonc-parser" ;
12
+ import { writeFile } from "node:fs/promises" ;
11
13
import { join , relative , resolve } from "node:path" ;
12
14
import terminalLink from "terminal-link" ;
13
15
import { z } from "zod" ;
@@ -24,7 +26,7 @@ import {
24
26
} from "../cli/common.js" ;
25
27
import { readConfig } from "../utilities/configFiles.js" ;
26
28
import { createFileFromTemplate } from "../utilities/createFileFromTemplate" ;
27
- import { createFile , pathExists } from "../utilities/fileSystem" ;
29
+ import { createFile , pathExists , readFile } from "../utilities/fileSystem" ;
28
30
import { getUserPackageManager } from "../utilities/getUserPackageManager" ;
29
31
import { printStandloneInitialBanner } from "../utilities/initialBanner.js" ;
30
32
import { logger } from "../utilities/logger" ;
@@ -80,7 +82,11 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
80
82
81
83
intro ( "Initializing project" ) ;
82
84
83
- const authorization = await login ( { embedded : true , defaultApiUrl : options . apiUrl , profile : options . profile } ) ;
85
+ const authorization = await login ( {
86
+ embedded : true ,
87
+ defaultApiUrl : options . apiUrl ,
88
+ profile : options . profile ,
89
+ } ) ;
84
90
85
91
if ( ! authorization . ok ) {
86
92
if ( authorization . error === "fetch failed" ) {
@@ -145,6 +151,12 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
145
151
// Create the trigger dir
146
152
await createTriggerDir ( dir , options ) ;
147
153
154
+ // Add trigger.config.ts to tsconfig.json
155
+ await addConfigFileToTsConfig ( dir , options ) ;
156
+
157
+ // Ignore .trigger dir
158
+ await gitIgnoreDotTriggerDir ( dir , options ) ;
159
+
148
160
const projectDashboard = terminalLink (
149
161
"project dashboard" ,
150
162
`${ authorization . dashboardUrl } /projects/v3/${ selectedProject . externalRef } `
@@ -255,6 +267,101 @@ async function createTriggerDir(dir: string, options: InitCommandOptions) {
255
267
} ) ;
256
268
}
257
269
270
+ async function gitIgnoreDotTriggerDir ( dir : string , options : InitCommandOptions ) {
271
+ return await tracer . startActiveSpan ( "gitIgnoreDotTriggerDir" , async ( span ) => {
272
+ try {
273
+ const projectDir = resolve ( process . cwd ( ) , dir ) ;
274
+ const gitIgnorePath = join ( projectDir , ".gitignore" ) ;
275
+
276
+ span . setAttributes ( {
277
+ "cli.projectDir" : projectDir ,
278
+ "cli.gitIgnorePath" : gitIgnorePath ,
279
+ } ) ;
280
+
281
+ if ( ! ( await pathExists ( gitIgnorePath ) ) ) {
282
+ // Create .gitignore file
283
+ await createFile ( gitIgnorePath , ".trigger" ) ;
284
+
285
+ log . step ( `Added .trigger to .gitignore` ) ;
286
+
287
+ span . end ( ) ;
288
+
289
+ return ;
290
+ }
291
+
292
+ // Check if .gitignore already contains .trigger
293
+ const gitIgnoreContent = await readFile ( gitIgnorePath ) ;
294
+
295
+ if ( gitIgnoreContent . includes ( ".trigger" ) ) {
296
+ span . end ( ) ;
297
+
298
+ return ;
299
+ }
300
+
301
+ const newGitIgnoreContent = `${ gitIgnoreContent } \n.trigger` ;
302
+
303
+ await writeFile ( gitIgnorePath , newGitIgnoreContent , "utf-8" ) ;
304
+
305
+ log . step ( `Added .trigger to .gitignore` ) ;
306
+
307
+ span . end ( ) ;
308
+ } catch ( e ) {
309
+ if ( ! ( e instanceof SkipCommandError ) ) {
310
+ recordSpanException ( span , e ) ;
311
+ }
312
+
313
+ span . end ( ) ;
314
+
315
+ throw e ;
316
+ }
317
+ } ) ;
318
+ }
319
+
320
+ async function addConfigFileToTsConfig ( dir : string , options : InitCommandOptions ) {
321
+ return await tracer . startActiveSpan ( "createTriggerDir" , async ( span ) => {
322
+ try {
323
+ const projectDir = resolve ( process . cwd ( ) , dir ) ;
324
+ const tsconfigPath = join ( projectDir , "tsconfig.json" ) ;
325
+
326
+ span . setAttributes ( {
327
+ "cli.projectDir" : projectDir ,
328
+ "cli.tsconfigPath" : tsconfigPath ,
329
+ } ) ;
330
+
331
+ const tsconfigContent = await readFile ( tsconfigPath ) ;
332
+
333
+ const edits = modify ( tsconfigContent , [ "include" , - 1 ] , "trigger.config.ts" , {
334
+ isArrayInsertion : true ,
335
+ formattingOptions : {
336
+ tabSize : 2 ,
337
+ insertSpaces : true ,
338
+ eol : "\n" ,
339
+ } ,
340
+ } ) ;
341
+
342
+ logger . debug ( "tsconfig.json edits" , { edits } ) ;
343
+
344
+ const newTsconfigContent = applyEdits ( tsconfigContent , edits ) ;
345
+
346
+ logger . debug ( "new tsconfig.json content" , { newTsconfigContent } ) ;
347
+
348
+ await writeFile ( tsconfigPath , newTsconfigContent , "utf-8" ) ;
349
+
350
+ log . step ( `Added trigger.config.ts to tsconfig.json` ) ;
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
+
258
365
async function installPackages ( dir : string , options : InitCommandOptions ) {
259
366
return await tracer . startActiveSpan ( "installPackages" , async ( span ) => {
260
367
const installSpinner = spinner ( ) ;
@@ -332,8 +439,8 @@ async function writeConfigFile(
332
439
spnnr . start ( "Creating config file" ) ;
333
440
334
441
const projectDir = resolve ( process . cwd ( ) , dir ) ;
335
- const templatePath = resolveInternalFilePath ( "./templates/trigger.config.mjs " ) ;
336
- const outputPath = join ( projectDir , "trigger.config.mjs " ) ;
442
+ const templatePath = resolveInternalFilePath ( "./templates/trigger.config.ts " ) ;
443
+ const outputPath = join ( projectDir , "trigger.config.ts " ) ;
337
444
338
445
span . setAttributes ( {
339
446
"cli.projectDir" : projectDir ,
0 commit comments