-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclone-all.js
93 lines (75 loc) · 3.15 KB
/
clone-all.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
import { execSync } from 'child_process';
import { existsSync } from 'fs';
import { exit, stdin, stdout } from 'process';
import readline from 'readline';
import userNames from './people.js';
const io = readline.createInterface({ input: stdin, output: stdout });
const trueAnswers = ['y', 'yes', 'Y', 'Yes'];
const cleanFolder = (repoName, userName) => execSync(`cd ${repoName} && rm -rf ${userName} && mkdir ${userName}`);
const cloneForAll = async (repoName, shouldInstall, shouldPullInExistingFolders) => {
// if folder doesn't exist, create it
if (!existsSync(repoName)) {
execSync(`mkdir ${repoName}`);
console.log('Folder created');
}
const erroredUsernames = [];
// loop over every person
await userNames.forEach((userName) => {
const localPath = `${repoName}/${userName}`;
try {
// if shouldPullInExistingFolders is true, delete the folder and create a new one
if (existsSync(localPath)) {
if (shouldPullInExistingFolders) {
execSync(`cd ${repoName}/${userName} && git pull`)
// console.log(`Cleaning ${localPath}...`);
// cleanFolder(repoName, userName);
// skip otherwise
} else {
console.log(`${userName} already cloned. Skipping...`);
return;
}
}
// clone the repo
return execSync(`cd ${repoName} && git clone git@github.com:${userName}/${repoName}.git ${userName} ${shouldInstall ? `&& cd ./${userName} && npm install` : ''}`)
} catch (e) {
// add to errored usernames if wasn't able to clone
erroredUsernames.push(userName);
}
});
return { erroredUsernames };
}
// main
const init = async () => {
if (!userNames.length) {
console.error('No usernames provided!');
exit(1);
}
// ask for repo name
await io.question('Repo name: ', async (repoName) => {
// ask for whether to override the existing clones
await io.question('Should pull in existing folders? (y/n): ', async (pullInExisting) => {
const shouldPullInExistingFolders = trueAnswers.includes(pullInExisting);
// ask for whether to run NPM INSTALL on each project
await io.question('Run npm install in each project? [y/n] ', async runInstall => {
const shouldInstall = runInstall === 'y' || runInstall === 'yes' || runInstall === 'Y' || runInstall === 'Yes';
// clone everybody's repos
const { erroredUsernames } = await cloneForAll(repoName, shouldInstall, shouldPullInExistingFolders);
// if there were any errors, print them
if (erroredUsernames.length) {
console.error(`Couldn't fetch the repo for ${erroredUsernames.join(', ')}. Removing their empty folders...`);
// remove the empty folders and exit with an error
erroredUsernames.forEach(userName => {
execSync(`cd ${repoName} && rm -rf ${userName}`);
})
exit(1);
} else {
// surprise - surprise, everybody submitted their work!
console.info(`No way, everybody submitted today!`);
exit(0);
}
});
})
});
}
// run me
init();