From dfe2fb5b96bd18532480567634a85707bc3d7830 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Thu, 4 Apr 2019 04:24:25 +0000 Subject: [PATCH] Refactor TypeScript definition to CommonJS compatible export (#32) --- index.d.ts | 123 ++++++++++++++++++++++++++---------------------- index.js | 1 + index.test-d.ts | 32 +++++++------ package.json | 14 +++--- 4 files changed, 93 insertions(+), 77 deletions(-) diff --git a/index.d.ts b/index.d.ts index 2d059a7..e728044 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,67 +1,78 @@ -export interface Options { - /** - * Overwrite existing file. - * - * @default true - */ - readonly overwrite?: boolean; +declare namespace cpFile { + interface Options { + /** + Overwrite existing file. + + @default true + */ + readonly overwrite?: boolean; + } + + interface ProgressData { + /** + Absolute path to source. + */ + src: string; + + /** + Absolute path to destination. + */ + dest: string; + + /** + File size in bytes. + */ + size: number; + + /** + Copied size in bytes. + */ + written: number; + + /** + Copied percentage, a value between `0` and `1`. + */ + percent: number; + } + + interface ProgressEmitter { + /** + For empty files, the `progress` event is emitted only once. + */ + on(event: 'progress', handler: (data: ProgressData) => void): Promise; + } } -export interface ProgressData { +declare const cpFile: { /** - * Absolute path to source. - */ - src: string; + Copy a file. - /** - * Absolute path to destination. - */ - dest: string; + @param source - File you want to copy. + @param destination - Where you want the file copied. + @returns A `Promise` that resolves when the file is copied. - /** - * File size in bytes. - */ - size: number; + @example + ``` + import cpFile = require('cp-file'); - /** - * Copied size in bytes. - */ - written: number; + (async () => { + await cpFile('source/unicorn.png', 'destination/unicorn.png'); + console.log('File copied'); + })(); + ``` + */ + (source: string, destination: string, options?: cpFile.Options): Promise & cpFile.ProgressEmitter; /** - * Copied percentage, a value between `0` and `1`. - */ - percent: number; -} + Copy a file synchronously. -export interface ProgressEmitter { - /** - * For empty files, the `progress` event is emitted only once. - */ - on(event: 'progress', handler: (data: ProgressData) => void): Promise; -} + @param source - File you want to copy. + @param destination - Where you want the file copied. + */ + sync(source: string, destination: string, options?: cpFile.Options): void; -/** - * Copy a file. - * - * @param source - File you want to copy. - * @param destination - Where you want the file copied. - * @returns A `Promise` that resolves when the file is copied. - */ -export default function cpFile( - source: string, - destination: string, - options?: Options -): Promise & ProgressEmitter; + // TODO: Remove this for the next major release + default: typeof cpFile; +}; -/** - * Copy a file synchronously. - * - * @param source - File you want to copy. - * @param destination - Where you want the file copied. - */ -export function sync( - source: string, - destination: string, - options?: Options -): void; +export = cpFile; diff --git a/index.js b/index.js index 7b64548..5dbcf56 100644 --- a/index.js +++ b/index.js @@ -64,6 +64,7 @@ const cpFile = (source, destination, options) => { }; module.exports = cpFile; +// TODO: Remove this for the next major release module.exports.default = cpFile; const checkSourceIsFile = (stat, source) => { diff --git a/index.test-d.ts b/index.test-d.ts index d2eb910..02b31a9 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,7 +1,7 @@ -import {expectType} from 'tsd-check'; -import cpFile, {sync as cpFileSync, ProgressEmitter, ProgressData} from '.'; +import {expectType} from 'tsd'; +import cpFile = require('.'); +import {ProgressEmitter, ProgressData} from '.'; -// `cpFile` expectType & ProgressEmitter>( cpFile('source/unicorn.png', 'destination/unicorn.png') ); @@ -9,19 +9,23 @@ expectType & ProgressEmitter>( cpFile('source/unicorn.png', 'destination/unicorn.png', {overwrite: false}) ); expectType>( - cpFile('source/unicorn.png', 'destination/unicorn.png').on('progress', data => { - expectType(data); + cpFile('source/unicorn.png', 'destination/unicorn.png').on( + 'progress', + data => { + expectType(data); - expectType(data.src); - expectType(data.dest); - expectType(data.size); - expectType(data.written); - expectType(data.percent); - }) + expectType(data.src); + expectType(data.dest); + expectType(data.size); + expectType(data.written); + expectType(data.percent); + } + ) ); -// `cpFileSync` -expectType(cpFileSync('source/unicorn.png', 'destination/unicorn.png')); +expectType(cpFile.sync('source/unicorn.png', 'destination/unicorn.png')); expectType( - cpFileSync('source/unicorn.png', 'destination/unicorn.png', {overwrite: false}) + cpFile.sync('source/unicorn.png', 'destination/unicorn.png', { + overwrite: false + }) ); diff --git a/package.json b/package.json index 0330b70..fd7387e 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "node": ">=6" }, "scripts": { - "test": "xo && nyc ava && tsd-check" + "test": "xo && nyc ava && tsd" }, "files": [ "cp-file-error.js", @@ -52,15 +52,15 @@ "safe-buffer": "^5.0.1" }, "devDependencies": { - "ava": "^1.2.1", + "ava": "^1.4.1", "clear-module": "^3.1.0", - "coveralls": "^3.0.0", - "del": "^4.0.0", + "coveralls": "^3.0.3", + "del": "^4.1.0", "import-fresh": "^3.0.0", "nyc": "^13.3.0", - "sinon": "^7.2.6", - "tsd-check": "^0.3.0", - "uuid": "^3.0.0", + "sinon": "^7.3.1", + "tsd": "^0.7.2", + "uuid": "^3.3.2", "xo": "^0.24.0" } }