-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·139 lines (123 loc) · 4.72 KB
/
cli.js
File metadata and controls
executable file
·139 lines (123 loc) · 4.72 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env node
'use strict';
const fs = require('mz/fs');
const Multispinner = require('multispinner');
const expandHomeDir = require('expand-home-dir');
const chalk = require('chalk');
const dotsSpinner = require('cli-spinners').dots;
const api = require('./');
function makePadding(spaces) {
if (spaces === 0) {
return '';
} else if (spaces % 2 === 0) {
const ch = makePadding(spaces / 2);
return ch + ch;
}
return makePadding(spaces - 1) + ' ';
}
function makeTask(token, moduleInfo, multiSpinners, maxPad) {
const moduleName = api.getName(moduleInfo);
const padding = makePadding(maxPad - moduleName.length + 1);
return api.isAppreciated(token, moduleInfo).then(
x => {
if (x.error) {
multiSpinners.spinners[moduleName].text += padding + chalk.red(x.error);
multiSpinners.error(moduleName);
return;
}
multiSpinners.spinners[moduleName].text += padding + (x.starred ? chalk.blue('★ Starred.') : chalk.yellow('☆ Not starred!'));
multiSpinners.success(moduleName);
return Promise.resolve();
},
err => {
multiSpinners.spinners[moduleName].text += padding + err;
multiSpinners.error(moduleName);
return Promise.reject(err);
}
);
}
function checkIsAppreciateAppreciated(token, multiSpinners) {
return api.isAppreciated(token, api.getAppreciateModuleInfo()).then(
x => {
if (x.error) {
return null;
}
return {
multiSpinners: multiSpinners,
starred: x.starred
};
}
);
}
// No command line arguments so far.
api.readAuthToken()
.then(
token => {
console.info(chalk.cyan('Project dependencies: '));
fs.readFile('./package.json')
.then(
data => {
let pkgJSON = JSON.parse(data.toString('utf8'));
let dependencies = api.getProjectDependencies(pkgJSON);
return api.findNodeModulesOnGithub(dependencies);
},
err => {
console.error('Could not read package.json in the current folder.');
console.error('Error:', err);
console.error('Please execute this program in your Node project folder.');
return Promise.reject(null);
}
).then(moduleInfos => api.getUniqueRepos(moduleInfos))
.then(uniqueModuleInfos => {
// make the repository names unique to allow for multiple packages
// pointing to the same repository.
const spinnerNames = uniqueModuleInfos.map(x => {
return api.getName(x);
});
spinnerNames.sort();
const maxPadding = Math.max.apply(
null,
spinnerNames.map(x => {
return x.length;
})
);
const multiSpinners = new Multispinner(spinnerNames, {
clear: false,
autoStart: true,
frames: dotsSpinner.frames,
interval: dotsSpinner.interval
});
let allModuleInfos = uniqueModuleInfos.map(x => {
return makeTask(token, x, multiSpinners, maxPadding);
});
// Place checking for musically-ut/appreciate at the top of the
// list for recovery afterwards.
allModuleInfos.unshift(checkIsAppreciateAppreciated(token, multiSpinners));
return Promise.all(allModuleInfos);
})
.then(allModuleInfosResolved => {
const appreciatedInfo = allModuleInfosResolved[0];
if (appreciatedInfo && !appreciatedInfo.starred) {
// Shameless self-plug.
appreciatedInfo.multiSpinners.on('done', () => {
console.log(chalk.cyan(
'Please also consider starring ' +
chalk.green('musically-ut/appreciate') +
'! ✨'
));
});
}
})
.catch(err => {
console.error(chalk.red('Error: '), err);
});
},
err => {
const tokenFile = expandHomeDir('~/.appreciate');
console.error('Could not read the Access token from', chalk.blue(tokenFile));
console.error('Error:', chalk.red(err.message || err));
console.error('Please generate an Github access token ' +
'using the following URL and place it in', chalk.blue(tokenFile));
console.error(chalk.blue('\n\thttps://help.github.com/articles/creating-an-access-token-for-command-line-use/'));
}
);