Skip to content

Commit

Permalink
feat: add pull as as subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosabu committed Dec 4, 2024
1 parent 6cabaab commit 8b11e89
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 24 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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`)}`)
})
Expand All @@ -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)
})

Expand All @@ -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)
})
})
Expand All @@ -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`)}`)
})
})
Expand All @@ -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`)}`)
})
})
Expand Down Expand Up @@ -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`)}`)
})

Expand All @@ -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`)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>', 'space ID')
.option('-p, --path <path>', 'path to save the file. Default is .storyblok/components')

componentsCommand
.command('pull')
.option('-f, --filename <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>', 'suffix to add to the file name (e.g. components.<suffix>.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()
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ export const commands = {
LOGOUT: 'logout',
USER: 'user',
PULL_LANGUAGES: 'pull-languages',
PULL_COMPONENTS: 'pull-components',
COMPONENTS: 'Components',
} as const

export const colorPalette = {
PRIMARY: '#45bfb9',
LOGIN: '#8556D3',
USER: '#8BC34A',
PULL_LANGUAGES: '#FFC107',
PULL_COMPONENTS: '#FF5722',
COMPONENTS: '#FF5722',
} as const

export interface ReadonlyArray<T> {
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down

0 comments on commit 8b11e89

Please sign in to comment.