UNPKG

@viewdo/dxp-story-cli

Version:
86 lines (70 loc) 2.14 kB
const fs = require('fs') const path = require('path') Array.prototype.diff = function(a) { return this.filter(function(i) {return a.indexOf(i) < 0;}); } const flattenArray = list => list.reduce( (a, b) => a.concat(Array.isArray(b) ? flattenArray(b) : b), [] ) const resolvePath = (object, key, child_key, sub_key) => { let path = object[key] if(path && path[child_key]){ if(path[child_key] && path[child_key][sub_key]) path = path[child_key][sub_key] else path = path[child_key] } return (typeof path == 'string') ? path : null } const distinctArray = list => [...new Set(list)] const reconcileLists = (current, previous, protected_items) => { let remove = previous .filter(namespace => !current.includes(namespace) && !protected_items.includes(namespace)) let add = current .filter(namespace => !previous.includes(namespace) && !protected_items.includes(namespace)) return { add, remove } } const isValidDomain = (v) => { if (!v) return false; var re = /^(?!:\/\/)([a-zA-Z0-9-\*]+\.){0,5}[a-zA-Z0-9-][a-zA-Z0-9-]+\.[a-zA-Z]{2,64}?$/gi; return re.test(v); } const requireValue = (name) => { throw new Error(`A value for ${name} is required`.red) } const logger = (msg) => { console.log('ViewDO Pipe: '.blue + msg) } const execShell = (cmd, options = {}) => { const shell = require('shelljs') console.log(cmd.blue) let command = shell.exec(cmd, options) if(command.code > 0) throw new Error(command.stdout) return command } const findFiles = (dir, file_list = []) => { return fs.readdirSync(dir) .map(file => fs.statSync(path.join(dir, file)).isDirectory() ? findFiles(path.join(dir, file), file_list) : file_list.concat(path.join(dir, file))[0]) } const replaceInText = (vars, text) => { let result = text.toString() Object.getOwnPropertyNames(vars).forEach(p => { result = result.replace('$' + p.toUpperCase(), vars[p].toString()) }) return result } module.exports = { distinctArray, findFiles, isValidDomain, flattenArray, requireValue, reconcileLists, replaceInText, resolvePath, execShell, logger }