-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·41 lines (32 loc) · 1.83 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
#!/usr/bin/env node
const { exec } = require('child_process');
const fetch = require('node-fetch');
const prompt = require('prompt-sync')();
const type = require('./letters.js');
const run = (...args) => new Promise((resolve, reject) => exec(...args, (err, stdout, stderr) => err ? (() => {
reject(err);
console.log(stdout, stderr);
})() : resolve(stdout)));
process.argv.shift();
if (isNaN(process.argv[0])) process.argv.shift();
const args = process.argv;
const [year, message, username] = args.map(v => isNaN(v) ? v : +v);
(async () => {
if (!year || !message) return console.log('Welcome to GitWrite! GitWrite lets you add a message to your GitHub contribution graph. To begin, make a new GitHub repo, clone it locally, and get ready to add commits!\n\nRun this command again with this syntax: gitwrite <year> <message>.\nMessage should be in qotes. Messages can only have alphanumeric characters, spaces. and some symbols.');
const gitUsername = (await run('git config user.name')).trim();
const data = await fetch('https://api.github.com/users/' + (username ? username : gitUsername)).then(r => r.json());
if (data.message) return console.log('Invalid GitHub username "' + gitUsername + '"');
const commands = type(year, message);
console.log(`Welcome, ${username ? username: gitUsername}!`);
console.log(`Using git repo in directory ${process.cwd()},`);
console.log(`Staging ${commands.length / 6} commits to write ${message} in ${year}...`);
prompt('Press enter to continue...');
for (let i = 0; i < commands.length; i++) {
const command = commands[i];
await run(command);
console.log(`${i + 1}/${commands.length} (${Math.round((i + 1) / commands.length * 1000) / 10}%)`);
}
console.log('Pushing changes...');
await run('git push');
console.log('Changes pushed!');
})();