UNPKG

@boostercloud/cli

Version:

CLI of the Booster Framework, the next level of abstraction for cloud-native applications

136 lines (135 loc) 6.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkCurrentDirIsABoosterProject = checkCurrentDirIsABoosterProject; exports.checkItIsABoosterProject = checkItIsABoosterProject; exports.checkProjectAlreadyExists = checkProjectAlreadyExists; exports.checkResourceExists = checkResourceExists; exports.checkCurrentDirBoosterVersion = checkCurrentDirBoosterVersion; const fs_extra_1 = require("fs-extra"); const path = require("path"); const project_initializer_1 = require("./project-initializer"); const brand_1 = require("../common/brand"); const generator_1 = require("./generator"); const filenames_1 = require("../common/filenames"); const logger_1 = require("../services/logger"); const user_prompt_1 = require("../services/user-prompt"); const semver_1 = require("../services/semver"); function checkIndexFileIsBooster(indexFilePath) { const contents = (0, fs_extra_1.readFileSync)(indexFilePath); if (!contents.includes('Booster.start(')) { throw new Error('The main application file does not start a Booster application. Verify you are in the right project'); } } async function checkCurrentDirIsABoosterProject() { return checkItIsABoosterProject(process.cwd()); } async function checkItIsABoosterProject(projectPath) { const projectAbsolutePath = path.resolve(projectPath); try { const tsConfigJsonContents = require(path.join(projectAbsolutePath, 'tsconfig.json')); const indexFilePath = path.normalize(path.join(projectAbsolutePath, tsConfigJsonContents.compilerOptions.rootDir, 'index.ts')); checkIndexFileIsBooster(indexFilePath); } catch (e) { throw new Error(`There was an error when recognizing the application. Make sure you are in the root path of a Booster project:\n${e.message}`); } } async function checkProjectAlreadyExists(name) { const projectDirectoryPath = (0, project_initializer_1.projectDir)({ projectName: name }); const projectDirectoryExists = (0, fs_extra_1.existsSync)(projectDirectoryPath); if (projectDirectoryExists) { await user_prompt_1.default.confirmPrompt({ message: brand_1.default.dangerize(`Project folder "${name}" already exists. Do you want to overwrite it?`), }).then((confirm) => { if (!confirm) throw new Error("The folder you're trying to use already exists. Please use another project name"); (0, fs_extra_1.removeSync)(projectDirectoryPath); }); } } async function checkResourceExists(name, placementDir, extension) { const resourcePath = (0, generator_1.filePath)({ name, placementDir, extension }); const resourceExists = (0, fs_extra_1.existsSync)(resourcePath); const resourceName = (0, filenames_1.classNameToFileName)(name); const resourceType = (0, generator_1.getResourceType)(placementDir); logger_1.logger.info(brand_1.default.mellancholize('Checking if resource already exists...')); if (resourceExists) { await user_prompt_1.default.confirmPrompt({ message: brand_1.default.dangerize(`Resource: "${resourceName}${extension}" already exists. Do you want to overwrite it?`), }).then((confirm) => { if (!confirm) throw new Error(`The '${resourceType}' resource "${resourceName}${extension}" already exists. Please use another resource name`); (0, fs_extra_1.removeSync)(resourcePath); }); } } async function checkCurrentDirBoosterVersion(version) { return checkBoosterVersion(version, process.cwd()); } async function checkBoosterVersion(cliVersion, projectPath) { const projectVersion = await getBoosterVersion(projectPath); await compareVersionsAndDisplayMessages(cliVersion, projectVersion); } async function getBoosterVersion(projectPath) { const projectAbsolutePath = path.resolve(projectPath); try { const packageJsonContents = require(path.join(projectAbsolutePath, 'package.json')); const version = packageJsonContents.dependencies['@boostercloud/framework-core']; const versionParts = version .replace('workspace:', '') // We remove the workspace protocol in case we're in the Booster monorepo .replace('^', '') .replace('.tgz', '') .split('-'); return versionParts[versionParts.length - 1]; } catch (e) { throw new Error(`There was an error when recognizing the application. Make sure you are in the root path of a Booster project:\n${e.message}`); } } class HigherCliVersionError extends Error { constructor(cliVersion, projectVersion, section) { super(`CLI version ${cliVersion} is higher than your project Booster version ${projectVersion} in the '${section}' section. Please upgrade your project Booster dependencies.`); this.cliVersion = cliVersion; this.projectVersion = projectVersion; this.section = section; } } class LowerCliVersionError extends Error { constructor(cliVersion, projectVersion, section) { super(`CLI version ${cliVersion} is lower than your project Booster version ${projectVersion}. Please upgrade your @boostercloud/cli to the same version with "npm install -g @boostercloud/cli@${projectVersion}"`); this.cliVersion = cliVersion; this.projectVersion = projectVersion; this.section = section; } } async function compareVersionsAndDisplayMessages(cliVersion, projectVersion) { const cliSemVersion = new semver_1.default(cliVersion); const projectSemVersion = new semver_1.default(projectVersion); if (cliSemVersion.equals(projectSemVersion)) { return; } if (cliSemVersion.equalsInBreakingSection(projectSemVersion)) { if (cliSemVersion.equalsInFeatureSection(projectSemVersion)) { if (!cliSemVersion.equalsInFixSection(projectSemVersion)) { //differences in the 'fix' part logger_1.logger.info(`WARNING: Project Booster version differs in the 'fix' section. CLI version: ${cliVersion}. Project Booster version: ${projectVersion}`); } } else if (cliSemVersion.greaterInFeatureSectionThan(projectSemVersion)) { //cli higher than project in 'feat' section throw new HigherCliVersionError(cliVersion, projectVersion, 'feature'); } else { //cli lower than project in 'feat' section throw new LowerCliVersionError(cliVersion, projectVersion, 'feature'); } } else if (cliSemVersion.greaterInBreakingSectionThan(projectSemVersion)) { //cli higher than project in 'breaking' section throw new HigherCliVersionError(cliVersion, projectVersion, 'breaking'); } else { //cli lower than project in 'breaking' section throw new LowerCliVersionError(cliVersion, projectVersion, 'breaking'); } }