UNPKG

@naxodev/gonx

Version:

Modern Nx plugin to use Go in a Nx workspace

78 lines 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildStringFlagIfValid = exports.buildFlagIfEnabled = exports.executeCommand = exports.extractProjectRoot = void 0; exports.extractCWD = extractCWD; const devkit_1 = require("@nx/devkit"); const child_process_1 = require("child_process"); const path_1 = require("path"); const fileutils_1 = require("nx/src/utils/fileutils"); /** * Extract the project root from the executor context. * * @param context the executor context */ const extractProjectRoot = (context) => context.projectsConfigurations.projects[context.projectName].root; exports.extractProjectRoot = extractProjectRoot; /** * Execute and log a command, then return the result to executor. * * @param parameters the parameters of the command * @param options the options of the command */ const executeCommand = async (parameters = [], options = {}) => { try { const { executable = 'go', cwd = null, env = {} } = options; const command = [executable, ...parameters].join(' '); devkit_1.logger.info(`Executing command: ${command}`); (0, child_process_1.execSync)(command, { cwd, env: Object.assign(process.env, env), stdio: [0, 1, 2], }); return { success: true }; } catch (error) { devkit_1.logger.error(error); return { success: false }; } }; exports.executeCommand = executeCommand; /** * Add a flag to an array of parameter if it is enabled. * * @param flag the flag to add * @param enabled true if flag should be added */ const buildFlagIfEnabled = (flag, enabled) => enabled ? [flag] : []; exports.buildFlagIfEnabled = buildFlagIfEnabled; /** * Add a string flag to an array of parameter if it is valid. * * @param flag the flag to add * @param value the value of the flag */ const buildStringFlagIfValid = (flag, value) => (value ? [`${flag}=${value}`] : []); exports.buildStringFlagIfValid = buildStringFlagIfValid; /** * Extracts the current working directory (CWD) for the build process. * If the 'main' option is provided, returns the directory containing the main.go file. * Otherwise, returns the project root directory. * * @param options - The build executor schema options. * @param context - The executor context. * @returns The resolved CWD path. */ function extractCWD(options, context) { const projectRoot = (0, exports.extractProjectRoot)(context); const projectName = context.projectName; if (options.main) { const mainFilePath = (0, path_1.join)(projectRoot, options.main); if (!(0, fileutils_1.fileExists)(mainFilePath)) { throw new Error(`Main file ${options.main} does not exist in project ${projectName}`); } // Return the directory containing the main.go file return (0, path_1.dirname)(mainFilePath); } return projectRoot; } //# sourceMappingURL=execute-command.js.map