-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·135 lines (117 loc) · 3.42 KB
/
index.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
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
#!/usr/bin/env node
/**
* scarfit
* Add scarf analytics to a Node pkg, git commit/push, and release an npm patch
*
* @author Ahmad Awais <https://twitter.com/MrAhmadAwais/>
*/
const ora = require('ora');
const execa = require('execa');
const alert = require('cli-alerts');
const readPkgUp = require('read-pkg-up');
const to = require('await-to-js').default;
const Configstore = require('configstore');
const scarfPackage = require('scarf-package');
const handleError = require('cli-handle-error');
const {green: g, yellow: y, red: r, blue: b} = require('chalk');
const init = require('./utils/init');
const cli = require('./utils/cli');
const log = require('./utils/log');
const ask = require('./utils/ask');
let pkgName, err, res;
const input = cli.input;
const flags = cli.flags;
const {clear, debug, name, add, dep, git, release, public} = flags;
const spinner = ora({text: ``});
(async () => {
init({clear});
input.includes(`help`) && cli.showHelp(0);
const pkg = await readPkgUp();
const guessedName = pkg && pkg.packageJson && pkg.packageJson.name;
const config = new Configstore(`scarfit`, {});
const username = config.get(`username`);
const apiToken = config.get(`apiToken`);
if (!username) {
const username = await ask({
message: `Your Scarf.sh username?`,
hint: `(Gets stored in ~/.config/scarfit/)`
});
config.set(`username`, username);
}
if (!apiToken) {
const apiToken = await ask({
message: `Your Scarf.sh API Token (from settings)?`,
hint: `(Gets stored in ~/.config/scarfit/)`
});
config.set(`apiToken`, apiToken);
}
if (!name) {
pkgName = await ask({
message: `Name of the npm package?`,
initial: guessedName
});
}
// 1. Add package to scarf.
if (add) {
spinner.start(`${y`SCARF`} package adding…`);
const options = {
name: name ? name : pkgName,
username: config.get(`username`),
apiToken: config.get(`apiToken`)
};
[err, res] = await to(scarfPackage(options));
handleError(`SCARF add package`, err);
if (res.status === 200) {
spinner.succeed(`${g`SCARF`} package added`);
} else if (
res.status === 400 &&
res.data === `'${options.name}' is not an available package name`
) {
spinner.info(
`${b`SCARF`} package '${options.name}' already exists`
);
} else {
spinner.fail(`${r`SCARF`} package addition ${r`FAILED`}`);
}
}
// 2. Add scarf dependency.
if (dep) {
spinner.start(`${y`DEPENDENCY`} @scarf/scarf adding…`);
[err, res] = await to(execa(`npm`, [`i`, `@scarf/scarf`]));
handleError(`SCARF dependency`, err);
spinner.succeed(`${g`DEPENDENCY`} @scarf/scarf added`);
}
// 3. Git commit push.
if (git) {
spinner.start(`${y`GIT`} commit/push…`);
[err, res] = await to(execa(`git`, [`add`, `.`]));
[err, res] = await to(
execa(`git`, [`commit`, `-m`, `📦 NEW: Analyze`])
);
[err, res] = await to(execa(`git`, [`push`]));
handleError(`GIT commit/push`, err);
spinner.succeed(`${g`GIT`} commit/push`);
}
// 4. Release a patch.
if (release) {
spinner.start(`${y`PATCH`} releasing…`);
[err, res] = await to(
execa(`npm`, [`version`, `patch`, `-m`, `🚀 RELEASE: Patch`])
);
if (public) {
[err, res] = await to(
execa(`npm`, [`publish`, `--access`, `public`])
);
} else {
[err, res] = await to(execa(`npm`, [`publish`]));
}
handleError(`Patch release`, err);
spinner.succeed(`${g`PATCH`} released`);
}
alert({
type: `success`,
name: `ALL DONE`,
msg: ``
});
debug && log(flags);
})();