-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprchain-commander.ts
50 lines (43 loc) · 1.72 KB
/
prchain-commander.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { getBranches, getWorkdir } from "./helpers"
import commandLineArgs, { OptionDefinition } from "command-line-args";
import fs from 'fs';
import { getUserSelection } from "./helpers/select";
export interface PRChainCommanderConfig {
enterBranchesPrompt:string
branchesFilter: (branch:string) => boolean,
branchesForUserSelection: (branches:string[]) => string[],
makeCmd: (branches:string[], selectedBranchIdx:number) => string
}
export async function prchainCommander (cfg: PRChainCommanderConfig) {
const workDir = getWorkdir()
const optionDefinitions: OptionDefinition[] = [
{ name: 'chain', alias: 'c', type: String, defaultValue: "main.prchain" },
{ name: 'branches', alias: 'b', type: String, multiple: true },
{ name: 'output', alias: 'o', type: String },
]
// handle Ctrl + c
process.on('SIGINT', function() {
process.exit();
});
const options = commandLineArgs(optionDefinitions, {partial: true});
const output = options["output"]
const branches = getBranches(cfg.enterBranchesPrompt, options["branches"], workDir, options["chain"]).filter(cfg.branchesFilter)
if (!!output) {
const branch = await getUserSelection(cfg.branchesForUserSelection(branches))
// write to output (if specified)
if(output) {
const i = branches.findIndex(b => b == branch)
fs.writeFileSync(output, cfg.makeCmd(branches, i))
}
} else {
// make commands
let cmds = []
for(let i = 1; i < branches.length; i++) {
cmds.push(cfg.makeCmd(branches, i))
}
// print commands
console.log(" ")
console.log(" ")
cmds.forEach(c => console.log(c))
}
}