forked from eduardoboucas/staticman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaticman.js
506 lines (400 loc) · 13.7 KB
/
Staticman.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// https://github.com/bashlk/staticman-netlify-function/blob/master/functions/staticman/staticman.js
/* eslint-disable no-underscore-dangle */
import markdownTable from 'markdown-table'
import moment from 'moment'
import NodeRSA from 'node-rsa'
import objectPath from 'object-path'
import slugify from 'slug'
import { v1 as uuidv1 } from 'uuid'
import yaml from 'js-yaml'
import SiteConfig from './siteConfig.js'
import parse_config from './config.js'
import errorHandler from './ErrorHandler.js'
import * as gitFactory from './GitServiceFactory.js'
import * as Transforms from './Transforms.js'
// eslint-disable-next-line no-console
const log = console.log.bind(console)
class Staticman {
constructor(parameters) {
// eslint-disable-next-line no-constructor-return
return (async () => {
this.parameters = parameters
this.config = parse_config()
const {
repository,
username,
branch = 'main',
} = parameters
// To avoid encoding / secret / transport / environment variable issues,
// the RSA key's value can have substituted any [NEWLINE] character in the string to
// [SPACE] characters. So now update the values to valid PEM format.
const key = 'githubPrivateKey'
const v = this.config.get(key)
this.config.set(key, v?.trim()
.replace(/ +/g, ' ')
.replace(/ /g, '\n')
.replace(/BEGIN\nRSA\nPRIVATE\nKEY-----\s*/, 'BEGIN RSA PRIVATE KEY-----\n')
.replace(/\s*-----END\nRSA\nPRIVATE\nKEY-----/, '\n-----END RSA PRIVATE KEY-----'))
// Initialise the Git service API
this.git = await gitFactory.create(this.config, {
branch,
repository,
username,
})
// Generate unique id
this.uid = uuidv1()
// NOTE: this key is used to decrypt any (unlikely to happen with our streamlined setup)
// encrypted strings (eg: tokens or secrets) in a user's `staticman.yml` file.
// To keep things simple, we'll simply use the *required* `githubPrivateKey` RSA key for this
// so that a user doesn't have to make a second RSA private key just for this.
this.rsa = new NodeRSA()
this.rsa.importKey(this.config.get('githubPrivateKey'), 'private')
return this
})()
}
_applyInternalFields(data) {
const internalFields = {
_id: this.uid,
}
// Inject parent, if present
if (this.options.parent) {
internalFields._parent = this.options.parent
}
return Object.assign(internalFields, data)
}
_applyGeneratedFields(data) {
const generatedFields = this.siteConfig.get('generatedFields')
if (!generatedFields) return data
Object.keys(generatedFields).forEach((field) => {
const generatedField = generatedFields[field]
if ((typeof generatedField === 'object') && (!(generatedField instanceof Array))) {
const options = generatedField.options || {}
// eslint-disable-next-line default-case
switch (generatedField.type) {
case 'date':
// eslint-disable-next-line no-param-reassign
data[field] = this._createDate(options)
break
// TODO: Remove 'github' when v2 API is no longer supported
case 'github':
case 'user':
if (this.gitUser && typeof options.property === 'string') {
// eslint-disable-next-line no-param-reassign
data[field] = objectPath.get(this.gitUser, options.property)
}
break
case 'slugify':
if (
typeof options.field === 'string' &&
typeof data[options.field] === 'string'
) {
// eslint-disable-next-line no-param-reassign
data[field] = slugify(data[options.field]).toLowerCase()
}
break
}
} else {
// eslint-disable-next-line no-param-reassign
data[field] = generatedField
}
})
return data
}
_applyTransforms(fields) {
const transforms = this.siteConfig.get('transforms')
if (!transforms) return Promise.resolve(fields)
// This doesn't serve any purpose for now, but we might want to have
// asynchronous transforms in the future.
const queue = []
Object.keys(transforms).forEach((field) => {
if (!fields[field]) return
const transformNames = [].concat(transforms[field])
transformNames.forEach((transformName) => {
const transformFn = Transforms[transformName]
if (transformFn) {
// eslint-disable-next-line no-param-reassign
fields[field] = transformFn(fields[field])
}
})
})
return Promise.all(queue).then(() => fields)
}
// eslint-disable-next-line class-methods-use-this
_createDate(options = {}) {
const date = new Date()
switch (options.format) {
case 'timestamp':
return date.getTime()
case 'timestamp-seconds':
return Math.floor(date.getTime() / 1000)
case 'iso8601':
default:
return date.toISOString()
}
}
_createFile(fields) {
return new Promise((resolve, reject) => {
switch (this.siteConfig.get('format').toLowerCase()) {
case 'json':
// eslint-disable-next-line no-promise-executor-return
return resolve(JSON.stringify(fields))
case 'yaml':
case 'yml':
try {
const output = yaml.safeDump(fields)
// eslint-disable-next-line no-promise-executor-return
return resolve(output)
} catch (err) {
// eslint-disable-next-line no-promise-executor-return
return reject(err)
}
case 'frontmatter':
// eslint-disable-next-line no-case-declarations
const transforms = this.siteConfig.get('transforms')
// eslint-disable-next-line no-case-declarations
const contentField = transforms && Object.keys(transforms).find((field) => transforms[field] === 'frontmatterContent')
if (!contentField) {
// eslint-disable-next-line no-promise-executor-return
return reject(errorHandler('NO_FRONTMATTER_CONTENT_TRANSFORM'))
}
// eslint-disable-next-line no-case-declarations
const content = fields[contentField]
// eslint-disable-next-line no-case-declarations
const attributeFields = { ...fields }
delete attributeFields[contentField]
try {
const output = `---\n${yaml.safeDump(attributeFields)}---\n${content}\n`
// eslint-disable-next-line no-promise-executor-return
return resolve(output)
} catch (err) {
// eslint-disable-next-line no-promise-executor-return
return reject(err)
}
default:
// eslint-disable-next-line no-promise-executor-return
return reject(errorHandler('INVALID_FORMAT'))
}
})
}
_generateReviewBody(fields) {
const table = [
['Field', 'Content'],
]
Object.keys(fields).forEach((field) => {
table.push([field, fields[field]])
})
return this.siteConfig.get('pullRequestBody') + markdownTable(table)
}
_getNewFilePath(data) {
const configFilename = this.siteConfig.get('filename')
const filename = (configFilename && configFilename.length)
? this._resolvePlaceholders(configFilename, {
fields: data,
options: this.options,
})
: this.uid
let path = this._resolvePlaceholders(this.siteConfig.get('path'), {
fields: data,
options: this.options,
})
// Remove trailing slash, if existing
if (path.slice(-1) === '/') {
path = path.slice(0, -1)
}
const extension = this.siteConfig.get('extension').length
? this.siteConfig.get('extension')
: this._getExtensionForFormat(this.siteConfig.get('format'))
return `${path}/${filename}.${extension}`
}
// eslint-disable-next-line class-methods-use-this, consistent-return
_getExtensionForFormat(format) {
// eslint-disable-next-line default-case
switch (format.toLowerCase()) {
case 'json':
return 'json'
case 'yaml':
case 'yml':
return 'yml'
case 'frontmatter':
return 'md'
}
}
_resolvePlaceholders(subject, baseObject) {
const matches = subject.match(/{(.*?)}/g)
if (!matches) return subject
matches.forEach((match) => {
const escapedMatch = match.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&')
const property = match.slice(1, -1)
let newText
switch (property) {
case '@timestamp':
newText = new Date().getTime()
break
case '@id':
newText = this.uid
break
default:
// eslint-disable-next-line no-case-declarations
const timeIdentifier = '@date:'
if (property.indexOf(timeIdentifier) === 0) {
const timePattern = property.slice(timeIdentifier.length)
newText = moment().format(timePattern)
} else {
newText = objectPath.get(baseObject, property) || ''
}
}
// eslint-disable-next-line no-param-reassign
subject = subject.replace(new RegExp(escapedMatch, 'g'), newText)
})
return subject
}
_validateConfig(config) {
if (!config) {
return errorHandler('MISSING_CONFIG_BLOCK')
}
const requiredFields = [
'allowedFields',
'branch',
'format',
'path',
]
const missingFields = []
// Checking for missing required fields
requiredFields.forEach((requiredField) => {
if (objectPath.get(config, requiredField) === undefined) {
missingFields.push(requiredField)
}
})
if (missingFields.length) {
return errorHandler('MISSING_CONFIG_FIELDS', {
data: missingFields,
})
}
this.siteConfig = SiteConfig(config, this.rsa)
return null
}
_validateFields(fields) {
const missingRequiredFields = []
const invalidFields = []
Object.keys(fields).forEach((field) => {
// Check for any invalid fields
if ((this.siteConfig.get('allowedFields').indexOf(field) === -1) && (fields[field] !== '')) {
invalidFields.push(field)
}
// Trim fields
if (typeof fields[field] === 'string') {
// eslint-disable-next-line no-param-reassign
fields[field] = fields[field].trim()
}
})
// Check for missing required fields
this.siteConfig.get('requiredFields').forEach((field) => {
if ((fields[field] === undefined) || (fields[field] === '')) {
missingRequiredFields.push(field)
}
})
if (missingRequiredFields.length) {
return errorHandler('MISSING_REQUIRED_FIELDS', {
data: missingRequiredFields,
})
}
if (invalidFields.length) {
return errorHandler('INVALID_FIELDS', {
data: invalidFields,
})
}
return null
}
decrypt(encrypted) {
return this.rsa.decrypt(encrypted, 'utf8')
}
getParameters() {
return this.parameters
}
getSiteConfig(force) {
if (this.siteConfig && !force) return Promise.resolve(this.siteConfig)
if (!this.configPath) return Promise.reject(errorHandler('NO_CONFIG_PATH'))
return this.git.readFile(this.configPath.file).then((data) => {
const config = objectPath.get(data, this.configPath.path)
const validationErrors = this._validateConfig(config)
if (validationErrors) {
return Promise.reject(validationErrors)
}
if (config.branch !== this.parameters.branch) {
return Promise.reject(errorHandler('BRANCH_MISMATCH'))
}
return this.siteConfig
})
}
processEntry(fields, options) {
this.fields = { ...fields }
this.options = { ...options }
log({ fields, options })
this.setConfigPath()
return this.getSiteConfig().then((/* _fields */) => {
// Validate fields
const fieldErrors = this._validateFields(fields)
log({ fieldErrors })
if (fieldErrors) return Promise.reject(fieldErrors)
// Add generated fields
// eslint-disable-next-line no-param-reassign
fields = this._applyGeneratedFields(fields)
// Apply transforms
return this._applyTransforms(fields)
// eslint-disable-next-line arrow-body-style
}).then((transformedFields) => {
return this._applyInternalFields(transformedFields)
// eslint-disable-next-line arrow-body-style
}).then((extendedFields) => {
// Create file
return this._createFile(extendedFields)
// eslint-disable-next-line newline-per-chained-call
}).then((data) => {
const filePath = this._getNewFilePath(fields)
const commitMessage = this._resolvePlaceholders(this.siteConfig.get('commitMessage'), {
fields,
options,
})
const newBranch = `staticman_${this.uid}`
return this.git.writeFileAndSendReview(
filePath,
data,
newBranch,
commitMessage,
this._generateReviewBody(fields),
)
// eslint-disable-next-line arrow-body-style, newline-per-chained-call
}).then((/* _result */) => {
return {
fields,
redirect: options.redirect ? options.redirect : false,
}
// eslint-disable-next-line newline-per-chained-call, arrow-body-style
}).catch((err) => {
return Promise.reject(errorHandler('ERROR_PROCESSING_ENTRY', {
err,
instance: this,
}))
})
}
processMerge(fields, options) {
this.fields = { ...fields }
this.options = { ...options }
return this.getSiteConfig().then((/* _config */) => true).catch((err) => Promise.reject(errorHandler('ERROR_PROCESSING_MERGE', {
err,
instance: this,
})))
}
setConfigPath(configPath) {
// Default config path
if (!configPath) {
this.configPath = {
file: 'staticman.yml',
path: this.parameters.property || '',
}
return
}
this.configPath = configPath
}
}
export default Staticman