diff options
Diffstat (limited to 'std/fs/copy.ts')
-rw-r--r-- | std/fs/copy.ts | 52 |
1 files changed, 29 insertions, 23 deletions
diff --git a/std/fs/copy.ts b/std/fs/copy.ts index f5e7f5078..783597882 100644 --- a/std/fs/copy.ts +++ b/std/fs/copy.ts @@ -23,21 +23,25 @@ async function ensureValidCopy( options: CopyOptions, isCopyFolder = false ): Promise<Deno.FileInfo> { - const destStat: Deno.FileInfo | null = await Deno.lstat(dest).catch( - (): Promise<null> => Promise.resolve(null) - ); - - if (destStat) { - if (isCopyFolder && !destStat.isDirectory()) { - throw new Error( - `Cannot overwrite non-directory '${dest}' with directory '${src}'.` - ); - } - if (!options.overwrite) { - throw new Error(`'${dest}' already exists.`); + let destStat: Deno.FileInfo | null; + + try { + destStat = await Deno.lstat(dest); + } catch (err) { + if (err instanceof Deno.DenoError && err.kind == Deno.ErrorKind.NotFound) { + return; } } + if (isCopyFolder && !destStat.isDirectory()) { + throw new Error( + `Cannot overwrite non-directory '${dest}' with directory '${src}'.` + ); + } + if (!options.overwrite) { + throw new Error(`'${dest}' already exists.`); + } + return destStat!; } @@ -51,19 +55,19 @@ function ensureValidCopySync( try { destStat = Deno.lstatSync(dest); - } catch { - // ignore error + } catch (err) { + if (err instanceof Deno.DenoError && err.kind == Deno.ErrorKind.NotFound) { + return; + } } - if (destStat!) { - if (isCopyFolder && !destStat!.isDirectory()) { - throw new Error( - `Cannot overwrite non-directory '${dest}' with directory '${src}'.` - ); - } - if (!options.overwrite) { - throw new Error(`'${dest}' already exists.`); - } + if (isCopyFolder && !destStat!.isDirectory()) { + throw new Error( + `Cannot overwrite non-directory '${dest}' with directory '${src}'.` + ); + } + if (!options.overwrite) { + throw new Error(`'${dest}' already exists.`); } return destStat!; @@ -186,6 +190,7 @@ function copyDirSync(src: string, dest: string, options: CopyOptions): void { /** * Copy a file or directory. The directory can have contents. Like `cp -r`. + * Requires the `--allow-read` and `--alow-write` flag. * @param src the file/directory path. * Note that if `src` is a directory it will copy everything inside * of this directory, not the entire directory itself @@ -224,6 +229,7 @@ export async function copy( /** * Copy a file or directory. The directory can have contents. Like `cp -r`. + * Requires the `--allow-read` and `--alow-write` flag. * @param src the file/directory path. * Note that if `src` is a directory it will copy everything inside * of this directory, not the entire directory itself |