-
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.
refactor(public/fs): armonizes callbacks behavior for fs functions
BREAKING CHANGE: Callbacks for fs functions now receive an object with the appropriate data for each case
- Loading branch information
Showing
5 changed files
with
49 additions
and
40 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 |
---|---|---|
@@ -1,27 +1,27 @@ | ||
import fs from 'fs-extra'; | ||
import { exists, absolute } from '~/utils/file'; | ||
import { IFsReadOptions } from '../types'; | ||
import { IFsReadOptions, TReadFn } from '../types'; | ||
import { TScript } from '~/types'; | ||
|
||
export default async function read( | ||
file: string | string[], | ||
fn: (raw?: string) => TScript, | ||
src: string | string[], | ||
fn: TReadFn, | ||
options: IFsReadOptions = {} | ||
): Promise<TScript> { | ||
return Array.isArray(file) | ||
? Promise.all(file.map((item) => each(item, fn, options))) | ||
: each(file, fn, options); | ||
return Array.isArray(src) | ||
? Promise.all(src.map((file) => each(file, fn, options))) | ||
: each(src, fn, options); | ||
} | ||
|
||
export async function each( | ||
file: string, | ||
fn: (raw?: string) => TScript, | ||
src: string, | ||
fn: TReadFn, | ||
options: IFsReadOptions | ||
): Promise<TScript> { | ||
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; | ||
src = absolute({ path: src, cwd }); | ||
const doesExist = await exists(src, { fail: options.fail }); | ||
const raw = doesExist ? await fs.readFile(src).then(String) : undefined; | ||
|
||
return fn(raw); | ||
return fn({ src, 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
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