Skip to content

Commit

Permalink
feat: copy and move take a from option
Browse files Browse the repository at this point in the history
if a from option is passed the source folder structure will be replicated on destination
  • Loading branch information
rafamel committed Jun 28, 2024
1 parent 52976dd commit 5a6a273
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
35 changes: 31 additions & 4 deletions src/helpers/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,27 @@ export async function getPathPairs(
paths: string | string[],
destination: string,
context: Context,
options: { single: boolean; glob: boolean; strict: boolean }
options: {
single: boolean;
glob: boolean;
strict: boolean;
from: string | null;
}
): Promise<Array<[string, string]>> {
const from = options.from;
const dest = getAbsolutePath(destination, context);
const sources = await getPaths(paths, context, options);
const arr = Array.isArray(paths) ? paths : [paths];
const sources = await getPaths(
arr.map((x) => path.resolve(from || './', x)),
context,
options
);

if (options.single) {
if (sources.length > 1) {
throw new Error(`Multiple sources provided for single mode`);
}
return [[sources[0], dest]];
if (!from) return [[sources[0], dest]];
}

const destExists = await fs.pathExists(dest);
Expand All @@ -87,9 +98,25 @@ export async function getPathPairs(
throw new Error(`Destination path is not a directory: ${dest}`);
}

if (!from) {
return sources.map((source) => [
source,
path.join(dest, path.basename(source))
]);
}

const fromExists = await fs.pathExists(from);
const isFromDir = fromExists
? await fs.stat(from).then((x) => x.isDirectory())
: false;
if (!isFromDir) {
throw new Error(`Options.from path is not a directory: ${from}`);
}

const absoluteFrom = getAbsolutePath(from, context);
return sources.map((source) => [
source,
path.join(dest, path.basename(source))
path.join(dest, source.substring(absoluteFrom.length))
]);
}

Expand Down
13 changes: 11 additions & 2 deletions src/tasks/filesystem/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface CopyOptions {
strict?: boolean;
/** Whether to error, ignore, or overwrite existing files */
exists?: 'error' | 'ignore' | 'overwrite';
/** Absolute path, or relative to the cwd, to resolve paths from; if not null, the source folder structure will be replicated on destination */
from?: string | null;
}

/**
Expand All @@ -32,13 +34,20 @@ export function copy(
log('debug', 'Copy', paths, 'to', destination),
async (ctx: Context): Promise<void> => {
const opts = shallow(
{ glob: false, single: false, strict: false, exists: 'error' },
{
glob: false,
single: false,
strict: false,
exists: 'error',
from: null
},
options || undefined
);
const pairs = await getPathPairs(paths, destination, ctx, {
glob: opts.glob,
single: opts.single,
strict: opts.strict
strict: opts.strict,
from: opts.from
});

for (const pair of pairs) {
Expand Down
14 changes: 12 additions & 2 deletions src/tasks/filesystem/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface MoveOptions {
strict?: boolean;
/** Whether to error, ignore, or overwrite existing files */
exists?: 'error' | 'ignore' | 'overwrite';
/** Absolute path, or relative to the cwd, to resolve paths from; if not null, the source folder structure will be replicated on destination */
from?: string | null;
}

/**
Expand All @@ -32,17 +34,25 @@ export function move(
log('debug', 'Move', paths, 'to', destination),
async (ctx: Context): Promise<void> => {
const opts = shallow(
{ glob: false, single: false, strict: false, exists: 'error' },
{
glob: false,
single: false,
strict: false,
exists: 'error',
from: null
},
options || undefined
);
const pairs = await getPathPairs(paths, destination, ctx, {
glob: opts.glob,
single: opts.single,
strict: opts.strict
strict: opts.strict,
from: opts.from
});

for (const pair of pairs) {
if (isCancelled(ctx)) return;

await usePair(
pair,
ctx,
Expand Down

0 comments on commit 5a6a273

Please sign in to comment.