Skip to content

Commit

Permalink
- updating command to fallback to listing functionality when no name …
Browse files Browse the repository at this point in the history
…or dest is passed

- Moving retrieval logic out of command and into the local-dev-lib
  • Loading branch information
TanyaScales committed Jan 25, 2024
1 parent 467716f commit 829f52a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 59 deletions.
91 changes: 38 additions & 53 deletions packages/cli/commands/cms/reactModules.js
Original file line number Diff line number Diff line change
@@ -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;
};
12 changes: 6 additions & 6 deletions packages/cli/lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 829f52a

Please sign in to comment.