@@ -2,23 +2,17 @@ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node
22
33import { 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+ }
0 commit comments