-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
044ff92
commit ea4a3a6
Showing
6 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { access, constants, mkdir, writeFile } from 'node:fs/promises' | ||
import { join, resolve } from 'node:path' | ||
|
||
import { handleAPIError, handleFileSystemError } from '../../utils' | ||
import { ofetch } from 'ofetch' | ||
import { regionsDomain } from '../../constants' | ||
import { session } from '../../session' | ||
import { apiClient } from '../../api' | ||
|
||
export interface SpaceInternationalizationOptions { | ||
languages: SpaceLanguage[] | ||
default_lang_name: string | ||
} | ||
export interface SpaceLanguage { | ||
code: string | ||
name: string | ||
} | ||
|
||
export const pullLanguages = async (space: string): Promise<SpaceInternationalizationOptions | undefined> => { | ||
try { | ||
const { client } = apiClient() | ||
if (!client) { | ||
throw new Error('Client not initialized') | ||
} | ||
|
||
const { state } = session() | ||
const response = await ofetch(`https://${regionsDomain[state.region]}/v1/spaces/${space}`, { | ||
headers: { | ||
Authorization: state.password, | ||
}, | ||
}) | ||
return { | ||
default_lang_name: response.space.default_lang_name, | ||
languages: response.space.languages, | ||
} | ||
} | ||
catch (error) { | ||
handleAPIError('pull_languages', error as Error) | ||
} | ||
} | ||
|
||
export const saveLanguagesToFile = async (space: string, internationalizationOptions: SpaceInternationalizationOptions, path?: string) => { | ||
try { | ||
const data = JSON.stringify(internationalizationOptions, null, 2) | ||
const filename = `languages.${space}.json` | ||
const resolvedPath = path ? resolve(process.cwd(), path) : process.cwd() | ||
const filePath = join(resolvedPath, filename) | ||
|
||
// Check if the path exists, and create it if it doesn't | ||
try { | ||
await access(resolvedPath, constants.F_OK) | ||
} | ||
catch { | ||
await mkdir(resolvedPath, { recursive: true }) | ||
} | ||
|
||
return await writeFile(filePath, data, { | ||
mode: 0o600, | ||
}) | ||
} | ||
catch (error) { | ||
handleFileSystemError('write', error as Error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { colorPalette, commands } from '../../constants' | ||
import { CommandError, handleError, konsola } from '../../utils' | ||
import { getProgram } from '../../program' | ||
import { session } from '../../session' | ||
import { pullLanguages, saveLanguagesToFile } from './actions' | ||
import chalk from 'chalk' | ||
|
||
const program = getProgram() // Get the shared singleton instance | ||
|
||
export const pullLanguagesCommand = program | ||
.command('pull-languages') | ||
.description(`Download your space's languages schema as json`) | ||
.option('-s, --space <space>', 'space ID') | ||
.option('-p, --path <path>', 'path to save the file') | ||
.action(async (options) => { | ||
const { space, path } = options | ||
konsola.title(` ${commands.PULL_LANGUAGES} `, colorPalette.PULL_LANGUAGES, 'Pulling languages...') | ||
|
||
const { state, initializeSession } = session() | ||
await initializeSession() | ||
|
||
if (!state.isLoggedIn) { | ||
handleError(new CommandError(`You are currently not logged in. Please login first to get your user info.`)) | ||
return | ||
} | ||
|
||
if (!space) { | ||
handleError(new CommandError(`Please provide the space as argument --space YOUR_SPACE_ID.`)) | ||
return | ||
} | ||
|
||
try { | ||
const internationalization = await pullLanguages(space) | ||
|
||
if (!internationalization || !internationalization.languages) { | ||
konsola.warn(`No languages found in the space ${space}`) | ||
return | ||
} | ||
await saveLanguagesToFile(space, internationalization, path) | ||
konsola.ok(`Languages schema downloaded successfully at ${chalk.hex(colorPalette.PRIMARY)(path ? `${path}/languages.${space}.json` : `languages.${space}.json`)}`) | ||
} | ||
catch (error) { | ||
handleError(error as Error, true) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters