@mbc-cqrs-serverless/cli
Version:
a CLI to get started with MBC CQRS serverless framework
121 lines (120 loc) • 5.76 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.exportsForTesting = void 0;
exports.default = newAction;
const chalk_1 = __importDefault(require("chalk"));
const child_process_1 = require("child_process");
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const ui_1 = require("../ui");
/* eslint-disable no-console */
async function newAction(name = '', options, command) {
const [projectName, version = 'latest'] = name.split('@');
ui_1.logger.info(`Executing command '${command.name()}' for application '${projectName}' with options '${JSON.stringify(options)}'`);
let packageVersion;
if (version === 'latest') {
packageVersion = `^${getPackageVersion('@mbc-cqrs-serverless/core', true)[0]}`; // use the latest patch and minor versions
}
else {
const versions = getPackageVersion('@mbc-cqrs-serverless/core');
const regex = new RegExp(`^${version}(?![0-9]).*$`); // start with version and not directly follow by a digit
const matchVersions = versions.filter((v) => regex.test(v));
if (versions.includes(version)) {
packageVersion = version; // specific version
}
else if (matchVersions.length !== 0) {
packageVersion = `^${matchVersions.at(-1)}`; // use the patch and minor versions
}
else {
ui_1.logger.error(`The specified package version does not exist. Please choose a valid version! ${versions}`);
return;
}
}
const destDir = path_1.default.join(process.cwd(), projectName);
ui_1.logger.title('MBC', `Generating MBC cqrs serverless application in ${chalk_1.default.green(destDir)}`);
(0, fs_1.mkdirSync)(destDir, { recursive: true });
useTemplate(destDir);
usePackageVersion(destDir, packageVersion, projectName);
// mv gitignore .gitignore
const gitignore = path_1.default.join(destDir, 'gitignore');
(0, fs_1.copyFileSync)(gitignore, path_1.default.join(destDir, '.gitignore'));
(0, fs_1.unlinkSync)(gitignore);
// mv infra/gitignore infra/.gitignore
const infraGitignore = path_1.default.join(destDir, 'infra/gitignore');
(0, fs_1.copyFileSync)(infraGitignore, path_1.default.join(destDir, 'infra/.gitignore'));
(0, fs_1.unlinkSync)(infraGitignore);
// replace project_name in .env.local
updateEnvLocal(path_1.default.join(destDir, '.env.local'), '%%projectName%%', projectName);
// cp .env.local .env
(0, fs_1.copyFileSync)(path_1.default.join(destDir, '.env.local'), path_1.default.join(destDir, '.env'));
// git init
ui_1.logger.title('git', 'Initializing git repository.');
let logs = (0, child_process_1.execSync)('git init', { cwd: destDir });
ui_1.logger.success('Initialized a git repository.');
// npm install
ui_1.logger.title('deps', `Installing dependencies`);
logs = (0, child_process_1.execSync)('npm i --ignore-scripts', { cwd: destDir });
(0, child_process_1.execSync)('npx prisma generate', { cwd: destDir });
console.log(logs.toString());
ui_1.logger.success(`Dependencies installed`);
ui_1.logger.title('MBC', `Your application was created!`);
}
function useTemplate(destDir) {
if (isLatestCli()) {
(0, fs_1.cpSync)(path_1.default.join(__dirname, '../../templates'), destDir, {
recursive: true,
});
}
else {
(0, child_process_1.execSync)('npm i @mbc-cqrs-serverless/cli', { cwd: destDir });
(0, fs_1.cpSync)(path_1.default.join(destDir, 'node_modules/@mbc-cqrs-serverless/cli/templates'), destDir, { recursive: true });
(0, fs_1.rmSync)(path_1.default.join(destDir, 'node_modules'), {
recursive: true,
});
}
}
function isLatestCli() {
const latestVersion = getPackageVersion('@mbc-cqrs-serverless/cli', true)[0];
const packageJson = JSON.parse((0, fs_1.readFileSync)(path_1.default.join(__dirname, '../../package.json')).toString());
const curVersion = packageJson.version;
return latestVersion === curVersion;
}
function usePackageVersion(destDir, packageVersion, projectName) {
const packageJson = JSON.parse((0, fs_1.readFileSync)(path_1.default.join(__dirname, '../../package.json')).toString());
const fname = path_1.default.join(destDir, 'package.json');
const tplPackageJson = JSON.parse((0, fs_1.readFileSync)(fname).toString());
if (projectName) {
tplPackageJson.name = projectName;
}
tplPackageJson.dependencies['@mbc-cqrs-serverless/core'] = packageVersion;
tplPackageJson.devDependencies['@mbc-cqrs-serverless/cli'] =
packageJson.version;
(0, fs_1.writeFileSync)(fname, JSON.stringify(tplPackageJson, null, 2));
}
function getPackageVersion(packageName, isLatest = false) {
if (isLatest) {
const latestVersion = (0, child_process_1.execSync)(`npm view ${packageName} dist-tags.latest`)
.toString()
.trim();
return [latestVersion];
}
const versions = JSON.parse((0, child_process_1.execSync)(`npm view ${packageName} versions --json`).toString());
return versions;
}
function updateEnvLocal(envPath, searchValue, replaceValue) {
const envLocalContent = (0, fs_1.readFileSync)(envPath, 'utf8');
const newEnvLocalContent = envLocalContent.replaceAll(searchValue, replaceValue);
(0, fs_1.writeFileSync)(envPath, newEnvLocalContent);
}
exports.exportsForTesting = {
usePackageVersion,
getPackageVersion,
isLatestCli,
updateEnvLocal,
};
if (process.env.NODE_ENV !== 'test') {
exports.exportsForTesting = undefined;
}