diff --git a/packages/cli/commands/cms/reactModules.js b/packages/cli/commands/cms/reactModules.js index 2aba92a73..d1eef4fbd 100644 --- a/packages/cli/commands/cms/reactModules.js +++ b/packages/cli/commands/cms/reactModules.js @@ -1,82 +1,67 @@ const fs = require('fs'); const { logger } = require('@hubspot/cli-lib/logger'); -const { - listGithubRepoContents, - downloadGithubRepoContents, -} = require('@hubspot/local-dev-lib/github'); -const { - throwErrorWithMessage, -} = require('@hubspot/local-dev-lib/errors/standardErrors'); +const { retrieveDefaultModule } = require('@hubspot/local-dev-lib/cms/modules'); const { i18n } = require('../../lib/lang'); const path = require('path'); const { trackCommandUsage } = require('../../lib/usageTracking'); const i18nKey = 'cli.commands.cms.subcommands.reactModule'; -exports.command = 'get-react-module [name] [dest] [--list]'; +exports.command = 'get-react-module [--name] [--dest]'; exports.describe = i18n(`${i18nKey}.describe`); exports.handler = async options => { - const { name, dest, list } = options; + const { name, dest } = options; + trackCommandUsage('get-react-modules'); - //TODO: trackCommandUsage() - //TODO: i18n - //TODO: Move logic to local-dev-lib -- modules.ts -- fetchReactModules() and call from here + const destPath = dest + ? path.join(dest, `${name}`) + : path.join(process.cwd(), `${name}`); - if (list) { - try { - const contents = await listGithubRepoContents( - 'HubSpot/cms-sample-assets', - 'modules/', - 'dir' - ); + if (fs.existsSync(destPath)) { + logger.error( + i18n(`${i18nKey}.errors.pathExists`, { + path: destPath, + }) + ); + return; + } - logger.group('React modules available to download:'); - contents.forEach(module => { - logger.log(module.name); - }); - logger.groupEnd('React modules available to download:'); - } catch (e) { - console.log(e); // TODO: Error handling - } - } else { - const destPath = path.join(dest, `${name}`); + try { + const modules = await retrieveDefaultModule(name, destPath); - if (fs.existsSync(destPath)) { - throwErrorWithMessage(`${i18nKey}.errors.pathExists`, { - path: destPath, + if (!name) { + logger.group(i18n(`${i18nKey}.groupLabel`)); + modules.forEach(module => { + logger.log(module.name); }); - } - - try { - await downloadGithubRepoContents( - 'HubSpot/cms-sample-assets', - `modules/${name}`, - destPath + logger.groupEnd(i18n(`${i18nKey}.groupLabel`)); + } else { + logger.success( + i18n(`${i18nKey}.success.moduleDownloaded`, { + moduleName: name, + path: destPath, + }) ); - - logger.success(`${name} succesfully downloaded to ${destPath}`); - } catch (e) { - // Error to catch: requested module name is incorrect -- results in bad request error - console.log(e); // TODO: Error handling + } + } catch (e) { + if (e.cause.code === 'ERR_BAD_REQUEST') { + logger.error(i18n(`${i18nKey}.errors.invalidName`)); + } else { + logger.error(e); } } }; exports.builder = yargs => { - yargs.positional('name', { - describe: i18n(`${i18nKey}.positionals.name.describe`), + yargs.option('name', { + describe: i18n(`${i18nKey}.options.name.describe`), type: 'string', }); - yargs.positional('dest', { - describe: i18n(`${i18nKey}.positionals.dest.describe`), + yargs.option('dest', { + describe: i18n(`${i18nKey}.options.dest.describe`), type: 'string', }); - yargs.option('list', { - describe: i18n(`${i18nKey}.options.list.describe`), - type: 'boolean', - default: false, - }); return yargs; }; diff --git a/packages/cli/lang/en.lyaml b/packages/cli/lang/en.lyaml index 67e714775..68ffc4138 100644 --- a/packages/cli/lang/en.lyaml +++ b/packages/cli/lang/en.lyaml @@ -148,18 +148,18 @@ en: verbose: describe: "View a detailed output of the lighthouse sores" reactModule: - describe: "Get a specified React module boilerplate" - positionals: + describe: "Get a specified default React module" + options: name: describe: "Name of the react modules to be fetched" dest: describe: "Destination to download the react module to" - options: - list: - describe: "List out react module available to download" + success: + moduleDownloaded: "\"{{ moduleName }}\" succesfully downloaded to \"{{ path }}\"" errors: - unusablePath: "\"{{ path }}\" is not a usable path to a directory." pathExists: "Folder already exists at \"{{ path }}\"" + invalidName: "Module not found with that name, please check the spelling of the module you are trying to retrieve." + groupLabel: "React modules available to download:" create: describe: "Create HubSpot sample apps and CMS assets. Supported assets are {{ supportedAssetTypes }}." errors: