-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·62 lines (48 loc) · 1.43 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
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var map = require('through2-map');
var cmd = require('./lib/cmd');
var tpls = require('./lib/tpl.json');
var keywords = process.argv.slice(2);
var options = cmd();
var cwd = process.cwd();
var result = options.append.reduce(function(prev, curr, idx, arr) {
if(keywords.indexOf(curr) >= 0)
return prev.concat(tpls[curr]);
}, []);
var ws = fs.createWriteStream('./.gitignore', {
flags: options.force ? 'w' : 'a'
});
if(options.custom) {
var rules = options.custom.reduce(function(memo, curr) {
return memo + curr + '\n';
}, '\n');
ws.write('#=<<< custom =#' + rules + '#= custom >>>=#\n\n');
}
if(keywords.indexOf('rm') < 0) {
ws.end(result.join(''));
}
var stream = fs.ReadStream(cwd + '/.gitignore');
var wsNew = fs.createWriteStream(cwd + '/.gitignore_new');
var remove = map({wantStrings: true}, function(str) {
if(options.remove.length <= 0) return str;
options.remove.forEach(function(arg) {
if(tpls[arg]) {
str = str.replace(tpls[arg], '');
}
});
return str;
});
stream.pipe(remove).pipe(wsNew).on('finish', function() {
console.info('.gitignore file has been generated.');
});
wsNew.on('error', function(err) {
return console.error('Error occured, pls try again.');
});
fs.unlink(cwd + '/.gitignore', function(err) {
if(err) throw err;
fs.rename(cwd + '/.gitignore_new', cwd + '/.gitignore', function(err) {
if(err) throw err;
});
});