Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript definition, update dependencies #29

Merged
merged 3 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ exports.createWriteStream = fs.createWriteStream.bind(fs);
exports.createReadStream = (path, options) => new Promise((resolve, reject) => {
const read = fs.createReadStream(path, options);

read.once('error', err => {
reject(new CpFileError(`Cannot read from \`${path}\`: ${err.message}`, err));
read.once('error', error => {
reject(new CpFileError(`Cannot read from \`${path}\`: ${error.message}`, error));
});

read.once('readable', () => {
Expand All @@ -25,122 +25,122 @@ exports.createReadStream = (path, options) => new Promise((resolve, reject) => {
});
});

exports.stat = path => fsP.stat(path).catch(err => {
throw new CpFileError(`Cannot stat path \`${path}\`: ${err.message}`, err);
exports.stat = path => fsP.stat(path).catch(error => {
throw new CpFileError(`Cannot stat path \`${path}\`: ${error.message}`, error);
});

exports.lstat = path => fsP.lstat(path).catch(err => {
throw new CpFileError(`lstat \`${path}\` failed: ${err.message}`, err);
exports.lstat = path => fsP.lstat(path).catch(error => {
throw new CpFileError(`lstat \`${path}\` failed: ${error.message}`, error);
});

exports.utimes = (path, atime, mtime) => fsP.utimes(path, atime, mtime).catch(err => {
throw new CpFileError(`utimes \`${path}\` failed: ${err.message}`, err);
exports.utimes = (path, atime, mtime) => fsP.utimes(path, atime, mtime).catch(error => {
throw new CpFileError(`utimes \`${path}\` failed: ${error.message}`, error);
});

exports.chmod = (path, mode) => fsP.chmod(path, mode).catch(err => {
throw new CpFileError(`chmod \`${path}\` failed: ${err.message}`, err);
exports.chmod = (path, mode) => fsP.chmod(path, mode).catch(error => {
throw new CpFileError(`chmod \`${path}\` failed: ${error.message}`, error);
});

exports.chown = (path, uid, gid) => fsP.chown(path, uid, gid).catch(err => {
throw new CpFileError(`chown \`${path}\` failed: ${err.message}`, err);
exports.chown = (path, uid, gid) => fsP.chown(path, uid, gid).catch(error => {
throw new CpFileError(`chown \`${path}\` failed: ${error.message}`, error);
});

exports.openSync = (path, flags, mode) => {
try {
return fs.openSync(path, flags, mode);
} catch (err) {
} catch (error) {
if (flags.includes('w')) {
throw new CpFileError(`Cannot write to \`${path}\`: ${err.message}`, err);
throw new CpFileError(`Cannot write to \`${path}\`: ${error.message}`, error);
}

throw new CpFileError(`Cannot open \`${path}\`: ${err.message}`, err);
throw new CpFileError(`Cannot open \`${path}\`: ${error.message}`, error);
}
};

// eslint-disable-next-line max-params
exports.readSync = (fd, buffer, offset, length, position, path) => {
try {
return fs.readSync(fd, buffer, offset, length, position);
} catch (err) {
throw new CpFileError(`Cannot read from \`${path}\`: ${err.message}`, err);
} catch (error) {
throw new CpFileError(`Cannot read from \`${path}\`: ${error.message}`, error);
}
};

// eslint-disable-next-line max-params
exports.writeSync = (fd, buffer, offset, length, position, path) => {
try {
return fs.writeSync(fd, buffer, offset, length, position);
} catch (err) {
throw new CpFileError(`Cannot write to \`${path}\`: ${err.message}`, err);
} catch (error) {
throw new CpFileError(`Cannot write to \`${path}\`: ${error.message}`, error);
}
};

exports.statSync = path => {
try {
return fs.statSync(path);
} catch (err) {
throw new CpFileError(`stat \`${path}\` failed: ${err.message}`, err);
} catch (error) {
throw new CpFileError(`stat \`${path}\` failed: ${error.message}`, error);
}
};

exports.fstatSync = (fd, path) => {
try {
return fs.fstatSync(fd);
} catch (err) {
throw new CpFileError(`fstat \`${path}\` failed: ${err.message}`, err);
} catch (error) {
throw new CpFileError(`fstat \`${path}\` failed: ${error.message}`, error);
}
};

exports.futimesSync = (fd, atime, mtime, path) => {
try {
return fs.futimesSync(fd, atime, mtime, path);
} catch (err) {
throw new CpFileError(`futimes \`${path}\` failed: ${err.message}`, err);
} catch (error) {
throw new CpFileError(`futimes \`${path}\` failed: ${error.message}`, error);
}
};

exports.utimesSync = (path, atime, mtime) => {
try {
return fs.utimesSync(path, atime, mtime);
} catch (err) {
throw new CpFileError(`utimes \`${path}\` failed: ${err.message}`, err);
} catch (error) {
throw new CpFileError(`utimes \`${path}\` failed: ${error.message}`, error);
}
};

exports.chmodSync = (path, mode) => {
try {
return fs.chmodSync(path, mode);
} catch (err) {
throw new CpFileError(`chmod \`${path}\` failed: ${err.message}`, err);
} catch (error) {
throw new CpFileError(`chmod \`${path}\` failed: ${error.message}`, error);
}
};

exports.chownSync = (path, uid, gid) => {
try {
return fs.chownSync(path, uid, gid);
} catch (err) {
throw new CpFileError(`chown \`${path}\` failed: ${err.message}`, err);
} catch (error) {
throw new CpFileError(`chown \`${path}\` failed: ${error.message}`, error);
}
};

exports.makeDir = path => makeDir(path, {fs}).catch(err => {
throw new CpFileError(`Cannot create directory \`${path}\`: ${err.message}`, err);
exports.makeDir = path => makeDir(path, {fs}).catch(error => {
throw new CpFileError(`Cannot create directory \`${path}\`: ${error.message}`, error);
});

exports.makeDirSync = path => {
try {
makeDir.sync(path, {fs});
} catch (err) {
throw new CpFileError(`Cannot create directory \`${path}\`: ${err.message}`, err);
} catch (error) {
throw new CpFileError(`Cannot create directory \`${path}\`: ${error.message}`, error);
}
};

if (fs.copyFileSync) {
exports.copyFileSync = (src, dest, flags) => {
try {
fs.copyFileSync(src, dest, flags);
} catch (err) {
throw new CpFileError(`Cannot copy from \`${src}\` to \`${dest}\`: ${err.message}`, err);
} catch (error) {
throw new CpFileError(`Cannot copy from \`${src}\` to \`${dest}\`: ${error.message}`, error);
}
};
}
67 changes: 67 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
export interface Options {
/**
* Overwrite existing file.
*
* @default true
*/
readonly overwrite?: boolean;
}

export 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;
}

export interface ProgressEmitter {
/**
* For empty files, the `progress` event is emitted only once.
*/
on(event: 'progress', handler: (data: ProgressData) => void): Promise<void>;
}

/**
* Copy a file.
*
* @param source - File you want to copy.
* @param destination - Where you want the file copied.
* @returns A `Promise`.
*/
export default function cpFile(
source: string,
destination: string,
options?: Options
): Promise<void> & ProgressEmitter;

/**
* 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;
17 changes: 10 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const CpFileError = require('./cp-file-error');
const fs = require('./fs');
const ProgressEmitter = require('./progress-emitter');

module.exports = (src, dest, opts) => {
const cpFile = (src, dest, opts) => {
if (!src || !dest) {
return Promise.reject(new CpFileError('`src` and `dest` required'));
}
Expand Down Expand Up @@ -63,6 +63,9 @@ module.exports = (src, dest, opts) => {
return promise;
};

module.exports = cpFile;
module.exports.default = cpFile;

const checkSrcIsFile = (stat, src) => {
if (stat.isDirectory()) {
throw Object.assign(new CpFileError(`EISDIR: illegal operation on a directory '${src}'`), {
Expand All @@ -86,12 +89,12 @@ const copySyncNative = (src, dest, opts) => {
const flags = opts.overwrite ? null : fsConstants.COPYFILE_EXCL;
try {
fs.copyFileSync(src, dest, flags);
} catch (err) {
if (!opts.overwrite && err.code === 'EEXIST') {
} catch (error) {
if (!opts.overwrite && error.code === 'EEXIST') {
return;
}

throw err;
throw error;
}

fs.utimesSync(dest, stat.atime, stat.mtime);
Expand All @@ -115,12 +118,12 @@ const copySyncFallback = (src, dest, opts) => {

try {
write = fs.openSync(dest, opts.overwrite ? 'w' : 'wx');
} catch (err) {
if (!opts.overwrite && err.code === 'EEXIST') {
} catch (error) {
if (!opts.overwrite && error.code === 'EEXIST') {
return;
}

throw err;
throw error;
}

writeSync();
Expand Down
27 changes: 27 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {expectType} from 'tsd-check';
import cpFile, {sync as cpFileSync, ProgressEmitter, ProgressData} from '.';

// CpFile
expectType<Promise<void> & ProgressEmitter>(
cpFile('src/unicorn.png', 'dist/unicorn.png')
);
expectType<Promise<void> & ProgressEmitter>(
cpFile('src/unicorn.png', 'dist/unicorn.png', {overwrite: false})
);
expectType<Promise<void>>(
cpFile('src/unicorn.png', 'dist/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);
})
);

// CpFile (sync)
expectType<void>(cpFileSync('src/unicorn.png', 'dist/unicorn.png'));
expectType<void>(
cpFileSync('src/unicorn.png', 'dist/unicorn.png', {overwrite: false})
);
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
"node": ">=6"
},
"scripts": {
"test": "xo && nyc ava"
"test": "xo && nyc ava && tsd-check"
},
"files": [
"cp-file-error.js",
"fs.js",
"index.js",
"index.d.ts",
"progress-emitter.js"
],
"keywords": [
Expand All @@ -45,20 +46,21 @@
],
"dependencies": {
"graceful-fs": "^4.1.2",
"make-dir": "^1.0.0",
"make-dir": "^2.0.0",
"nested-error-stacks": "^2.0.0",
"pify": "^3.0.0",
"pify": "^4.0.1",
"safe-buffer": "^5.0.1"
},
"devDependencies": {
"ava": "*",
"clear-module": "^2.1.0",
"ava": "^1.2.1",
"clear-module": "^3.1.0",
"coveralls": "^3.0.0",
"del": "^3.0.0",
"import-fresh": "^2.0.0",
"nyc": "^11.2.1",
"sinon": "^5.0.0",
"import-fresh": "^3.0.0",
"nyc": "^13.3.0",
"sinon": "^7.2.6",
"tsd-check": "^0.3.0",
"uuid": "^3.0.0",
"xo": "*"
"xo": "^0.24.0"
}
}
Loading