UNPKG

k6-node

Version:

CLI tool that enables k6 installation via npm packages

65 lines (64 loc) 2.42 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.detectPlatform = exports.getK6BinaryPath = void 0; const node_fs_1 = __importDefault(require("node:fs")); const node_path_1 = __importDefault(require("node:path")); const node_os_1 = __importDefault(require("node:os")); const installer_1 = require("./install/installer"); /** * Gets the k6 binary based on: * - configuration (.k6path file) * - or installed binary */ const getK6BinaryPath = async () => { const moduleDir = process.cwd(); const k6PathFile = node_path_1.default.join(moduleDir, '.k6path'); // use binary from .k6path file if (node_fs_1.default.existsSync(k6PathFile)) { try { const customPath = node_fs_1.default.readFileSync(k6PathFile, 'utf8').trim(); if (node_fs_1.default.existsSync(customPath)) { console.debug(`Using k6 from custom path: ${customPath}`); return customPath; } else { throw new Error(`k6 binary not found at custom path specified in .k6path: ${customPath}`); } } catch (error) { if (error instanceof Error) { throw new Error(`Error reading .k6path file: ${error.message}`); } throw error; } } // use the internally downloaded binary const platform = (0, exports.detectPlatform)(); const internalBinaryPath = node_path_1.default.join(__dirname, 'k6', platform === 'windows' ? 'k6.exe' : 'k6'); if (node_fs_1.default.existsSync(internalBinaryPath)) { console.debug(`Using internally installed k6: ${internalBinaryPath}`); return internalBinaryPath; } console.log('k6 binary not found. Installing...'); return await (0, installer_1.installBinary)(); }; exports.getK6BinaryPath = getK6BinaryPath; /** * */ const detectPlatform = () => { switch (node_os_1.default.platform()) { case 'win32': return 'windows'; case 'darwin': return 'macos'; case 'linux': return 'linux'; default: throw new Error(`The current system "${require('node:os').platform()}" is not supported`); } }; exports.detectPlatform = detectPlatform;