Skip to content

Commit a566d87

Browse files
authored
Add copyright script (#7991)
Copied over our copyright script from the react repo. I made a small fix to handle shebangs.
1 parent 5b9a2ce commit a566d87

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"postinstall": "is-ci || husky install .husky",
2222
"check-all": "npm-run-all prettier lint:fix tsc rss",
2323
"rss": "node scripts/generateRss.js",
24-
"deadlinks": "node scripts/deadLinkChecker.js"
24+
"deadlinks": "node scripts/deadLinkChecker.js",
25+
"copyright": "node scripts/copyright.js"
2526
},
2627
"dependencies": {
2728
"@codesandbox/sandpack-react": "2.13.5",

scripts/copyright.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
const fs = require('fs');
11+
const glob = require('glob');
12+
13+
const META_COPYRIGHT_COMMENT_BLOCK =
14+
`/**
15+
* Copyright (c) Meta Platforms, Inc. and affiliates.
16+
*
17+
* This source code is licensed under the MIT license found in the
18+
* LICENSE file in the root directory of this source tree.
19+
*/`.trim() + '\n\n';
20+
21+
const files = glob.sync('**/*.{js,ts,tsx,jsx,rs}', {
22+
ignore: [
23+
'**/dist/**',
24+
'**/node_modules/**',
25+
'**/tests/fixtures/**',
26+
'**/__tests__/fixtures/**',
27+
],
28+
});
29+
30+
const updatedFiles = new Map();
31+
let hasErrors = false;
32+
files.forEach((file) => {
33+
try {
34+
const result = processFile(file);
35+
if (result != null) {
36+
updatedFiles.set(file, result);
37+
}
38+
} catch (e) {
39+
console.error(e);
40+
hasErrors = true;
41+
}
42+
});
43+
if (hasErrors) {
44+
console.error('Update failed');
45+
process.exit(1);
46+
} else {
47+
for (const [file, source] of updatedFiles) {
48+
fs.writeFileSync(file, source, 'utf8');
49+
}
50+
console.log('Update complete');
51+
}
52+
53+
function processFile(file) {
54+
if (fs.lstatSync(file).isDirectory()) {
55+
return;
56+
}
57+
let source = fs.readFileSync(file, 'utf8');
58+
let shebang = '';
59+
60+
if (source.startsWith('#!')) {
61+
const newlineIndex = source.indexOf('\n');
62+
if (newlineIndex === -1) {
63+
shebang = `${source}\n`;
64+
source = '';
65+
} else {
66+
shebang = source.slice(0, newlineIndex + 1);
67+
source = source.slice(newlineIndex + 1);
68+
}
69+
}
70+
71+
if (source.indexOf(META_COPYRIGHT_COMMENT_BLOCK) === 0) {
72+
return null;
73+
}
74+
if (/^\/\*\*/.test(source)) {
75+
source = source.replace(/\/\*\*[^\/]+\/\s+/, META_COPYRIGHT_COMMENT_BLOCK);
76+
} else {
77+
source = `${META_COPYRIGHT_COMMENT_BLOCK}${source}`;
78+
}
79+
80+
if (shebang) {
81+
return `${shebang}${source}`;
82+
}
83+
return source;
84+
}

0 commit comments

Comments
 (0)