-
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.
feat!:
.storyblok
directory encapsulation as default path
BREAKING CHANGE: Generated files will no longer be saved on the root of the project by default, they will be encapsulated inside of a `.storyblok` folder.
- Loading branch information
1 parent
4e52b6b
commit c3b09d9
Showing
6 changed files
with
104 additions
and
26 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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { parse, resolve } from 'node:path' | ||
import { access, constants, 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 | ||
const resolvedPath = parse(filePath).dir | ||
try { | ||
await access(resolvedPath, constants.F_OK) | ||
} | ||
catch { | ||
try { | ||
await mkdir(resolvedPath, { recursive: true }) | ||
} | ||
catch (mkdirError) { | ||
handleFileSystemError('mkdir', mkdirError as Error) | ||
return // Exit early if the directory creation fails | ||
} | ||
} | ||
|
||
try { | ||
await writeFile(filePath, data, { mode: 0o600 }) | ||
} | ||
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) |