UNPKG

@saashub/qoq-utils

Version:
141 lines (134 loc) 4.61 kB
'use strict'; var localPkg = require('local-pkg'); var path = require('path'); var child_process = require('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) => localPkg.isPackageExists(name); const getPackageInfo = (name) => { const response = localPkg.getPackageInfoSync(name); if (!response) { throw new Error(`Package ${name} not installed!`); } return response; }; const getPackageJson = (cwd) => localPkg.loadPackageJSONSync(cwd); const getRelativePath = (path) => path.replace(process.cwd(), '.'); const resolveCwdPath = (path$1) => path.resolve(`${process.cwd()}${path$1}`); const resolveCwdRelativePath = (path) => getRelativePath(resolveCwdPath(path)); /* eslint-disable no-redeclare */ exports.EExitCode = void 0; (function (EExitCode) { EExitCode[EExitCode["OK"] = 0] = "OK"; EExitCode[EExitCode["ERROR"] = 1] = "ERROR"; EExitCode[EExitCode["EXCEPTION"] = 2] = "EXCEPTION"; })(exports.EExitCode || (exports.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 = child_process.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 ?? exports.EExitCode.OK); } }); }); } exports.executeCommand = executeCommand; exports.getPackageInfo = getPackageInfo; exports.getPackageJson = getPackageJson; exports.getRelativePath = getRelativePath; exports.isPackageInstalled = isPackageInstalled; exports.objectMergeRight = objectMergeRight; exports.resolveCwdPath = resolveCwdPath; exports.resolveCwdRelativePath = resolveCwdRelativePath;