diff --git a/src/exposed/fs/index.ts b/src/exposed/fs/index.ts index 711e222..c560e76 100644 --- a/src/exposed/fs/index.ts +++ b/src/exposed/fs/index.ts @@ -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 diff --git a/src/exposed/fs/write.ts b/src/exposed/fs/write.ts new file mode 100644 index 0000000..2daf8dd --- /dev/null +++ b/src/exposed/fs/write.ts @@ -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; +function write(file: string, options?: IFsOptions): () => Promise; +function write( + file: string, + raw: string, + options?: IFsOptions +): () => Promise; +/** + * 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 { + 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 } + ); + }; +}