main
js 68 lines 1.55 KB
Raw
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
59 if (source.indexOf(META_COPYRIGHT_COMMENT_BLOCK) === 0) {
60 return null;
61 }
62 if (/^\/\*\*/.test(source)) {
63 source = source.replace(/\/\*\*[^\/]+\/\s+/, META_COPYRIGHT_COMMENT_BLOCK);
64 } else {
65 source = `${META_COPYRIGHT_COMMENT_BLOCK}${source}`;
66 }
67 return source;
68 }