-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"`); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |