Feat/scaffold job catalog file (#283)

* feat: adds a job catalog file when an integration is created via the CLI

* added a changeset

* fix: create-integration CLI throws an error when it tries to install dependencies

* Update getUserPkgManager.ts to main

---------

Co-authored-by: Matt Aitken <matt@mattaitken.com>
This commit is contained in:
Chigala
2023-08-09 10:42:20 +01:00
committed by GitHub
parent ab007f2708
commit 3ce6decef4
3 changed files with 71 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/cli": patch
---
creates a job catalog file when a new integration is created with the create-integration command
+62 -7
View File
@@ -9,6 +9,7 @@ import { generateIntegrationFiles } from "../utils/generateIntegrationFiles.js";
import { installDependencies } from "../utils/installDependencies.js";
import { logger } from "../utils/logger.js";
import { resolvePath } from "../utils/parseNameAndPath.js";
import { getPackageName } from "../utils/getPackagName.js";
const CLIOptionsSchema = z.object({
packageName: z.string().optional(),
@@ -196,13 +197,9 @@ export default defineConfig([
// If inside the monorepo:
if (triggerMonorepoPath) {
// Add the dependency to the nextjs-example package.json
// Add the paths to the nextjs-example tsconfig paths
await updateNextJsExampleProjectWithNewIntegration(
triggerMonorepoPath,
resolvedPath,
resolvedOptions
);
//adds the integration run script to the job-catalog/package.json
//adds the path to the integration to the job-catalog/tsconfig.json
await updateJobCatalogWithNewIntegration(triggerMonorepoPath, resolvedPath, resolvedOptions);
}
// Install the dependencies
@@ -531,3 +528,61 @@ async function updateNextJsExampleProjectWithNewIntegration(
await writeJSONFile(tsConfigPath, newTsConfig);
}
async function updateJobCatalogWithNewIntegration(
monorepoPath: string,
integrationPath: string,
resolvedOptions: ResolvedCLIOptions
) {
const packageName = getPackageName(resolvedOptions.packageName);
const jobCatalogPath = pathModule.join(monorepoPath, "examples", "job-catalog");
const jobCatalogSrcFolder = pathModule.join(jobCatalogPath, "src");
const integrationFile = `export {}`;
await createFileInPath(jobCatalogSrcFolder, `${packageName}.ts`, integrationFile);
const packageJsonPath = pathModule.join(jobCatalogPath, "package.json");
const packageJson = await readJSONFile(packageJsonPath);
const newPackageJson = {
...packageJson,
scripts: {
...packageJson.scripts,
[packageName]: `nodemon --watch src/${packageName}.ts -r tsconfig-paths/register -r dotenv/config src/${packageName}.ts`,
},
dependencies: {
...packageJson.dependencies,
[resolvedOptions.packageName]: `workspace:*`,
},
};
// Move "dev:trigger script" to the last position in the scripts object
if (newPackageJson.scripts.hasOwnProperty("dev:trigger")) {
const devTriggerScript = newPackageJson.scripts["dev:trigger"];
delete newPackageJson.scripts["dev:trigger"];
newPackageJson.scripts["dev:trigger"] = devTriggerScript;
}
await writeJSONFile(packageJsonPath, newPackageJson);
const tsConfigPath = pathModule.join(jobCatalogPath, "tsconfig.json");
const tsConfig = await readJSONFile(tsConfigPath);
const newTsConfig = {
...tsConfig,
compilerOptions: {
...tsConfig.compilerOptions,
paths: {
...tsConfig.compilerOptions.paths,
[resolvedOptions.packageName]: [
`../../integrations/${pathModule.basename(integrationPath)}/src/index`,
],
[`${resolvedOptions.packageName}/*`]: [
`../../integrations/${pathModule.basename(integrationPath)}/src/*`,
],
},
},
};
await writeJSONFile(tsConfigPath, newTsConfig);
}
+4
View File
@@ -0,0 +1,4 @@
export function getPackageName(inputString: string): string {
const packageName = inputString.split("/")[1];
return packageName ?? "";
}