nitp-build-tools
Version:
Official build system for NIT Patna's backend services - A powerful Maven-based build automation tool with cross-platform support
162 lines (140 loc) • 4.7 kB
JavaScript
/**
* NITP Build Tools - Main Library Entry Point
*
* This file provides programmatic access to the build system functionality
* for use in other Node.js applications.
*
* Created by: Ashish Kumar (https://github.com/ashishkr375)
*/
const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process');
const chalk = require('chalk');
class NITPBuildSystem {
constructor(options = {}) {
this.config = this.loadConfig();
this.options = {
env: options.env || 'dev',
verbose: options.verbose || false,
cwd: options.cwd || process.cwd()
};
}
/**
* Load build configuration
*/
loadConfig() {
try {
const configPath = path.join(__dirname, 'nitp-build-v2.json');
return JSON.parse(fs.readFileSync(configPath, 'utf8'));
} catch (error) {
throw new Error(`Failed to load configuration: ${error.message}`);
}
}
/**
* Execute a build command
* @param {string} commandName - Name of the command to execute
* @param {object} options - Execution options
* @returns {Promise} - Promise that resolves when command completes
*/
async execute(commandName, options = {}) {
const command = this.config.scripts[commandName];
if (!command) {
throw new Error(`Command not found: ${commandName}`);
}
const execOptions = {
...this.options,
...options
};
return this.executeCommand(command.command, execOptions);
}
/**
* Execute raw command
* @param {string} command - Command to execute
* @param {object} options - Execution options
* @returns {Promise} - Promise that resolves when command completes
*/
async executeCommand(command, options = {}) {
const { env = 'dev', verbose = false, cwd = process.cwd() } = options;
const args = command.split(' ');
const cmd = args[0];
const cmdArgs = args.slice(1);
return new Promise((resolve, reject) => {
const child = spawn(cmd, cmdArgs, {
stdio: verbose ? 'inherit' : 'pipe',
shell: true,
cwd,
env: {
...process.env,
SPRING_PROFILES_ACTIVE: env
}
});
let output = '';
let errorOutput = '';
if (!verbose) {
child.stdout?.on('data', (data) => {
output += data.toString();
});
child.stderr?.on('data', (data) => {
errorOutput += data.toString();
});
}
child.on('close', (code) => {
if (code === 0) {
resolve({ code, output, errorOutput });
} else {
const error = new Error(`Command failed with code ${code}`);
error.code = code;
error.output = output;
error.errorOutput = errorOutput;
reject(error);
}
});
child.on('error', (error) => {
reject(error);
});
});
}
/**
* Get available commands
* @returns {Array} - Array of available command names
*/
getAvailableCommands() {
return Object.keys(this.config.scripts);
}
/**
* Get command information
* @param {string} commandName - Name of the command
* @returns {object} - Command configuration
*/
getCommandInfo(commandName) {
return this.config.scripts[commandName];
}
/**
* Get available modules
* @returns {Array} - Array of available module names
*/
getAvailableModules() {
return Object.keys(this.config.modules);
}
/**
* Get available environments
* @returns {Array} - Array of available environment names
*/
getAvailableEnvironments() {
return Object.keys(this.config.environments);
}
}
// Export classes and utilities
module.exports = {
NITPBuildSystem,
chalk,
// Helper functions
async createBuildSystem(options) {
return new NITPBuildSystem(options);
},
// Constants
VERSION: '2.1.0',
AUTHOR: 'Ashish Kumar',
GITHUB_URL: 'https://github.com/ashishkr375',
LINKEDIN_URL: 'https://www.linkedin.com/in/ashish-kumar-nitp/'
};