diff --git a/jest.config.js b/jest.config.js index 1f73e377f..c7d907be3 100644 --- a/jest.config.js +++ b/jest.config.js @@ -19,7 +19,7 @@ module.exports = { }, 'packages/cli/': { statements: 60, - branches: 50, + branches: 49, functions: 60, lines: 60, }, diff --git a/packages/cli/src/commands/eject.ts b/packages/cli/src/commands/eject.ts new file mode 100644 index 000000000..47c2366bc --- /dev/null +++ b/packages/cli/src/commands/eject.ts @@ -0,0 +1,29 @@ +import { spawn } from 'child_process'; + +import type { CommandArgs } from '../wrapper'; +import type { VerifyConfigOptions } from '../types'; + +export type EjectOptions = { + type: 'component'; + name: string; + contentDir?: string; + force: boolean; +} & VerifyConfigOptions; + +export const handleEject = async ({ argv }: CommandArgs) => { + process.stdout.write(`\nLaunching eject using NPX.\n\n`); + const npxExecutableName = process.platform === 'win32' ? 'npx.cmd' : 'npx'; + spawn( + npxExecutableName, + [ + '-y', + '@redocly/realm', + 'eject', + `${argv.type}`, + `${argv.name}`, + `--contentDir=${argv.contentDir}`, + argv.force ? `--force=${argv.force}` : '', + ], + { stdio: 'inherit' } + ); +}; diff --git a/packages/cli/src/commands/translations.ts b/packages/cli/src/commands/translations.ts index 58b604460..17af93e82 100644 --- a/packages/cli/src/commands/translations.ts +++ b/packages/cli/src/commands/translations.ts @@ -3,12 +3,12 @@ import { spawn } from 'child_process'; import type { CommandArgs } from '../wrapper'; import type { VerifyConfigOptions } from '../types'; -export type TranslateProjectOptions = { +export type TranslationsOptions = { locale: string[]; contentDir?: string; } & VerifyConfigOptions; -export const translateProject = async ({ argv }: CommandArgs) => { +export const handleTranslations = async ({ argv }: CommandArgs) => { process.stdout.write(`\nLaunching translations using NPX.\n\n`); const npxExecutableName = process.platform === 'win32' ? 'npx.cmd' : 'npx'; spawn( diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 7f197bbfb..557db3aa9 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -21,7 +21,8 @@ import { } from './utils/update-version-notifier'; import { commandWrapper } from './wrapper'; import { previewProject } from './commands/preview-project'; -import { translateProject } from './commands/translations'; +import { handleTranslations } from './commands/translations'; +import { handleEject } from './commands/eject'; import { PRODUCT_PLANS } from './commands/preview-project/constants'; import { commonPushHandler } from './commands/push'; @@ -30,6 +31,7 @@ import type { OutputFormat, RuleSeverity } from '@redocly/openapi-core'; import type { BuildDocsArgv } from './commands/build-docs/types'; import type { PushStatusOptions } from './cms/commands/push-status'; import type { PushArguments } from './types'; +import type { EjectOptions } from './commands/eject'; if (!('replaceAll' in String.prototype)) { require('core-js/actual/string/replace-all'); @@ -800,7 +802,39 @@ yargs }, }), (argv) => { - commandWrapper(translateProject)(argv); + process.env.REDOCLY_CLI_COMMAND = 'translations'; + commandWrapper(handleTranslations)(argv); + } + ) + .command( + 'eject ', + 'Ejects components.', + (yargs) => + yargs + .positional('type', { required: true, choices: ['component'] }) + .positional('name', { type: 'string', required: true }) + .options({ + contentDir: { + alias: 'd', + type: 'string', + description: 'A destination folder to eject components into.', + default: '.', + }, + force: { + alias: 'f', + type: 'boolean', + description: + 'Skips the "overwrite existing" confirmation when ejecting a component that is already ejected in the destination.', + }, + 'lint-config': { + description: 'Severity level for config file linting.', + choices: ['warn', 'error', 'off'] as ReadonlyArray, + default: 'warn' as RuleSeverity, + }, + }), + (argv) => { + process.env.REDOCLY_CLI_COMMAND = 'eject'; + commandWrapper(handleEject)(argv as Arguments); } ) .completion('completion', 'Generate autocomplete script for `redocly` command.') diff --git a/packages/cli/src/types.ts b/packages/cli/src/types.ts index 44903c685..45313b7fd 100644 --- a/packages/cli/src/types.ts +++ b/packages/cli/src/types.ts @@ -12,7 +12,8 @@ import type { BuildDocsArgv } from './commands/build-docs/types'; import type { PushOptions as CMSPushOptions } from './cms/commands/push'; import type { PushStatusOptions } from './cms/commands/push-status'; import type { PreviewProjectOptions } from './commands/preview-project/types'; -import type { TranslateProjectOptions } from './commands/translations'; +import type { TranslationsOptions } from './commands/translations'; +import type { EjectOptions } from './commands/eject'; export type Totals = { errors: number; @@ -39,7 +40,8 @@ export type CommandOptions = | BuildDocsArgv | PushStatusOptions | PreviewProjectOptions - | TranslateProjectOptions; + | TranslationsOptions + | EjectOptions; export type VerifyConfigOptions = { config?: string;