disk-action
v0.2.1
Published
Implement standard disk actions
Downloads
2
Maintainers
Readme
disk-action
Easy to use module in order to create / delete / modify files and directory. All unexistent directories will be create 'on-the-fly', and delete works for either file or directory. ** BE CAREFUL ** when specifying directory: all content will be erased !!
Important: This package is NOT backward compatible with 0.1.x versions... Be careful if you update your npm project...
Install
Install with npm:
npm install disk-action
Basic Usage
Require the module:
Disk = require 'disk-action'
Initialize with encoding (default is 'utf-8'):
disk = new Disk('utf-8')
Read a file:
disk.read
filename: 'directory/not/created/hello.txt'
cb: (data) ->
console.log data
Methods available:
- read() read a file
- touch() create empty file
- write() similar to create() for backward compatibility
- create() create a file or directory
- append() append content to a file
- copy() copy a file/directory
- move() move a file directory
- replace() replace string in file
- delete() delete file/directory
Methods usage:
Read a file:
disk.read
filename: 'directory/not/created/hello.txt'
cb: (data) ->
console.log data
Create file:
disk.create
filename: 'directory/not/created/hello.txt'
content: 'I like coffee'
cb: () ->
console.log 'File created'
Create file with content:
disk.create
filename: 'directory/not/created/hello.txt'
content: 'I like coffee'
Create directory:
disk.create
dirname: 'another/directory'
Append to file:
disk.append
filename: 'directory/not/created/hello.txt'
content: 'But JS is not my best friend... ;)'
Copy files:
disk.copy
source: 'directory/not/created/hello.txt'
destination: 'another/directory/hello2.txt'
Move files:
disk.move
source: 'directory/not/created/hello.txt'
destination: 'another/directory/hello2.txt'
Replace content in file:
disk.replace
filename: 'directory/not/created/hello.txt'
to_replace: 'coffee'
replace_with: 'CoffeeScript'
Delete file or directory:
disk.delete
filename: 'directory/not/created/hello.txt'
Extended usage
All methods supports an optional callback parameter:
disk.write
filename: 'directory/not/created/hello.txt'
content: 'I like coffee'
cb: do_something()
or:
disk.read
filename: 'directory/not/created/hello.txt'
cb: (data) ->
console.log data
Run tests
You can run unit-tests using mocha with:
npm test