-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
40 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import fs from 'fs-extra'; | ||
import { exists, absolute } from '~/utils/file'; | ||
import expose from '~/utils/expose'; | ||
import { IFsReadOptions } from './types'; | ||
import { TScript } from '~/types'; | ||
|
||
export default expose(read); | ||
|
||
/** | ||
* Reads a `file` and passes it as an argument to a callback `fn`, which can return a `TScript`. | ||
* It is an *exposed* function: call `read.fn()`, which takes the same arguments, in order to execute on call. | ||
* @returns An asynchronous function -hence, calling `read` won't have any effect until the returned function is called. | ||
*/ | ||
function read( | ||
file: string, | ||
fn: (raw?: string) => TScript, | ||
options: IFsReadOptions = {} | ||
): () => Promise<TScript> { | ||
return async () => { | ||
const cwd = process.cwd(); | ||
file = absolute({ path: file, cwd }); | ||
const doesExist = await exists(file, { fail: options.fail }); | ||
const raw = doesExist ? await fs.readFile(file).then(String) : undefined; | ||
|
||
return fn(raw); | ||
}; | ||
} |
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