-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (131 loc) · 3.5 KB
/
index.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const libScan = require('./lib/scan.js')
/**
* hash the cdn-file only
* @param {*} cnf a js-json format, see the `conf()` output.
*/
function hash(cnf) {
const conf = parse(cnf)
return cdnx(conf)
}
/**
* hash the cdn-files and mend txt-files.
* @param {*} cnf a js-json format, see the `conf()` output.
*/
function mend(cnf) {
const conf = parse(cnf)
return txtx(conf)
}
function conf() {
const fs = require('fs')
const t = fs.readFileSync(__dirname + '/conf.js', 'utf8')
console.log('``` conf.js')
console.log(t)
console.log('```')
}
//
function cdnx(conf) {
const libCdnx = require('./lib/cdnx.js')
const cdnx = []
const ot = conf.outFile.cdnType
for (const e of conf.cdnFile) {
const i = libScan.list(e)
const l = libCdnx.deal(i, ot)
for (const c of l) {
cdnx.push(c)
}
}
return cdnx
}
function txtx(conf) {
const cx = cdnx(conf)
console.log('=== cdn-files:' + cx.length + ' ===')
const cs = conf.cnfBase.charset
const ot = conf.outFile.txtType
const ch = conf.cnfBase.cdnHost
const txtx = []
const libTxtx = require('./lib/txtx.js')
for (const e of conf.txtFile) {
const i = libScan.list(e)
const l = libTxtx.deal(i, cs, cx, ot, ch)
for (const c of l) {
txtx.push(c)
}
}
return txtx
}
function parse(cnf) {
//
if (cnf.cnfBase == null) {
console.error('need `cnfBase`')
process.exit(1)
}
// cnfBase: {
// cdnHost: cnf.cnfBase.cdnHost,
// charset: "utf8",
// recurse: true,
// webRoot: "./",
// }
const cnfBase = {}
let cdnHost = cnf.cnfBase.cdnHost
if (cdnHost == null) {
console.warn('need `cnfBase.cdnHost`, use \'\' as default')
cdnHost = ''
}
cnfBase.cdnHost = cdnHost
let charset = cnf.cnfBase.charset
if (charset == null) {
console.warn('need `cnfBase.charset`, use `utf8` as default')
charset = 'utf8'
}
cnfBase.charset = charset
let recurse = cnf.cnfBase.recurse
if (recurse == null) {
console.warn('need `cnfBase.recurse`, use `true` as default')
recurse = 'utf8'
}
cnfBase.recurse = recurse
let webRoot = cnf.cnfBase.webRoot
if (webRoot == null) {
console.warn('need `cnfBase.webRoot`, use `./` as default')
recurse = './'
}
cnfBase.webRoot = webRoot
//
if (!Array.isArray(cnf.cdnFile) || cnf.cdnFile.length == 0) {
console.error('need `cdnFile` not empty')
process.exit(1)
}
if (!Array.isArray(cnf.txtFile) || cnf.txtFile.length == 0) {
console.error('need `txtFile` not empty')
process.exit(1)
}
const libItem = require('./lib/item.js')
const cdnFile = libItem.parse(cnfBase, cnf.cdnFile)
const txtFile = libItem.parse(cnfBase, cnf.txtFile)
//
const outFile = {}
let cdnType = cnf.outFile.cdnType
if (cdnType == null) {
console.warn('need `outFile.cdnType`, use `dryrun` as default')
cdnType = 'dryrun'
}
outFile.cdnType = cdnType
let txtType = cnf.outFile.txtType
if (txtType == null) {
console.warn('need `outFile.txtType`, use `dryrun` as default')
txtType = 'dryrun'
}
outFile.txtType = txtType
const result = {
cnfBase: cnfBase,
cdnFile: cdnFile,
txtFile: txtFile,
outFile: outFile
}
return result
}
module.exports = {
hash: hash,
mend: mend,
conf: conf
}