Skip to content

Commit

Permalink
fix: no node fs module in runtime
Browse files Browse the repository at this point in the history
closes #840
  • Loading branch information
jasonkuhrt committed May 11, 2024
1 parent ecf5032 commit dc299d9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/layers/2_generator/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { getPath } from '@dprint/typescript'
import _ from 'json-bigint'
import fs from 'node:fs/promises'
import * as Path from 'node:path'
import { fileExists } from '../../lib/prelude.js'
import type { OptionsInput } from './generateCode.js'
import { generateCode, type Input as GenerateInput } from './generateCode.js'
import { fileExists } from './prelude.js'

export interface Input {
name?: string
Expand Down
13 changes: 13 additions & 0 deletions src/layers/2_generator/prelude.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import fs from 'node:fs/promises'
import { errorFromMaybeError } from '../../lib/prelude.js'

export const fileExists = async (path: string) => {
return Boolean(
await fs.stat(path).catch((_: unknown) => {
const error = errorFromMaybeError(_)
return `code` in error && typeof error.code === `string` && error.code === `ENOENT`
? null
: Promise.reject(error)
}),
)
}
17 changes: 2 additions & 15 deletions src/lib/prelude.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ConditionalSimplifyDeep } from 'type-fest/source/conditional-simplify.js'

/* eslint-disable */
export type RemoveIndex<T> = {
[K in keyof T as string extends K ? never : number extends K ? never : K]: T[K]
Expand Down Expand Up @@ -180,23 +182,8 @@ export type Values<T> = T[keyof T]

export type GetKeyOr<T, Key, Or> = Key extends keyof T ? T[Key] : Or

import type { ConditionalSimplifyDeep } from 'type-fest/source/conditional-simplify.js'

export type SimplifyDeep<T> = ConditionalSimplifyDeep<T, Function | Iterable<unknown> | Date, object>

import fs from 'node:fs/promises'

export const fileExists = async (path: string) => {
return Boolean(
await fs.stat(path).catch((_: unknown) => {
const error = errorFromMaybeError(_)
return `code` in error && typeof error.code === `string` && error.code === `ENOENT`
? null
: Promise.reject(error)
}),
)
}

export type As<T, U> = U extends T ? U : never

export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
Expand Down

0 comments on commit dc299d9

Please sign in to comment.