Skip to content

Commit

Permalink
feat(public/fs): takes string array as src for copy and move
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed May 2, 2019
1 parent 7832dcc commit 0521531
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 59 deletions.
81 changes: 50 additions & 31 deletions src/public/fs/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,63 @@ function copy(
filter?: TCopyFilterFn
): () => Promise<void>;
/**
* Recursive copy.
* Recursive copy. If an array of paths is passed as `src`, `dest` will be expected to be a directory.
* 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> {
function copy(
src: string | 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);
if (Array.isArray(src)) {
for (let source of src) {
await trunk(source, path.join(dest, path.parse(source).base), args);
}
} else {
await trunk(src, dest, args);
}
};
}

const cwd = await core.cwd();
src = absolute({ path: src, cwd });
dest = absolute({ path: dest, cwd });
/** @hidden */
export async function trunk(
src: string,
dest: string,
args: any[]
): Promise<void> {
const options: IFsWriteOptions = Object.assign(
{ overwrite: true },
args.find((x) => typeof x === 'object') || {}
);
const filter: TCopyFilterFn =
args.find((x) => typeof x === 'function') || (() => true);

const relatives = {
src: './' + path.relative(cwd, src),
dest: './' + path.relative(cwd, dest)
};
const cwd = await core.cwd();
src = absolute({ path: src, cwd });
dest = absolute({ path: dest, cwd });

const srcExist = await exists(src, { fail: options.fail });
if (!srcExist) {
logger.info(`Copy skipped: "${relatives.src}" to "${relatives.dest}"`);
return;
}
const relatives = {
src: './' + path.relative(cwd, src),
dest: './' + path.relative(cwd, dest)
};

const msg = `Copy "${relatives.src}" to "${relatives.dest}"?`;
if (!(await confirm(msg, options))) return;
const srcExist = await exists(src, { fail: options.fail });
if (!srcExist) {
logger.info(`Copy skipped: "${relatives.src}" to "${relatives.dest}"`);
return;
}

await fs
.copy(src, dest, {
overwrite: options.overwrite,
errorOnExist: options.fail,
filter
})
.catch(rejects);
logger.info(`Copied: "${relatives.src}" to "${relatives.dest}"`);
};
const msg = `Copy "${relatives.src}" to "${relatives.dest}"?`;
if (!(await confirm(msg, options))) return;

await fs
.copy(src, dest, {
overwrite: options.overwrite,
errorOnExist: options.fail,
filter
})
.catch(rejects);
logger.info(`Copied: "${relatives.src}" to "${relatives.dest}"`);
}
71 changes: 43 additions & 28 deletions src/public/fs/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,63 @@ import logger from '~/utils/logger';
export default expose(move);

/**
* Move files or directories.
* Move files or directories. If an array of paths is passed as `src`, `dest` will be expected to be a directory.
* It is an *exposed* function: call `move.fn()`, which takes the same arguments, in order to execute on call.
* @returns An asynchronous function -hence, calling `move` won't have any effect until the returned function is called.
*/
function move(
src: string,
src: string | string[],
dest: string,
options: IFsWriteOptions = {}
): () => Promise<void> {
return async () => {
options = Object.assign({ overwrite: true }, options);
if (Array.isArray(src)) {
for (let source of src) {
await trunk(source, path.join(dest, path.parse(source).base), options);
}
} else {
await trunk(src, dest, options);
}
};
}

/** @hidden */
export async function trunk(
src: string,
dest: string,
options: IFsWriteOptions
): Promise<void> {
options = Object.assign({ overwrite: true }, options);

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

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

const srcExist = await exists(src, { fail: options.fail });
if (!srcExist) {
logger.info(`Move skipped: "${relatives.src}" to "${relatives.dest}"`);
return;
}

const srcExist = await exists(src, { fail: options.fail });
if (!srcExist) {
const destExists = await exists(dest);
if (destExists) {
if (options.fail) {
throw Error(`Destination already exists: ${relatives.dest}`);
}
if (!options.overwrite) {
logger.info(`Move skipped: "${relatives.src}" to "${relatives.dest}"`);
return;
}
}

const destExists = await exists(dest);
if (destExists) {
if (options.fail) {
throw Error(`Destination already exists: ${relatives.dest}`);
}
if (!options.overwrite) {
logger.info(`Move skipped: "${relatives.src}" to "${relatives.dest}"`);
return;
}
}

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

await fs.move(src, dest, { overwrite: options.overwrite }).catch(rejects);
logger.info(`Moved: "${relatives.src}" to "${relatives.dest}"`);
};
await fs.move(src, dest, { overwrite: options.overwrite }).catch(rejects);
logger.info(`Moved: "${relatives.src}" to "${relatives.dest}"`);
}

0 comments on commit 0521531

Please sign in to comment.