-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
43 lines (35 loc) · 1.14 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
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);
const write = async (filepath, output, dryRun) => {
if (!dryRun) {
await writeFile(filepath, output);
}
return output;
};
module.exports = async ({
patterns = [],
comment = '',
filepath = path.resolve(process.cwd(), '.gitignore'),
dryRun = false
}) => {
const commented = str => (comment ? `${str} # ${comment}` : str);
const managed = patterns.sort();
const contents = await readFile(filepath, 'utf-8');
const current = contents.split(/\r?\n/);
const corrected = current.map(
pattern => (managed.includes(pattern) ? commented(pattern) : pattern)
);
const additions = managed
.filter(pattern => !corrected.includes(commented(pattern)))
.map(commented);
const outputPatterns = corrected.concat(additions);
const output = `${outputPatterns.join('\n')}${
outputPatterns[outputPatterns.length - 1] === '' ? '' : '\n'
}`;
return current.join('\n') !== output
? write(filepath, output, dryRun)
: current.join('\n');
};