-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathadd-license.js
86 lines (72 loc) · 2.32 KB
/
add-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
78
79
80
81
82
83
84
85
86
/*
Copyright 2022 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const fs = require("fs");
const path = require("path");
const Handlebars = require("handlebars");
const SOURCE_TEMPLATE = "source-header.handlebars";
async function walk(dir, matchesFilter) {
let files = fs.readdirSync(dir);
files = await Promise.all(
files
.filter((file) => matchesFilter(file, dir))
.map(async (file) => {
const filePath = path.join(dir, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
return walk(filePath, matchesFilter);
} else if (stats.isFile()) {
return filePath;
}
})
);
return files.reduce((all, folderContents) => all.concat(folderContents), []);
}
async function run() {
const template = fs.readFileSync(
path.resolve(__dirname, ".assets", SOURCE_TEMPLATE),
"utf-8"
);
const renderTemplate = Handlebars.compile(template);
const templateText = renderTemplate({
year: new Date().getFullYear(),
});
const IGNORED = [
"node_modules",
".git",
"alloy.js",
"alloy.min.js",
"at.js",
"at.min.js",
];
const sourceFiles = await walk(path.resolve(__dirname), (file, dir) => {
const filePath = path.join(dir, file);
const stats = fs.statSync(filePath);
for (const ignoredString of IGNORED) {
if (filePath.includes(ignoredString)) {
return false;
}
}
if (stats.isDirectory()) {
return true;
}
if (stats.isFile()) {
return file.endsWith(".js");
}
return false;
});
sourceFiles.forEach((file) => {
const contents = fs.readFileSync(path.resolve(file), "utf-8");
if (templateText.slice(0, 2) !== contents.slice(0, 2)) {
fs.writeFileSync(path.resolve(file), `${templateText}${contents}`);
}
});
}
run();