-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.js
211 lines (185 loc) · 6.67 KB
/
sync.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* 設定
*/
const config = {
// Commerble Web APIの認証情報は環境変数で指定します。
// 使用する環境変数名は apiEndpointEnvKey,apiUsernameEnvKey,apiPasswordEnvKeyで指定します。
// 例 set CBAPI_ENDPOINT=http://172.31.51.85/data
apiEndpointEnvKey: 'CBAPI_ENDPOINT',
apiUsernameEnvKey: 'CBAPI_USERNAME',
apiPasswordEnvKey: 'CBAPI_PASSWORD',
apiItemLimit: 100,
templateDirPath: './templates',
mailTemplatePrefix: 'Mail',
sharedTemplates: [
'ModdSharedViewStart',
],
escapeTemplates: [
'BundleScript'
]
}
/**
* 以下同期スクリプト
*/
const chokidar = require('chokidar')
const fetch = require('node-fetch')
const fs = require('fs').promises
const path = require('path')
const stripBom = require('strip-bom')
const typeMap = {
template: '.txt',
cshtml: '.cshtml',
mail: '.cshtml',
csx: '.csx'
}
async function main() {
const mode = process.argv[2]
if (mode == 'all') {
await uploadAll()
return
}
if (mode == 'watch') {
await watch()
return
}
console.log(process.argv)
console.log('sync.js <mode>')
console.log('mode = upload | sync')
}
async function uploadAll(){
const files = await getFiles(config.templateDirPath)
const plan = []
let nameMaxSize = 0
for (const file of files) {
const relative = path.relative(config.templateDirPath, file)
const nameExt = relative.replace(/[\\\/]/g, '')
const ext = path.extname(nameExt)
const name = nameExt.replace(ext, '')
if (config.sharedTemplates.some(_ => _ == name)) {
plan.unshift(file)
} else {
plan.push(file)
}
nameMaxSize = Math.max(nameMaxSize, name.length)
}
for (const file of plan) {
await upload(file, nameMaxSize)
}
console.log('Done!')
}
async function watch() {
const files = await getFiles(config.templateDirPath)
chokidar.watch(config.templateDirPath, {
persistent: true
}).on('all', (event, file, stats) => {
if(file.endsWith('.cshtml')||file.endsWith('.csx')||file.endsWith('.txt')) {
if (event == 'add' && !files.includes(file)) {
upload(file)
}
else if(event == 'change') {
setTimeout(() => {upload(file)}, 500)
}
}
}).on('error', console.error)
}
async function upload(file, nameMaxSize = 28) {
const relative = path.relative(config.templateDirPath, file)
const nameExt = relative.replace(/[\\\/]/g, '')
const ext = path.extname(nameExt)
const name = nameExt.replace(ext, '')
const type = resolveType(name, ext)
const template = await getTemplate(name)
let text = stripBom(await fs.readFile(file, 'utf8'))
if (config.escapeTemplates.includes(name)) {
text = escapeTemplate(text)
}
const model = {
...(template || { Name: name }),
Type: type,
Text: text || `/*${name}*/`
}
const now = new Date()
const nowText = `${now.toLocaleTimeString()}.${now.getMilliseconds()}`.padEnd(12, '0')
const valid = await validateTemplate(model.Name, model.Type, model.Text)
if (valid.startsWith('NG')) {
console.error(`\x1b[31m[${nowText}] ${name.padEnd(nameMaxSize)}\t${valid}\x1b[0m`)
return
}
const result = await upsertTemplate(model)
if (result.startsWith('NG')) {
console.error(`[${nowText}] ${name.padEnd(nameMaxSize)}\t${result}`)
return
}
console.log(`[${nowText}] ${name.padEnd(nameMaxSize)}\t${result}`)
}
async function getFiles(root) {
const files = []
const dirs = [root]
do{
const dir = dirs.pop()
const items = await fs.readdir(dir, { withFileTypes: true })
for (const item of items) {
if (item.isFile())
files.push(path.join(dir, item.name))
else if (item.isDirectory())
dirs.push(path.join(dir, item.name))
}
}while(dirs.length > 0)
return files
}
async function getTemplate(name) {
let response = await fetch(ep() + `/meta/Templates?$select=Id,Name&$filter=Name eq '${name}'`, { headers: { Authorization: auth() }})
if (!response.ok) {
const text = await response.text()
console.error('fetch error:', text)
return []
}
const data = await response.json()
return data.value[0] || null
}
async function validateTemplate(name, type, content) {
const [url, model] = type == 'cshtml' ? [ ep() + '/template/validate', { Template: content, WithViews: !config.sharedTemplates.some(_ => _ == name) } ]
: type == 'mail' ? [ ep() + '/mail/validate', { Template: content } ]
: type == 'csx' ? [ ep() + '/query/validate', { Script: content } ]
: [null,null]
if (url == null)
return 'skip'
const response = await fetch(url, { method: 'post', headers: { Authorization: auth(), 'Content-Type': 'application/json' }, body: JSON.stringify(model) })
if (!response.ok) {
const contentType = response.headers.get('Content-Type')
if (contentType && contentType.includes('application/json')) {
const json = await response.json()
return 'NG:\n' + json.Message
}
else {
const text = await response.text()
return 'NG:\n' + text
}
}
return 'OK'
}
async function upsertTemplate(template) {
const [ method, url ] = template.Id ? [ 'put', ep() + `/meta/Templates(${template.Id})`] : ['post', ep() + '/meta/Templates']
const response = await fetch(url, { method, headers: { Authorization: auth(), 'Content-Type': 'application/json' }, body: JSON.stringify(template)})
if (!response.ok) {
const text = await response.text()
return 'NG:\n' + text
}
return 'OK'
}
function ep() {
const url = process.env[config.apiEndpointEnvKey]
return url.endsWith('/') ? url.splice(0, url.length - 1) : url
}
function auth() {
return 'Basic ' + Buffer.from(`${process.env[config.apiUsernameEnvKey]}:${process.env[config.apiPasswordEnvKey]}`).toString('base64')
}
function resolveType(name, ext) {
if (ext == typeMap.mail && name.startsWith(config.mailTemplatePrefix))
return 'mail'
return Object.keys(typeMap).find(key => typeMap[key] == ext)
}
function escapeTemplate(text) {
return text.replace(/\{\{/g, '{ {')
}
main().catch(message => console.error(message));