@@ -9,7 +9,8 @@ import { generateIntegrationFiles } from "../utils/generateIntegrationFiles";
99import { getPackageName } from "../utils/getPackagName" ;
1010import { installDependencies } from "../utils/installDependencies" ;
1111import { logger } from "../utils/logger" ;
12- import { resolvePath } from "../utils/parseNameAndPath" ;
12+ import { relativePath , resolvePath } from "../utils/parseNameAndPath" ;
13+ import { createIntegrationFileFromTemplate } from "../utils/createIntegrationFileFromTemplate" ;
1314
1415const CLIOptionsSchema = z . object ( {
1516 packageName : z . string ( ) . optional ( ) ,
@@ -91,108 +92,107 @@ export async function createIntegrationCommand(path: string, cliOptions: any) {
9192 process . exit ( 1 ) ;
9293 }
9394
94- // Create the package.json
95- const packageJson = {
96- name : resolvedOptions . packageName ,
97- version : "0.0.1" ,
98- description : `Trigger.dev integration for ${ resolvedOptions . sdkPackage } ` ,
99- main : "./dist/index.js" ,
100- types : "./dist/index.d.ts" ,
101- publishConfig : {
102- access : "public" ,
103- } ,
104- files : [ "dist/index.js" , "dist/index.d.ts" , "dist/index.js.map" ] ,
105- devDependencies : {
106- "@types/node" : "16.x" ,
107- rimraf : "^3.0.2" ,
108- tsup : "7.1.x" ,
109- typescript : "4.9.4" ,
110- } ,
111- scripts : {
112- clean : "rimraf dist" ,
113- build : "npm run clean && npm run build:tsup" ,
114- "build:tsup" : "tsup" ,
115- typecheck : "tsc --noEmit" ,
116- } ,
117- dependencies : {
118- [ latestVersion . name ] : `^${ latestVersion . version } ` ,
119- [ sdkVersion . name ] : sdkVersion . version ,
120- [ integrationKitVersion . name ] : integrationKitVersion . version ,
121- } ,
122- engines : {
123- node : ">=16.8.0" ,
124- } ,
125- } ;
95+ const integrationVersion = await getInternalOrExternalPackageVersion ( {
96+ path : "integrations/github" ,
97+ packageName : "@trigger.dev/github" ,
98+ tag : "latest" ,
99+ monorepoPath : triggerMonorepoPath ,
100+ prependWorkspace : false ,
101+ } ) ;
102+
103+ if ( ! integrationVersion ) {
104+ logger . error (
105+ `Could not find the latest version of @trigger.dev/github. Please try again later.`
106+ ) ;
126107
127- await createFileInPath ( resolvedPath , "package.json" , JSON . stringify ( packageJson , null , 2 ) ) ;
108+ process . exit ( 1 ) ;
109+ }
128110
129- // Create the tsconfig.json
130- const tsconfigJson = {
131- compilerOptions : {
132- composite : false ,
133- declaration : false ,
134- declarationMap : false ,
135- esModuleInterop : true ,
136- forceConsistentCasingInFileNames : true ,
137- inlineSources : false ,
138- isolatedModules : true ,
139- moduleResolution : "node16" ,
140- noUnusedLocals : false ,
141- noUnusedParameters : false ,
142- preserveWatchOutput : true ,
143- skipLibCheck : true ,
144- strict : true ,
145- experimentalDecorators : true ,
146- emitDecoratorMetadata : true ,
147- sourceMap : true ,
148- resolveJsonModule : true ,
149- lib : [ "es2019" ] ,
150- module : "commonjs" ,
151- target : "es2021" ,
152- } ,
153- include : [ "./src/**/*.ts" , "tsup.config.ts" ] ,
154- exclude : [ "node_modules" ] ,
111+ const baseVariables = {
112+ packageName : resolvedOptions . packageName ,
113+ sdkPackage : resolvedOptions . sdkPackage ,
114+ integrationVersion : integrationVersion ,
115+ latestVersion : latestVersion ,
116+ sdkVersion : sdkVersion ,
117+ integrationKitVersion : integrationKitVersion ,
118+ triggerMonorepoPath,
155119 } ;
156120
157- await createFileInPath ( resolvedPath , "tsconfig.json" , JSON . stringify ( tsconfigJson , null , 2 ) ) ;
158-
159- const readme = `
160- # ${ resolvedOptions . packageName }
161- ` ;
162-
163- await createFileInPath ( resolvedPath , "README.md" , readme ) ;
164-
165- // Create the tsup.config.ts
166- const tsupConfig = `
167- import { defineConfig } from "tsup";
168-
169- export default defineConfig([
170- {
171- name: "main",
172- entry: ["./src/index.ts"],
173- outDir: "./dist",
174- platform: "node",
175- format: ["cjs"],
176- legacyOutput: true,
177- sourcemap: true,
178- clean: true,
179- bundle: true,
180- splitting: false,
181- dts: true,
182- treeshake: {
183- preset: "smallest",
184- },
185- esbuildPlugins: [],
186- external: ["http", "https", "util", "events", "tty", "os", "timers"],
187- },
188- ]);
121+ const getOutputPath = ( relativePath : string ) => pathModule . join ( resolvedPath , relativePath ) ;
189122
190- ` ;
123+ const miscIntegrationFiles = [
124+ {
125+ relativeTemplatePath : "package.json.j2" ,
126+ outputPath : getOutputPath ( "package.json" ) ,
127+ } ,
128+ {
129+ // use `tsc --showConfig` to update external tsconfig
130+ relativeTemplatePath : `tsconfig-${ triggerMonorepoPath ? "internal" : "external" } .json.j2` ,
131+ outputPath : getOutputPath ( "tsconfig.json" ) ,
132+ } ,
133+ {
134+ relativeTemplatePath : `tsup.config-${ triggerMonorepoPath ? "internal" : "external" } .js.j2` ,
135+ outputPath : getOutputPath ( "tsup.config.ts" ) ,
136+ } ,
137+ {
138+ relativeTemplatePath : "README.md.j2" ,
139+ outputPath : getOutputPath ( "README.md" ) ,
140+ } ,
141+ ] ;
191142
192- await createFileInPath ( resolvedPath , "tsup.config.ts" , tsupConfig ) ;
143+ await createIntegrationFiles ( miscIntegrationFiles , baseVariables ) ;
193144
145+ // create src/*
194146 if ( resolvedOptions . skipGeneratingCode ) {
195- await createFileInPath ( resolvedPath , "src/index.ts" , "export {}" ) ;
147+ const getSrcOutputPath = ( relativePath : string ) =>
148+ getOutputPath ( pathModule . join ( "src" , relativePath ) ) ;
149+
150+ const srcIntegrationFiles = [
151+ {
152+ relativeTemplatePath : pathModule . join ( "payload-examples" , "index.js.j2" ) ,
153+ outputPath : getSrcOutputPath ( pathModule . join ( "payload-examples" , "index.ts" ) ) ,
154+ } ,
155+ {
156+ relativeTemplatePath : "events.js.j2" ,
157+ outputPath : getSrcOutputPath ( "events.ts" ) ,
158+ } ,
159+ {
160+ relativeTemplatePath : "index.js.j2" ,
161+ outputPath : getSrcOutputPath ( "index.ts" ) ,
162+ } ,
163+ {
164+ relativeTemplatePath : "models.js.j2" ,
165+ outputPath : getSrcOutputPath ( "models.ts" ) ,
166+ } ,
167+ {
168+ relativeTemplatePath : "schemas.js.j2" ,
169+ outputPath : getSrcOutputPath ( "schemas.ts" ) ,
170+ } ,
171+ {
172+ relativeTemplatePath : "types.js.j2" ,
173+ outputPath : getSrcOutputPath ( "types.ts" ) ,
174+ } ,
175+ {
176+ relativeTemplatePath : "utils.js.j2" ,
177+ outputPath : getSrcOutputPath ( "utils.ts" ) ,
178+ } ,
179+ {
180+ relativeTemplatePath : "webhooks.js.j2" ,
181+ outputPath : getSrcOutputPath ( "webhooks.ts" ) ,
182+ } ,
183+ ] ;
184+
185+ const validIdentifier = pathModule
186+ . basename ( path )
187+ . replace ( / [ ^ a - z A - Z 0 - 9 ] + / g, "" )
188+ . replace ( / ^ [ 0 - 9 ] + / g, "" ) ;
189+
190+ await createIntegrationFiles ( srcIntegrationFiles , {
191+ ...baseVariables ,
192+ apiKeyPropertyName : "apiKey" , // TODO: prompt for this
193+ authMethod : resolvedOptions . authMethod ,
194+ identifier : validIdentifier . length ? validIdentifier : "packageName" ,
195+ } ) ;
196196 } else {
197197 await attemptToGenerateIntegrationFiles ( pathModule . join ( resolvedPath , "src" ) , resolvedOptions ) ;
198198 }
@@ -295,8 +295,9 @@ const resolveOptionsWithPrompts = async (
295295 resolvedOptions . skipGeneratingCode = true ;
296296 }
297297
298+ resolvedOptions . authMethod = await promptAuthMethod ( ) ;
299+
298300 if ( ! resolvedOptions . skipGeneratingCode ) {
299- resolvedOptions . authMethod = await promptAuthMethod ( ) ;
300301 resolvedOptions . extraInfo = await promptExtraInfo ( ) ;
301302 }
302303 } catch ( err ) {
@@ -448,11 +449,13 @@ async function getInternalOrExternalPackageVersion({
448449 tag,
449450 path,
450451 monorepoPath,
452+ prependWorkspace = true ,
451453} : {
452454 packageName : string ;
453455 tag : string ;
454456 path : string ;
455457 monorepoPath ?: string ;
458+ prependWorkspace ?: boolean ;
456459} ) : Promise < { name : string ; version : string } | undefined > {
457460 if ( ! monorepoPath ) {
458461 return await getLatestPackageVersion ( packageName , tag ) ;
@@ -470,7 +473,7 @@ async function getInternalOrExternalPackageVersion({
470473
471474 return {
472475 name : packageJson . name ,
473- version : `workspace:^${ packageJson . version } ` ,
476+ version : `${ prependWorkspace ? " workspace:^" : "" } ${ packageJson . version } ` ,
474477 } ;
475478}
476479
@@ -550,3 +553,26 @@ async function updateJobCatalogWithNewIntegration(
550553 } ;
551554 await writeJSONFile ( tsConfigPath , newTsConfig ) ;
552555}
556+
557+ const createIntegrationFiles = async (
558+ files : {
559+ relativeTemplatePath : string ;
560+ outputPath : string ;
561+ } [ ] ,
562+ variables ?: Record < string , any >
563+ ) => {
564+ for ( const file of files ) {
565+ const result = await createIntegrationFileFromTemplate ( { ...file , variables } ) ;
566+ handleCreateResult ( file . outputPath , result ) ;
567+ }
568+ } ;
569+
570+ const handleCreateResult = (
571+ outputPath : string ,
572+ result : Awaited < ReturnType < typeof createIntegrationFileFromTemplate > >
573+ ) => {
574+ if ( ! result . success ) {
575+ throw new Error ( `Failed to create ${ pathModule . basename ( outputPath ) } : ${ result . error } ` ) ;
576+ }
577+ logger . success ( `✔ Created ${ pathModule . basename ( outputPath ) } at ${ relativePath ( outputPath ) } ` ) ;
578+ } ;
0 commit comments