UNPKG

@naxodev/gonx

Version:

Modern Nx plugin to use Go in a Nx workspace

120 lines 4.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.goBlueprintGenerator = goBlueprintGenerator; const devkit_1 = require("@nx/devkit"); const child_process_1 = require("child_process"); const path_1 = require("path"); const fs_1 = require("fs"); const normalize_options_1 = require("../../utils/normalize-options"); const generator_1 = require("../init/generator"); /** * Gets the path to the go-blueprint binary from the npm package */ function getGoBlueprintBinary() { try { return require.resolve('@melkeydev/go-blueprint/bin/go-blueprint'); } catch (error) { devkit_1.logger.error('go-blueprint npm package is not installed.\n' + 'This should not happen as it is a dependency of this package.'); throw new Error('go-blueprint binary not found in npm package'); } } /** * Builds the go-blueprint command arguments with the provided options */ function buildGoBlueprintArgs(projectName, blueprintOptions) { const args = ['create']; // Add required flags args.push('--name', projectName); args.push('--framework', blueprintOptions.framework); args.push('--driver', blueprintOptions.driver); args.push('--git', blueprintOptions.git); // Add advanced features if specified if (blueprintOptions.feature && blueprintOptions.feature.length > 0) { args.push('--advanced'); blueprintOptions.feature.forEach((feature) => { args.push('--feature', feature); }); } return args; } /** * Executes go-blueprint command in the specified directory using the npm package binary */ function executeGoBlueprintCommand(args, executionDir) { return new Promise((resolve, reject) => { var _a; try { const goBlueprintBin = getGoBlueprintBinary(); const childProcess = (0, child_process_1.fork)(goBlueprintBin, args, { cwd: executionDir, stdio: ['pipe', 'pipe', 'pipe', 'ipc'], env: { ...process.env, }, }); (_a = childProcess.stderr) === null || _a === void 0 ? void 0 : _a.on('data', (data) => { process.stderr.write(data); }); childProcess.on('close', (code) => { if (code === 0) { resolve(); } else { reject(new Error(`go-blueprint execution failed with exit code ${code}`)); } }); childProcess.on('error', (error) => { reject(new Error(`go-blueprint execution failed: ${error.message}`)); }); } catch (error) { reject(new Error(`Failed to start go-blueprint: ${error.message}`)); } }); } async function goBlueprintGenerator(tree, schema) { // Normalize options using the standard utility const options = await (0, normalize_options_1.normalizeOptions)(tree, schema, 'application'); // Initialize the workspace (adds necessary dependencies, etc.) await (0, generator_1.initGenerator)(tree, { skipFormat: true, addGoDotWork: schema.addGoDotWork, }); // Extract project name from normalized options const pathSegments = options.projectRoot .split('/') .filter((segment) => segment.length > 0); const projectName = pathSegments[pathSegments.length - 1]; // Create temporary files directory const filesDir = (0, path_1.join)(__dirname, 'files'); const tempProjectDir = (0, path_1.join)(filesDir, projectName); try { // Create the files directory if it doesn't exist if (!(0, fs_1.existsSync)(filesDir)) { (0, fs_1.mkdirSync)(filesDir, { recursive: true }); } // Build and execute go-blueprint command in the files directory const args = buildGoBlueprintArgs(projectName, schema); await executeGoBlueprintCommand(args, filesDir); // Use generateFiles to copy from the temporary files directory to the project root (0, devkit_1.generateFiles)(tree, tempProjectDir, options.projectRoot, { ...schema, projectName, tmpl: '', }); } finally { // Clean up the temporary files directory if ((0, fs_1.existsSync)(filesDir)) { (0, fs_1.rmSync)(filesDir, { recursive: true, force: true }); } } // Format files if not skipped if (!schema.skipFormat) { await (0, devkit_1.formatFiles)(tree); } } exports.default = goBlueprintGenerator; //# sourceMappingURL=go-blueprint.js.map