Skip to content

Commit

Permalink
feat(public/fs): allows rw and json to take overwrite option (IFsWrit…
Browse files Browse the repository at this point in the history
…eOptions)
  • Loading branch information
rafamel committed May 18, 2019
1 parent 02b0548 commit cc6db76
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/public/fs/json.ts
@@ -1,7 +1,7 @@
import { IOfType } from '~/types';
import expose from '~/utils/expose';
import rw from './rw';
import { IFsOptions } from './types';
import { IFsWriteOptions } from './types';

export default expose(json);

Expand All @@ -15,7 +15,7 @@ function json(
fn: (
json?: IOfType<any>
) => IOfType<any> | void | Promise<IOfType<any> | void>,
options: IFsOptions = {}
options: IFsWriteOptions = {}
): () => Promise<void> {
return async () => {
const _fn = async (raw?: string): Promise<string | undefined> => {
Expand Down
12 changes: 8 additions & 4 deletions src/public/fs/rw.ts
Expand Up @@ -3,8 +3,9 @@ import fs from 'fs-extra';
import { exists, absolute } from '~/utils/file';
import expose from '~/utils/expose';
import confirm from '~/utils/confirm';
import { IFsOptions } from './types';
import { IFsWriteOptions } from './types';
import logger from '~/utils/logger';
import { open } from '~/utils/errors';

export default expose(rw);

Expand All @@ -16,9 +17,11 @@ export default expose(rw);
function rw(
file: string,
fn: (raw?: string) => string | void | Promise<string | void>,
options: IFsOptions = {}
options: IFsWriteOptions = {}
): () => Promise<void> {
return async () => {
options = Object.assign({ overwrite: true }, options);

const cwd = process.cwd();
file = absolute({ path: file, cwd });
const relative = './' + path.relative(cwd, file);
Expand All @@ -27,8 +30,9 @@ function rw(

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

const response = await fn(raw);
if (response === undefined) {
const response: string | void = await fn(raw);

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

0 comments on commit cc6db76

Please sign in to comment.