Skip to content

Commit dfe2fb5

Browse files
BendingBendersindresorhus
authored andcommitted
Refactor TypeScript definition to CommonJS compatible export (#32)
1 parent ecf85a3 commit dfe2fb5

File tree

4 files changed

+93
-77
lines changed

4 files changed

+93
-77
lines changed

index.d.ts

Lines changed: 67 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,78 @@
1-
export interface Options {
2-
/**
3-
* Overwrite existing file.
4-
*
5-
* @default true
6-
*/
7-
readonly overwrite?: boolean;
1+
declare namespace cpFile {
2+
interface Options {
3+
/**
4+
Overwrite existing file.
5+
6+
@default true
7+
*/
8+
readonly overwrite?: boolean;
9+
}
10+
11+
interface ProgressData {
12+
/**
13+
Absolute path to source.
14+
*/
15+
src: string;
16+
17+
/**
18+
Absolute path to destination.
19+
*/
20+
dest: string;
21+
22+
/**
23+
File size in bytes.
24+
*/
25+
size: number;
26+
27+
/**
28+
Copied size in bytes.
29+
*/
30+
written: number;
31+
32+
/**
33+
Copied percentage, a value between `0` and `1`.
34+
*/
35+
percent: number;
36+
}
37+
38+
interface ProgressEmitter {
39+
/**
40+
For empty files, the `progress` event is emitted only once.
41+
*/
42+
on(event: 'progress', handler: (data: ProgressData) => void): Promise<void>;
43+
}
844
}
945

10-
export interface ProgressData {
46+
declare const cpFile: {
1147
/**
12-
* Absolute path to source.
13-
*/
14-
src: string;
48+
Copy a file.
1549
16-
/**
17-
* Absolute path to destination.
18-
*/
19-
dest: string;
50+
@param source - File you want to copy.
51+
@param destination - Where you want the file copied.
52+
@returns A `Promise` that resolves when the file is copied.
2053
21-
/**
22-
* File size in bytes.
23-
*/
24-
size: number;
54+
@example
55+
```
56+
import cpFile = require('cp-file');
2557
26-
/**
27-
* Copied size in bytes.
28-
*/
29-
written: number;
58+
(async () => {
59+
await cpFile('source/unicorn.png', 'destination/unicorn.png');
60+
console.log('File copied');
61+
})();
62+
```
63+
*/
64+
(source: string, destination: string, options?: cpFile.Options): Promise<void> & cpFile.ProgressEmitter;
3065

3166
/**
32-
* Copied percentage, a value between `0` and `1`.
33-
*/
34-
percent: number;
35-
}
67+
Copy a file synchronously.
3668
37-
export interface ProgressEmitter {
38-
/**
39-
* For empty files, the `progress` event is emitted only once.
40-
*/
41-
on(event: 'progress', handler: (data: ProgressData) => void): Promise<void>;
42-
}
69+
@param source - File you want to copy.
70+
@param destination - Where you want the file copied.
71+
*/
72+
sync(source: string, destination: string, options?: cpFile.Options): void;
4373

44-
/**
45-
* Copy a file.
46-
*
47-
* @param source - File you want to copy.
48-
* @param destination - Where you want the file copied.
49-
* @returns A `Promise` that resolves when the file is copied.
50-
*/
51-
export default function cpFile(
52-
source: string,
53-
destination: string,
54-
options?: Options
55-
): Promise<void> & ProgressEmitter;
74+
// TODO: Remove this for the next major release
75+
default: typeof cpFile;
76+
};
5677

57-
/**
58-
* Copy a file synchronously.
59-
*
60-
* @param source - File you want to copy.
61-
* @param destination - Where you want the file copied.
62-
*/
63-
export function sync(
64-
source: string,
65-
destination: string,
66-
options?: Options
67-
): void;
78+
export = cpFile;

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const cpFile = (source, destination, options) => {
6464
};
6565

6666
module.exports = cpFile;
67+
// TODO: Remove this for the next major release
6768
module.exports.default = cpFile;
6869

6970
const checkSourceIsFile = (stat, source) => {

index.test-d.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
import {expectType} from 'tsd-check';
2-
import cpFile, {sync as cpFileSync, ProgressEmitter, ProgressData} from '.';
1+
import {expectType} from 'tsd';
2+
import cpFile = require('.');
3+
import {ProgressEmitter, ProgressData} from '.';
34

4-
// `cpFile`
55
expectType<Promise<void> & ProgressEmitter>(
66
cpFile('source/unicorn.png', 'destination/unicorn.png')
77
);
88
expectType<Promise<void> & ProgressEmitter>(
99
cpFile('source/unicorn.png', 'destination/unicorn.png', {overwrite: false})
1010
);
1111
expectType<Promise<void>>(
12-
cpFile('source/unicorn.png', 'destination/unicorn.png').on('progress', data => {
13-
expectType<ProgressData>(data);
12+
cpFile('source/unicorn.png', 'destination/unicorn.png').on(
13+
'progress',
14+
data => {
15+
expectType<ProgressData>(data);
1416

15-
expectType<string>(data.src);
16-
expectType<string>(data.dest);
17-
expectType<number>(data.size);
18-
expectType<number>(data.written);
19-
expectType<number>(data.percent);
20-
})
17+
expectType<string>(data.src);
18+
expectType<string>(data.dest);
19+
expectType<number>(data.size);
20+
expectType<number>(data.written);
21+
expectType<number>(data.percent);
22+
}
23+
)
2124
);
2225

23-
// `cpFileSync`
24-
expectType<void>(cpFileSync('source/unicorn.png', 'destination/unicorn.png'));
26+
expectType<void>(cpFile.sync('source/unicorn.png', 'destination/unicorn.png'));
2527
expectType<void>(
26-
cpFileSync('source/unicorn.png', 'destination/unicorn.png', {overwrite: false})
28+
cpFile.sync('source/unicorn.png', 'destination/unicorn.png', {
29+
overwrite: false
30+
})
2731
);

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"node": ">=6"
2121
},
2222
"scripts": {
23-
"test": "xo && nyc ava && tsd-check"
23+
"test": "xo && nyc ava && tsd"
2424
},
2525
"files": [
2626
"cp-file-error.js",
@@ -52,15 +52,15 @@
5252
"safe-buffer": "^5.0.1"
5353
},
5454
"devDependencies": {
55-
"ava": "^1.2.1",
55+
"ava": "^1.4.1",
5656
"clear-module": "^3.1.0",
57-
"coveralls": "^3.0.0",
58-
"del": "^4.0.0",
57+
"coveralls": "^3.0.3",
58+
"del": "^4.1.0",
5959
"import-fresh": "^3.0.0",
6060
"nyc": "^13.3.0",
61-
"sinon": "^7.2.6",
62-
"tsd-check": "^0.3.0",
63-
"uuid": "^3.0.0",
61+
"sinon": "^7.3.1",
62+
"tsd": "^0.7.2",
63+
"uuid": "^3.3.2",
6464
"xo": "^0.24.0"
6565
}
6666
}

0 commit comments

Comments
 (0)