avil13-co 2.2.5 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +62 -21
  2. package/dist/ask.d.ts +4 -3
  3. package/dist/ask.js +54 -32
  4. package/dist/cli-commands/index.d.ts +7 -2
  5. package/dist/cli-commands/index.js +44 -37
  6. package/dist/commands/checkout-branch.js +5 -9
  7. package/dist/commands/delete-barnch.d.ts +1 -0
  8. package/dist/commands/delete-barnch.js +34 -23
  9. package/dist/commands/exec.d.ts +1 -1
  10. package/dist/commands/exec.js +11 -2
  11. package/dist/commands/get-branches.d.ts +2 -4
  12. package/dist/commands/get-branches.js +18 -35
  13. package/dist/commands/get-current-branch-name.d.ts +1 -1
  14. package/dist/commands/get-current-branch-name.js +1 -3
  15. package/dist/commands/merge-branch.js +27 -31
  16. package/dist/commands/sync-branches.d.ts +2 -0
  17. package/dist/commands/sync-branches.js +106 -0
  18. package/dist/index.d.ts +1 -1
  19. package/dist/index.js +40 -18
  20. package/dist/prompt.d.ts +2 -0
  21. package/dist/prompt.js +24 -0
  22. package/dist/terminal.d.ts +5 -0
  23. package/dist/terminal.js +40 -0
  24. package/package.json +1 -1
  25. package/src/__tests__/ask.spec.ts +17 -2
  26. package/src/__tests__/cli-commands.spec.ts +31 -0
  27. package/src/__tests__/commands.spec.ts +110 -0
  28. package/src/__tests__/sync-branches.spec.ts +223 -0
  29. package/src/__tests__/terminal.spec.ts +56 -0
  30. package/src/ask.ts +51 -47
  31. package/src/cli-commands/index.ts +36 -46
  32. package/src/commands/checkout-branch.ts +4 -12
  33. package/src/commands/delete-barnch.ts +30 -23
  34. package/src/commands/exec.ts +11 -7
  35. package/src/commands/get-branches.ts +15 -41
  36. package/src/commands/get-current-branch-name.ts +2 -6
  37. package/src/commands/merge-branch.ts +25 -34
  38. package/src/commands/sync-branches.ts +94 -0
  39. package/src/index.ts +40 -28
  40. package/src/prompt.ts +18 -0
  41. package/src/terminal.ts +32 -0
package/src/index.ts CHANGED
@@ -1,42 +1,54 @@
1
- import { ask } from './ask';
2
- import { Commands, getHelp, getWishOfUser } from './cli-commands';
1
+ import { ask, askBranches } from './ask';
2
+ import { Commands, getHelp, parseArgs } from './cli-commands';
3
3
  import { BranchesType, checkoutBranch, getBranches, getCurrentBranchName } from './commands';
4
- import { deleteBranch } from './commands/delete-barnch';
4
+ import { deleteBranches } from './commands/delete-barnch';
5
5
  import { mergeBranch } from './commands/merge-branch';
6
+ import { syncBranches } from './commands/sync-branches';
7
+ import { finishTerminal, installInterruptHandler, print, startTerminal } from './terminal';
6
8
 
