Skip to content

Commit

Permalink
feat(public/fs): makes errors occurred in user provided callbacks ins…
Browse files Browse the repository at this point in the history
…tances of OpenError
  • Loading branch information
rafamel committed May 18, 2019
1 parent cc6db76 commit a5784f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/public/fs/copy.ts
Expand Up @@ -5,6 +5,7 @@ import { IFsWriteOptions } from './types';
import expose, { TExposedOverload } from '~/utils/expose';
import confirm from '~/utils/confirm';
import logger from '~/utils/logger';
import { open } from '~/utils/errors';

export type TCopyFilterFn =
| ((src: string, dest: string) => boolean)
Expand Down Expand Up @@ -60,7 +61,7 @@ export async function trunk(
{ overwrite: true },
args.find((x) => typeof x === 'object') || {}
);
const filter: TCopyFilterFn =
let filter: TCopyFilterFn =
args.find((x) => typeof x === 'function') || (() => true);

const cwd = process.cwd();
Expand All @@ -84,7 +85,13 @@ export async function trunk(
await fs.copy(src, dest, {
overwrite: options.overwrite,
errorOnExist: options.fail,
filter
async filter(src: string, dest: string): Promise<boolean> {
try {
return await filter(src, dest);
} catch (err) {
throw open(err);
}
}
});
logger.info(`Copied: "${relatives.src}" to "${relatives.dest}"`);
}
7 changes: 6 additions & 1 deletion src/public/fs/rw.ts
Expand Up @@ -30,7 +30,12 @@ function rw(

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

const response: string | void = await fn(raw);
let response: string | void;
try {
response = await fn(raw);
} catch (e) {
throw open(e);
}

if (response === undefined || (doesExist && !options.overwrite)) {
logger.info(`Write skipped: ${relative}`);
Expand Down

0 comments on commit a5784f5

Please sign in to comment.