cfn-forge
Version:
CloudFormation deployment automation tool with git-based workflows
161 lines (137 loc) • 3.97 kB
JavaScript
/**
* CFN-Forge Status Command
*
* Shows project and git repository status
*/
const fs = require('fs');
const chalk = require('chalk');
const { execSync } = require('child_process');
const yaml = require('js-yaml');
/**
* Check if current directory is a git repository
*/
function checkGitRepository() {
try {
execSync('git rev-parse --is-inside-work-tree', { stdio: 'pipe' });
return true;
} catch (error) {
return false;
}
}
/**
* Get git repository information
*/
function getGitInfo() {
if (!checkGitRepository()) {
return null;
}
try {
const branch = execSync('git branch --show-current', { encoding: 'utf8' }).trim();
const status = execSync('git status --porcelain', { encoding: 'utf8' }).trim();
const hasChanges = status.length > 0;
const changedFiles = status ? status.split('\n').length : 0;
return {
branch,
hasChanges,
changedFiles,
status,
};
} catch (error) {
return { error: error.message };
}
}
/**
* Get CFN-Forge project information
*/
function getProjectInfo() {
const configExists = fs.existsSync('.cfn-forge.yaml');
if (!configExists) {
return { inProject: false };
}
try {
const configContent = fs.readFileSync('.cfn-forge.yaml', 'utf8');
const config = yaml.load(configContent);
// Check for template files
const templateFiles = [
'template.yaml',
'template.yml',
'cloudformation.yaml',
'cloudformation.yml',
'stack.yaml',
'stack.yml',
].filter((file) => fs.existsSync(file));
// Check for parameter files
const parameterFiles = [
'parameters.json',
'parameters.yaml',
'params.json',
].filter((file) => fs.existsSync(file));
return {
inProject: true,
config,
templateFiles,
parameterFiles,
};
} catch (error) {
return {
inProject: true,
error: error.message,
};
}
}
/**
* Status command implementation
*/
async function statusCommand(options) {
const projectInfo = getProjectInfo();
const gitInfo = getGitInfo();
if (options.json) {
console.log(JSON.stringify({ project: projectInfo, git: gitInfo }, null, 2));
return;
}
console.log(chalk.cyan('CFN-Forge Status\n'));
// Project Status
console.log(chalk.bold('Project Status:'));
if (projectInfo.inProject) {
console.log(chalk.green('CFN-Forge project detected'));
if (projectInfo.config?.name) {
console.log(chalk.white(` Project: ${projectInfo.config.name}`));
}
if (projectInfo.templateFiles.length > 0) {
console.log(chalk.white(` Templates: ${projectInfo.templateFiles.join(', ')}`));
} else {
console.log(chalk.yellow(' No template files found'));
}
if (projectInfo.parameterFiles.length > 0) {
console.log(chalk.white(` Parameters: ${projectInfo.parameterFiles.join(', ')}`));
}
if (projectInfo.error) {
console.log(chalk.yellow(` Config error: ${projectInfo.error}`));
}
} else {
console.log(chalk.yellow('Not in a CFN-Forge project'));
console.log(chalk.white(' Run `cfn-forge init` to initialize a project'));
}
console.log();
// Git Status
console.log(chalk.bold('Git Status:'));
if (gitInfo) {
if (gitInfo.error) {
console.log(chalk.red(`Git error: ${gitInfo.error}`));
} else {
console.log(chalk.green('Git repository detected'));
console.log(chalk.white(` Branch: ${gitInfo.branch}`));
if (gitInfo.hasChanges) {
console.log(chalk.yellow(` ${gitInfo.changedFiles} uncommitted changes`));
console.log(chalk.white(' Run `git status` for details'));
} else {
console.log(chalk.green(' Working directory clean'));
}
}
} else {
console.log(chalk.yellow('Not a git repository'));
console.log(chalk.white(' Run `git init` to initialize git repository'));
}
console.log();
}
module.exports = { statusCommand };