-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.js
271 lines (239 loc) · 8.48 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
* 設定
*/
const config = {
// Commerble Web APIの認証情報は環境変数で指定します。
// 使用する環境変数名は apiEndpointEnvKey,apiUsernameEnvKey,apiPasswordEnvKeyで指定します。
// 例 set CBAPI_ENDPOINT=http://172.31.51.85/data
apiEndpointEnvKey: 'CBAPI_SB_ENDPOINT',
apiUsernameEnvKey: 'CBAPI_SB_USERNAME',
apiPasswordEnvKey: 'CBAPI_SB_PASSWORD',
apiItemLimit: 100,
templateDirPath: './templates',
mailTemplatePrefix: 'Mail',
sharedTemplates: [
'ModdSharedViewStart',
'ModdSharedHelpers',
'ModdSharedFunctions',
],
escapeTemplates: [
'BundleStyle',
'BundleScript',
]
}
/**
* 以下同期スクリプト
*/
const chokidar = require('chokidar')
const fetch = require('node-fetch')
const fs = require('fs').promises
const path = require('path')
function stripBom(string) {
if (typeof string !== 'string') {
throw new TypeError(`Expected a string, got ${typeof string}`);
}
// Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
// conversion translates it to FEFF (UTF-16 BOM).
if (string.charCodeAt(0) === 0xFEFF) {
return string.slice(1);
}
return string;
}
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 validateAll() {
const [files, nameMaxSize] = await getPlan(config.templateDirPath);
for (const file of files) {
const last = files[files.length - 1] === file;
const model = await getModel(file);
const valid = await validateTemplate(model.Name, model.Type, model.Text);
const prefix = last ? '└' : '├';
if (valid.startsWith('NG')) {
console.error(`\x1b[31m\t${prefix} ${model.Name.padEnd(nameMaxSize)}\t${valid}\x1b[0m`)
}
else {
console.log(`\t${prefix} ${model.Name.padEnd(nameMaxSize)}\t${valid}`)
}
}
}
async function uploadAll() {
const [files, nameMaxSize] = await getPlan(config.templateDirPath);
for (const file of files) {
const success = await upload(file, nameMaxSize)
if (!success) {
console.log('Panic!')
process.exit(1);
}
}
console.log('Done!')
}
async function watch() {
const files = await getFiles(config.templateDirPath)
function unitOfWork(file) {
upload(file).then(success => {
if (success) {
const [name] = resolveTemplateInfo(file);
if (config.sharedTemplates.includes(name)) {
validateAll();
}
}
})
}
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)) {
unitOfWork(file)
}
else if (event == 'change') {
setTimeout(() => { unitOfWork(file) }, 500)
}
}
}).on('error', console.error)
}
async function upload(file, nameMaxSize = 28) {
const model = await getModel(file);
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}] ${model.Name.padEnd(nameMaxSize)}\t${valid}\x1b[0m`)
return false
}
const result = await upsertTemplate(model)
if (result.startsWith('NG')) {
console.error(`[${nowText}] ${model.Name.padEnd(nameMaxSize)}\t${result}`)
return false
}
console.log(`[${nowText}] ${model.Name.padEnd(nameMaxSize)}\t${result}`)
return true;
}
function resolveTemplateInfo(file) {
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)
return [name, type];
}
async function getModel(file) {
const [name, type] = resolveTemplateInfo(file)
const template = await getTemplate(name)
let text = stripBom(await fs.readFile(file, 'utf8'))
if (config.escapeTemplates.includes(name)) {
text = escapeTemplate(text)
}
return {
...(template || { Name: name }),
Type: type,
Text: text || `/*${name}*/`
}
}
async function getPlan(root) {
const files = await getFiles(root)
const sorted = []
let nameMaxSize = 0
for (const file of files) {
const relative = path.relative(root, file)
const nameExt = relative.replace(/[\\\/]/g, '')
const ext = path.extname(nameExt)
const name = nameExt.replace(ext, '')
if (config.sharedTemplates.some(_ => _ == name)) {
sorted.unshift(file)
} else {
sorted.push(file)
}
nameMaxSize = Math.max(nameMaxSize, name.length)
}
return [sorted, nameMaxSize];
}
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' + response.statusText + 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.slice(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));