diff --git a/commands/create/api-sample.ts b/commands/create/api-sample.ts index 92691cc2b..e60d60610 100644 --- a/commands/create/api-sample.ts +++ b/commands/create/api-sample.ts @@ -1,37 +1,32 @@ -// @ts-nocheck -const { - createApiSamplePrompt, -} = require('../../lib/prompts/createApiSamplePrompt'); -const { confirmPrompt } = require('../../lib/prompts/promptUtils'); -const { logger } = require('@hubspot/local-dev-lib/logger'); -const path = require('path'); -const fs = require('fs-extra'); -const { fetchRepoFile } = require('@hubspot/local-dev-lib/api/github'); -const { cloneGithubRepo } = require('@hubspot/local-dev-lib/github'); -const { i18n } = require('../../lib/lang'); -const { debugError } = require('../../lib/errorHandlers'); -const { EXIT_CODES } = require('../../lib/enums/exitCodes'); +import path from 'path'; +import fs from 'fs-extra'; +import { fetchRepoFile } from '@hubspot/local-dev-lib/api/github'; +import { cloneGithubRepo } from '@hubspot/local-dev-lib/github'; -module.exports = { +import { createApiSamplePrompt } from '../../lib/prompts/createApiSamplePrompt'; +import { confirmPrompt } from '../../lib/prompts/promptUtils'; +import { debugError } from '../../lib/errorHandlers'; +import { EXIT_CODES } from '../../lib/enums/exitCodes'; +import { CreatableCmsAsset, ApiSampleConfig } from '../../types/Cms'; +import { commands } from '../../lang/en'; +import { uiLogger } from '../../lib/ui/logger'; + +const ApiSampleAssetType: CreatableCmsAsset = { hidden: true, dest: ({ dest }) => dest, validate: ({ name }) => { if (!name) { - logger.error( - i18n(`commands.create.subcommands.apiSample.errors.nameRequired`) - ); + uiLogger.error(commands.create.subcommands.apiSample.errors.nameRequired); return false; } return true; }, - execute: async ({ dest, name, assetType, options }) => { + execute: async ({ dest, name, assetType, commandArgs }) => { const filePath = path.join(dest, name); if (fs.existsSync(filePath)) { const overwrite = await confirmPrompt( - i18n(`commands.create.subcommands.apiSample.folderOverwritePrompt`, { - folderName: filePath, - }), + commands.create.subcommands.apiSample.folderOverwritePrompt(filePath), { defaultAnswer: false } ); if (overwrite) { @@ -43,7 +38,7 @@ module.exports = { let samplesConfig; try { - const { data } = await fetchRepoFile( + const { data } = await fetchRepoFile( 'HubSpot/sample-apps-list', 'samples.json', 'main' @@ -54,9 +49,7 @@ module.exports = { } if (!samplesConfig) { - logger.error( - i18n(`commands.create.subcommands.apiSample.errors.noSamples`) - ); + uiLogger.error(commands.create.subcommands.apiSample.errors.noSamples); process.exit(EXIT_CODES.ERROR); } @@ -64,32 +57,32 @@ module.exports = { await createApiSamplePrompt(samplesConfig); if (!sampleType || !sampleLanguage) { - logger.error( - i18n(`commands.create.subcommands.apiSample.errors.noSamples`) - ); + uiLogger.error(commands.create.subcommands.apiSample.errors.noSamples); process.exit(EXIT_CODES.ERROR); } - logger.info( - i18n(`commands.create.subcommands.apiSample.info.sampleChosen`, { + uiLogger.info( + commands.create.subcommands.apiSample.info.sampleChosen( sampleType, - sampleLanguage, - }) + sampleLanguage + ) ); const created = await cloneGithubRepo(`HubSpot/${sampleType}`, filePath, { type: assetType, sourceDir: sampleLanguage, - ...options, + ...commandArgs, }); if (created) { if (fs.existsSync(`${filePath}/.env.template`)) { fs.copySync(`${filePath}/.env.template`, `${filePath}/.env`); } - logger.success( - i18n(`commands.create.subcommands.apiSample.success.sampleCreated`, { - filePath, - }) + uiLogger.success( + commands.create.subcommands.apiSample.success.sampleCreated(filePath) ); } }, }; + +export default ApiSampleAssetType; +// TODO: Remove +module.exports = ApiSampleAssetType; diff --git a/lib/prompts/createApiSamplePrompt.ts b/lib/prompts/createApiSamplePrompt.ts index a41551f3a..6d60005ef 100644 --- a/lib/prompts/createApiSamplePrompt.ts +++ b/lib/prompts/createApiSamplePrompt.ts @@ -1,17 +1,7 @@ import { promptUser } from './promptUtils'; import { i18n } from '../lang'; import { PromptConfig } from '../../types/Prompts'; - -type SampleChoice = { - name: string; - description: string; - id: string; - languages: string[]; -}; - -type SampleConfig = { - samples: SampleChoice[]; -}; +import { ApiSampleChoice, ApiSampleConfig } from '../../types/Cms'; type SampleTypePromptResponse = { sampleType?: string; @@ -25,7 +15,7 @@ type CreateApiSamplePromptResponse = SampleTypePromptResponse & LanguagePromptResponse; function getSampleTypesPrompt( - choices: SampleChoice[] + choices: ApiSampleChoice[] ): PromptConfig { return { type: 'rawlist', @@ -76,7 +66,7 @@ function getLanguagesPrompt( } export async function createApiSamplePrompt( - samplesConfig: SampleConfig + samplesConfig: ApiSampleConfig ): Promise { try { const { samples } = samplesConfig; diff --git a/types/Cms.ts b/types/Cms.ts new file mode 100644 index 000000000..e64d08dbb --- /dev/null +++ b/types/Cms.ts @@ -0,0 +1,28 @@ +// TODO: Add real type +export type CreateArgs = object; + +export type CmsAssetOperationArgs = { + assetType: string; + name: string; + dest: string; + getInternalVersion: boolean; + commandArgs: CreateArgs; +}; + +export type CreatableCmsAsset = { + hidden: boolean; + dest: (args: CmsAssetOperationArgs) => string; + validate: (args: CmsAssetOperationArgs) => boolean; + execute: (args: CmsAssetOperationArgs) => Promise; +}; + +export type ApiSampleChoice = { + name: string; + description: string; + id: string; + languages: string[]; +}; + +export type ApiSampleConfig = { + samples: ApiSampleChoice[]; +};