UNPKG

@coderebus/react-archetype

Version:

React-archetype is a cli tool (or a generator) that will generate apps based on the rendering pattern (CSR,SSR, SSG, ISR etc) of your choice.

90 lines (89 loc) 4.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.installPackageManagerIfNeeded = void 0; const tslib_1 = require("tslib"); const child_process_1 = require("child_process"); const inquirer_1 = tslib_1.__importDefault(require("inquirer")); const chalk_1 = tslib_1.__importDefault(require("chalk")); const node_emoji_1 = tslib_1.__importDefault(require("node-emoji")); /** * @description: method to check if the selected package manager is installed or not * @param {TCommandType} pkgManager: package manager to check * @returns {boolean} true if installed, false otherwise */ function isPackageManagerInstalled(pkgManager) { try { // Run the command to check if the package manager is installed if (pkgManager === 'pnpm') { (0, child_process_1.execSync)('pnpm --version', { stdio: 'ignore' }); } else if (pkgManager === 'yarn') { (0, child_process_1.execSync)('yarn --version', { stdio: 'ignore' }); } return true; } catch (error) { return false; } } /** * @description: method to install the selected package manager * @param {TCommandType} pkgManager: package manager to install */ function installPackageManager(pkgManager) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { // Run the command to install the package manager if (pkgManager === 'pnpm') { (0, child_process_1.execSync)('npm install -g pnpm', { stdio: 'inherit' }); } else if (pkgManager === 'yarn') { (0, child_process_1.execSync)('npm install -g yarn', { stdio: 'inherit' }); } console.log(); console.info(chalk_1.default.bgGreenBright.bold(` ${pkgManager.toUpperCase()} installed successfully... `)); console.log(); } catch (error) { console.error(`Error occurred while installing ${pkgManager}: ${error.message}`); } }); } /** * @description: method to install the selected package manager if not installed * @param {TCommandType} pkgManager: package manager to install */ function installPackageManagerIfNeeded(pkgManager) { return tslib_1.__awaiter(this, void 0, void 0, function* () { // Check if pnpm or yarn is already installed if (!isPackageManagerInstalled(pkgManager)) { // Prompt the user to install the package manager console.log(); const answer = yield inquirer_1.default.prompt([ { type: 'confirm', name: 'install', message: chalk_1.default.yellowBright(`${pkgManager.toUpperCase()} is not installed. Do you want us to install it for you?`), default: true, }, ]); if (!answer.install) { console.info(chalk_1.default.greenBright(` ${chalk_1.default.bold(`Please install ${pkgManager.toUpperCase()} manually, by following the steps below:`)} 1. Install the package manager using npm - ${node_emoji_1.default.get('package')} ${chalk_1.default.bold.cyanBright(`[npm install -g ${pkgManager}]`)} 2. After installing the package manager, run the plugin again - ${node_emoji_1.default.get('rocket')} ${chalk_1.default.bold.cyanBright('[npx @genesisx/create-workspace]')} `)); console.log(); console.info(chalk_1.default.greenBright(` OR Visit the official ${pkgManager.toUpperCase()} website: ${pkgManager === 'pnpm' ? 'https://pnpm.io/installation/' : 'https://classic.yarnpkg.com/lang/en/docs/install/'}`.trimStart())); process.exit(0); } else { yield installPackageManager(pkgManager); } } }); } exports.installPackageManagerIfNeeded = installPackageManagerIfNeeded;