-
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.
Merge branch 'next' into feature/pull-components-cmd
- Loading branch information
Showing
9 changed files
with
246 additions
and
49 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
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,30 @@ | ||
import type { CommandOptions } from '../../types' | ||
|
||
/** | ||
* Interface representing the options for the `pull-languages` command. | ||
*/ | ||
export interface PullLanguagesOptions extends CommandOptions { | ||
/** | ||
* The path to save the languages file to. | ||
* Defaults to `.storyblok/languages`. | ||
* @default `.storyblok/languages` | ||
*/ | ||
path?: string | ||
/** | ||
* The space ID. | ||
* @required true | ||
*/ | ||
space: string | ||
/** | ||
* The filename to save the file as. | ||
* Defaults to `languages`. The file will be saved as `<filename>.<space>.json`. | ||
* @default `languages | ||
*/ | ||
filename?: string | ||
/** | ||
* The suffix to add to the filename. | ||
* Defaults to the space ID. | ||
* @default space | ||
*/ | ||
suffix?: string | ||
} |
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,9 @@ | ||
/** | ||
* Interface representing the default options for a CLI command. | ||
*/ | ||
export interface CommandOptions { | ||
/** | ||
* Indicates whether verbose output is enabled. | ||
*/ | ||
verbose: boolean | ||
} |
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,51 @@ | ||
import { vol } from 'memfs' | ||
import { resolvePath, saveToFile } from './filesystem' | ||
import { resolve } from 'node:path' | ||
|
||
// tell vitest to use fs mock from __mocks__ folder | ||
// this can be done in a setup file if fs should always be mocked | ||
vi.mock('node:fs') | ||
vi.mock('node:fs/promises') | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks() | ||
// reset the state of in-memory fs | ||
vol.reset() | ||
}) | ||
|
||
describe('filesystem utils', async () => { | ||
describe('saveToFile', async () => { | ||
it('should save the data to the file', async () => { | ||
const filePath = '/path/to/file.txt' | ||
const data = 'Hello, World!' | ||
|
||
await saveToFile(filePath, data) | ||
|
||
const content = vol.readFileSync(filePath, 'utf8') | ||
expect(content).toBe(data) | ||
}) | ||
|
||
it('should create the directory if it does not exist', async () => { | ||
const filePath = '/path/to/new/file.txt' | ||
const data = 'Hello, World!' | ||
|
||
await saveToFile(filePath, data) | ||
|
||
const content = vol.readFileSync(filePath, 'utf8') | ||
expect(content).toBe(data) | ||
}) | ||
}) | ||
|
||
describe('resolvePath', async () => { | ||
it('should resolve the path correctly', async () => { | ||
const path = '/path/to/file' | ||
const folder = 'folder' | ||
|
||
const resolvedPath = resolvePath(path, folder) | ||
expect(resolvedPath).toBe(resolve(process.cwd(), path)) | ||
|
||
const resolvedPathWithoutPath = resolvePath(undefined, folder) | ||
expect(resolvedPathWithoutPath).toBe(resolve(process.cwd(), '.storyblok/folder')) | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,27 +1,27 @@ | ||
import { parse } from 'node:path' | ||
import { access, constants, mkdir, writeFile } from 'node:fs/promises' | ||
import { parse, resolve } from 'node:path' | ||
import { mkdir, writeFile } from 'node:fs/promises' | ||
import { handleFileSystemError } from './error/filesystem-error' | ||
|
||
export const saveToFile = async (filePath: string, data: string) => { | ||
// Check if the path exists, and create it if it doesn't | ||
// Get the directory path | ||
const resolvedPath = parse(filePath).dir | ||
|
||
// Ensure the directory exists | ||
try { | ||
await access(resolvedPath, constants.F_OK) | ||
await mkdir(resolvedPath, { recursive: true }) | ||
} | ||
catch { | ||
try { | ||
await mkdir(resolvedPath, { recursive: true }) | ||
} | ||
catch (mkdirError) { | ||
handleFileSystemError('mkdir', mkdirError as Error) | ||
return // Exit early if the directory creation fails | ||
} | ||
catch (mkdirError) { | ||
handleFileSystemError('mkdir', mkdirError as Error) | ||
return // Exit early if the directory creation fails | ||
} | ||
|
||
// Write the file | ||
try { | ||
await writeFile(filePath, data, { mode: 0o600 }) | ||
await writeFile(filePath, data) | ||
} | ||
catch (writeError) { | ||
handleFileSystemError('write', writeError as Error) | ||
} | ||
} | ||
|
||
export const resolvePath = (path: string | undefined, folder: string) => path ? resolve(process.cwd(), path) : resolve(resolve(process.cwd(), '.storyblok'), folder) |