-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require Node.js 12.20 and move to ESM
- Loading branch information
1 parent
0f39a71
commit ef6798c
Showing
9 changed files
with
117 additions
and
131 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,50 @@ | ||
declare namespace moveFile { | ||
interface Options { | ||
/** | ||
Overwrite existing destination file. | ||
export interface Options { | ||
/** | ||
Overwrite existing destination file. | ||
@default true | ||
*/ | ||
readonly overwrite?: boolean; | ||
@default true | ||
*/ | ||
readonly overwrite?: boolean; | ||
|
||
/** | ||
[Permissions](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation) for created directories. | ||
/** | ||
[Permissions](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation) for created directories. | ||
It has no effect on Windows. | ||
It has no effect on Windows. | ||
@default 0o777 | ||
*/ | ||
readonly directoryMode?: number; | ||
} | ||
@default 0o777 | ||
*/ | ||
readonly directoryMode?: number; | ||
} | ||
|
||
declare const moveFile: { | ||
/** | ||
Move a file. | ||
/** | ||
Move a file asynchronously. | ||
@param source - File you want to move. | ||
@param destination - Where you want the file moved. | ||
@returns A `Promise` that resolves when the file has been moved. | ||
@param sourcePath - The file you want to move. | ||
@param destinationPath - Where you want the file moved. | ||
@returns A `Promise` that resolves when the file has been moved. | ||
@example | ||
``` | ||
import moveFile = require('move-file'); | ||
@example | ||
``` | ||
import {moveFile} from 'move-file'; | ||
(async () => { | ||
await moveFile('source/unicorn.png', 'destination/unicorn.png'); | ||
console.log('The file has been moved'); | ||
})(); | ||
``` | ||
*/ | ||
(source: string, destination: string, options?: moveFile.Options): Promise<void>; | ||
await moveFile('source/unicorn.png', 'destination/unicorn.png'); | ||
console.log('The file has been moved'); | ||
``` | ||
*/ | ||
export function moveFile(sourcePath: string, destinationPath: string, options?: Options): Promise<void>; | ||
|
||
/** | ||
Move a file synchronously. | ||
/** | ||
Move a file synchronously. | ||
@param source - File you want to move. | ||
@param destination - Where you want the file moved. | ||
*/ | ||
sync(source: string, destination: string, options?: moveFile.Options): void; | ||
}; | ||
@param sourcePath - The file you want to move. | ||
@param destinationPath - Where you want the file moved. | ||
@example | ||
``` | ||
import {moveFileSync} from 'move-file'; | ||
export = moveFile; | ||
moveFileSync('source/unicorn.png', 'destination/unicorn.png'); | ||
console.log('The file has been moved'); | ||
``` | ||
*/ | ||
export function moveFileSync(sourcePath: string, destinationPath: string, options?: Options): void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,55 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const pathExists = require('path-exists'); | ||
import path from 'node:path'; | ||
import fs, {promises as fsP} from 'node:fs'; | ||
import {pathExists} from 'path-exists'; | ||
|
||
const fsP = fs.promises; | ||
|
||
module.exports = async (source, destination, options) => { | ||
if (!source || !destination) { | ||
throw new TypeError('`source` and `destination` file required'); | ||
export async function moveFile(sourcePath, destinationPath, {overwrite = true, directoryMode} = {}) { | ||
if (!sourcePath || !destinationPath) { | ||
throw new TypeError('`sourcePath` and `destinationPath` required'); | ||
} | ||
|
||
options = { | ||
overwrite: true, | ||
...options | ||
}; | ||
|
||
if (!options.overwrite && await pathExists(destination)) { | ||
throw new Error(`The destination file exists: ${destination}`); | ||
if (!overwrite && await pathExists(destinationPath)) { | ||
throw new Error(`The destination file exists: ${destinationPath}`); | ||
} | ||
|
||
await fsP.mkdir(path.dirname(destination), { | ||
await fsP.mkdir(path.dirname(destinationPath), { | ||
recursive: true, | ||
mode: options.directoryMode | ||
mode: directoryMode, | ||
}); | ||
|
||
try { | ||
await fsP.rename(source, destination); | ||
await fsP.rename(sourcePath, destinationPath); | ||
} catch (error) { | ||
if (error.code === 'EXDEV') { | ||
await fsP.copyFile(source, destination); | ||
await fsP.unlink(source); | ||
await fsP.copyFile(sourcePath, destinationPath); | ||
await fsP.unlink(sourcePath); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
module.exports.sync = (source, destination, options) => { | ||
if (!source || !destination) { | ||
throw new TypeError('`source` and `destination` file required'); | ||
export function moveFileSync(sourcePath, destinationPath, {overwrite = true, directoryMode} = {}) { | ||
if (!sourcePath || !destinationPath) { | ||
throw new TypeError('`sourcePath` and `destinationPath` required'); | ||
} | ||
|
||
options = { | ||
overwrite: true, | ||
...options | ||
}; | ||
|
||
if (!options.overwrite && fs.existsSync(destination)) { | ||
throw new Error(`The destination file exists: ${destination}`); | ||
if (!overwrite && fs.existsSync(destinationPath)) { | ||
throw new Error(`The destination file exists: ${destinationPath}`); | ||
} | ||
|
||
fs.mkdirSync(path.dirname(destination), { | ||
fs.mkdirSync(path.dirname(destinationPath), { | ||
recursive: true, | ||
mode: options.directoryMode | ||
mode: directoryMode, | ||
}); | ||
|
||
try { | ||
fs.renameSync(source, destination); | ||
fs.renameSync(sourcePath, destinationPath); | ||
} catch (error) { | ||
if (error.code === 'EXDEV') { | ||
fs.copyFileSync(source, destination); | ||
fs.unlinkSync(source); | ||
fs.copyFileSync(sourcePath, destinationPath); | ||
fs.unlinkSync(sourcePath); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,37 @@ | ||
import {expectError, expectType} from 'tsd'; | ||
import moveFile = require('.'); | ||
import {moveFile, moveFileSync} from './index.js'; | ||
|
||
expectType<Promise<void>>( | ||
moveFile('source/unicorn.png', 'destination/unicorn.png') | ||
moveFile('source/unicorn.png', 'destination/unicorn.png'), | ||
); | ||
expectType<Promise<void>>( | ||
moveFile('source/unicorn.png', 'destination/unicorn.png', {overwrite: false}) | ||
moveFile('source/unicorn.png', 'destination/unicorn.png', {overwrite: false}), | ||
); | ||
expectType<Promise<void>>( | ||
moveFile('source/unicorn.png', 'destination/unicorn.png', { | ||
directoryMode: 0o700 | ||
}) | ||
directoryMode: 0o700, | ||
}), | ||
); | ||
expectError( | ||
await moveFile('source/unicorn.png', 'destination/unicorn.png', { | ||
directoryMode: '700' | ||
}) | ||
directoryMode: '700', | ||
}), | ||
); | ||
expectType<void>( | ||
moveFile.sync('source/unicorn.png', 'destination/unicorn.png') | ||
moveFileSync('source/unicorn.png', 'destination/unicorn.png'), | ||
); | ||
expectType<void>( | ||
moveFile.sync('source/unicorn.png', 'destination/unicorn.png', { | ||
overwrite: false | ||
}) | ||
moveFileSync('source/unicorn.png', 'destination/unicorn.png', { | ||
overwrite: false, | ||
}), | ||
); | ||
expectType<void>( | ||
moveFile.sync('source/unicorn.png', 'destination/unicorn.png', { | ||
directoryMode: 0o700 | ||
}) | ||
moveFileSync('source/unicorn.png', 'destination/unicorn.png', { | ||
directoryMode: 0o700, | ||
}), | ||
); | ||
expectError( | ||
moveFile.sync('source/unicorn.png', 'destination/unicorn.png', { | ||
directoryMode: '700' | ||
}) | ||
moveFileSync('source/unicorn.png', 'destination/unicorn.png', { | ||
directoryMode: '700', | ||
}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.