mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-22 03:42:14 +00:00
Add copyright script (#7991)
Copied over our copyright script from the react repo. I made a small fix to handle shebangs.
This commit is contained in:
@@ -21,7 +21,8 @@
|
||||
"postinstall": "is-ci || husky install .husky",
|
||||
"check-all": "npm-run-all prettier lint:fix tsc rss",
|
||||
"rss": "node scripts/generateRss.js",
|
||||
"deadlinks": "node scripts/deadLinkChecker.js"
|
||||
"deadlinks": "node scripts/deadLinkChecker.js",
|
||||
"copyright": "node scripts/copyright.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@codesandbox/sandpack-react": "2.13.5",
|
||||
|
||||
84
scripts/copyright.js
Normal file
84
scripts/copyright.js
Normal file
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* 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) {
|
||||
if (fs.lstatSync(file).isDirectory()) {
|
||||
return;
|
||||
}
|
||||
let source = fs.readFileSync(file, 'utf8');
|
||||
let shebang = '';
|
||||
|
||||
if (source.startsWith('#!')) {
|
||||
const newlineIndex = source.indexOf('\n');
|
||||
if (newlineIndex === -1) {
|
||||
shebang = `${source}\n`;
|
||||
source = '';
|
||||
} else {
|
||||
shebang = source.slice(0, newlineIndex + 1);
|
||||
source = source.slice(newlineIndex + 1);
|
||||
}
|
||||
}
|
||||
|
||||
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}`;
|
||||
}
|
||||
|
||||
if (shebang) {
|
||||
return `${shebang}${source}`;
|
||||
}
|
||||
return source;
|
||||
}
|
||||
Reference in New Issue
Block a user