-
Notifications
You must be signed in to change notification settings - Fork 0
/
split-banch-per-folder.js
executable file
·70 lines (53 loc) · 1.79 KB
/
split-banch-per-folder.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
#!/usr/bin/env node
const fs = require('mz/fs');
const path = require('path');
const simpleGit = require('simple-git/promise');
const gitPath = path.join(__dirname, 'browser-compat-data/');
const util = require('util');
const glob = util.promisify(require('glob'));
function exec(cmd) {
const exec = require('child_process').exec;
return new Promise((resolve, reject) => {
exec(cmd, {
cwd: gitPath
}, (error, stdout, stderr) => {
if (error) {
reject(error);
}
resolve(stdout? stdout : stderr);
});
});
}
(async function main() {
const args = process.argv.slice(2);
const git = simpleGit(gitPath);
const targetBranch = args[0] || 'master';
const origBranch = args[1] || (await git.status()).current;
await git.checkout(origBranch);
const folders = (await glob(gitPath + '*/'))
.map(s => s.replace(gitPath, ''));
function folderToBranchName(folderName) {
return origBranch + '_' + folderName.replace(/[^a-z0-9]/ig,'');
}
for (const folder of folders) {
try {
await exec(`git ls-files --error-unmatch ${folder}`);
} catch (e) {
continue;
}
await git.checkout(targetBranch);
const newBranchName = folderToBranchName(folder);
await exec(`git checkout -b ${newBranchName}`);
await exec(`git checkout ${origBranch} -- ${folder}`);
try {
await exec(`! git diff --cached --quiet --exit-code`);
} catch (e) {
console.log(`${folder}, no changes skipping`);
await git.checkout(targetBranch);
await exec(`git branch -D ${newBranchName}`);
continue;
}
await git.commit(`Updating folder ${folder}`);
}
await git.checkout(origBranch);
}());