UNPKG

@saashub/qoq-utils

Version:
132 lines (126 loc) 4.37 kB
import { getPackageInfoSync, loadPackageJSONSync, isPackageExists } from 'local-pkg'; import { resolve } from 'path'; import { spawn } from 'child_process'; const walk = (first, second) => { const firstObjectKeys = Object.keys(first); const mergedObject = firstObjectKeys.reduce((acc, key) => { if (Object.prototype.hasOwnProperty.call(second, key) && second[key] === undefined) { return acc; } const firstValue = first[key]; if (Object.prototype.hasOwnProperty.call(second, key)) { const secondValue = second[key]; if (typeof firstValue === 'object' && !Array.isArray(firstValue) && typeof secondValue === 'object' && !Array.isArray(secondValue)) { acc[key] = walk(firstValue, secondValue); } else { try { acc[key] = structuredClone(secondValue); } catch { acc[key] = secondValue; } } } else { try { acc[key] = structuredClone(firstValue); } catch { acc[key] = firstValue; } } return acc; }, {}); Object.keys(second) .filter((key) => !firstObjectKeys.includes(key)) .forEach((key) => { try { mergedObject[key] = structuredClone(second[key]); } catch { mergedObject[key] = second[key]; } }); return mergedObject; }; /** * Method merges objects and tries to clone them with `structuredClone` * Object on the right have precedence in every key, and If key is set to 'undefined' * it will be removed from object on the left. * * @param first input object * @param args input objects * @returns merged object */ function objectMergeRight(first, ...args) { if (args.length < 1) { throw new Error('objectMergeRight needs at least two objects as arguments!'); } const [second, third, ...rest] = args; const mergedObject = walk(first, second); if (third) { return objectMergeRight(mergedObject, third, ...rest); } return mergedObject; } const isPackageInstalled = (name) => isPackageExists(name); const getPackageInfo = (name) => { const response = getPackageInfoSync(name); if (!response) { throw new Error(`Package ${name} not installed!`); } return response; }; const getPackageJson = (cwd) => loadPackageJSONSync(cwd); const getRelativePath = (path) => path.replace(process.cwd(), '.'); const resolveCwdPath = (path) => resolve(`${process.cwd()}${path}`); const resolveCwdRelativePath = (path) => getRelativePath(resolveCwdPath(path)); /* eslint-disable no-redeclare */ var EExitCode; (function (EExitCode) { EExitCode[EExitCode["OK"] = 0] = "OK"; EExitCode[EExitCode["ERROR"] = 1] = "ERROR"; EExitCode[EExitCode["EXCEPTION"] = 2] = "EXCEPTION"; })(EExitCode || (EExitCode = {})); async function executeCommand(command, args = [], stdio = 'inherit', captureOutput = false) { // const commandArgs return new Promise((resolve, reject) => { // eslint-disable-next-line sonarjs/os-command const child = spawn(command, args, { shell: true, stdio, }); let capturedOutput = ''; if (child.stdout) { child.stdout.on('data', (data) => { if (captureOutput) { capturedOutput += data.toString('utf-8'); } else { process.stdout.write(data.toString('utf-8')); } }); } if (child.stderr) { child.stderr.on('data', (data) => { process.stderr.write(data.toString('utf-8')); }); } child.on('error', (error) => { reject(error); }); child.on('close', (code) => { if (captureOutput) { resolve(capturedOutput); } else { resolve(code ?? EExitCode.OK); } }); }); } export { EExitCode, executeCommand, getPackageInfo, getPackageJson, getRelativePath, isPackageInstalled, objectMergeRight, resolveCwdPath, resolveCwdRelativePath };