node-system-bridge
Version:
A cross-platform system automation library for Node.js and Electron, written in Rust
120 lines (106 loc) • 4.3 kB
JavaScript
/**
* File Operations - Complete Example and Documentation
*
* Features demonstrated:
* - Copy file
* - Copy directory (recursive)
* - Move file
* - Move directory
* - Delete file
* - Delete directory (recursive)
*
* API Reference:
* - FileOps.copyPath(source, dest) - Copy file or directory
* - FileOps.movePath(source, dest) - Move file or directory
* - FileOps.deletePath(path) - Delete file or directory
*
* Parameters:
* - source: Source path (file or directory)
* - dest: Destination path
* - path: Path to delete (file or directory)
*
* Features:
* - Automatically detects whether path is file or directory
* - Directory operations automatically recursively process all subfiles and subdirectories
* - Move operations automatically use copy+delete strategy when crossing file systems
* - Provides clear error messages
*
* Notes:
* - Source path must exist
* - Parent directory of destination path will be created automatically
* - If destination path exists, move operation will delete destination first
* - Delete operation will recursively delete all contents in directory
*/
import { FileOps } from '../index.js'
import { writeFileSync, mkdirSync, existsSync, readFileSync } from 'fs'
import { join } from 'path'
import { tmpdir } from 'os'
async function demo() {
console.log('File Operations Demo\n')
// Create temporary directory for demo
const tempDir = join(tmpdir(), 'file-ops-demo')
if (!existsSync(tempDir)) {
mkdirSync(tempDir, { recursive: true })
}
const sourceFile = join(tempDir, 'source.txt')
const destFile = join(tempDir, 'dest.txt')
const movedFile = join(tempDir, 'moved.txt')
const sourceDir = join(tempDir, 'sourceDir')
const destDir = join(tempDir, 'destDir')
try {
// 1. Create test file
console.log('1. Creating test file...')
writeFileSync(sourceFile, 'This is source file content')
console.log(` Created: ${sourceFile}`)
// 2. Copy file
console.log('\n2. Copying file...')
FileOps.copyPath(sourceFile, destFile)
console.log(` Copied: ${sourceFile} -> ${destFile}`)
console.log(` Destination file content: ${readFileSync(destFile, 'utf-8')}`)
// 3. Move file
console.log('\n3. Moving file...')
FileOps.movePath(destFile, movedFile)
console.log(` Moved: ${destFile} -> ${movedFile}`)
console.log(` Source file exists: ${existsSync(destFile)}`)
console.log(` Destination file exists: ${existsSync(movedFile)}`)
// 4. Create test directory
console.log('\n4. Creating test directory...')
mkdirSync(sourceDir, { recursive: true })
writeFileSync(join(sourceDir, 'file1.txt'), 'File 1')
writeFileSync(join(sourceDir, 'file2.txt'), 'File 2')
mkdirSync(join(sourceDir, 'subdir'), { recursive: true })
writeFileSync(join(sourceDir, 'subdir', 'file3.txt'), 'File 3')
console.log(` Created directory: ${sourceDir}`)
// 5. Copy directory
console.log('\n5. Copying directory...')
FileOps.copyPath(sourceDir, destDir)
console.log(` Copied: ${sourceDir} -> ${destDir}`)
console.log(` Files in destination directory:`)
const files = [join(destDir, 'file1.txt'), join(destDir, 'file2.txt'), join(destDir, 'subdir', 'file3.txt')]
files.forEach(file => {
if (existsSync(file)) {
console.log(` - ${file}: ${readFileSync(file, 'utf-8')}`)
}
})
// 6. Delete file
console.log('\n6. Deleting file...')
FileOps.deletePath(movedFile)
console.log(` Deleted: ${movedFile}`)
console.log(` File exists: ${existsSync(movedFile)}`)
// 7. Delete directory
console.log('\n7. Deleting directory...')
FileOps.deletePath(sourceDir)
console.log(` Deleted: ${sourceDir}`)
console.log(` Directory exists: ${existsSync(sourceDir)}`)
console.log('\n✅ All operations completed!')
console.log(`\nTip: Temporary files are located at ${tempDir}`)
console.log('You can manually delete this directory to clean up test files')
} catch (error) {
console.error('❌ Error:', error.message)
throw error
}
}
demo().catch(err => {
console.error('Error:', err.message)
process.exit(1)
})