├── .travis.yml ├── .jshintrc ├── .gitignore ├── package.json ├── index.js ├── README.md ├── LICENSE └── test └── index.js /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.8" 4 | - "0.10" -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "asi": true, 3 | "laxbreak": true, 4 | "node": true, 5 | "strict": true 6 | } 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | 16 | node_modules 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "then-fs", 3 | "version": "2.0.0", 4 | "description": "promised FS", 5 | "main": "index.js", 6 | "dependencies": { 7 | "promise": ">=3.2 <8" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/then/fs.git" 12 | }, 13 | "keywords": [ 14 | "promise", 15 | "then", 16 | "fs", 17 | "core" 18 | ], 19 | "author": "Nathan Zadoks", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/then/fs/issues" 23 | }, 24 | "devDependencies": { 25 | "bash-color": "0.0.3" 26 | }, 27 | "scripts": { 28 | "test": "node test/index.js" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var fs = Object.create(require('fs')) 3 | var Promise = require('promise') 4 | 5 | module.exports = exports = fs 6 | for (var key in fs) 7 | if (!(typeof fs[key] != 'function' 8 | || key.match(/Sync$/) 9 | || key.match(/^[A-Z]/) 10 | || key.match(/^create/) 11 | || key.match(/^(un)?watch/) 12 | )) 13 | add(key) 14 | 15 | function add(key) { 16 | var original = fs[key] 17 | if (key !== 'exists') 18 | fs[key] = Promise.denodeify(original) 19 | else 20 | fs[key] = function() { 21 | var args = [].slice.call(arguments) 22 | return new Promise(function(resolve, reject) { 23 | args.push(resolve) 24 | original.apply(null, args) 25 | }) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # then-fs 3 | 4 | Promised version of the node.js `fs` module. 5 | All non-callback-taking functions of fs are simply inherited. 6 | 7 | [![Build Status](https://img.shields.io/travis/then/fs/master.svg)](https://travis-ci.org/then/fs) 8 | [![Dependency Status](https://img.shields.io/david/then/fs.svg)](https://david-dm.org/then/fs) 9 | [![NPM version](https://badge.fury.io/js/then-fs.png)](http://badge.fury.io/js/then-fs) 10 | 11 | ## Installation 12 | 13 | $ npm install then-fs 14 | 15 | ## API 16 | 17 | The API for `then-fs` is exactly that of the built in `fs` module except that any function which normally takes a callback returns a promise instead. 18 | 19 | Example: 20 | 21 | ```js 22 | function readJSON(path) { 23 | return fs.readFile(path, 'utf8').then(JSON.parse) 24 | } 25 | readJSON(__dirname + '/package.json') 26 | .done(function (obj) { 27 | console.dir(oj) 28 | }) 29 | ``` 30 | 31 | ## License 32 | 33 | MIT 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 then (Promises/A+ implementations) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert') 2 | var color = require('bash-color') 3 | var Promise = require('promise') 4 | var fs = require('../') 5 | 6 | var failed = false 7 | var ready = Promise.resolve(null) 8 | function action(fn, message) { 9 | ready = ready.then(function () { 10 | return fn() 11 | .then(function () { 12 | console.info(color.green('V ') + message) 13 | }, function (ex) { 14 | console.error(color.red('X ') + message) 15 | console.error(ex.stack || ex.message || ex) 16 | failed = true 17 | }) 18 | }) 19 | } 20 | 21 | action(fs.mkdir.bind(null, __dirname + '/fixture'), 'mkdir') 22 | action(function () { 23 | return fs.stat(__dirname + '/fixture') 24 | .then(function (stat) { 25 | assert(stat.isDirectory()) 26 | }) 27 | }, 'stat directory') 28 | 29 | action(fs.writeFile.bind(null, __dirname + '/fixture/file.txt', 'hello world'), 'writeFile') 30 | action(function () { 31 | return fs.readFile(__dirname + '/fixture/file.txt', 'utf8') 32 | .then(function (txt) { 33 | assert(txt === 'hello world') 34 | }) 35 | }, 'readFile') 36 | action(function () { 37 | return fs.readdir(__dirname + '/fixture') 38 | .then(function (files) { 39 | assert(Array.isArray(files)) 40 | assert(files.length === 1) 41 | assert(files[0] === 'file.txt') 42 | }) 43 | }, 'readdir') 44 | action(fs.unlink.bind(null, __dirname + '/fixture/file.txt'), 'unlink') 45 | 46 | action(fs.rmdir.bind(null, __dirname + '/fixture'), 'rmdir') 47 | action(function () { 48 | return fs.stat(__dirname + '/fixture') 49 | .then(function (stat) { 50 | throw new Error('directory still exists after rmdir') 51 | }, function () {}) 52 | }, 'stat directory') 53 | 54 | ready.done(function () { 55 | if (failed) process.exit(1) 56 | }) --------------------------------------------------------------------------------