-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlicense.js
77 lines (66 loc) · 2.1 KB
/
license.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
/* eslint no-console: ["error", { allow: ["warn", "error", "info"] }] */
const licenseHeader =
"\n" +
"Copyright (c) 2016 - 2020, Matteo Gaggiano and the angular-mimetype contributors\n" +
"SPDX-License-Identifier: MIT\n" +
"\n";
const searchFor = " * SPDX-License-Identifier: MIT";
const { resolve, join, basename } = require("path");
const { statSync, readdirSync, readFileSync, writeFileSync } = require("fs");
const directoriesPath = [
join(__dirname, "src"),
join(__dirname, "spec")
];
const licenseContent = readFileSync(join(__dirname, "LICENSE.txt"), {encoding: "utf8"});
const fileTypes = ["js"];
const isValidFileType = (fileTypes) => {
return (file) => fileTypes.length === 0 || fileTypes.filter(ft => {
return file.lastIndexOf("." + ft) === file.length - (ft.length + 1);
}).length > 0;
};
const getFiles = (path, fileTypes = []) => {
const subdirs = readdirSync(path);
const files = subdirs.map((subdir) => {
const res = resolve(path, subdir);
return (statSync(res)).isDirectory() ? getFiles(res, fileTypes) : res;
});
return files
.reduce((a, f) => a.concat(f), [])
.filter(isValidFileType(fileTypes));
};
let files = [];
for (const directory of directoriesPath) {
files = [...files, ...getFiles(directory, fileTypes)];
}
const edited = [];
for (const file of files) {
let content = readFileSync(file, {encoding: "utf8"});
let lines = content.split("\n");
let found = false;
for (const line of lines) {
found = found || line.indexOf(searchFor) === 0;
}
if (!found) {
const header = [
",",
"/**",
" *",
` * ${basename(file)}`,
...licenseHeader.split("\n").map((l) => ` * ${l}`),
...licenseContent.split("\n").map((l) => ` * ${l}`),
" *",
" */",
",",
""
];
lines.unshift.apply(lines, header);
content = lines.join("\n");
writeFileSync(file, content, {flag: "w"});
edited.push({path: file, name: basename(file)});
}
}
if (edited.length > 0) {
console.info(`License: Edited ${edited.length} files:`, edited);
} else {
console.info("License: No file updated");
}