mv-file
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

mv-file

version codecov release node.js license

English | 中文

A simple and flexible utility for moving files and directories with support for glob patterns, concurrent operations, and event handling.

Features

  • 🎯 Glob pattern support
  • 📂 Setting directory structure
  • 🚀 Concurrent file operations
  • 🎭 Listening to operation events
  • 🧹 Optional cleanup of empty directories

Installation

npm install mv-file

Usage

1. moveFile

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,
	}
)

2. createFileMover

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)
})

API

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>

PathMapping

path mapping table.

  • key: source path
  • value: target path
interface PathMapping {
	[source: string]: string
}

MoveOptions

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

FileMover Class

Methods

move(pathMap: PathMapping): Promise<void>

Moves files according to the provided path mapping.

Events

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

FileMoverError

Use a custom FileMoverError class for error handling:

class FileMoverError extends Error {
	code: string
	source?: string
	target?: string
	originalError?: Error
}

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Package Sidebar

Install

npm i mv-file

Weekly Downloads

2

Version

1.0.6

License

MIT

Unpacked Size

30.2 kB

Total Files

8

Last publish

Collaborators

  • mengqing723