|
| 1 | +import fs from 'node:fs' |
| 2 | +import path from 'node:path' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | + |
| 5 | +// Resolve __dirname in ES module |
| 6 | +const __filename = fileURLToPath(import.meta.url) |
| 7 | +const __dirname = path.dirname(__filename) |
| 8 | + |
| 9 | +const regexToIgnore = [ |
| 10 | + /^\./, // ignore all files and directories starting with '.' |
| 11 | + /__image_snapshots__/, |
| 12 | + /^(node_modules|public|docs|dist)/, // ignore common directories |
| 13 | + /^(LICENSE|HEADER)$/, |
| 14 | + /^vite\./, |
| 15 | + /^tsconfig\./, |
| 16 | + /.*\.(md|yaml|json|html|png|d\.ts|css)$/, |
| 17 | + /setup-safetest.js/, |
| 18 | +] |
| 19 | + |
| 20 | +// Define the expected header |
| 21 | +const expectedHeader = `/* |
| 22 | +Copyright 2024-present HiveMQ GmbH |
| 23 | +
|
| 24 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 25 | +you may not use this file except in compliance with the License. |
| 26 | +You may obtain a copy of the License at |
| 27 | +
|
| 28 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 29 | +
|
| 30 | +Unless required by applicable law or agreed to in writing, software |
| 31 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 32 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 33 | +See the License for the specific language governing permissions and |
| 34 | +limitations under the License. |
| 35 | +*/\n` |
| 36 | + |
| 37 | +// Function to check if a file starts with the expected header |
| 38 | +function checkFileHeader(filePath) { |
| 39 | + const content = fs.readFileSync(filePath, 'utf8') |
| 40 | + if (!content.startsWith(expectedHeader)) { |
| 41 | + console.error(`File ${filePath} does not start with the expected header.`) |
| 42 | + process.exitCode = 1 |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +const thisFile = process.argv[1].split('/').slice(-1)[0] |
| 47 | + |
| 48 | +// Recursively check all files in a directory |
| 49 | +function checkDirectory(dirPath) { |
| 50 | + const files = fs.readdirSync(dirPath) |
| 51 | + for (const file of files) { |
| 52 | + const filePath = path.join(dirPath, file) |
| 53 | + const stat = fs.statSync(filePath) |
| 54 | + |
| 55 | + const ignore = file === thisFile || regexToIgnore.some((regex) => regex.test(file)) |
| 56 | + |
| 57 | + console.log(filePath, stat.isDirectory() ? ' (d)' : '', ignore ? ' IGNORE' : '') |
| 58 | + if (!ignore) { |
| 59 | + if (stat.isDirectory()) { |
| 60 | + checkDirectory(filePath) |
| 61 | + } else { |
| 62 | + checkFileHeader(filePath) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Check files in the current directory or specified directory |
| 69 | +const dirToCheck = process.argv[2] || '.' |
| 70 | +checkDirectory(path.resolve(__dirname, dirToCheck)) |
0 commit comments