Skip to content

Commit

Permalink
Merge branch 'feature/refactor-language-command-updated-dx' into feat…
Browse files Browse the repository at this point in the history
…ure/pull-components-cmd
  • Loading branch information
alvarosabu committed Jan 16, 2025
2 parents 2722b29 + 5dcf13a commit 20bca5a
Show file tree
Hide file tree
Showing 32 changed files with 924 additions and 873 deletions.
20 changes: 17 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@
"STUB": "true"
}
},
{
"type": "node",
"request": "launch",
"name": "Debug login by token",
"program": "${workspaceFolder}/dist/index.mjs",
"args": ["login", "--token", "1234567890"],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"sourceMaps": true,
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"env": {
"STUB": "true"
}
},
{
"type": "node",
"request": "launch",
Expand Down Expand Up @@ -60,7 +74,7 @@
"request": "launch",
"name": "Debug Pull languages",
"program": "${workspaceFolder}/dist/index.mjs",
"args": ["pull-languages", "--space", "295018", "--path", ".storyblok"],
"args": ["languages", "pull", "--space", "2950182323", "--path", ".storyblok"],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"sourceMaps": true,
Expand All @@ -72,9 +86,9 @@
{
"type": "node",
"request": "launch",
"name": "Debug Pull Components",
"name": "Debug Test",
"program": "${workspaceFolder}/dist/index.mjs",
"args": ["pull-components", "--space", "295018", "--path", ".storyblok"],
"args": ["test"],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"sourceMaps": true,
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"chalk": "^5.3.0",
"commander": "^12.1.0",
"dotenv": "^16.4.5",
"ofetch": "^1.4.0",
"storyblok-js-client": "^6.9.2"
},
"devDependencies": {
Expand Down
22 changes: 0 additions & 22 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
import { vol } from 'memfs'
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { pullLanguages, saveLanguagesToFile } from './actions'
import { fetchLanguages, saveLanguagesToFile } from './actions'

const handlers = [
http.get('https://api.storyblok.com/v1/spaces/12345', async ({ request }) => {
Expand Down Expand Up @@ -44,7 +44,7 @@ describe('pull languages actions', () => {
vol.reset()
})

describe('pullLanguages', () => {
describe('fetchLanguages', () => {
it('should pull languages successfully with a valid token', async () => {
const mockResponse = {
default_lang_name: 'en',
Expand All @@ -59,12 +59,12 @@ describe('pull languages actions', () => {
},
],
}
const result = await pullLanguages('12345', 'valid-token', 'eu')
const result = await fetchLanguages('12345', 'valid-token', 'eu')
expect(result).toEqual(mockResponse)
})
})
it('should throw an masked error for invalid token', async () => {
await expect(pullLanguages('12345', 'invalid-token', 'eu')).rejects.toThrow(
await expect(fetchLanguages('12345', 'invalid-token', 'eu')).rejects.toThrow(
new Error(`The user is not authorized to access the API`),
)
})
Expand All @@ -90,7 +90,7 @@ describe('pull languages actions', () => {
verbose: false,
space: '12345',
})
const content = vol.readFileSync('/temp/languages.12345.json', 'utf8')
const content = vol.readFileSync('/temp/languages.json', 'utf8')
expect(content).toBe(JSON.stringify(mockResponse, null, 2))
})
})
Expand Down
45 changes: 45 additions & 0 deletions src/commands/languages/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { join } from 'node:path'
import { handleAPIError, handleFileSystemError } from '../../utils'
import type { FetchError } from '../../utils/fetch'
import { customFetch } from '../../utils/fetch'
import { resolvePath, saveToFile } from '../../utils/filesystem'
import type { PullLanguagesOptions } from './constants'
import type { RegionCode } from '../../constants'
import type { SpaceInternationalization } from '../../types'
import { getStoryblokUrl } from '../../utils/api-routes'

export const fetchLanguages = async (space: string, token: string, region: RegionCode): Promise<SpaceInternationalization | undefined> => {
try {
const url = getStoryblokUrl(region)
const response = await customFetch<{
space: SpaceInternationalization
}>(`${url}/spaces/${space}`, {
headers: {
Authorization: token,
},
})

return {
default_lang_name: response.space.default_lang_name,
languages: response.space.languages,
}
}
catch (error) {
handleAPIError('pull_languages', error as FetchError)
}
}

export const saveLanguagesToFile = async (space: string, internationalizationOptions: SpaceInternationalization, options: PullLanguagesOptions) => {
try {
const { filename = 'languages', suffix, path } = options
const data = JSON.stringify(internationalizationOptions, null, 2)
const name = suffix ? `${filename}.${suffix}.json` : `${filename}.json`
const resolvedPath = resolvePath(path, `languages/${space}/`)
const filePath = join(resolvedPath, name)

await saveToFile(filePath, data)
}
catch (error) {
handleFileSystemError('write', error as Error)
}
}
File renamed without changes.
Loading

0 comments on commit 20bca5a

Please sign in to comment.