UNPKG

poku

Version:

🐷 Poku makes testing easy for Node.js, Bun, Deno, and you at the same time.

132 lines (131 loc) 3.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.enforce = void 0; const promises_1 = require("fs/promises"); const node_process_1 = require("process"); const poku_js_1 = require("../configs/poku.js"); const get_arg_js_1 = require("../parsers/get-arg.js"); const format_js_1 = require("./format.js"); const write_js_1 = require("./write.js"); const errors = []; const pathExists = async (arg, path) => { try { await (0, promises_1.stat)(path); } catch { errors.push(`--${arg}: ./${path} doesn't exists.`); } }; const checkUselessValue = (arg) => { const prefix = arg.length === 1 ? '-' : '--'; if (typeof (0, get_arg_js_1.getArg)(arg, prefix) !== 'undefined') errors.push(`${prefix}${arg}: this flag shouldn't receive a value.`); }; const checkRequiredValue = (arg) => { const prefix = arg.length === 1 ? '-' : '--'; if ((0, get_arg_js_1.hasArg)(arg, prefix) && typeof (0, get_arg_js_1.getArg)(arg, prefix) === 'undefined') errors.push(`${prefix}${arg}: this flag require a value.`); }; const checkFlags = () => { const allowedFlags = new Set([ '--concurrency', '--config', '--debug', '--denoAllow', '--denoCjs', '--denoDeny', '--enforce', '--envFile', '--exclude', '--failFast', '--filter', '--killPid', '--killPort', '--killRange', '--only', '--quiet', '--sequential', '--watch', '--watchInterval', '-c', '-d', '-q', '-w', '-x', ]); const args = node_process_1.argv.slice(2); for (const arg of args) { const flag = arg.split('=')[0]; if (!allowedFlags.has(flag) && flag.startsWith('-')) errors.push(`${flag}: unrecognized flag.`); } }; const checkValues = async () => { for (const flag of [ 'debug', 'enforce', 'failFast', 'only', 'quiet', 'sequential', 'watch', 'd', 'x', 'q', 'w', ]) checkUselessValue(flag); for (const flag of [ 'concurrency', 'config', 'killPid', 'killPort', 'watchInterval', 'c', ]) checkRequiredValue(flag); if (poku_js_1.GLOBAL.configFile) await pathExists('config', poku_js_1.GLOBAL.configFile); if (poku_js_1.GLOBAL.envFile) await pathExists('envFile', poku_js_1.GLOBAL.envFile); if ((0, get_arg_js_1.getArg)('concurrency') && typeof poku_js_1.GLOBAL.configs.concurrency === 'undefined') errors.push('--concurrency: expects for a valid integer.'); }; const checkConfigFile = () => { const allowedProps = new Set([ '$schema', 'include', 'sequential', 'debug', 'filter', 'exclude', 'failFast', 'envFile', 'exclude', 'failFast', 'concurrency', 'quiet', 'envFile', 'kill', 'platform', 'deno', ]); for (const prop in poku_js_1.GLOBAL.configsFromFile) { if (!allowedProps.has(prop)) errors.push(`${prop}: unrecognized property in the config file.`); } }; const enforce = async () => { checkFlags(); checkConfigFile(); await checkValues(); if (errors.length > 0) { (0, write_js_1.hr)(); (0, write_js_1.log)(`${(0, format_js_1.format)('Ensure Enabled').bold()}\n`); (0, write_js_1.log)(errors.map((flag) => (0, format_js_1.format)(flag).fail()).join('\n')); (0, write_js_1.hr)(); (0, node_process_1.exit)(1); } }; exports.enforce = enforce;