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.

93 lines (92 loc) 5.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.archgenerator = void 0; const tslib_1 = require("tslib"); const shaper_utils_1 = require("@code-shaper/shaper-utils"); const inquirer_1 = tslib_1.__importDefault(require("inquirer")); const path_1 = tslib_1.__importDefault(require("path")); const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); const chalk_1 = tslib_1.__importDefault(require("chalk")); const _utils_1 = require("./utils"); const baseGen_1 = tslib_1.__importDefault(require("./modules/base/baseGen")); const { commonQuestionsProjectSetup } = _utils_1.questions; const { isEmptyDir, createDir } = _utils_1.fileDirOps; const { getListOfDirectories, identifyMonorepoWorkspace, identifyPackageManager, handleEmptyCwd } = _utils_1.helpers; const { installPackageManagerIfNeeded } = _utils_1.installPackageManager; const { reactArchTypes, appConstants: { PACKAGE_JSON }, destinationPathContants: { APPS_DIR }, } = _utils_1.constants; exports.archgenerator = { id: 'generator', name: 'generator', description: 'generates a react/next application', generate: archGenerator, }; /** * @name : options * @type : ArchGeneratorOptions * @description : options object that is returned after the user has answered all the questions. Some of the properties are added dynamically after being processed. */ let options; /** * This function is the main executor function that starts the generation of a new React/Next application in both existing and new project flows. * * It first checks the current working directory (CWD) if it's empty or if it's a NX, Turbo or Lerna based monorepo workspace. * * If the CWD is not empty and not a monorepo, it displays a warning and returns. * * If the CWD is not empty and a monorepo, it prompts the user to select the directory inside of their workspace where the new application will be created. * * If the CWD is empty, it handles the empty directory case. * * Then it prompts the user with a set of questions about the new application to be created. * Based on the answers from the user, it generates a new React/Next application. * * After the application has been generated, it initializes a Git repository inside the application directory. * * @param {string} rootDir - The root directory where the new application will be created. * @param {ArchGeneratorOptions} inputOptions - The input options for the generator. */ function archGenerator(rootDir, inputOptions) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { const isEmptyCwd = isEmptyDir(rootDir) ? true : false; const packageJsonPath = path_1.default.join(rootDir, PACKAGE_JSON); let monorepoWorkspace, packageManager, isStandalone = false; if (!isEmptyCwd && fs_extra_1.default.existsSync(packageJsonPath)) { const packageJson = JSON.parse(fs_extra_1.default.readFileSync(packageJsonPath).toString()); monorepoWorkspace = identifyMonorepoWorkspace(rootDir, packageJson) || undefined; packageManager = identifyPackageManager(rootDir).command || undefined; } else if (isEmptyCwd || !fs_extra_1.default.existsSync(packageJsonPath)) { isStandalone = true; } if (isStandalone) { yield handleEmptyCwd(); } const questions = commonQuestionsProjectSetup(isStandalone, !!monorepoWorkspace, getListOfDirectories(rootDir, true)); options = yield inquirer_1.default.prompt(questions, inputOptions); const { itemName, appType, archType, cmdType, parentDir, customPackages, appsDir } = options; appsDir && createDir(path_1.default.join(rootDir, APPS_DIR)); options['appName'] = shaper_utils_1.cc.snakeCase(itemName); options['parentDir'] = isEmptyCwd || isStandalone ? '' : parentDir || APPS_DIR; options['appType'] = appType === 'REACT' ? 'react' : 'next'; options['archType'] = (options['appType'] === 'react' ? Object.keys(reactArchTypes).find(key => reactArchTypes[key] === archType) : ''); options['monorepo'] = monorepoWorkspace || false; options['pkgManager'] = packageManager || cmdType.toLowerCase(); options['customPackages'] = customPackages || []; options['noInstall'] = process.argv.slice(2)[0] === '--no-install' || process.argv.slice(2)[0] === '-ni' ? true : false; options['pkgManager'] !== 'npm' && (yield installPackageManagerIfNeeded(options['pkgManager'])); // Call the generator function yield (0, baseGen_1.default)(options); } catch (err) { console.log(chalk_1.default.white.bold.bgRedBright(' ERROR OCCURED WHILE GENERATING APP ')); console.log(err); } return Promise.resolve(); }); }