Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/kasper9n/embler. Turn binaries into applications npmjs.com/package/embler
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162const JoiBase = require('joi')const path = require('path')const fs = require('fs')
module.exports.prepare = (workingDir) => { return JoiBase.extend((Joi) => { return { type: 'string', base: Joi.string(), messages: { 'string.endsWith': '{{#label}} must end with any of {{#endings}}', }, rules: { endsWith: { method(...endings) { return this.$_addRule({ name: 'endsWith', args: { endings } }) }, args: [ { name: 'endings', assert: Joi.array() }, ], validate(value, helpers, args) { for (const ending of args.endings) { if (value.endsWith(ending)) return value } return helpers.error('string.endsWith', { endings: args.endings }) }, }, }, } }).extend((Joi) => { return { type: 'path', base: Joi.string(), messages: { 'path.existingFile': '{{#label}} path must be an existing file', 'path.existingDir': '{{#label}} path must be an existing directory', }, validate(value) { return { value: path.resolve(workingDir, value) } }, rules: { existingFile: { method() { return this.$_addRule('existingFile') }, validate(value, helpers) { if (!fs.existsSync(value)) return helpers.error('path.existingFile') if (!fs.statSync(value).isFile()) return helpers.error('path.existingFile') return value }, }, existingDir: { method() { return this.$_addRule('existingDir') }, validate(value, helpers) { if (!fs.existsSync(value)) return helpers.error('path.existingDir') if (!fs.statSync(value).isDirectory()) return helpers.error('path.existingDir') return value }, }, }, } })}