-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlicence_inserter.js
47 lines (41 loc) · 1.26 KB
/
licence_inserter.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
const replace = require('replace-in-file');
const fs = require('fs');
const { resolve } = require('path');
const LICENCE_SUFFIX = '::END::LICENCE::';
const LICENCE_PREFIX = '::START::LICENCE::';
const LICENCE_REGEX = new RegExp(LICENCE_PREFIX + '([\\s\\S]*)' + LICENCE_SUFFIX);
const FILE_EXTENSIONS = ['js', 'ts', 'html', 'css'];
const IGNORE_DIRS = ['dist', 'node_modules', 'e2e'];
function readHeader() {
return fs.readFileSync(resolve(__dirname, 'licence-header.txt'), 'utf-8');
}
function buildGlobs() {
return FILE_EXTENSIONS.map(ext => resolve(__dirname, '**', '*.' + ext));
}
function buildIgnore() {
return IGNORE_DIRS.map(dir => resolve(__dirname, dir, '**'));
}
function updateFiles() {
console.log('Updating licence in files');
const globs = buildGlobs();
console.log(globs);
const licenceHeader = readHeader();
const options = {
files: globs,
from: LICENCE_REGEX,
to: LICENCE_PREFIX + licenceHeader + LICENCE_SUFFIX,
ignore: buildIgnore()
};
try {
const changes = replace.sync(options);
if (changes.length === 0) {
console.log('No files updated');
} else {
console.log('Modified files:', changes.join('\n\t'));
}
}
catch (error) {
console.error('Error occurred:', error);
}
}
updateFiles();