-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-repo-rename.js
66 lines (54 loc) · 1.98 KB
/
gh-repo-rename.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const shell = require('shelljs');
const { Command } = require('commander');
const path= require('path');
const defaultName = 'default-repository';
const program = new Command();
program.version(require(path.join(__dirname,'package.json')).version);
program
.name("gh repo-rename [options] [organization/repository] [newRepositoriName]")
.option('-d --default' ,' set default name for the repository')
program.addHelpText('after', `
- You can change the name of a repo in a organization
`);
program.parse(process.argv);
if (process.argv.length === 1) program.help();
function exec(executable, ...args) {
let command = `${executable} ${args.join('')}`;
let result = shell.exec(command, {silent: true});
if (result.code !== 0) {
shell.echo(`Error: Command "${command}" failed\n${result.stderr}`);
shell.exit(result.code);
}
return result.stdout.replace(/\s+$/,'');
}
const gh = (...args) => exec("gh", ...args);
function showError(error) {
if (error) {
throw(`Error!: ${error}`);
}
}
try {
if (!shell.which('git')) {
showError('Sorry, this extension requires git installed!');
}
if (!shell.which('gh')) {
showError('Sorry, this extension requires GitHub Cli (gh) installed!');
}
if(!program.opts() && !program.args[0]) {
showError('Sorry, you need to write an organization/repository');
}
if(!program.args[1] && !program.opts().default) {
showError('Sorry, you need to write an name for the repository, or you can put the -d option');
}
if(program.opts().default) {
gh(`api -X PATCH /repos/${program.args[0]} -f name=${defaultName}`);
console.log(`Name of the repository changed to ${defaultName} succesfuly`);
}
else {
gh(`api -X PATCH /repos/${program.args[0]} -f name=${program.args[1]}`);
console.log(`Name of the repository changed to ${program.args[1]} succesfuly`);
}
}
catch(error) {
console.log(error);
}