-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathupdate_headers.js
58 lines (51 loc) · 1.49 KB
/
update_headers.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
/**
* @license
* Copyright 2020 Ludan Stoecklé
* SPDX-License-Identifier: Apache-2.0
*/
const fs = require('fs');
var walk = function (dir) {
var results = [];
var list = fs.readdirSync(dir);
list.forEach(function (file) {
file = dir + '/' + file;
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
results = results.concat(walk(file));
} else {
/* Is a file */
if (
(file.endsWith('.js') || file.endsWith('.ts')) &&
file.indexOf('node_modules') == -1 &&
file.indexOf('/dist/') == -1
) {
results.push(file);
}
}
});
return results;
};
const res = walk('.');
const template = `/**
* @license
* EXISTING_COPYRIGHT
* EXISTING_LICENSE
*/
`;
for (let i = 0; i < res.length; i++) {
const file = res[i];
const content = fs.readFileSync(file, 'utf-8');
var firstLine = content.split(/\r?\n/)[0];
var secondLine = content.split(/\r?\n/)[1];
if (firstLine.startsWith('// Copyright') && secondLine.startsWith('// SPDX-License-Identifier:')) {
console.log(file);
const filled = template
.replace('EXISTING_COPYRIGHT', firstLine.substring(3))
.replace('EXISTING_LICENSE', secondLine.substring(3));
const final = filled + content.split(/\r?\n/).slice(2).join('\r\n');
// console.log(final);
fs.writeFileSync(file, final, 'utf-8');
}
}
// console.log(JSON.stringify(res));