@re-shell/cli
Version:
Full-stack development platform uniting microservices and microfrontends. Build complete applications with .NET (ASP.NET Core Web API, Minimal API), Java (Spring Boot, Quarkus, Micronaut, Vert.x), Rust (Actix-Web, Warp, Rocket, Axum), Python (FastAPI, Dja
212 lines (211 loc) • 8.44 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildMicrofrontend = buildMicrofrontend;
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const child_process_1 = require("child_process");
const util_1 = require("util");
const chalk_1 = __importDefault(require("chalk"));
const spinner_1 = require("../utils/spinner");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
/**
* Builds one or all microfrontends in the project
*
* @param name - Name of the microfrontend to build (optional, builds all if omitted)
* @param options - Build options
* @version 0.1.0
*/
async function buildMicrofrontend(name, options = {}) {
const { spinner } = options;
try {
// Validate we're in a Re-Shell project
if (spinner) {
spinner.setText('Validating Re-Shell project...');
}
const isInReshellProject = fs.existsSync('package.json') && (fs.existsSync('apps') || fs.existsSync('packages'));
if (!isInReshellProject) {
if (spinner) {
spinner.stop();
(0, spinner_1.flushOutput)();
}
throw new Error('Not in a Re-Shell project. Please run this command from the root of a Re-Shell project.');
}
// Build env variables
const env = {
...process.env,
NODE_ENV: options.production ? 'production' : 'development',
};
if (name) {
// Build a specific microfrontend
if (spinner) {
spinner.setText(`Validating microfrontend "${name}"...`);
}
const mfPath = path.resolve(process.cwd(), 'apps', name);
if (!fs.existsSync(mfPath)) {
if (spinner) {
spinner.stop();
(0, spinner_1.flushOutput)();
}
throw new Error(`Microfrontend "${name}" not found in apps directory.`);
}
const packageJsonPath = path.join(mfPath, 'package.json');
if (!fs.existsSync(packageJsonPath)) {
if (spinner) {
spinner.stop();
(0, spinner_1.flushOutput)();
}
throw new Error(`package.json not found for microfrontend "${name}".`);
}
// Determine build command based on package.json
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
let buildCommand = 'npm run build';
if (fs.existsSync(path.join(mfPath, 'pnpm-lock.yaml'))) {
buildCommand = 'pnpm build';
}
else if (fs.existsSync(path.join(mfPath, 'yarn.lock'))) {
buildCommand = 'yarn build';
}
// Optionally add bundle analysis
if (options.analyze) {
buildCommand += ' -- --analyze';
}
if (spinner) {
spinner.setText(`Building microfrontend "${name}"...`);
}
else {
console.log(chalk_1.default.cyan(`Building microfrontend "${name}"...`));
}
const originalCwd = process.cwd();
process.chdir(mfPath);
try {
const { stdout, stderr } = await execAsync(buildCommand, { env });
if (spinner) {
spinner.stop();
(0, spinner_1.flushOutput)();
}
if (stdout)
console.log(stdout);
if (stderr)
console.error(stderr);
console.log(chalk_1.default.green(`✓ Successfully built microfrontend "${name}"`));
}
catch (error) {
if (spinner) {
spinner.stop();
(0, spinner_1.flushOutput)();
}
throw new Error(`Failed to build microfrontend "${name}": ${error.message}`);
}
finally {
process.chdir(originalCwd);
}
}
else {
// Build all microfrontends
if (spinner) {
spinner.setText('Scanning for microfrontends...');
}
const appsDir = path.resolve(process.cwd(), 'apps');
if (!fs.existsSync(appsDir)) {
if (spinner) {
spinner.stop();
(0, spinner_1.flushOutput)();
}
throw new Error('Apps directory not found. Is this a valid Re-Shell project?');
}
// Get all directories in the apps folder
const appDirs = fs
.readdirSync(appsDir, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
.filter(dirName => {
const packageJsonPath = path.join(appsDir, dirName, 'package.json');
return fs.existsSync(packageJsonPath);
});
if (appDirs.length === 0) {
if (spinner) {
spinner.stop();
(0, spinner_1.flushOutput)();
}
console.log(chalk_1.default.yellow('No microfrontends found to build.'));
return;
}
if (spinner) {
spinner.setText(`Building ${appDirs.length} microfrontend${appDirs.length > 1 ? 's' : ''}...`);
}
else {
console.log(chalk_1.default.cyan(`Building ${appDirs.length} microfrontend${appDirs.length > 1 ? 's' : ''}...`));
}
// Use the project's package manager
let buildCommand = 'npm run build';
if (fs.existsSync('pnpm-lock.yaml')) {
buildCommand = 'pnpm run build';
}
else if (fs.existsSync('yarn.lock')) {
buildCommand = 'yarn build';
}
try {
const { stdout, stderr } = await execAsync(buildCommand, { env });
if (spinner) {
spinner.stop();
(0, spinner_1.flushOutput)();
}
if (stdout)
console.log(stdout);
if (stderr)
console.error(stderr);
console.log(chalk_1.default.green(`✓ Successfully built all microfrontends`));
}
catch (error) {
if (spinner) {
spinner.stop();
(0, spinner_1.flushOutput)();
}
throw new Error(`Failed to build microfrontends: ${error.message}`);
}
}
}
catch (error) {
if (spinner) {
spinner.stop();
(0, spinner_1.flushOutput)();
}
throw error;
}
}