Skip to content

Commit

Permalink
feat(exposed/fs): adds copy
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 29, 2019
1 parent dd86f4e commit 9a5c313
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
86 changes: 86 additions & 0 deletions src/exposed/fs/copy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import path from 'path';
import fs from 'fs-extra';
import core from '~/core';
import { absolute, exists } from '~/utils/file';
import { IFsWriteOptions } from './types';
import expose, { TExposedOverload } from '~/utils/expose';
import confirm from './utils/confirm';
import { rejects } from 'errorish';
import logger from '~/utils/logger';

export type TCopyFilterFn =
| ((src: string, dest: string) => boolean)
| ((src: string, dest: string) => Promise<boolean>);

export default expose(copy) as TExposedOverload<
typeof copy,
| [string, string]
| [string, string, IFsWriteOptions]
| [string, string, TCopyFilterFn]
| [string, string, IFsWriteOptions | undefined, TCopyFilterFn]
>;

function copy(
src: string,
dest: string,
filter?: TCopyFilterFn
): () => Promise<void>;
function copy(
src: string,
dest: string,
options?: IFsWriteOptions,
filter?: TCopyFilterFn
): () => Promise<void>;
/**
* Recursive copy.
* It is an *exposed* function: call `copy.fn()`, which takes the same arguments, in order to execute on call.
* @returns An asynchronous function -hence, calling `copy` won't have any effect until the returned function is called.
*/
function copy(src: string, dest: string, ...args: any[]): () => Promise<void> {
return async () => {
const options: IFsWriteOptions = Object.assign(
{ overwrite: true },
args.find((x) => typeof x === 'object') || {}
);
const filter: TCopyFilterFn =
args.find((x) => typeof x === 'function') || (() => true);

const cwd = await core.cwd();
src = absolute({ path: src, cwd });
dest = absolute({ path: dest, cwd });

const relatives = {
src: path.relative(src, cwd),
dest: path.relative(dest, cwd)
};

const srcExist = await exists(src, { fail: options.fail });
if (!srcExist) return;

const destExists = await exists(dest);
if (destExists) {
if (options.fail) {
throw Error(`Destination already exists: ${relatives.dest}`);
}
if (!options.overwrite) return;
}

if (
!(await confirm(
`Copy "${relatives.src}" to "${relatives.dest}"?`,
options
))
) {
return;
}

await fs
.copy(src, dest, {
overwrite: options.overwrite,
errorOnExist: options.fail,
filter
})
.catch(rejects);
logger.info(`Copied "${relatives.src}" to "${relatives.dest}"`);
};
}
3 changes: 2 additions & 1 deletion src/exposed/fs/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export { default as copy } from './copy';
export { default as json } from './json';
export { default as remove } from './remove';
export { default as mkdir } from './mkdir';
export { default as rw } from './rw';
export { default as write } from './write';

// TODO: copy, move
// TODO: move

0 comments on commit 9a5c313

Please sign in to comment.