UNPKG

@gatling.io/cli

Version:

Gatling JS is a JavaScript/TypeScript interface for the [Gatling load testing tool](https://gatling.io/).

76 lines (75 loc) 3.21 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.compileProtoFiles = void 0; const node_child_process_1 = require("node:child_process"); const promises_1 = __importDefault(require("node:fs/promises")); const node_path_1 = __importDefault(require("node:path")); const log_1 = require("../log"); const compileProtoFiles = async (protocPath, protoFolder, protoTargetFolder) => { await promises_1.default.rm(protoTargetFolder, { recursive: true, force: true }); const protoFiles = await getFiles(protoFolder, ""); const allGeneratedFiles = []; for (const protoFile of protoFiles) { const sourceFile = node_path_1.default.join(protoFolder, protoFile); const protoPath = node_path_1.default.dirname(sourceFile); const targetFile = protoFile + "c"; const targetPath = node_path_1.default.join(protoTargetFolder, targetFile); await promises_1.default.mkdir(node_path_1.default.dirname(targetPath), { recursive: true }); await runProtoc(protocPath, sourceFile, protoPath, targetPath); allGeneratedFiles.push(targetFile); } if (protoFiles.length > 0) { await promises_1.default.writeFile(node_path_1.default.join(protoTargetFolder, "compiled-protobuf-files"), allGeneratedFiles.join("\n"), { encoding: "utf-8" }); } }; exports.compileProtoFiles = compileProtoFiles; const getFiles = async (dir, relativePath) => { const dirents = await listFiles(dir); const files = await Promise.all(dirents.map((dirent) => { const res = node_path_1.default.resolve(dir, dirent.name); return dirent.isDirectory() ? getFiles(res, node_path_1.default.join(relativePath, dirent.name)) : dirent.name.endsWith(".proto") ? [node_path_1.default.join(relativePath, dirent.name)] : []; })); return files.flat(); }; const listFiles = async (dir) => { try { return await promises_1.default.readdir(dir, { withFileTypes: true }); } catch (e) { if (e?.code === "ENOENT") { // Directory does not exist: there are no proto files to compile. return []; } else { throw e; } } }; const runProtoc = async (protocPath, sourceFile, protoPath, targetFile) => { // FIXME can't build simulations without the bundle... const args = [`--proto_path=${protoPath}`, `--descriptor_set_out=${targetFile}`, sourceFile]; const spawned = (0, node_child_process_1.spawn)(protocPath, args, { env: process.env, stdio: [process.stdin, process.stdout, process.stderr] }); return new Promise((resolve, reject) => { spawned.on("error", (error) => log_1.logger.error("Failed to run protoc process: " + error.toString())); spawned.on("close", (code) => { if (code === 0) { resolve(); } else { reject(Error("Protoc process finished with code " + code)); } }); }); };