From 0905b3d2f96c76f1d56df79616da56a436318bda Mon Sep 17 00:00:00 2001 From: Alessandro Vergani Date: Tue, 17 Oct 2017 17:00:05 +0200 Subject: [PATCH] Add copyFile to node file system (#20185) --- types/node/index.d.ts | 63 ++++++++++++++++++++++++++++++++++++---- types/node/node-tests.ts | 10 +++++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/types/node/index.d.ts b/types/node/index.d.ts index 7f619fe377..6455482803 100644 --- a/types/node/index.d.ts +++ b/types/node/index.d.ts @@ -108,10 +108,10 @@ interface NodeRequire extends NodeRequireFunction { } interface NodeExtensions { - '.js': (m: NodeModule, filename: string) => any; - '.json': (m: NodeModule, filename: string) => any; - '.node': (m: NodeModule, filename: string) => any; - [ext: string]: (m: NodeModule, filename: string) => any; + '.js': (m: NodeModule, filename: string) => any; + '.json': (m: NodeModule, filename: string) => any; + '.node': (m: NodeModule, filename: string) => any; + [ext: string]: (m: NodeModule, filename: string) => any; } declare var require: NodeRequire; @@ -4260,6 +4260,9 @@ declare module "fs" { /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating executable by others. */ export const S_IXOTH: number; + + /** Constant for fs.copyFile. Flag indicating the destination file should not be overwritten if it already exists. */ + export const COPYFILE_EXCL: number; } /** @@ -4342,6 +4345,54 @@ declare module "fs" { * @param fd A file descriptor. */ export function fdatasyncSync(fd: number): void; + + /** + * Asynchronously copies src to dest. By default, dest is overwritten if it already exists. + * No arguments other than a possible exception are given to the callback function. + * Node.js makes no guarantees about the atomicity of the copy operation. + * If an error occurs after the destination file has been opened for writing, Node.js will attempt + * to remove the destination. + * @param src A path to the source file. + * @param dest A path to the destination file. + */ + export function copyFile(src: PathLike, dest: PathLike, callback: (err: NodeJS.ErrnoException) => void): void; + /** + * Asynchronously copies src to dest. By default, dest is overwritten if it already exists. + * No arguments other than a possible exception are given to the callback function. + * Node.js makes no guarantees about the atomicity of the copy operation. + * If an error occurs after the destination file has been opened for writing, Node.js will attempt + * to remove the destination. + * @param src A path to the source file. + * @param dest A path to the destination file. + * @param flags An integer that specifies the behavior of the copy operation. The only supported flag is fs.constants.COPYFILE_EXCL, which causes the copy operation to fail if dest already exists. + */ + export function copyFile(src: PathLike, dest: PathLike, flags: number, callback: (err: NodeJS.ErrnoException) => void): void; + + // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. + export namespace copyFile { + /** + * Asynchronously copies src to dest. By default, dest is overwritten if it already exists. + * No arguments other than a possible exception are given to the callback function. + * Node.js makes no guarantees about the atomicity of the copy operation. + * If an error occurs after the destination file has been opened for writing, Node.js will attempt + * to remove the destination. + * @param src A path to the source file. + * @param dest A path to the destination file. + * @param flags An optional integer that specifies the behavior of the copy operation. The only supported flag is fs.constants.COPYFILE_EXCL, which causes the copy operation to fail if dest already exists. + */ + export function __promisify__(src: PathLike, dst: PathLike, flags?: number): Promise; + } + + /** + * Synchronously copies src to dest. By default, dest is overwritten if it already exists. + * Node.js makes no guarantees about the atomicity of the copy operation. + * If an error occurs after the destination file has been opened for writing, Node.js will attempt + * to remove the destination. + * @param src A path to the source file. + * @param dest A path to the destination file. + * @param flags An optional integer that specifies the behavior of the copy operation. The only supported flag is fs.constants.COPYFILE_EXCL, which causes the copy operation to fail if dest already exists. + */ + export function copyFileSync(src: PathLike, dest: PathLike, flags?: number): void; } declare module "path" { @@ -6404,8 +6455,8 @@ declare module "http2" { export type ClientSessionOptions = SessionOptions; export type ServerSessionOptions = SessionOptions; - export interface SecureClientSessionOptions extends ClientSessionOptions, tls.ConnectionOptions {} - export interface SecureServerSessionOptions extends ServerSessionOptions, tls.TlsOptions {} + export interface SecureClientSessionOptions extends ClientSessionOptions, tls.ConnectionOptions { } + export interface SecureServerSessionOptions extends ServerSessionOptions, tls.TlsOptions { } export interface ServerOptions extends ServerSessionOptions { allowHTTP1?: boolean; diff --git a/types/node/node-tests.ts b/types/node/node-tests.ts index 7883ef10f2..68ce6f5a18 100644 --- a/types/node/node-tests.ts +++ b/types/node/node-tests.ts @@ -339,6 +339,16 @@ namespace fs_tests { const v2 = fs.realpathSync('/path/to/folder', { encoding: s }); typeof v2 === "string" ? s = v2 : b = v2; } + + { + fs.copyFile('/path/to/src', '/path/to/dest', (err) => console.error(err)); + fs.copyFile('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_EXCL, (err) => console.error(err)); + + fs.copyFileSync('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_EXCL); + + const cf = util.promisify(fs.copyFile); + cf('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_EXCL).then(console.log); + } } ///////////////////////////////////////////////////////