8000 Add TypeScript definition, update dependencies by BendingBender · Pull Request #4 · sindresorhus/move-file · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add TypeScript definition, update dependencies #4

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

Merged
merged 1 commit 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
< 8000 /div>
Diff view
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ os:
- osx
language: node_js
node_js 8000 :
- '10'
- '8'
33 changes: 33 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export interface Options {
/**
* Overwrite existing destination file.
*
* @default true
*/
readonly overwrite?: boolean;
}

/**
* Move a file.
*
* @param source - File you want to move.
* @param destination - Where you want the file moved.
* @returns A `Promise`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that this is not a useful description. I know you took it from the readme, but feel free to improve anything or comment if something could be improved. I really want the docs and everything to be very high quality.

*/
export default function mo 8000 veFile(
source: string,
destination: string,
options?: Options
): Promise<void>;

/**
* Move a file synchronously.
*
* @param source - File you want to move.
* @param destination - Where you want the file moved.
*/
export function sync(
source: string,
destination: string,
options?: Options
): void;
21 changes: 12 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const cpFile = require('cp-file');
const pathExists = require('path-exists');
const makeDir = require('make-dir');

module.exports = async (source, destination, options) => {
const moveFile = async (source, destination, options) => {
if (!source || !destination) {
throw new TypeError('`source` and `destination` file required');
}

options = Object.assign({overwrite: true}, options);
options = {overwrite: true, ...options};

if (!options.overwrite && await pathExists(destination)) {
throw new Error('Destination file exists');
Expand All @@ -21,24 +21,27 @@ module.exports = async (source, destination, options) => {

try {
await util.promisify(fs.rename)(source, destination);
} catch (err) {
if (err.code === 'EXDEV') {
} catch (error) {
if (error.code === 'EXDEV') {
// TODO: Remove this when Node.js 10 is target
const copy = fs.copyFile ? util.promisify(fs.copyFile) : cpFile;
await copy(source, destination);
await util.promisify(fs.unlink)(source);
} else {
throw err;
throw error;
}
}
};

module.exports = moveFile;
module.exports.default = moveFile;

module.exports.sync = (source, destination, options) => {
if (!source || !destination) {
throw new TypeError('`source` and `destination` file required');
}

options = Object.assign({overwrite: true}, options);
options = {overwrite: true, ...options};

if (!options.overwrite && fs.existsSync(destination)) {
throw new Error('Destination file exists');
Expand All @@ -48,14 +51,14 @@ module.exports.sync = (source, destination, options) => {

try {
fs.renameSync(source, destination);
} catch (err) {
if (err.code === 'EXDEV') {
} catch (error) {
if (error.code === 'EXDEV') {
// TODO: Remove this when Node.js 10 is target
const copy = fs.copyFileSync || cpFile.sync;
copy(source, destination);
fs.unlinkSync(source);
} else {
throw err;
8000 throw error;
}
}
};
15 changes: 15 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {expectType} from 'tsd-check';
import moveFile, {sync as moveFileSync} from '.';

expectType<Promise<void>>(
moveFile('source/unicorn.png', 'destination/unicorn.png')
);
expectType<Promise<void>>(
moveFile('source/unicorn.png', 'destination/unicorn.png', {overwrite: false})
);
expectType<void>(moveFileSync('source/unicorn.png', 'destination/unicorn.png'));
expectType<void>(
moveFileSync('source/unicorn.png', 'destination/unicorn.png', {
overwrite: false
})
);
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"move",
Expand All @@ -40,10 +41,11 @@
"path-exists": "^3.0.0"
},
"devDependencies": {
"ava": "*",
"ava": "^1.2.1",
"sinon": "^4.1.0",
"temp-write": "^3.3.0",
"tempy": "^0.2.1",
"xo": "*"
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
}
12 changes: 6 additions & 6 deletions test/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import test from 'ava';
import tempy from 'tempy';
import tempWrite from 'temp-write';
import sinon from 'sinon';
import m from '..';
import moveFile from '..';

const fixture = '🦄';

test('missing `source` or `destination` throws', async t => {
await t.throws(m());
await t.throwsAsync(moveFile());
});

test('move a file', async t => {
const destination = tempy.file();
await m(tempWrite.sync(fixture), destination);
await moveFile(tempWrite.sync(fixture), destination);
t.is(fs.readFileSync(destination, 'utf8'), fixture);
});

Expand All @@ -23,14 +23,14 @@ test.serial('move a file across devices', async t => {
fs.rename = sinon.stub(fs, 'rename').throws(exdevError);

const destination = tempy.file();
await m(tempWrite.sync(fixture), destination);
await moveFile(tempWrite.sync(fixture), destination);
t.is(fs.readFileSync(destination, 'utf8'), fixture);
fs.rename.restore();
});

test('overwrite option', async t => {
await t.throws(
m(tempWrite.sync('x'), tempWrite.sync('y'), {overwrite: false}),
await t.throwsAsync(
moveFile(tempWrite.sync('x'), tempWrite.sync('y'), {overwrite: false}),
/Destination file exists/
);
});
10 changes: 5 additions & 5 deletions test/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import test from 'ava';
import tempy from 'tempy';
import tempWrite from 'temp-write';
import sinon from 'sinon';
import m from '..';
import moveFile from '..';

const fixture = '🦄';

test('missing `source` or `destination` throws', t => {
t.throws(() => {
m.sync();
moveFile.sync();
});
});

test('move a file', t => {
const destination = tempy.file();
m.sync(tempWrite.sync(fixture), destination);
moveFile.sync(tempWrite.sync(fixture), destination);
t.is(fs.readFileSync(destination, 'utf8'), fixture);
});

Expand All @@ -25,13 +25,13 @@ test('move a file across devices', t => {
fs.renameSync = sinon.stub(fs, 'renameSync').throws(exdevError);

const destination = tempy.file();
m.sync(tempWrite.sync(fixture), destination);
moveFile.sync(tempWrite.sync(fixture), destination);
t.is(fs.readFileSync(destination, 'utf8'), fixture);
fs.renameSync.restore();
});

test('overwrite option', t => {
t.throws(() => {
m.sync(tempWrite.sync('x'), tempWrite.sync('y'), {overwrite: false});
moveFile.sync(tempWrite.sync('x'), tempWrite.sync('y'), {overwrite: false});
}, /Destination file exists/);
});
0