|
| 1 | +import { renderTemplate, GerminatorError, Helpers } from '@germinator/core'; |
| 2 | +import * as Handlebars from 'handlebars'; |
| 3 | +import * as YAML from 'js-yaml'; |
| 4 | +import * as faker from 'faker'; |
| 5 | +import { Chance } from 'chance'; |
| 6 | +import * as bcrypt from 'bcrypt'; |
| 7 | +import moment from 'moment'; |
| 8 | +import get from 'lodash.get'; |
| 9 | +import handlebarHelpers from 'handlebars-helpers'; |
| 10 | +import repeatHelper from 'handlebars-helper-repeat-root-fixed'; |
| 11 | + |
| 12 | +// insecure password hashed cache - uses same salt for all passwords! |
| 13 | +// hashSync is really slow so this is useful for mock data |
| 14 | +// we can't provide a seed to getSalt either so we'll just hard code one |
| 15 | +const passwordCache: { [plainText: string]: string } = {}; |
| 16 | +const insecurePasswordSalt = '$2b$10$lAuv4qM.z6qZwQ/WhmHvEu'; |
| 17 | + |
| 18 | +export function makeHelpers(chance: Chance.Chance = new Chance()): Helpers { |
| 19 | + const helpers = { |
| 20 | + ...handlebarHelpers, |
| 21 | + repeat: repeatHelper, |
| 22 | + array(...args: any[]) { |
| 23 | + return args.slice(0, args.length - 1); |
| 24 | + }, |
| 25 | + concat(...args: any[]) { |
| 26 | + return args.slice(0, args.length - 1).join(''); |
| 27 | + }, |
| 28 | + moment( |
| 29 | + date?: string | Date, |
| 30 | + ...args: ( |
| 31 | + | string |
| 32 | + | { |
| 33 | + hash: { |
| 34 | + format?: string; |
| 35 | + utc?: boolean; |
| 36 | + [op: string]: string | any; |
| 37 | + }; |
| 38 | + data: { |
| 39 | + root: any; |
| 40 | + [key: string]: any; |
| 41 | + }; |
| 42 | + } |
| 43 | + )[] |
| 44 | + ) { |
| 45 | + if (date && date instanceof Date) { |
| 46 | + date = date.toISOString(); |
| 47 | + } |
| 48 | + |
| 49 | + if (!date || typeof date !== 'string') { |
| 50 | + throw new GerminatorError('moment helper requires a date {{moment date}}'); |
| 51 | + } |
| 52 | + |
| 53 | + const [ctx] = args.splice(-1, 1); |
| 54 | + const hash = (ctx && typeof ctx !== 'string' && ctx.hash) || {}; |
| 55 | + const data = (ctx && typeof ctx !== 'string' && ctx.data) || { root: {} }; |
| 56 | + |
| 57 | + const { format, utc } = hash; |
| 58 | + |
| 59 | + let value = utc ? moment.utc(date) : moment(date); |
| 60 | + |
| 61 | + for (const arg of args) { |
| 62 | + if (!arg || typeof arg !== 'string') { |
| 63 | + throw new GerminatorError( |
| 64 | + 'moment helper received an undefined argument (did you forget to quote an operation? "[add,days,5]")', |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + let op = YAML.load( |
| 70 | + // we actually render this string, so that "[add,{{x}},days]" is convenient |
| 71 | + renderTemplate(arg, { ...data.root, ...data, root: undefined }, helpers), |
| 72 | + ); |
| 73 | + |
| 74 | + if (!Array.isArray(op)) { |
| 75 | + // {{moment "utcdate" utc=true local}} |
| 76 | + op = [op]; |
| 77 | + } |
| 78 | + |
| 79 | + const [method, ...methodArgs] = op as any[]; |
| 80 | + |
| 81 | + if (!(value as any)[method]) { |
| 82 | + throw new GerminatorError(`no such moment operation existed (${method})`); |
| 83 | + } |
| 84 | + |
| 85 | + value = (value as any)[method](...methodArgs); |
| 86 | + } catch (err) { |
| 87 | + throw new GerminatorError(`failed to parse "${arg}" moment helper argument`); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return format ? value.format(format) : value.toISOString(); |
| 92 | + }, |
| 93 | + momentAdd(count: number, period: string) { |
| 94 | + if (count === undefined || typeof count !== 'number') { |
| 95 | + throw new GerminatorError('momentAdd helper requires {{momentAdd 5 "days"}}'); |
| 96 | + } |
| 97 | + |
| 98 | + if (period === undefined || typeof period !== 'string') { |
| 99 | + throw new GerminatorError('momentAdd helper requires {{momentAdd 5 "days"}}'); |
| 100 | + } |
| 101 | + |
| 102 | + return `[add,${count},${period}]`; |
| 103 | + }, |
| 104 | + momentSubtract(count: number, period: string) { |
| 105 | + if (!count || typeof count !== 'number') { |
| 106 | + throw new GerminatorError('momentSubtract helper requires {{momentSubtract 5 "days"}}'); |
| 107 | + } |
| 108 | + |
| 109 | + if (!period || typeof period !== 'string') { |
| 110 | + throw new GerminatorError('momentSubtract helper requires {{momentSubtract 5 "days"}}'); |
| 111 | + } |
| 112 | + |
| 113 | + return `[subtract,${count},${period}]`; |
| 114 | + }, |
| 115 | + password(password?: string, ctx?: { hash: { rounds?: number; insecure?: boolean } }) { |
| 116 | + if (!password || typeof password !== 'string') { |
| 117 | + throw new GerminatorError('password helper requires password {{password "pwd"}}'); |
| 118 | + } |
| 119 | + |
| 120 | + const rounds = ctx?.hash?.rounds ?? 10; |
| 121 | + const insecure = ctx?.hash?.insecure; |
| 122 | + |
| 123 | + if (insecure) { |
| 124 | + if (!passwordCache[password]) { |
| 125 | + passwordCache[password] = bcrypt.hashSync(password, insecurePasswordSalt); |
| 126 | + } |
| 127 | + |
| 128 | + return passwordCache[password]; |
| 129 | + } |
| 130 | + |
| 131 | + return bcrypt.hashSync(password, rounds); |
| 132 | + }, |
| 133 | + faker(name: string | object | undefined, ctx?: { hash: any }) { |
| 134 | + if (!name || typeof name === 'object') { |
| 135 | + throw new GerminatorError('faker helper requires data type {{faker "email"}}'); |
| 136 | + } |
| 137 | + |
| 138 | + const fn = get(faker, name); |
| 139 | + |
| 140 | + if (!fn) { |
| 141 | + throw new GerminatorError(`${name} is not a valid faker.js value type`); |
| 142 | + } |
| 143 | + |
| 144 | + return fn(ctx && Object.keys(ctx.hash).length > 0 ? { ...ctx.hash } : undefined); |
| 145 | + }, |
| 146 | + chance(name: string, ...args: any[]) { |
| 147 | + if (!name || typeof name === 'object') { |
| 148 | + throw new GerminatorError('chance helper requires data type {{chance "email"}}'); |
| 149 | + } |
| 150 | + |
| 151 | + const fn = (chance as any)[name]; |
| 152 | + |
| 153 | + if (!fn) { |
| 154 | + throw new GerminatorError(`${name} is not a valid chance value type`); |
| 155 | + } |
| 156 | + |
| 157 | + const [ctx]: { hash: any }[] = args; |
| 158 | + |
| 159 | + // If the first argument is not the chance context (containing hash), |
| 160 | + // assume we are passing non-object args to chance (eg. array or scalar) |
| 161 | + if (!ctx.hash) { |
| 162 | + return fn.call(chance, ...args.slice(0, args.length - 1)); |
| 163 | + } |
| 164 | + |
| 165 | + if (name === 'date' && ctx) { |
| 166 | + const { min, max, ...opts } = ctx.hash; |
| 167 | + const minDate = min !== undefined && new Date(min); |
| 168 | + const maxDate = max !== undefined && new Date(max); |
| 169 | + |
| 170 | + // we'll help out by toISOString here |
| 171 | + const date = moment.utc(chance.date({ ...opts, min: minDate, max: maxDate })); |
| 172 | + |
| 173 | + return date.toISOString(); |
| 174 | + } |
| 175 | + |
| 176 | + return fn.call(chance, ctx ? { ...ctx.hash } : {}); |
| 177 | + }, |
| 178 | + }; |
| 179 | + |
| 180 | + return helpers; |
| 181 | +} |
0 commit comments