|
| 1 | +#!/usr/bin/env node |
| 2 | +// SPDX-License-Identifier: GPL-3.0-or-later WITH EXCEPTIONS |
| 3 | +// Copyright (c) 2025 James Prevett |
| 4 | + |
| 5 | +import { readdirSync } from 'node:fs'; |
| 6 | +import { readFile, writeFile } from 'node:fs/promises'; |
| 7 | +import { join, relative } from 'node:path'; |
| 8 | +import { parseArgs, styleText } from 'node:util'; |
| 9 | + |
| 10 | +const { |
| 11 | + positionals: dirs, |
| 12 | + values: { license: expectedLicense, write, verbose, force }, |
| 13 | +} = parseArgs({ |
| 14 | + allowPositionals: true, |
| 15 | + options: { |
| 16 | + license: { type: 'string', short: 'L' }, |
| 17 | + write: { type: 'boolean', short: 'w', default: false }, |
| 18 | + verbose: { type: 'boolean', short: 'v', default: false }, |
| 19 | + force: { type: 'boolean', short: 'f', default: false }, |
| 20 | + }, |
| 21 | +}); |
| 22 | + |
| 23 | +if (write && !expectedLicense) { |
| 24 | + console.error(styleText('red', 'You must specify a license to write with --license/-L')); |
| 25 | + process.exit(1); |
| 26 | +} |
| 27 | + |
| 28 | +const licenseSpec = /^\s*\/(?:\/|\*) SPDX-License-Identifier: (.+)/; |
| 29 | + |
| 30 | +async function check_file(path, display) { |
| 31 | + const content = await readFile(path, 'utf-8'); |
| 32 | + |
| 33 | + const match = licenseSpec.exec(content); |
| 34 | + |
| 35 | + if (!match) { |
| 36 | + console.error(styleText('red', 'Missing:'), display); |
| 37 | + return 'missing'; |
| 38 | + } |
| 39 | + |
| 40 | + if (!expectedLicense) { |
| 41 | + if (verbose) console.log(styleText(['dim'], 'Found: ') + display); |
| 42 | + return 'with license'; |
| 43 | + } |
| 44 | + |
| 45 | + const [, license] = match; |
| 46 | + |
| 47 | + if (license == expectedLicense) { |
| 48 | + if (verbose) console.log(styleText(['green', 'dim'], 'Correct: ') + display); |
| 49 | + return 'correct'; |
| 50 | + } |
| 51 | + |
| 52 | + console.warn(styleText('yellow', 'Mismatch:'), display); |
| 53 | + return 'mismatched'; |
| 54 | +} |
| 55 | + |
| 56 | +async function write_file(path, display) { |
| 57 | + const content = await readFile(path, 'utf-8'); |
| 58 | + |
| 59 | + const match = licenseSpec.exec(content); |
| 60 | + |
| 61 | + if (!match) { |
| 62 | + await writeFile(path, `// SPDX-License-Identifier: ${expectedLicense}\n${content}`, 'utf-8'); |
| 63 | + |
| 64 | + console.log('Added: ' + display); |
| 65 | + return 'added'; |
| 66 | + } |
| 67 | + |
| 68 | + const [, license] = match; |
| 69 | + |
| 70 | + if (license == expectedLicense) { |
| 71 | + if (verbose) console.log(styleText(['green', 'dim'], 'Correct: ') + display); |
| 72 | + return 'correct'; |
| 73 | + } |
| 74 | + |
| 75 | + process.stdout.write(styleText('yellow', 'Mismatch: ') + display); |
| 76 | + |
| 77 | + if (!force) { |
| 78 | + console.log(styleText('whiteBright', ' (skipped)')); |
| 79 | + return 'skipped'; |
| 80 | + } |
| 81 | + |
| 82 | + await writeFile(path, content.replace(licenseSpec, `// SPDX-License-Identifier: ${expectedLicense}`), 'utf-8'); |
| 83 | + console.log(styleText('whiteBright', ' (overwritten)')); |
| 84 | + return 'overwritten'; |
| 85 | +} |
| 86 | + |
| 87 | +function check_dir(dir, display) { |
| 88 | + const entries = readdirSync(dir, { withFileTypes: true }); |
| 89 | + |
| 90 | + const results = []; |
| 91 | + |
| 92 | + for (const entry of entries) { |
| 93 | + if (entry.isDirectory()) { |
| 94 | + results.push(...check_dir(join(dir, entry.name), join(display, entry.name))); |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + if (!entry.isFile()) continue; |
| 99 | + |
| 100 | + const op = write ? write_file : check_file; |
| 101 | + results.push(op(join(dir, entry.name), join(display, entry.name))); |
| 102 | + } |
| 103 | + |
| 104 | + return results; |
| 105 | +} |
| 106 | + |
| 107 | +if (!dirs.length) dirs.push(join(import.meta.dirname, '../src')); |
| 108 | + |
| 109 | +if (verbose) console.log('Checking:', dirs.join(', ')); |
| 110 | + |
| 111 | +const promises = []; |
| 112 | + |
| 113 | +for (const dir of dirs) { |
| 114 | + promises.push(...check_dir(dir, relative(join(import.meta.dirname, '..'), dir))); |
| 115 | +} |
| 116 | + |
| 117 | +const styles = Object.assign(Object.create(null), { |
| 118 | + missing: 'red', |
| 119 | + 'with license': 'white', |
| 120 | + correct: 'green', |
| 121 | + mismatched: 'yellow', |
| 122 | + skipped: 'white', |
| 123 | + added: 'cyan', |
| 124 | + overwritten: 'magenta', |
| 125 | +}); |
| 126 | + |
| 127 | +try { |
| 128 | + const raw_results = await Promise.all(promises); |
| 129 | + const results = Object.create(null); |
| 130 | + for (const result of raw_results) { |
| 131 | + if (!(result in results)) results[result] = 0; |
| 132 | + results[result]++; |
| 133 | + } |
| 134 | + console.log( |
| 135 | + write ? 'Wrote' : 'Checked', |
| 136 | + styleText('blueBright', raw_results.length.toString()), |
| 137 | + 'files:', |
| 138 | + Object.entries(results) |
| 139 | + .map(([key, value]) => `${key in styles ? styleText(styles[key], value.toString()) : value} ${key}`) |
| 140 | + .join(', ') |
| 141 | + ); |
| 142 | +} catch (error) { |
| 143 | + console.error(styleText('red', error.toString())); |
| 144 | + process.exit(1); |
| 145 | +} |
0 commit comments