Skip to content

Commit

Permalink
Refactor TypeScript definition to CommonJS compatible export (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 4, 2019
1 parent ecf85a3 commit dfe2fb5
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 77 deletions.
123 changes: 67 additions & 56 deletions index.d.ts
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;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
32 changes: 18 additions & 14 deletions index.test-d.ts
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
})
);
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"node": ">=6"
},
"scripts": {
"test": "xo && nyc ava && tsd-check"
"test": "xo && nyc ava && tsd"
},
"files": [
"cp-file-error.js",
Expand Down Expand Up @@ -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"
}
}

0 comments on commit dfe2fb5

Please sign in to comment.