7
- const run = async () => {
8
- const userWish = getWishOfUser();
9
-
10
- if (Commands.Help === userWish) {
9
+ export const run = async () => {
10
+ const { command, filter } = parseArgs();
11
+ if (command === Commands.Help) {
11
12
  console.log(getHelp());
12
13
  return;
13
14
  }
14
15
 
15
- let branchType: BranchesType = BranchesType.All;
16
-
17
- if (Commands.ShowMerged === userWish) {
18
- branchType = BranchesType.Merged;
19
- } else if (Commands.ShowUnMerged === userWish) {
20
- branchType = BranchesType.NotMerged;
21
- }
22
-
23
- const branches = await getBranches(branchType);
24
-
25
- const { branch } = await ask('Select branch', branches);
26
-
27
- if (Commands.DeleteBranch === userWish) {
28
- deleteBranch(branch);
16
+ if (command === Commands.Sync) {
17
+ startTerminal();
18
+ await syncBranches();
29
19
  return;
30
20
  }
31
21
 
32
- if (Commands.MergeBranch === userWish) {
33
- mergeBranch(branch);
34
- return;
22
+ const branchType = command === Commands.ShowMerged ? BranchesType.Merged
23
+ : command === Commands.ShowUnMerged ? BranchesType.NotMerged : BranchesType.All;
24
+ const deleting = command === Commands.DeleteBranch;
25
+ let branches = await getBranches(branchType, deleting);
26
+ if (deleting) {
27
+ const currentBranch = await getCurrentBranchName();
28
+ branches = branches.filter(branch => branch !== currentBranch);
29
+ if (!branches.length) throw new Error('No local branches available for deletion.');
35
30
  }
36
31
 
37
- await checkoutBranch(branch);
38
-
39
- await getCurrentBranchName()
32
+ startTerminal();
33
+ if (deleting) {
34
+ const selected = await askBranches('Select branches to delete (Space to select)', branches, filter);
35
+ await deleteBranches(selected);
36
+ } else {
37
+ const { branch } = await ask('Select branch', branches, filter);
38
+ if (command === Commands.MergeBranch) await mergeBranch(branch);
39
+ else {
40
+ await checkoutBranch(branch);
41
+ print(await getCurrentBranchName());
42
+ }
43
+ }
40
44
  };
41
45
 
42
- run();
46
+ const removeInterruptHandler = installInterruptHandler();
47
+ run().catch(error => {
48
+ finishTerminal();
49
+ console.error('Error: ' + (error instanceof Error ? error.message : String(error)));
50
+ process.exitCode = 1;
51
+ }).finally(() => {
52
+ finishTerminal();
53
+ removeInterruptHandler();
54
+ });
package/src/prompt.ts ADDED
@@ -0,0 +1,18 @@
1
+ import inquirer, { Answers, QuestionCollection } from 'inquirer';
2
+ import { finishTerminal } from './terminal';
3
+
4
+ export function prompt<T extends Answers = Answers>(questions: QuestionCollection<T>): Promise<T> {
5
+ const result = inquirer.prompt<T>(questions);
6
+ // Inquirer 8 re-sends SIGINT asynchronously and may exit before Node handles
7
+ // it. Handle readline's raw-mode Ctrl+C synchronously instead.
8
+ const ui = result.ui as any;
9
+ if (ui) {
10
+ ui.rl.removeListener('SIGINT', ui.onForceClose);
11
+ ui.rl.once('SIGINT', () => {
12
+ ui.close();
13
+ finishTerminal(true);
14
+ process.exit(130);
15
+ });
16
+ }
17
+ return result;
18
+ }
@@ -0,0 +1,32 @@
1
+ // Keep interactive output isolated from the user's shell history.
2
+ let active = false;
3
+ let messages: string[] = [];
4
+
5
+ export function startTerminal() {
6
+ active = Boolean(process.stdout.isTTY && process.stdin.isTTY);
7
+ messages = [];
8
+ if (active) process.stdout.write('\x1b[?1049h');
9
+ }
10
+
11
+ export function print(message: string) {
12
+ if (!message.trim()) return;
13
+ if (active) messages.push(message.trimEnd());
14
+ console.log(message.trimEnd());
15
+ }
16
+
17
+ export function finishTerminal(cancelled = false) {
18
+ if (!active) return;
19
+ active = false;
20
+ process.stdout.write('\x1b[?1049l\x1b[?25h');
21
+ if (!cancelled && messages.length) console.log(messages.join('\n'));
22
+ messages = [];
23
+ }
24
+
25
+ export function installInterruptHandler() {
26
+ const onInterrupt = () => {
27
+ finishTerminal(true);
28
+ process.exit(130);
29
+ };
30
+ process.on('SIGINT', onInterrupt);
31
+ return () => process.removeListener('SIGINT', onInterrupt);
32
+ }