-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
62 lines (48 loc) · 1.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
const debug = require('debug')('plugin:faker')
const { faker } = require('@faker-js/faker')
const get = require('lodash.get')
module.exports.Plugin = FakerPlugin
module.exports.pluginInterfaceVersion = 2
function FakerPlugin (script, events) {
this.script = script
this.events = events
this.locale = this.script.config.plugins.faker.locale
if (this.locale) {
faker.locale = this.locale
debug('Locale set to', this.locale)
}
script.config.processor = script.config.processor || {}
script.config.processor.fakerPluginCreateVariables = fakerPluginCreateVariables
script.scenarios.forEach(scenario => {
scenario.beforeScenario = scenario.beforeScenario || []
scenario.beforeScenario.push('fakerPluginCreateVariables')
})
debug('Plugin initialized')
return this
}
function fakerPluginCreateVariables (req, userContext, events, done) {
let ctx = userContext
let cb = done
if (arguments.length === 3) {
// called as a "function"
ctx = req
cb = events
}
const variables = ctx.vars
if (!variables || variables.length === 0) {
return cb()
}
Object.keys(variables).forEach(key => {
const val = variables[key]
if (/\$faker\.[A-Za-z.]+$/.test(val)) {
const path = val.replace('$faker.', '')
const func = get(faker, path)
if (func && typeof func === 'function') {
const fakedValue = func()
variables[key] = fakedValue
debug(`fakerPluginCreateVariables: ${path} -> ${fakedValue}`)
}
}
})
return cb()
}