Skip to content

Commit

Permalink
Use Git Command
Browse files Browse the repository at this point in the history
  • Loading branch information
pshaddel committed Feb 19, 2023
1 parent f5b4c08 commit b245c83
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "conventional-branch",
"displayName": "Conventional Branch",
"description": "Customizable Conventional Branch for VSCode",
"version": "0.0.1",
"version": "0.0.2",
"publisher": "pshaddel",
"engines": {
"vscode": "^1.75.0"
Expand Down
28 changes: 25 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function activate(context: vscode.ExtensionContext) {
const {
format,
maxBranchNameLength,
minBranchNameLength,
separator,
forceBranchNameLowerCase,
removeBranchNameWhiteSpace,
Expand All @@ -31,7 +32,11 @@ export function activate(context: vscode.ExtensionContext) {
values.push(value);
}
} else if (field === "Branch") {
let value = await fetchText("Branch Name", maxBranchNameLength);
let value = await fetchText(
"Branch Name",
maxBranchNameLength,
minBranchNameLength
);
if (forceBranchNameLowerCase && value) {
value = value.toLowerCase();
}
Expand Down Expand Up @@ -62,7 +67,15 @@ export function activate(context: vscode.ExtensionContext) {
branch = branch.replace(/}/g, "");

// show the branch name
vscode.window.showInformationMessage(branch);
try {
await runGitCommand(`git checkout -b ${branch}`);
// vscode.window.showInformationMessage(`Branch ${branch} created`, {
// });
} catch (error) {
vscode.window.showInformationMessage(
`We want to create this branch: ${branch} but we got this error: ${error}`
);
}
}
);

Expand All @@ -81,10 +94,19 @@ async function fetchType() {
return type;
}

async function fetchText(field: string, maxLength?: number) {
async function fetchText(
field: string,
maxLength?: number,
minLength?: number
) {
return vscode.window.showInputBox({
placeHolder: `Enter a ${field}`,
validateInput: (value) => {
if (minLength) {
if (!value || value.length < minLength) {
return `${field} must be at least ${minLength} characters`;
}
}
if (!maxLength) {
return;
}
Expand Down
16 changes: 15 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ const SETTING_DEFAULT_FORMAT = "{Type}/{TicketNumber}/{Branch}";
// const SETTING_DEFAULT_FORMAT = "{AUTHOR}/{Type}/JIRA-{TicketNumber}/{Branch}";
const SETTING_DEFAULT_FORCE_BRANCH_NAME_LOWER_CASE = true;
const SETTING_DEFAULT_REMOVE_BRANCH_NAME_WHITE_SPACE = true;

const SETTING_DEFAULT_MIN_BRANCH_NAME_LENGTH = 2;
interface Settings {
format: string;
maxBranchNameLength: number;
separator: string;
forceBranchNameLowerCase: boolean;
removeBranchNameWhiteSpace: boolean;
minBranchNameLength: number;
}
export async function fetchSettings(): Promise<Settings> {
const config = vscode.workspace.getConfiguration("conventional-branch");
let format: string | undefined = config.get("format");
let minBranchNameLength: number | undefined = config.get(
"minBranchNameLength"
);
let maxBranchNameLength: number | undefined = config.get(
"maxBranchNameLength"
);
Expand Down Expand Up @@ -75,11 +79,21 @@ export async function fetchSettings(): Promise<Settings> {
);
maxBranchNameLength = SETTING_DEFAULT_BRANCH_NAME_LENGTH;
}
if (minBranchNameLength === undefined) {
config.update(
"minBranchNameLength",
0,
vscode.ConfigurationTarget.Workspace,
true
);
minBranchNameLength = SETTING_DEFAULT_MIN_BRANCH_NAME_LENGTH;
}
return {
format,
maxBranchNameLength,
separator,
forceBranchNameLowerCase,
removeBranchNameWhiteSpace,
minBranchNameLength,
};
}

0 comments on commit b245c83

Please sign in to comment.