-
Notifications
You must be signed in to change notification settings - Fork 11
/
cli.js
executable file
·101 lines (84 loc) · 2.27 KB
/
cli.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
'use strict';
var meow = require('meow');
var chalk = require('chalk');
var source,
destination,
count = 0;
var cli = meow({
help: [
'Usage',
' $ copy-github-labels -t <token> <source-repo> <destination-repo>',
'',
'Options',
' -d Dry run, don\'t actually copy anything' ,
' -t, --token Token to authenticate with GitHub API' ,
'',
'Examples',
' $ copy-github-labels -t token jvandemo/source-repo jvandemo/destination-repo',
]
}, {
boolean: [
'd'
],
alias: {
t: 'token'
}
});
if (!cli.flags.token) {
cli.showHelp(1);
}
if (cli.input.length < 2) {
cli.showHelp(1);
}
source = cli.input[0];
destination = cli.input[1];
var options = {
dryRun: cli.flags.d
};
var copyGitHubLabels = require('copy-github-labels')(options);
copyGitHubLabels.authenticate({
type: "token",
token: cli.flags.token
});
if(cli.flags.d){
console.log(chalk.yellow('Dry run, no labels are copied for real:'));
}
/**
* Copy labels.
*
* The returned label looks like this:
* {
* "url":"https://api.github.com/repos/jvandemo/testje/labels/effort2:%20medium%20(day)",
* "name":"effort2: medium (day)",
* "color":"bfe5bf",
* "meta":{
* "x-ratelimit-limit":"5000",
* "x-ratelimit-remaining":"4832",
* "x-ratelimit-reset":"1444192372",
* "x-oauth-scopes":"gist, repo, user",
* "location":"https://api.github.com/repos/jvandemo/testje/labels/effort2:%20medium%20(day)",
* "etag":"\"87c21039795ca6752192f8cfe5954ecb\"",
* "status":"201 Created"
* }
* }
*/
copyGitHubLabels.copy(source, destination, function (err, label){
var error = JSON.parse(err);
var labelName = 'Unknown label';
if(label && label.name){
labelName = label.name;
}
count++;
if(err){
// If error occurs during first iteration and no label is returned,
// it is probably a general error like an invalid token so we should exit
// while we still can.
// if((!label) && (count === 1)){
// console.error(error.message);
// process.exit(1);
// }
return console.log(chalk.dim(count + '. ') + chalk.bold(labelName) + ': ' + chalk.red('failed (' + error.message) + ')');
}
console.log(chalk.dim(count + '. ') + chalk.bold(labelName) + ': ' + chalk.green('ok'));
});