-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor TypeScript definition to CommonJS compatible export (#32)
- Loading branch information
1 parent
ecf85a3
commit dfe2fb5
Showing
4 changed files
with
93 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<void>; | ||
} | ||
} | ||
|
||
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<void> & 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<void>; | ||
} | ||
@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<void> & 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,31 @@ | ||
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<Promise<void> & ProgressEmitter>( | ||
cpFile('source/unicorn.png', 'destination/unicorn.png') | ||
); | ||
expectType<Promise<void> & ProgressEmitter>( | ||
cpFile('source/unicorn.png', 'destination/unicorn.png', {overwrite: false}) | ||
); | ||
expectType<Promise<void>>( | ||
cpFile('source/unicorn.png', 'destination/unicorn.png').on('progress', data => { | ||
expectType<ProgressData>(data); | ||
cpFile('source/unicorn.png', 'destination/unicorn.png').on( | ||
'progress', | ||
data => { | ||
expectType<ProgressData>(data); | ||
|
||
expectType<string>(data.src); | ||
expectType<string>(data.dest); | ||
expectType<number>(data.size); | ||
expectType<number>(data.written); | ||
expectType<number>(data.percent); | ||
}) | ||
expectType<string>(data.src); | ||
expectType<string>(data.dest); | ||
expectType<number>(data.size); | ||
expectType<number>(data.written); | ||
expectType<number>(data.percent); | ||
} | ||
) | ||
); | ||
|
||
// `cpFileSync` | ||
expectType<void>(cpFileSync('source/unicorn.png', 'destination/unicorn.png')); | ||
expectType<void>(cpFile.sync('source/unicorn.png', 'destination/unicorn.png')); | ||
expectType<void>( | ||
cpFileSync('source/unicorn.png', 'destination/unicorn.png', {overwrite: false}) | ||
cpFile.sync('source/unicorn.png', 'destination/unicorn.png', { | ||
overwrite: false | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters