Skip to content

Commit

Permalink
refactor(public/fs): write, rw, and json callbacks take an object ins…
Browse files Browse the repository at this point in the history
…tead of several params

BREAKING CHANGE: write, rw, and json callbacks signature has changed
  • Loading branch information
rafamel committed May 20, 2019
1 parent 22fc5b4 commit d43f33a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
17 changes: 9 additions & 8 deletions src/public/fs/json.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IOfType } from '~/types';
import expose from '~/utils/expose';
import rw from './rw';
import { IFsUpdateOptions, TContentFn, TSource } from './types';
import { IFsUpdateOptions, TContentFn, TSource, TJsonFn } from './types';
import { IOfType } from '~/types';

export default expose(json);

Expand All @@ -12,15 +12,16 @@ export default expose(json);
*/
function json(
file: TSource,
fn: (
file: string,
json?: IOfType<any>
) => IOfType<any> | void | Promise<IOfType<any> | void>,
fn: TJsonFn,
options?: IFsUpdateOptions
): () => Promise<void> {
return async () => {
const _fn: TContentFn = async (file, raw) => {
const json = await fn(file, raw ? JSON.parse(raw) : undefined);
const _fn: TContentFn = async (data) => {
Object.defineProperty(data, 'json', {
enumerable: true,
get: (): IOfType<any> => (data.raw ? JSON.parse(data.raw) : undefined)
});
const json = await fn(data);
return json ? JSON.stringify(json, null, 2) : undefined;
};

Expand Down
4 changes: 1 addition & 3 deletions src/public/fs/rw/rw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ export async function each(
const cwd = process.cwd();
file = absolute({ path: file, cwd });
const relative = './' + path.relative(cwd, file);

const doesExist = await exists(file, { fail: options.fail });

const raw = doesExist ? await fs.readFile(file).then(String) : undefined;

let response: string | void;
try {
response = await fn(file, raw);
response = await fn({ file, raw });
} catch (e) {
throw open(e);
}
Expand Down
16 changes: 12 additions & 4 deletions src/public/fs/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IOfType } from '~/types';

export type TSource =
| string
| string[]
Expand All @@ -10,10 +12,16 @@ export type TCopyFilterFn =
| ((src: string, dest: string) => boolean)
| ((src: string, dest: string) => Promise<boolean>);

export type TContentFn = (
file: string,
raw?: string
) => string | void | Promise<string | void>;
export type TContentFn = (data: {
file: string;
raw?: string;
}) => string | void | Promise<string | void>;

export type TJsonFn = (data: {
file: string;
raw?: string;
json?: IOfType<any>;
}) => IOfType<any> | void | Promise<IOfType<any> | void>;

/**
* Options taken by read *fs* functions.
Expand Down
2 changes: 1 addition & 1 deletion src/public/fs/write/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function each(

if (typeof raw === 'function') {
try {
raw = await raw(file);
raw = await raw({ file });
} catch (err) {
throw open(err);
}
Expand Down

0 comments on commit d43f33a

Please sign in to comment.