|
| 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 | + * @noflow |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +const dotslash = require('fb-dotslash'); |
| 14 | +const {spawnSync} = require('node:child_process'); |
| 15 | +const fs = require('node:fs'); |
| 16 | +const path = require('node:path'); |
| 17 | +const {globSync} = require('tinyglobby'); |
| 18 | + |
| 19 | +const REPO_ROOT = path.resolve(__dirname, '..'); |
| 20 | +const CLANG_FORMAT = path.join(__dirname, 'clang-format'); |
| 21 | +const GENERATED_MARKER = Buffer.from('@' + 'generated'); |
| 22 | +const MAX_HEADER_BYTES = 4096; |
| 23 | +const MAX_FILES_PER_PROCESS = 30; |
| 24 | + |
| 25 | +/** @param {string} file */ |
| 26 | +function isGenerated(file) { |
| 27 | + const fd = fs.openSync(path.resolve(REPO_ROOT, file), 'r'); |
| 28 | + try { |
| 29 | + const header = Buffer.alloc(MAX_HEADER_BYTES); |
| 30 | + const bytesRead = fs.readSync(fd, header, 0, header.length, 0); |
| 31 | + return header.subarray(0, bytesRead).includes(GENERATED_MARKER); |
| 32 | + } finally { |
| 33 | + fs.closeSync(fd); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function main() { |
| 38 | + const files = |
| 39 | + process.argv.length > 2 |
| 40 | + ? process.argv.slice(2) |
| 41 | + : globSync('*/**/*.{h,cpp,m,mm}', {cwd: REPO_ROOT}); |
| 42 | + const sourceFiles = files.filter(file => !isGenerated(file)); |
| 43 | + |
| 44 | + for (let i = 0; i < sourceFiles.length; i += MAX_FILES_PER_PROCESS) { |
| 45 | + const result = spawnSync( |
| 46 | + dotslash, |
| 47 | + [CLANG_FORMAT, '-i', ...sourceFiles.slice(i, i + MAX_FILES_PER_PROCESS)], |
| 48 | + { |
| 49 | + cwd: REPO_ROOT, |
| 50 | + stdio: 'inherit', |
| 51 | + }, |
| 52 | + ); |
| 53 | + |
| 54 | + if (result.error != null) { |
| 55 | + throw result.error; |
| 56 | + } |
| 57 | + if (result.status !== 0) { |
| 58 | + process.exit(result.status ?? 1); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +main(); |
0 commit comments