Skip to content

Commit

Permalink
Require Node.js 10 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb authored Apr 21, 2020
1 parent 5d0cd0d commit c7286f3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ os:
- osx
language: node_js
node_js:
- '12'
- '10'
- '8'
3 changes: 0 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ declare const moveFile: {
@param destination - Where you want the file moved.
*/
sync(source: string, destination: string, options?: moveFile.Options): void;

// TODO: Remove this for the next major release
default: typeof moveFile;
};

export = moveFile;
28 changes: 9 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';
const util = require('util');
const path = require('path');
const fs = require('fs');
const cpFile = require('cp-file');
const pathExists = require('path-exists');
const makeDir = require('make-dir');

const moveFile = async (source, destination, options) => {
const fsP = fs.promises;

module.exports = async (source, destination, options) => {
if (!source || !destination) {
throw new TypeError('`source` and `destination` file required');
}
Expand All @@ -20,27 +19,20 @@ const moveFile = async (source, destination, options) => {
throw new Error(`The destination file exists: ${destination}`);
}

// TODO: Use the native `fs.mkdir` `recursive` option instead when targeting Node.js 10
await makeDir(path.dirname(destination));
await fsP.mkdir(path.dirname(destination), {recursive: true});

try {
await util.promisify(fs.rename)(source, destination);
await fsP.rename(source, destination);
} 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);
await fsP.copyFile(source, destination);
await fsP.unlink(source);
} else {
throw error;
}
}
};

module.exports = moveFile;
// TODO: Remove this for the next major release
module.exports.default = moveFile;

module.exports.sync = (source, destination, options) => {
if (!source || !destination) {
throw new TypeError('`source` and `destination` file required');
Expand All @@ -55,15 +47,13 @@ module.exports.sync = (source, destination, options) => {
throw new Error(`The destination file exists: ${destination}`);
}

makeDir.sync(path.dirname(destination));
fs.mkdirSync(path.dirname(destination), {recursive: true});

try {
fs.renameSync(source, destination);
} 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.copyFileSync(source, destination);
fs.unlinkSync(source);
} else {
throw error;
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -36,9 +36,7 @@
"partitions"
],
"dependencies": {
"cp-file": "^6.1.0",
"make-dir": "^3.0.0",
"path-exists": "^3.0.0"
"path-exists": "^4.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
Expand Down

0 comments on commit c7286f3

Please sign in to comment.