Skip to content

Commit d6e2349

Browse files
authored
Refactor fs and path helpers in renderers core (#787)
1 parent ab8cc90 commit d6e2349

File tree

9 files changed

+63
-19
lines changed

9 files changed

+63
-19
lines changed

.changeset/eager-bottles-pull.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codama/renderers-core': minor
3+
---
4+
5+
Add new path helpers and refactor fs helpers.

packages/renderers-core/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ deleteDirectory(directoryPath);
3434

3535
// Creates a new file at the given path with the given content.
3636
// Creates its parent directory, recursively, if it does not exist.
37-
createFile(filePath, content);
37+
writeFile(filePath, content);
3838
```
3939

4040
## Render maps

packages/renderers-core/src/RenderMap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CODAMA_ERROR__VISITORS__RENDER_MAP_KEY_NOT_FOUND, CodamaError } from '@codama/errors';
22

3-
import { createFile } from './fs';
3+
import { writeFile } from './fs';
44

55
export class RenderMap {
66
protected readonly _map: Map<string, string> = new Map();
@@ -67,7 +67,7 @@ export class RenderMap {
6767

6868
write(path: string): void {
6969
this._map.forEach((code, relativePath) => {
70-
createFile(`${path}/${relativePath}`, code);
70+
writeFile(`${path}/${relativePath}`, code);
7171
});
7272
}
7373
}

packages/renderers-core/src/fs.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node
22

33
import { CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE, CodamaError } from '@codama/errors';
44

5-
export function readJson<T extends object>(value: string): T {
6-
if (!__NODEJS__) {
7-
throw new CodamaError(CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE, { fsFunction: 'readFileSync' });
8-
}
5+
import { Path, pathDirectory } from './path';
96

10-
return JSON.parse(readFileSync(value, 'utf-8')) as T;
11-
}
12-
13-
export const createDirectory = (path: string): void => {
7+
export const createDirectory = (path: Path): void => {
148
if (!__NODEJS__) {
159
throw new CodamaError(CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE, { fsFunction: 'mkdirSync' });
1610
}
1711

1812
mkdirSync(path, { recursive: true });
1913
};
2014

21-
export const deleteDirectory = (path: string): void => {
15+
export const deleteDirectory = (path: Path): void => {
2216
if (!__NODEJS__) {
2317
throw new CodamaError(CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE, { fsFunction: 'rmSync' });
2418
}
@@ -28,14 +22,26 @@ export const deleteDirectory = (path: string): void => {
2822
}
2923
};
3024

31-
export const createFile = (path: string, content: string): void => {
25+
export const writeFile = (path: Path, content: string): void => {
3226
if (!__NODEJS__) {
3327
throw new CodamaError(CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE, { fsFunction: 'writeFileSync' });
3428
}
3529

36-
const directory = path.substring(0, path.lastIndexOf('/'));
30+
const directory = pathDirectory(path);
3731
if (!existsSync(directory)) {
3832
createDirectory(directory);
3933
}
4034
writeFileSync(path, content);
4135
};
36+
37+
export function readFile(path: Path): string {
38+
if (!__NODEJS__) {
39+
throw new CodamaError(CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE, { fsFunction: 'readFileSync' });
40+
}
41+
42+
return readFileSync(path, 'utf-8');
43+
}
44+
45+
export function readJson<T>(path: Path): T {
46+
return JSON.parse(readFile(path)) as T;
47+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './fs';
2-
export * from './RenderMap';
2+
export * from './path';
3+
export * from './renderMap';
34
export * from './writeRenderMapVisitor';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { dirname, join } from 'node:path';
2+
3+
export type Path = string;
4+
5+
export function joinPath(...paths: Path[]): string {
6+
if (!__NODEJS__) {
7+
return paths.join('/').replace(/\/+/g, '/');
8+
}
9+
10+
return join(...paths);
11+
}
12+
13+
export function pathDirectory(path: Path): Path {
14+
if (!__NODEJS__) {
15+
return path.substring(0, path.lastIndexOf('/'));
16+
}
17+
18+
return dirname(path);
19+
}

packages/renderers-core/src/writeRenderMapVisitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NodeKind } from '@codama/nodes';
22
import { mapVisitor, Visitor } from '@codama/visitors-core';
33

4-
import { RenderMap } from './RenderMap';
4+
import { RenderMap } from './renderMap';
55

66
export function writeRenderMapVisitor<TNodeKind extends NodeKind = NodeKind>(
77
visitor: Visitor<RenderMap, TNodeKind>,

packages/renderers-core/test/fs.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE, CodamaError } from '@codama/errors';
22
import { expect, test } from 'vitest';
33

4-
import { createDirectory, createFile, deleteDirectory, readJson } from '../src';
4+
import { createDirectory, deleteDirectory, readJson, writeFile } from '../src';
55

66
if (__NODEJS__) {
77
test('it reads JSON objects from files', () => {
@@ -24,8 +24,8 @@ if (__NODEJS__) {
2424
new CodamaError(CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE, { fsFunction: 'rmSync' }),
2525
);
2626
});
27-
test('it fails to call createFile', () => {
28-
expect(() => createFile('./path', 'content')).toThrow(
27+
test('it fails to call writeFile', () => {
28+
expect(() => writeFile('./path', 'content')).toThrow(
2929
new CodamaError(CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE, { fsFunction: 'writeFileSync' }),
3030
);
3131
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { expect, test } from 'vitest';
2+
3+
import { joinPath, pathDirectory } from '../src';
4+
5+
test('it joins path together', () => {
6+
const result = joinPath('foo', 'bar', 'baz');
7+
expect(result).toEqual('foo/bar/baz');
8+
});
9+
10+
test('it gets the directory of a path', () => {
11+
const result = pathDirectory('foo/bar/baz');
12+
expect(result).toEqual('foo/bar');
13+
});

0 commit comments

Comments
 (0)