UNPKG

europa-build

Version:

Tool for generating and maintaining Europa plugins and presets

125 lines 5.69 kB
/* * Copyright (C) 2022 neocotic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import { exec, execFile } from 'child_process'; import { EOL } from 'os'; import { promisify } from 'util'; import { CommonScriptProvider } from "./CommonScriptProvider"; const execute = promisify(exec); const executeFile = promisify(execFile); /** * An abstract {@link ScriptProvider} that provides logic useful for executing commands to support scripts. */ export class CommandScriptProvider extends CommonScriptProvider { /** * Creates an instance of {@link CommandScriptProvider} using the `options` provided. * * @param options - The options to be used. */ constructor(options) { super(options); } /** * Executes the command with the specified name using the `args` provided within the given directory and returns * anything written to STDOUT by the command. * * @param directoryPath - The path of the directory in which the command should be executed. * @param commandName - The name of the command to be executed. * @param [args] - Any arguments to be passed to the command during execution. * @return Anything written to STDOUT by the command. * @throws If an error occurs while attempting to execute the command. */ async exec(directoryPath, commandName, args = []) { this.getLogger().debug(`Executing '${commandName}' command with arguments [${args}] in directory: '${directoryPath}'`); try { const command = args.length ? [commandName].concat(...args).join(' ') : commandName; const { stdout } = await execute(command, { cwd: directoryPath }); this.logCommandOutput(commandName, stdout); return stdout; } catch (e) { throw new Error(`Failed to execute '${commandName}' command: ${CommandScriptProvider.getExecErrorMessage(e)}`); } } /** * Executes the bundled command with the specified name using the `args` provided within the given directory and * returns anything written to STDOUT by the command. * * @param directoryPath - The path of the directory in which the command should be executed. * @param commandName - The name of the bundled command to be executed. * @param [args] - Any arguments to be passed to the bundled command during execution. * @return Anything written to STDOUT by the bundled command. * @throws If an error occurs while attempting to execute the bundled command. */ async execBundledCommand(directoryPath, commandName, args = []) { const commandPath = await this.getBundledCommandPath(commandName); this.getLogger().debug(`Executing '${commandName}' command with arguments [${args}] in directory: '${directoryPath}'`); try { const { stdout } = await executeFile(commandPath, args, { cwd: directoryPath }); this.logCommandOutput(commandName, stdout); return stdout; } catch (e) { throw new Error(`Failed to execute '${commandName}' command: ${CommandScriptProvider.getExecErrorMessage(e)}`); } } /** * Returns the path of the bundled command. * * @param commandName - The name of the bundled command whose path is to be returned. * @return The path of the bundled command. */ getBundledCommandPath(commandName) { return this.getBundledPath('node_modules/.bin', commandName); } static getExecErrorMessage(error) { if (!error) { return 'An unknown error occurred'; } if (error instanceof Error) { if (CommandScriptProvider.hasErrorStderr(error)) { return error.stderr; } if (CommandScriptProvider.hasErrorStdout(error)) { return error.stdout; } } return `${error}`; } static hasErrorField(error, fieldName) { return fieldName in error && typeof error[fieldName] === 'string' && !!error[fieldName]; } static hasErrorStderr(error) { return CommandScriptProvider.hasErrorField(error, 'stderr'); } static hasErrorStdout(error) { return CommandScriptProvider.hasErrorField(error, 'stdout'); } logCommandOutput(commandName, output) { const logger = this.getLogger(); if (logger.isDebugEnabled()) { logger.debug(output ? `'${commandName}' command provided output:${EOL}${output}` : `'${commandName}' command provided no output`); } } } //# sourceMappingURL=CommandScriptProvider.js.map