forked from ChinaGodMan/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle-format-monkey-meta.js
More file actions
36 lines (36 loc) · 1.43 KB
/
Copy pathsingle-format-monkey-meta.js
File metadata and controls
36 lines (36 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const fs = require('fs')
const path = require('path')
// 格式化单个文件的 Tampermonkey 元数据头部
function formatMetadata(filePath) {
const fileContent = fs.readFileSync(filePath, 'utf-8')
const lines = fileContent.split('\n')
// 首先找到最长的标签长度
let maxLabelLength = 0
const metadataLines = lines.filter(line => line.startsWith('// @'))
metadataLines.forEach(line => {
const parts = line.split(/\s+/)
const label = parts[1].replace(/@/g, '') // 获取标签,去掉 @
maxLabelLength = Math.max(maxLabelLength, label.length)
})
// 然后根据最长标签长度格式化
const formattedLines = lines.map(line => {
if (line.startsWith('// @')) {
const parts = line.split(/\s+/)
const label = parts[1].replace(/@/g, '') // 获取标签,去掉 @
const value = parts.slice(2).join(' ')
return `// @${label.padEnd(maxLabelLength)} ${value}` // 使用最长标签长度进行对齐
}
return line
})
fs.writeFileSync(filePath, formattedLines.join('\n'), 'utf-8')
}
// 从命令行参数获取文件路径
const args = process.argv.slice(2)
if (args.length !== 1) {
console.error('请提供一个 JavaScript 文件的路径。')
process.exit(1)
}
const filePath = path.resolve(args[0])
// 格式化指定的 JavaScript 文件
formatMetadata(filePath)
console.log(`格式化完成: ${filePath}`)