UNPKG

@wdio/cli

Version:
60 lines (59 loc) 1.96 kB
export class EjsHelpers { useTypeScript; useEsm; constructor(config) { this.useTypeScript = config.useTypeScript ?? false; this.useEsm = config.useEsm ?? false; } if(condition, trueValue, falseValue = '') { return condition ? trueValue : falseValue; } ifTs = (trueValue, falseValue = '') => this.if(this.useTypeScript, trueValue, falseValue); ifEsm = (trueValue, falseValue = '') => this.if(this.useEsm, trueValue, falseValue); param(name, type) { return this.useTypeScript ? `${name}: ${type}` : name; } returns(type) { return this.useTypeScript ? `: ${type}` : ''; } import(exports, moduleId) { const individualExports = exports.split(',').map(id => id.trim()); const imports = this.useTypeScript ? individualExports : individualExports.filter(id => !id.startsWith('type ')); if (!imports.length) { return ''; } const modulePath = this.modulePathFrom(moduleId); return this.useEsm || this.useTypeScript ? `import { ${imports.join(', ')} } from '${modulePath}'` : `const { ${imports.join(', ')} } = require('${modulePath}')`; } modulePathFrom(moduleId) { if (!(moduleId.startsWith('.') && this.useEsm)) { return moduleId; } if (moduleId.endsWith('/') && this.useEsm) { return moduleId + 'index.js'; } return moduleId + '.js'; } export(keyword, name) { if (this.useTypeScript) { return `export ${keyword} ${name}`; } if (this.useEsm) { return `export ${keyword} ${name}`; } if (['class', 'function'].includes(keyword)) { return `module.exports.${name} = ${keyword} ${name}`; } return `module.exports.${name}`; } }