-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.lockable.js
417 lines (365 loc) · 13.4 KB
/
sync.lockable.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
* 設定
*/
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',
],
gitDefaultBranch: 'master',
disableUploadWatchAtDefaultBranch: true,
}
/**
* 以下同期スクリプト
*/
const chokidar = require('chokidar')
const fetch = require('node-fetch')
const fs = require('fs').promises
const path = require('path')
const { execSync } = require('child_process')
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
}
if (mode == 'unlock') {
const files = process.argv.slice(3);
await unlock(files);
return
}
console.log(process.argv)
console.log('sync.js <mode>')
console.log('mode = upload | sync | unlock')
}
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, false);
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 lockedTemplates = await getLockedTemplateNames();
if (lockedTemplates.length > 0) {
for (let name of lockedTemplates) {
console.error(`\x1b[31m${name}\x1b[0m`)
}
console.log('Panic!')
process.exit(1);
}
const [files, nameMaxSize] = await getPlan(config.templateDirPath);
for (const file of files) {
const success = await upload(file, false, 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, true).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, lock, nameMaxSize = 28) {
const [model, firstLine] = await getModel(file);
const now = new Date()
const nowText = `${now.toLocaleTimeString()}.${now.getMilliseconds()}`.padEnd(12, '0')
const modelName = model.Name.padEnd(nameMaxSize);
if (lock) {
if (config.disableUploadWatchAtDefaultBranch && isInDefaultBranch(await getHead())) {
console.error(`'watch' can not work in ${config.gitDefaultBranch} branch`);
process.exit(1);
}
if (isOldDefaultHead()) {
console.error(`${config.gitDefaultBranch} is old. You must pull it and merge to current branch.`);
process.exit(1);
}
if (hasAnyUpdatesInMain()) {
console.error(`${config.gitDefaultBranch} has some updates. You must merge to current branch. `);
process.exit(1);
}
const lockphrase = await getLockPhrase(model.Type);
if (firstLine === lockphrase || !firstLine.includes(LOCK_MAGIC)) {
model.Text = `${lockphrase}\n${model.Text}`;
}
else {
console.error(`\x1b[31m[${nowText}] ${modelName}\tNG: ${firstLine}\x1b[0m`)
return false;
}
}
const valid = await validateTemplate(model.Name, model.Type, model.Text)
if (valid.startsWith('NG')) {
console.error(`\x1b[31m[${nowText}] ${modelName}\t${valid}\x1b[0m`)
return false
}
const result = await upsertTemplate(model)
if (result.startsWith('NG')) {
console.error(`[${nowText}] ${modelName}\t${result}`)
return false
}
console.log(`[${nowText}] ${modelName}\t${result}`)
return true;
}
async function unlock(paths, nameMaxSize = 28) {
const now = new Date()
const nowText = `${now.toLocaleTimeString()}.${now.getMilliseconds()}`.padEnd(12, '0')
const files = await getFilesFromPath(paths);
for (let file of files) {
const [model, firstLine] = await getModel(file);
const lockphrase = await getLockPhrase(model.Type);
const modelName = model.Name.padEnd(nameMaxSize);
if (firstLine === lockphrase) {
const valid = await validateTemplate(model.Name, model.Type, model.Text)
if (valid.startsWith('NG')) {
console.error(`\x1b[31m[${nowText}] ${modelName}\t${valid}\x1b[0m`)
return false
}
const result = await upsertTemplate(model)
if (result.startsWith('NG')) {
console.error(`[${nowText}] ${modelName}\t${result}`)
return false
}
console.log(`[${nowText}] ${modelName}\tUNLOCK`);
}
else if (firstLine.includes(LOCK_MAGIC)) {
console.error(`\x1b[31m[${nowText}] ${modelName}\tNG: ${firstLine}\x1b[0m`)
}
else {
console.log(`[${nowText}] ${modelName}\tignore`);
}
}
}
const LOCK_MAGIC = '!!!locked!!!';
async function getLockPhrase(type) {
const head = await getHead();
const text = `${LOCK_MAGIC} in ${head}`;
if (type === 'cshtml' || type === 'mail') {
return `@* ${text} *@`
}
else if (type === 'csx' || type === 'template') {
return `/* ${text} */`
}
else {
return '';
}
}
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, withFirstLine = true) {
const [name, type] = resolveTemplateInfo(file)
const template = await getTemplate(name, withFirstLine);
let firstLine = null;
if (withFirstLine) {
firstLine = template?.Text?.split('\n',1)[0].trim() || '';
}
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}*/`
}, firstLine]
}
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 getFilesFromPath(paths) {
const files = []
for(let item of paths) {
const stats = await fs.stat(item);
if (stats.isDirectory()) {
files.push(...await getFiles(item));
}
else if (stats.isFile) {
files.push(item);
}
}
return files;
}
async function getTemplate(name, withText) {
let response = await fetch(ep() + `/meta/Templates?$select=Id,Name${withText?',Text':''}&$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 getLockedTemplateNames() {
const [method, url] = ['get', ep() + `/meta/Templates/?$select=Name,Text&$filter=contains(Text,'${encodeURIComponent(LOCK_MAGIC)}')`];
const response = await fetch(url, { method, headers: { Authorization: auth() } })
if (!response.ok ) {
throw new Error(await response.text());
}
const data = await response.json();
return data.value.map(d => `${d.Name}\t${d.Text.split('\n',1)[0].trim()}` );
}
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, '{{<"{{"}}')
}
async function getHead() {
return (await fs.readFile('.git/HEAD', 'utf8')).substring(5).split('\n',1)[0].trim();
}
function git(command) {
return execSync(`git ${command}`).toString();
}
function isInDefaultBranch(ref) {
return ref === 'refs/heads/' + config.gitDefaultBranch
}
function isOldDefaultHead() {
const remote = git(`ls-remote origin ${config.gitDefaultBranch}`).split('\t', 1)[0];
const local = git(`log --pretty=oneline -1 ${config.gitDefaultBranch}`).split(' ', 1)[0];
return remote != local;
}
function hasAnyUpdatesInMain() {
const mergedBranches = git('branch --merged').split('\n').map(l => l.trim());
return !mergedBranches.includes(config.gitDefaultBranch)
}
main().catch(message => console.error(message));