Skip to content

Commit

Permalink
feat: add eject command
Browse files Browse the repository at this point in the history
  • Loading branch information
tatomyr committed Aug 20, 2024
1 parent cb9023e commit 068767b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = {
},
'packages/cli/': {
statements: 60,
branches: 50,
branches: 49,
functions: 60,
lines: 60,
},
Expand Down
29 changes: 29 additions & 0 deletions packages/cli/src/commands/eject.ts
Original file line number Diff line number Diff line change
@@ -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<EjectOptions>) => {
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' }
);
};
4 changes: 2 additions & 2 deletions packages/cli/src/commands/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TranslateProjectOptions>) => {
export const handleTranslations = async ({ argv }: CommandArgs<TranslationsOptions>) => {
process.stdout.write(`\nLaunching translations using NPX.\n\n`);
const npxExecutableName = process.platform === 'win32' ? 'npx.cmd' : 'npx';
spawn(
Expand Down
38 changes: 36 additions & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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');
Expand Down Expand Up @@ -800,7 +802,39 @@ yargs
},
}),
(argv) => {
commandWrapper(translateProject)(argv);
process.env.REDOCLY_CLI_COMMAND = 'translations';
commandWrapper(handleTranslations)(argv);
}
)
.command(
'eject <type> <name>',
'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<RuleSeverity>,
default: 'warn' as RuleSeverity,
},
}),
(argv) => {
process.env.REDOCLY_CLI_COMMAND = 'eject';
commandWrapper(handleEject)(argv as Arguments<EjectOptions>);
}
)
.completion('completion', 'Generate autocomplete script for `redocly` command.')
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +40,8 @@ export type CommandOptions =
| BuildDocsArgv
| PushStatusOptions
| PreviewProjectOptions
| TranslateProjectOptions;
| TranslationsOptions
| EjectOptions;

export type VerifyConfigOptions = {
config?: string;
Expand Down

0 comments on commit 068767b

Please sign in to comment.