Skip to content

Commit

Permalink
feat; implement branch change prompt
Browse files Browse the repository at this point in the history
Signed-off-by: Hunter Achieng ([email protected])

Signed-off-by: Hunter Achieng <[email protected]>
  • Loading branch information
hunterachieng authored and mtuchi committed Mar 19, 2024
1 parent 5108c25 commit c5da855
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 11 deletions.
136 changes: 125 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tools/generate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@openfn/simple-ast": "0.4.1",
"@types/node": "18.17.5",
"esno": "0.16.3",
"inquirer": "^9.2.15",
"ts-node": "10.9.1",
"tsup": "6.3.0",
"typescript": "4.8.4",
Expand Down
2 changes: 2 additions & 0 deletions tools/generate/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import generateAdaptor from './generate-adaptor';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import process from 'node:process';
import promptForBranchChange from './prompt-branch-change';

const generate = async (args: any) => {
await promptForBranchChange(args);
await generateAdaptor(args);
};

Expand Down
39 changes: 39 additions & 0 deletions tools/generate/src/prompt-branch-change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { execSync } from 'node:child_process';
import inquirer from 'inquirer';

const checkIfBranchExists = (branchName: string): boolean => {
try {
execSync(`git rev-parse --verify ${branchName}`);
return true;
} catch (error) {
return false;
}
};

const promptForBranchChange = async (adaptorName: string) => {
const currentBranch = execSync('git branch --show-current').toString().trim();

if (currentBranch === 'main') {
const branchExists = checkIfBranchExists(adaptorName);

if (branchExists) {
console.log(`Branch "${adaptorName}" already exists.`);
} else {
const answers = await inquirer.prompt([
{
type: 'confirm',
name: 'createBranch',
message: `You are on the main branch. Would you like to create and switch to a new branch named "${adaptorName}"?`,
default: true,
},
]);

if (answers.createBranch) {
execSync(`git checkout -b ${adaptorName}`);
console.log(`Switched to new branch: ${adaptorName}`);
}
}
}
};

export default promptForBranchChange;

0 comments on commit c5da855

Please sign in to comment.