| 169 | * @param {boolean} [forTypings=false] - Keep CodeceptJS.* type names instead of expanding them. |
| 170 | */ |
| 171 | export async function buildLibWithDocs(forTypings = false) { |
| 172 | task.stopOnFailures() |
| 173 | const files = fs.readdirSync('lib/helper').filter(f => path.extname(f) === '.js') |
| 174 | ensureDir('docs/build') |
| 175 | |
| 176 | const partials = fs.readdirSync('docs/webapi').filter(f => path.extname(f) === '.mustache') |
| 177 | const placeholders = partials.map(file => `{{> ${path.basename(file, '.mustache')} }}`) |
| 178 | const templates = partials |
| 179 | .map(file => fs.readFileSync(`docs/webapi/${file}`).toString()) |
| 180 | .map(template => |
| 181 | template |
| 182 | .replace(/^/gm, ' * ') |
| 183 | .replace(/^/, '\n') |
| 184 | .replace(/\s*\* /, ''), |
| 185 | ) |
| 186 | |
| 187 | for (const file of files) { |
| 188 | const name = path.basename(file, '.js') |
| 189 | say(`Building helpers with docs for ${name}`) |
| 190 | copyFile(`lib/helper/${file}`, `docs/build/${file}`) |
| 191 | replaceInFile(`docs/build/${file}`, cfg => { |
| 192 | for (const i in placeholders) { |
| 193 | cfg.replace(placeholders[i], templates[i]) |
| 194 | } |
| 195 | if (!forTypings) { |
| 196 | cfg.replace(/CodeceptJS.LocatorOrString\?/g, '(string | object)?') |
| 197 | cfg.replace(/LocatorOrString\?/g, '(string | object)?') |
| 198 | cfg.replace(/CodeceptJS.LocatorOrString/g, 'string | object') |
| 199 | cfg.replace(/LocatorOrString/g, 'string | object') |
| 200 | cfg.replace(/CodeceptJS.StringOrSecret/g, 'string | object') |
| 201 | } |
| 202 | cfg.replace(/^import\s+([^'"`\s{]+)\s+from\s+['"`]([^'"`]+)['"`]/gm, "const $1 = require('$2')") |
| 203 | cfg.replace(/^import\s*\{\s*([^}]+)\s*\}\s*from\s+['"`]([^'"`]+)['"`]/gm, (match, imports, importPath) => { |
| 204 | if (imports.includes(' as ')) { |
| 205 | const parts = imports.split(',').map(i => i.trim()) |
| 206 | const assignments = parts.map(part => { |
| 207 | if (part.includes(' as ')) { |
| 208 | const [original, alias] = part.split(' as ').map(s => s.trim()) |
| 209 | return `const ${alias} = require('${importPath}').${original}` |
| 210 | } |
| 211 | return `const ${part} = require('${importPath}').${part}` |
| 212 | }) |
| 213 | return assignments.join(';\n') |
| 214 | } |
| 215 | return `const { ${imports} } = require('${importPath}')` |
| 216 | }) |
| 217 | cfg.replace(/^import\s+\*\s+as\s+([^'"`]+)\s+from\s+['"`]([^'"`]+)['"`]/gm, "const $1 = require('$2')") |
| 218 | |
| 219 | cfg.replace(/^export\s*\{\s*([^}]+)\s+as\s+default\s*\}/gm, 'module.exports = $1') |
| 220 | cfg.replace(/^export\s+default\s+(.+)/gm, 'module.exports = $1') |
| 221 | cfg.replace(/^export\s*\{\s*([^}]+)\s*\}/gm, 'module.exports = { $1 }') |
| 222 | cfg.replace(/^export\s+(class|function|const|let|var)\s+([^\s=]+)/gm, '$1 $2') |
| 223 | }) |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Generate documentation pages for all bundled helpers. |