-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitcrop.js
81 lines (76 loc) · 2.28 KB
/
gitcrop.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
import fs from 'fs-extra';
import chalk from 'chalk';
import prompt from 'prompt-sync';
import wildcard from 'wildcard';
function isOnIgnoreList(ignoreList, file) {
let matched = false;
ignoreList.forEach(ignoreEntry => {
const result = wildcard(ignoreEntry, file);
if(result) {
matched = true;
}
});
return matched;
}
function getCropList(ignoreList, parent, fileList, cropList) {
fileList.forEach(fileEntry => {
const fullPath = `${parent}${parent == ''? '' : '/'}${fileEntry}`;
if(isOnIgnoreList(ignoreList, fileEntry)) {
cropList.push(fullPath);
} else {
if(fs.lstatSync(fullPath).isDirectory()) {
const newFileList = fs.readdirSync(fullPath);
cropList = getCropList(ignoreList, fullPath, newFileList, cropList);
}
}
});
return cropList;
}
function crop(cropList) {
cropList.forEach(fileEntry => {
console.log(`Deleting ${fileEntry}\x1b[1M\x1bM`);
fs.rmSync(fileEntry);
console.log(`Deleted ${fileEntry} `);
})
}
function getIgnoreList() {
var ignoreList = [];
var ignoreFile = fs.readFileSync(`.gitignore`, 'utf-8');
const ignoreLines = ignoreFile.split(/\r?\n/);
ignoreLines.forEach(ignoreLine => {
if((ignoreLine.trim() != '') && (!ignoreLine.startsWith('#'))) {
ignoreList.push(ignoreLine);
}
});
return ignoreList;
}
export default function gitcrop(dryRun, quiet) {
const ignoreList = getIgnoreList();
const fileList = fs.readdirSync('.');
const cropList = getCropList(ignoreList, '', fileList, []);
if(cropList.length === 0) {
if(!quiet) {
console.info(chalk.yellow(`No files to crop`));
}
process.exit(0);
}
if(!quiet) {
console.log(`Files to crop:`);
cropList.forEach(fileEntry => {
console.log(` ${fileEntry}`);
});
if(!dryRun) {
var del = '';
while(!((del === 'y') || (del === 'n'))) {
var del = prompt()(`Crop (Y/n)?`, `Y`).toLowerCase();
}
if(del === 'y') {
crop(cropList);
}
}
} else {
if(!dryRun) {
crop(cropList);
}
}
}