|
| 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