node package manager


File / Directory Classes


  • File Class
  • Directory Class
  • File read/write Middleware
  • Sync + Async

In comparison to NodeJS sync-async contract: all functions with generic name are synchronous, and the **Async are asynchronous with the same interface and return deferred object. Sync versions never throw exceptions and are designed to be used in not performance critical applications, like bash scripts, etc.

This library is included into Atma.Toolkit, so creating custom scripts, you can use this API.

var file = new io.File('test.txt');

Path is always relative to the cwd (except windows os, when drive letter is used). To specify system absolute path, use file:// protocol.

Read file's content. If encoding is set to null raw Buffer is returned. For each read middleware pipeline is used, to skip it, set skipHooks to true.

var content = <?Object> {
    encoding: String | null, //> 'utf8' 
    skipHooks: Boolean //> false 
    .readAsync( <?Object> {
        encoding: String | null, //> 'utf8' 
        skipHooks: Boolean //> false 
    .done(function(content, file))
file.write(String | Buffer, <?Object>{
    skipHooks: Boolean
    .writeAsync(String | Buffer, <?Object>{
        skipHooks: Boolean
file.exists() //> Boolean; 
file.copyTo(<String> path) //> Boolean; 
file.copyToAsync(<String> path) //> Deferred; 
file.rename(<String> filename)
file.renameAsync(<String> filename) //> Deferred 
##### replace
Reads the content as string, replaces the matches and writes the result.
`@arguments`: same as for JavaScripts String `replace`.
`@return`: new content
var str = file.replace('foo', 'bar');
    .replaceAsync('foo', 'bar')
    .done(function(newContent) {})
    .fail(function(error) {});
##### remove
file.removeAsync() //> Deferred

Watch file for changes

file.unwatch(callback) //> Boolean; 

Each read will be cached. To control cache behaviour use next methods:

io.File.clearCache(<?String> path);

When path is null, then all cache is dropped.


There are some static methods, so that there is no need to initialize the File instance.

io.File[method] //> Function(filepath, [..args]) 
// methods: 
// sample 

Middleware pattern is used for all reads and writes. It can be used, for example, to compile coffee script to javascript on the fly. Or when reading *.yml file, the resulted content is not a YAML string, but already parsed object.

To get the idea, look at the hook definition sample:


Each middleware has unique name and is registerd in this way:

io.File.middleware['coffee'] = {
    read: function(<io.File> file, <Object> config){
        var coffee = require('coffee-script');
        file.content = coffee.compile(file.content);
    write: function(<io.File> file, <Object> config){
        // ... do smth with `content` before disk write 
        regexp: <RegExp>,
        method: <'read'|'write'>,
        handler: <Function | Object> handler,
        zIndex: <?Number> // default: 0 

Path is matched by the regexp. The greater zIndex ist the later it is called in a pipeline, otherwise the handlers are called in the order they were registerd.

Lately will be converted into plugins, @see Plugins

  • read

    • coffee ( -> javascript )
    • markdown ( -> html )
    • jshint ( -> run jshint )
    • json ( -> JSON.parse is used )
    • yml ( -> YAML parser is used )
  • write

    • uglify ( -> Minify source before write)
    • cssmin ( -> Minify source before write)
    • yml ( -> Stringify object to yml string )
    • json ( -> Stringify object to json )

There additional read/write middlewares as atma plugins:

  • atma-loader-traceur - Traceur
  • atma-loader-less - Less

For example, you want to use Traceur middelware and jshint for reading js files: via javascript

    js: ['hint:read', 'atma-loader-traceur:read' /* ... */],

via package.json

    "settings" : {
        "io": {
            "extensions": {
                "js": [ "hint:read", "atma-loader-traceur:read" ]

Define with RegExp a File Handler to completely override the read/write/exists/remove behaviour.

    .registerHandler(/defaults\.json$/i, Class({
        exists: function(){
            return true;
        read: function(){
            return { foo: 'bar' };
var dir = new io.Directory('src/');

Path is always relative to the cwd (except windows os, when drive letter is used). To specify system absolute path, use file:// protocol.

dir.exists()//> Boolean 
dir.existsAsync()//> Deferred 
dir.readFiles(<?String> pattern).files // Array<io.Files> 

Get list of all files in the directory. pattern is a glob pattern.

// all javascript files, also from sub-directories 
pattern = '*.js';
// only from base directory 
pattern = '/*.js'
// only from sub-directories 
pattern = '**/*.js'
    .readFilesAsync(<?String> pattern)

Copy files to destination directory. Before copying dir.readFiles can be called to copy only specific files.

dir.copyTo(<String> destination)
dir.copyToAsync(<String> destination) //> Deferred 
dir.rename(<String> folderName);
dir.renameAsync(<String> folderName) //> Deferred 

Removes all content recursively and the folder itself

dir.remove() //> Boolean 

Creates directory structure, if not already exists.


Watch directory for changes


There are some static methods, so that there is no need to initialize the Directory instance.

io.Directory[method] //> Function(dirpath, [..args]) 
// methods: 
// sample 
    .readFilesAsync('sub/', '**.js')

(c) MIT - Atma.js Project