-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: command to help generate integration patches
- Loading branch information
1 parent
78949fe
commit 111a365
Showing
3 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { Command } from '@commander-js/extra-typings'; | ||
import { exec as execCb } from 'child_process'; | ||
import { outputFileSync, writeJsonSync } from 'fs-extra/esm'; | ||
import { promisify } from 'util'; | ||
import * as z from 'zod'; | ||
|
||
const exec = promisify(execCb); | ||
|
||
interface Manifest { | ||
name: string; | ||
dependencies: string[]; | ||
devDependencies: string[]; | ||
environmentVariables: string[]; | ||
} | ||
|
||
export const integration = new Command('integration') | ||
.requiredOption('--integration-name <name>', 'Formatted name of the integration') | ||
.requiredOption('--source-branch <branch>', 'The branch containing your integration source code') | ||
.action(async (options) => { | ||
const manifest: Manifest = { | ||
name: options.integrationName, | ||
dependencies: [], | ||
devDependencies: [], | ||
environmentVariables: [], | ||
}; | ||
|
||
const { stdout: gitBranchStdOut } = await exec('git branch --format="%(refname:short)"'); | ||
const localBranches = gitBranchStdOut.split('\n').filter(Boolean); | ||
|
||
if (!localBranches.includes(options.sourceBranch)) { | ||
console.error(`Branch "${options.sourceBranch}" does not exist in your local repository.`); | ||
process.exit(1); | ||
} | ||
|
||
const { stdout: packagesDiffStdOut } = await exec( | ||
`git diff main...${options.sourceBranch} -- core/package.json`, | ||
); | ||
|
||
if (packagesDiffStdOut.length > 0) { | ||
const packages: string[] = []; | ||
const lines = packagesDiffStdOut.split('\n'); | ||
const packagePattern = /^\+ {4}"([^"]+)":/; | ||
|
||
lines.forEach((line) => { | ||
const match = line.match(packagePattern); | ||
|
||
if (match) { | ||
packages.push(match[1]); | ||
} | ||
}); | ||
|
||
if (packages.length > 0) { | ||
const { stdout: integrationPackageJsonRaw } = await exec( | ||
`git show ${options.sourceBranch}:core/package.json`, | ||
); | ||
|
||
const integrationPackageJson = z | ||
.object({ | ||
dependencies: z.object({}).passthrough(), | ||
devDependencies: z.object({}).passthrough(), | ||
}) | ||
.parse(JSON.parse(integrationPackageJsonRaw)); | ||
|
||
manifest.dependencies = packages.filter((pkg) => integrationPackageJson.dependencies[pkg]); | ||
manifest.devDependencies = packages.filter( | ||
(pkg) => integrationPackageJson.devDependencies[pkg], | ||
); | ||
} | ||
} | ||
|
||
const { stdout: envVarDiff } = await exec( | ||
`git diff main...${options.sourceBranch} -- core/.env.example`, | ||
); | ||
|
||
if (envVarDiff.length > 0) { | ||
const envVars: string[] = []; | ||
const lines = envVarDiff.split('\n'); | ||
const envPattern = /^\+([A-Z_]+)=/; | ||
|
||
lines.forEach((line) => { | ||
const match = line.match(envPattern); | ||
|
||
if (match) { | ||
envVars.push(match[1]); | ||
} | ||
}); | ||
|
||
if (envVars.length > 0) { | ||
manifest.environmentVariables = envVars; | ||
} | ||
} | ||
|
||
const integrationNameNormalized = options.integrationName.toLowerCase().replace(/\s/g, '-'); | ||
|
||
const { stdout: integrationDiff } = await exec( | ||
`git diff main...${options.sourceBranch} -- ':(exclude)core/package.json' ':(exclude)pnpm-lock.yaml'`, | ||
); | ||
|
||
outputFileSync(`integrations/${integrationNameNormalized}/integration.patch`, integrationDiff); | ||
writeJsonSync(`integrations/${integrationNameNormalized}/manifest.json`, manifest, { | ||
spaces: 2, | ||
}); | ||
|
||
console.log('Integration created successfully.'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters