(filePath)
| 2 | const path = require('path') |
| 3 | // 格式化单个文件的 Tampermonkey 元数据头部 |
| 4 | function formatMetadata(filePath) { |
| 5 | if (!filePath.includes('user.js')) { return } |
| 6 | const fileContent = fs.readFileSync(filePath, 'utf-8') |
| 7 | const lines = fileContent.split('\n') |
| 8 | // 首先找到最长的标签长度 |
| 9 | let maxLabelLength = 0 |
| 10 | const metadataLines = lines.filter(line => line.startsWith('// @')) |
| 11 | metadataLines.forEach(line => { |
| 12 | const parts = line.split(/\s+/) |
| 13 | const label = parts[1].replace(/@/g, '') // 获取标签,去掉 @ |
| 14 | maxLabelLength = Math.max(maxLabelLength, label.length) |
| 15 | }) |
| 16 | // 然后根据最长标签长度格式化 |
| 17 | const formattedLines = lines.map(line => { |
| 18 | if (line.startsWith('// @')) { |
| 19 | const parts = line.split(/\s+/) |
| 20 | const label = parts[1].replace(/@/g, '') // 获取标签,去掉 @ |
| 21 | const value = parts.slice(2).join(' ') |
| 22 | return `// @${label.padEnd(maxLabelLength)} ${value.trimEnd()}` // 使用最长标签长度进行对齐,并删除行尾空 |
| 23 | } |
| 24 | return line |
| 25 | }) |
| 26 | fs.writeFileSync(filePath, formattedLines.join('\n'), 'utf-8') |
| 27 | } |
| 28 | // 从命令行参数获取文件路径 |
| 29 | const args = process.argv.slice(2) |
| 30 | if (args.length !== 1) { |
no outgoing calls
no test coverage detected