-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- updating command to fallback to listing functionality when no name …
…or dest is passed - Moving retrieval logic out of command and into the local-dev-lib
- Loading branch information
1 parent
467716f
commit 829f52a
Showing
2 changed files
with
44 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters