forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyright.js
65 lines (58 loc) · 1.49 KB
/
copyright.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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const fs = require('fs');
const glob = require('glob');
const META_COPYRIGHT_COMMENT_BLOCK =
`/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/`.trim() + '\n\n';
const files = glob.sync('**/*.{js,ts,tsx,jsx,rs}', {
ignore: [
'**/dist/**',
'**/node_modules/**',
'**/tests/fixtures/**',
'**/__tests__/fixtures/**',
],
});
const updatedFiles = new Map();
let hasErrors = false;
files.forEach(file => {
try {
const result = processFile(file);
if (result != null) {
updatedFiles.set(file, result);
}
} catch (e) {
console.error(e);
hasErrors = true;
}
});
if (hasErrors) {
console.error('Update failed');
process.exit(1);
} else {
for (const [file, source] of updatedFiles) {
fs.writeFileSync(file, source, 'utf8');
}
console.log('Update complete');
}
function processFile(file) {
let source = fs.readFileSync(file, 'utf8');
if (source.indexOf(META_COPYRIGHT_COMMENT_BLOCK) === 0) {
return null;
}
if (/^\/\*\*/.test(source)) {
source = source.replace(/\/\*\*[^\/]+\/\s+/, META_COPYRIGHT_COMMENT_BLOCK);
} else {
source = `${META_COPYRIGHT_COMMENT_BLOCK}${source}`;
}
return source;
}