-
Notifications
You must be signed in to change notification settings - Fork 46
/
fmt.js
68 lines (67 loc) · 1.95 KB
/
fmt.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const readline = require('readline')
const fs = require('fs')
// 读取当前目录
fs.readdir('./', (error, files) => {
// 遍历目录
if (error === null) files.forEach(file => {
fs.lstat(file, (error, stats) => {
// 判断是否为特定目录
if (error === null && stats.isDirectory() && file.match(/^[a-zA-Z]/) !== null) {
// 根据命名规则读取文件
const tsFlie = `${file}/${file}.user.ts`
const jsFlie = `${file}/${file}.user.js`
fs.lstat(tsFlie, (error, stats) => {
if (error === null && stats.isFile()) GMheads(tsFlie, jsFlie)
else {
// 根据命名规则读取文件
const tsFlie = `${file}/${file}.ts`
const jsFlie = `${file}/${file}.js`
fs.lstat(tsFlie, (error, stats) => {
if (error === null && stats.isFile()) GMheads(tsFlie, jsFlie)
})
}
})
}
})
})
})
function GMheads(tsFlie, jsFlie) {
const header = []
const script = []
// 写入js
const writeJS = () => {
fs.writeFile(jsFlie, header.concat(script).join('\n'), error => {
if (error === null) console.log(jsFlie, 'ok')
else console.error(error)
})
}
// 读取js
const jsRL = readline.createInterface({
input: fs.createReadStream(jsFlie),
crlfDelay: Infinity
})
jsRL.on('line', line => {
if (line === '// ==UserScript==') {
jsRL.removeAllListeners()
jsRL.close()
console.log(jsFlie, 'ignore')
}
if (!line.startsWith('import') && !line.startsWith('export'))
script.push(line.replace(/^((?: )+)\1/, '$1'))
})
jsRL.on('close', () => {
// 读取ts
const tsRL = readline.createInterface({
input: fs.createReadStream(tsFlie),
crlfDelay: Infinity
})
tsRL.on('line', line => {
header.push(line)
if (line === '// ==/UserScript==') {
tsRL.removeAllListeners()
tsRL.close()
writeJS()
}
})
})
}