A simple and flexible utility for moving files and directories with support for glob
patterns, concurrent operations, and event handling.
- 🎯 Glob pattern support
- 📂 Setting directory structure
- 🚀 Concurrent file operations
- 🎭 Listening to operation events
- 🧹 Optional cleanup of empty directories
npm install mv-file
import { moveFile } from 'mv-file'
// Move files
await moveFile(
{
'src/html/': 'dist'
'src/file.txt': 'dist/file.txt',
'src/**/*.js': 'dist/js',
},
{
force: true,
clean: true,
}
)
import { createFileMover } from 'mv-file'
// // Create an operator instance
const mover = createFileMover({
force: true,
clean: true,
base: 'src',
dest: 'dist',
})
// Move files
await mover.move({
'src/js/**/*.js': 'dist/',
})
// => dist/js/**/*.js
// Listen to events
operator.on('copy:start', (source, target) => {
console.log(`Starting copy: ${source} -> ${target}`)
})
operator.on('copy:done', (source, target) => {
console.log(`Completed copy: ${source} -> ${target}`)
})
operator.on('error', (error) => {
console.error('Operation failed:', error.message)
})
import { moveFile, createFileMover } from 'mv-file'
moveFile
// Moves files
moveFile(pathMap: PathMapping, options?: MoveOptions): Promise<void>
createFileMover
// Create operator instance
const mover = createFileMover(options?: MoveOptions): FileMover
// Moves files
mover.move(pathMap: PathMapping): Promise<void>
path mapping table.
-
key
: source path -
value
: target path
interface PathMapping {
[source: string]: string
}
Property | Type | Default | Description |
---|---|---|---|
cwd |
string |
process.cwd() |
Current working directory |
base |
string |
Source base directory | |
dest |
string |
Target base directory | |
force |
boolean |
false |
Whether to force overwrite existing files |
clean |
boolean |
false |
Whether to clean empty directories after move |
verbose |
boolean |
false |
Enable verbose logging |
concurrency |
number |
4 |
Maximum number of concurrent operations |
Moves files according to the provided path mapping.
The FileOperator extends EventEmitter and provides the following events:
Event | Parameters | Description |
---|---|---|
copy:start |
(source: string, target: string) |
Emitted when a copy operation starts |
copy:done |
(source: string, target: string) |
Emitted when a copy operation completes |
clean:start |
(path: string) |
Emitted when cleanup starts |
clean:done |
(path: string) |
Emitted when cleanup completes |
error |
(error: FileMoverError) |
Emitted when an error occurs |
Use a custom FileMoverError
class for error handling:
class FileMoverError extends Error {
code: string
source?: string
target?: string
originalError?: Error
}
MIT
Contributions are welcome! Please feel free to submit a Pull Request.