Skip to content

Commit

Permalink
feat(exposed/fs): adds write
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 28, 2019
1 parent 3a235cd commit 8706280
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/exposed/fs/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { default as json } from './json';
export { default as remove } from './remove';
export { default as rw } from './rw';
export { default as write } from './write';

// TODO: copy, mkdir, move, rw
// TODO: copy, mkdir, move
37 changes: 37 additions & 0 deletions src/exposed/fs/write.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import expose, { TExposedOverload } from '~/utils/expose';
import rw from './rw';
import { IFsOptions } from './types';

export default expose(write) as TExposedOverload<
typeof write,
| [string]
| [string, string]
| [string, IFsOptions]
| [string, string, IFsOptions]
>;

function write(file: string, raw?: string): () => Promise<void>;
function write(file: string, options?: IFsOptions): () => Promise<void>;
function write(
file: string,
raw: string,
options?: IFsOptions
): () => Promise<void>;
/**
* Writes a `file` with `raw`. If no `raw` content is passed, it will simply ensure it does exist.
*/
function write(file: string, ...args: any[]): () => Promise<void> {
return async () => {
const raw: string = args.find((x) => typeof x === 'string');
const options: IFsOptions = args.find((x) => typeof x === 'object') || {};

return rw.fn(
file,
(content) => {
if (raw) return raw;
return content === undefined ? '' : undefined;
},
{ confirm: false, ...options, fail: false }
);
};
}

0 comments on commit 8706280

Please sign in to comment.