forked from metowolf/vCards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
59 lines (47 loc) · 1.54 KB
/
test.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
const fs = require('fs')
const chalk = require('chalk')
const glob = require("glob").sync
const readChunk = require('read-chunk')
const isPng = require('is-png')
const prettyBytes = require('pretty-bytes')
const yaml = require('js-yaml')
const check = (type, name) => {
const pathname = `data/${type}/${name}`
if (!fs.existsSync(`${pathname}.png`)) {
return chalk.red('(缺少对应 PNG 图像)')
}
const lstat = fs.lstatSync(`${pathname}.png`)
if (!lstat.isFile()) {
return chalk.red('(PNG 图像非标准文件)')
}
const buffer = readChunk.sync(`${pathname}.png`, 0, 8)
if (!isPng(buffer)) {
return chalk.red('(PNG 图像格式不合法)')
}
if (lstat.size > 1024 * 20) {
return `${chalk.red(`(PNG 图像超出大小限制 ${prettyBytes(lstat.size)} > 20 kB)`)}`
}
const data = fs.readFileSync(`${pathname}.yaml`, 'utf8')
const json = yaml.load(data)
for (let phone of json.basic.cellPhone) {
if (phone.toString().substr(0, 3) === '106') {
return chalk.red('(存在 106 短信通道号码)')
}
}
return true
}
let files = glob('data/*/*.yaml')
console.log(`\n当前目录下发现 ${chalk.green(files.length)} 个联系人信息\n`)
for (let [index, file] of files.entries()) {
const type = file.split('/')[1]
const name = file.split('/')[2].split('.')[0]
const result = check(type, name)
if (result !== true) {
console.log(` ${index + 1}\t${type}-${name} ${result}`)
console.log()
process.exit(1)
} else {
console.log(` ${index + 1}\t${type}-${name}`)
}
}
console.log()