seb-cli-tool
Version:
SEB CLI - Smart Embedded Board Configuration Tool - Cloud-First MCU Management
181 lines (153 loc) • 5.25 kB
JavaScript
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
// Colors for console output
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m'
};
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`);
}
function exec(command, options = {}) {
try {
return execSync(command, { encoding: 'utf8', stdio: 'pipe', ...options });
} catch (error) {
throw new Error(`Command failed: ${command}\n${error.message}`);
}
}
function checkGitStatus() {
log('Checking git status...', 'blue');
const status = exec('git status --porcelain');
if (status.trim()) {
log('⚠️ Warning: You have uncommitted changes!', 'yellow');
log('Consider committing or stashing changes before publishing.', 'yellow');
const proceed = process.argv.includes('--force');
if (!proceed) {
log('Use --force to proceed anyway.', 'cyan');
process.exit(1);
}
}
log('✅ Git status clean', 'green');
}
function checkNpmLogin() {
log('Checking npm login status...', 'blue');
try {
const whoami = exec('npm whoami');
log(`✅ Logged in as: ${whoami.trim()}`, 'green');
} catch (error) {
log('❌ Not logged in to npm', 'red');
log('Run: npm login', 'cyan');
process.exit(1);
}
}
function checkPackageJson() {
log('Validating package.json...', 'blue');
const packagePath = path.join(process.cwd(), 'package.json');
if (!fs.existsSync(packagePath)) {
log('❌ package.json not found', 'red');
process.exit(1);
}
const pkg = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
if (!pkg.name || !pkg.version) {
log('❌ Invalid package.json (missing name or version)', 'red');
process.exit(1);
}
log(`✅ Package: ${pkg.name}@${pkg.version}`, 'green');
return pkg;
}
function getPublishType() {
const args = process.argv.slice(2);
const type = args.find(arg => ['dev', 'beta', 'rc', 'prod', 'patch', 'minor', 'major'].includes(arg));
if (!type) {
log('Usage: node publish-workflow.js [dev|beta|rc|prod|patch|minor|major] [--force]', 'cyan');
log('\nPublishing types:', 'cyan');
log(' dev - Development version (1.0.17-dev.1)', 'cyan');
log(' beta - Beta version (1.0.17-beta.1)', 'cyan');
log(' rc - Release candidate (1.0.17-rc.1)', 'cyan');
log(' prod - Production patch (1.0.17 → 1.0.18)', 'cyan');
log(' patch - Production patch (1.0.17 → 1.0.18)', 'cyan');
log(' minor - Production minor (1.0.17 → 1.1.0)', 'cyan');
log(' major - Production major (1.0.17 → 2.0.0)', 'cyan');
process.exit(1);
}
return type;
}
function publish(type) {
log(`\n🚀 Publishing ${type} version...`, 'magenta');
let command;
switch (type) {
case 'dev':
command = 'npm run publish:dev';
break;
case 'beta':
command = 'npm run publish:beta';
break;
case 'rc':
command = 'npm run publish:rc';
break;
case 'prod':
case 'patch':
command = 'npm run publish:prod';
break;
case 'minor':
command = 'npm run publish:prod:minor';
break;
case 'major':
command = 'npm run publish:prod:major';
break;
default:
log(`❌ Unknown publish type: ${type}`, 'red');
process.exit(1);
}
try {
log(`Executing: ${command}`, 'cyan');
const output = exec(command, { stdio: 'inherit' });
log(`✅ Successfully published ${type} version!`, 'green');
// Show the new version
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
log(`📦 New version: ${pkg.version}`, 'green');
// Show installation instructions
if (['dev', 'beta', 'rc'].includes(type)) {
log(`\n🔧 To install this ${type} version:`, 'cyan');
log(` npm install -g seb-cli-tool@${type}`, 'cyan');
} else {
log(`\n🔧 This version is now the latest production release!`, 'cyan');
log(` npm install -g seb-cli-tool`, 'cyan');
}
} catch (error) {
log(`❌ Publishing failed: ${error.message}`, 'red');
process.exit(1);
}
}
function main() {
log('🔍 SEB CLI Tool - NPM Publishing Workflow', 'bright');
log('==========================================', 'bright');
const type = getPublishType();
const isProduction = ['prod', 'patch', 'minor', 'major'].includes(type);
if (isProduction) {
log(`\n⚠️ WARNING: You are about to publish a PRODUCTION version!`, 'red');
log('This will be the default version users get when they install the package.', 'red');
if (!process.argv.includes('--force')) {
log('\nTo confirm, run with --force flag:', 'cyan');
log(` node publish-workflow.js ${type} --force`, 'cyan');
process.exit(1);
}
}
checkGitStatus();
checkNpmLogin();
checkPackageJson();
publish(type);
log('\n🎉 Publishing workflow completed successfully!', 'green');
}
if (require.main === module) {
main();
}
module.exports = { publish, checkGitStatus, checkNpmLogin, checkPackageJson };