'use strict'; var childProcess = require('child_process'); /** @type {{ [kind: string]: string[] }} */ var KIND_LANGS = { js: ['js', 'javascript'], sh: ['sh'], }; /** @param {readonly string[]} evalLangs */ function normalizeKinds(evalLangs) { /** @type {string[]} */ var kinds = []; evalLangs.forEach(function (lang) { var kind = (lang === 'javascript') ? 'js' : lang; if (kind && kinds.indexOf(kind) === -1) { kinds.push(kind); } }); return kinds; } /** @param {string | undefined} content */ function parsePromptBlock(content) { var lines = String(content || '').split(/\r\n?|\n/); /** @type {{ command: string, output: string[] }[]} */ var commands = []; /** @type {false | { command: string, output: string[] }} */ var current = false; lines.forEach(function (line) { var match = line.match(/^[$%>]\s+(.*)$/); if (match) { current = { command: match[1], output: [] }; commands.push(current); } else if (current) { current.output.push(line); } }); return commands.map(function (item) { return ({ command: item.command, expected: item.output.join('\n') }); }); } /** @param {string} command */ function runPromptCommand(command) { var mergeStderrIntoStdout = "( ".concat(command, " ) 2>&1"); return new Promise(function (resolve) { childProcess.exec(mergeStderrIntoStdout, function (error, stdout) { resolve({ code: (error) ? ((typeof error.code === 'number') ? error.code : 1) : 0, output: String((stdout === null || stdout === undefined) ? '' : stdout), }); }); }); } /** * @param {{ command: string, expected: string }} item * @param {{ code: number, output: string }} result */ function checkPromptCommand(item, result) { var actual = String(result.output).replace(/\r\n/g, '\n').replace(/\n+$/, ''); var expected = String(item.expected).replace(/\r\n/g, '\n').replace(/\n+$/, ''); if (result.code !== 0) { return new Error("command `".concat(item.command, "` exited with code ").concat(result.code).concat(actual ? "\n".concat(actual) : '')); } if (actual !== expected) { return new Error([ "command `".concat(item.command, "` output did not match:"), '--- expected ---', expected, '--- actual ---', actual, ].join('\n')); } return false; } module.exports = { KIND_LANGS: KIND_LANGS, normalizeKinds: normalizeKinds, parsePromptBlock: parsePromptBlock, runPromptCommand: runPromptCommand, checkPromptCommand: checkPromptCommand, };