UNPKG

gauge-ts

Version:
86 lines (85 loc) 3.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Util = void 0; const node_child_process_1 = require("node:child_process"); const node_fs_1 = require("node:fs"); const node_path_1 = require("node:path"); const typescript_1 = require("typescript"); const uuid_1 = require("uuid"); // biome-ignore lint/complexity/noStaticOnlyClass: <explanation> class Util { static async importFile(file) { return await Promise.resolve(`${file}`).then(s => require(s)); } static writeFile(filePath, content) { (0, node_fs_1.writeFileSync)(filePath, content); } static exists(filePath) { return (0, node_fs_1.existsSync)(filePath); } static readFile(file) { return (0, node_fs_1.readFileSync)(file, "utf-8"); } static readFileBuffer(file) { return (0, node_fs_1.readFileSync)(file).buffer; } static spawn(command, args) { return (0, node_child_process_1.spawnSync)(command, args); } static isCustomParameterParser( // biome-ignore lint/suspicious/noExplicitAny: <explanation> object) { return object.canParse && object.parse !== undefined; } static getListOfFiles() { return Util.getImplDirs().reduce((files, dir) => { if (!(0, node_fs_1.existsSync)(dir)) { console.log(`Failed to load implementations from ${dir}`); return files; } return files.concat(Util.collectFilesIn(dir)); }, []); } static isTSFile(file) { return (0, node_path_1.extname)(file) === typescript_1.Extension.Ts; } static walkSync(dir, fileList = []) { const files = (0, node_fs_1.readdirSync)(dir); for (const file of files) { const filePath = (0, node_path_1.join)(dir, file); const stat = (0, node_fs_1.statSync)(filePath); if (stat.isDirectory()) { Util.walkSync(filePath, fileList); } else if (stat.isFile() && Util.isTSFile(file)) { fileList.push(filePath); } } return fileList; } static collectFilesIn(dir) { return Util.walkSync(dir); } static getImplDirs() { const projectRoot = process.env.GAUGE_PROJECT_ROOT; if (process.env.STEP_IMPL_DIR) { return process.env.STEP_IMPL_DIR.split(",").map((dir) => (0, node_path_1.join)(projectRoot, dir.trim())); } return [(0, node_path_1.join)(projectRoot, "tests")]; } static getNewTSFileName(dir) { let counter = 0; let fileName; do { const tmpl = `StepImplementation${counter || ""}${typescript_1.Extension.Ts}`; fileName = (0, node_path_1.join)(dir, tmpl); counter++; } while ((0, node_fs_1.existsSync)(fileName)); return fileName; } static getUniqueScreenshotFileName() { const dir = process.env.gauge_screenshots_dir ?? ""; return (0, node_path_1.join)(dir, `screenshot-${(0, uuid_1.v4)()}.png`); } } exports.Util = Util;