From 8b11e89a41bccdd78c1a3e6c3be0d113ffd5dca2 Mon Sep 17 00:00:00 2001 From: alvarosabu Date: Wed, 4 Dec 2024 17:24:53 +0100 Subject: [PATCH] feat: add pull as as subcommand --- .../actions.test.ts | 0 .../actions.ts | 0 .../constants.ts | 0 .../index.test.ts | 33 ++++++++++--------- .../{pull-components => components}/index.ts | 17 ++++++---- src/constants.ts | 4 +-- src/index.ts | 3 +- 7 files changed, 33 insertions(+), 24 deletions(-) rename src/commands/{pull-components => components}/actions.test.ts (100%) rename src/commands/{pull-components => components}/actions.ts (100%) rename src/commands/{pull-components => components}/constants.ts (100%) rename src/commands/{pull-components => components}/index.test.ts (86%) rename src/commands/{pull-components => components}/index.ts (84%) diff --git a/src/commands/pull-components/actions.test.ts b/src/commands/components/actions.test.ts similarity index 100% rename from src/commands/pull-components/actions.test.ts rename to src/commands/components/actions.test.ts diff --git a/src/commands/pull-components/actions.ts b/src/commands/components/actions.ts similarity index 100% rename from src/commands/pull-components/actions.ts rename to src/commands/components/actions.ts diff --git a/src/commands/pull-components/constants.ts b/src/commands/components/constants.ts similarity index 100% rename from src/commands/pull-components/constants.ts rename to src/commands/components/constants.ts diff --git a/src/commands/pull-components/index.test.ts b/src/commands/components/index.test.ts similarity index 86% rename from src/commands/pull-components/index.test.ts rename to src/commands/components/index.test.ts index 22f6099..3598055 100644 --- a/src/commands/pull-components/index.test.ts +++ b/src/commands/components/index.test.ts @@ -1,7 +1,7 @@ import { session } from '../../session' import { CommandError, konsola } from '../../utils' import { pullComponents, saveComponentsToFiles } from './actions' -import { pullComponentsCommand } from '.' +import { componentsCommand } from '.' import chalk from 'chalk' import { colorPalette } from '../../constants' @@ -56,12 +56,15 @@ vi.mock('../../utils', async () => { } }) -describe('pullComponents', () => { +describe('pull', () => { beforeEach(() => { vi.resetAllMocks() vi.clearAllMocks() // Reset the option values - pullComponentsCommand._optionValues = {} + componentsCommand._optionValues = {} + for (const command of componentsCommand.commands) { + command._optionValues = {} + } }) describe('default mode', () => { @@ -95,10 +98,10 @@ describe('pullComponents', () => { } vi.mocked(pullComponents).mockResolvedValue(mockResponse) - await pullComponentsCommand.parseAsync(['node', 'test', '--space', '12345']) + await componentsCommand.parseAsync(['node', 'test', 'pull', '--space', '12345']) expect(pullComponents).toHaveBeenCalledWith('12345', 'valid-token', 'eu') expect(saveComponentsToFiles).toHaveBeenCalledWith('12345', mockResponse, { - space: '12345', + path: undefined, }) expect(konsola.ok).toHaveBeenCalledWith(`Components downloaded successfully in ${chalk.hex(colorPalette.PRIMARY)(`./storyblok/components/components.12345.json`)}`) }) @@ -108,7 +111,7 @@ describe('pullComponents', () => { isLoggedIn: false, } const mockError = new CommandError(`You are currently not logged in. Please login first to get your user info.`) - await pullComponentsCommand.parseAsync(['node', 'test', '--space', '12345']) + await componentsCommand.parseAsync(['node', 'test', 'pull', '--space', '12345']) expect(konsola.error).toHaveBeenCalledWith(mockError, false) }) @@ -121,7 +124,7 @@ describe('pullComponents', () => { const mockError = new CommandError(`Please provide the space as argument --space YOUR_SPACE_ID.`) - await pullComponentsCommand.parseAsync(['node', 'test']) + await componentsCommand.parseAsync(['node', 'test', 'pull']) expect(konsola.error).toHaveBeenCalledWith(mockError, false) }) }) @@ -148,9 +151,9 @@ describe('pullComponents', () => { vi.mocked(pullComponents).mockResolvedValue(mockResponse) - await pullComponentsCommand.parseAsync(['node', 'test', '--space', '12345', '--path', '/path/to/components']) + await componentsCommand.parseAsync(['node', 'test', 'pull', '--space', '12345', '--path', '/path/to/components']) expect(pullComponents).toHaveBeenCalledWith('12345', 'valid-token', 'eu') - expect(saveComponentsToFiles).toHaveBeenCalledWith('12345', mockResponse, { path: '/path/to/components', space: '12345' }) + expect(saveComponentsToFiles).toHaveBeenCalledWith('12345', mockResponse, { path: '/path/to/components' }) expect(konsola.ok).toHaveBeenCalledWith(`Components downloaded successfully in ${chalk.hex(colorPalette.PRIMARY)(`/path/to/components/components.12345.json`)}`) }) }) @@ -177,9 +180,9 @@ describe('pullComponents', () => { vi.mocked(pullComponents).mockResolvedValue(mockResponse) - await pullComponentsCommand.parseAsync(['node', 'test', '--space', '12345', '--filename', 'custom']) + await componentsCommand.parseAsync(['node', 'test', 'pull', '--space', '12345', '--filename', 'custom']) expect(pullComponents).toHaveBeenCalledWith('12345', 'valid-token', 'eu') - expect(saveComponentsToFiles).toHaveBeenCalledWith('12345', mockResponse, { filename: 'custom', space: '12345' }) + expect(saveComponentsToFiles).toHaveBeenCalledWith('12345', mockResponse, { filename: 'custom' }) expect(konsola.ok).toHaveBeenCalledWith(`Components downloaded successfully in ${chalk.hex(colorPalette.PRIMARY)(`./storyblok/components/custom.12345.json`)}`) }) }) @@ -216,9 +219,9 @@ describe('pullComponents', () => { vi.mocked(pullComponents).mockResolvedValue(mockResponse) - await pullComponentsCommand.parseAsync(['node', 'test', '--space', '12345', '--separate-files']) + await componentsCommand.parseAsync(['node', 'test', 'pull', '--space', '12345', '--separate-files']) expect(pullComponents).toHaveBeenCalledWith('12345', 'valid-token', 'eu') - expect(saveComponentsToFiles).toHaveBeenCalledWith('12345', mockResponse, { separateFiles: true, space: '12345' }) + expect(saveComponentsToFiles).toHaveBeenCalledWith('12345', mockResponse, { separateFiles: true, path: undefined }) expect(konsola.ok).toHaveBeenCalledWith(`Components downloaded successfully in ${chalk.hex(colorPalette.PRIMARY)(`./storyblok/components`)}`) }) @@ -243,9 +246,9 @@ describe('pullComponents', () => { vi.mocked(pullComponents).mockResolvedValue(mockResponse) - await pullComponentsCommand.parseAsync(['node', 'test', '--space', '12345', '--separate-files', '--filename', 'custom']) + await componentsCommand.parseAsync(['node', 'test', 'pull', '--space', '12345', '--separate-files', '--filename', 'custom']) expect(pullComponents).toHaveBeenCalledWith('12345', 'valid-token', 'eu') - expect(saveComponentsToFiles).toHaveBeenCalledWith('12345', mockResponse, { separateFiles: true, filename: 'custom', space: '12345' }) + expect(saveComponentsToFiles).toHaveBeenCalledWith('12345', mockResponse, { separateFiles: true, filename: 'custom' }) expect(konsola.warn).toHaveBeenCalledWith(`The --filename option is ignored when using --separate-files`) }) }) diff --git a/src/commands/pull-components/index.ts b/src/commands/components/index.ts similarity index 84% rename from src/commands/pull-components/index.ts rename to src/commands/components/index.ts index 55a5711..65e43f0 100644 --- a/src/commands/pull-components/index.ts +++ b/src/commands/components/index.ts @@ -8,20 +8,25 @@ import type { PullComponentsOptions } from './constants' const program = getProgram() // Get the shared singleton instance -export const pullComponentsCommand = program - .command('pull-components') - .description(`Download your space's components schema as json`) +export const componentsCommand = program + .command('components') + .description(`Manage your space's block schema`) .option('-s, --space ', 'space ID') .option('-p, --path ', 'path to save the file. Default is .storyblok/components') + +componentsCommand + .command('pull') .option('-f, --filename ', 'custom name to be used in file(s) name instead of space id') .option('--sf, --separate-files [value]', 'Argument to create a single file for each component') .option('--su, --suffix ', 'suffix to add to the file name (e.g. components..json). By default, the space ID is used.') + .description(`Download your space's components schema as json`) .action(async (options: PullComponentsOptions) => { - konsola.title(` ${commands.PULL_COMPONENTS} `, colorPalette.PULL_COMPONENTS, 'Pulling components...') + konsola.title(` ${commands.COMPONENTS} `, colorPalette.COMPONENTS, 'Pulling components...') // Global options const verbose = program.opts().verbose // Command options - const { space, path, filename = 'components', suffix = options.space, separateFiles } = options + const { space, path } = componentsCommand.opts() + const { separateFiles, suffix = space, filename = 'components' } = options const { state, initializeSession } = session() await initializeSession() @@ -42,7 +47,7 @@ export const pullComponentsCommand = program konsola.warn(`No components found in the space ${space}`) return } - await saveComponentsToFiles(space, components, options) + await saveComponentsToFiles(space, components, { ...options, path }) const msgFilename = `${filename}.${suffix}.json` if (separateFiles) { diff --git a/src/constants.ts b/src/constants.ts index 9986a5d..b92294f 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -3,7 +3,7 @@ export const commands = { LOGOUT: 'logout', USER: 'user', PULL_LANGUAGES: 'pull-languages', - PULL_COMPONENTS: 'pull-components', + COMPONENTS: 'Components', } as const export const colorPalette = { @@ -11,7 +11,7 @@ export const colorPalette = { LOGIN: '#8556D3', USER: '#8BC34A', PULL_LANGUAGES: '#FFC107', - PULL_COMPONENTS: '#FF5722', + COMPONENTS: '#FF5722', } as const export interface ReadonlyArray { diff --git a/src/index.ts b/src/index.ts index 9f53f93..2e9763a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,8 @@ import './commands/login' import './commands/logout' import './commands/user' import './commands/pull-languages' -import './commands/pull-components' +import './commands/components' +import './commands/block' import { loginWithToken } from './commands/login/actions'