├── .gitignore ├── .jshintrc ├── .travis.yml ├── LICENSE ├── README.md ├── index.d.ts ├── index.js ├── package.json └── test ├── basic.js ├── mocha.opts ├── mz.js └── register.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.swp 3 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | {"node":true} 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | node_js: 4 | - "4" 5 | - "5" 6 | - "6" 7 | - "7" 8 | env: 9 | - ANY_PROMISE= 10 | - ANY_PROMISE=es6-promise 11 | - ANY_PROMISE=bluebird 12 | - ANY_PROMISE=rsvp 13 | - ANY_PROMISE=when 14 | - ANY_PROMISE=q 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2013 Kevin Beaty 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fs-promise 2 | 3 | `fs-promise` is now *deprecated*. Use [`mz/fs`][1] [`fs-extra^3.0`][2]. 4 | 5 | [1]: https://github.com/normalize/mz 6 | [2]: https://www.npmjs.org/package/fs-extra 7 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | // Type definitions for fs-promise 1.0 2 | // Project: https://github.com/kevinbeaty/fs-promise#readme 3 | // Definitions by: Thiago de Arruda 4 | // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped 5 | 6 | /// 7 | 8 | export * from "mz/fs"; 9 | export * from "fs-extra"; 10 | 11 | export interface WriteOptions { 12 | encoding?: string; 13 | mode?: number; 14 | flag?: string; 15 | } 16 | 17 | type JsonReplacerArray = Array; 18 | 19 | type JsonReplacerFunction = (key: string, value: any) => any; 20 | 21 | type JsonReplacer = JsonReplacerArray | JsonReplacerFunction; 22 | 23 | export interface WriteJsonOptions extends WriteOptions { 24 | spaces?: number; 25 | replacer?: JsonReplacer; 26 | } 27 | 28 | export interface ReadJsonOptions { 29 | encoding: string; 30 | flag?: string; 31 | reviver: (key: any, value: any) => any; 32 | } 33 | 34 | export function copy(src: string, dst: string): Promise; 35 | export function emptyDir(dir: string): Promise; 36 | export function ensureFile(file: string): Promise; 37 | export function ensureDir(dir: string): Promise; 38 | export function ensureLink(srcpath: string, dstpath: string): Promise; 39 | export function ensureSymlink(srcpath: string, dstpath: string, type?: "dir" | "file" | "junction"): Promise; 40 | export function mkdirs(dir: string): Promise; 41 | export function move(src: string, dst: string): Promise; 42 | export function outputFile(file: string, data: string | Buffer, options?: WriteOptions): Promise; 43 | export function outputJson(file: string, data: any, options?: WriteJsonOptions): Promise; 44 | export function readJson(file: string, options?: ReadJsonOptions): Promise; 45 | export function remove(path: string): Promise; 46 | export function writeJson(file: string, data: any, options?: WriteJsonOptions): Promise; 47 | 48 | // aliases 49 | export { 50 | emptyDir as emptydir, 51 | ensureFile as createFile, 52 | ensureLink as createLink, 53 | ensureSymlink as createSymlink, 54 | mkdirs as mkdirp, 55 | outputJson as outputJSON, 56 | readJson as readJSON, 57 | writeJson as writeJSON 58 | }; 59 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var mzfs = require('mz/fs') 4 | var fsExtra = require('fs-extra') 5 | var Promise = require('any-promise') 6 | var thenifyAll = require('thenify-all') 7 | var slice = Array.prototype.slice 8 | 9 | // thenify-all for all fs-extra that make sense to be promises 10 | var fsExtraKeys = [ 11 | 'copy', 12 | 'emptyDir', 13 | 'ensureFile', 14 | 'ensureDir', 15 | 'ensureLink', 16 | 'ensureSymlink', 17 | 'mkdirs', 18 | 'move', 19 | 'outputFile', 20 | 'outputJson', 21 | 'readJson', 22 | 'remove', 23 | 'writeJson', 24 | // aliases 25 | 'createFile', 26 | 'createLink', 27 | 'createSymlink', 28 | 'emptydir', 29 | 'mkdirp', 30 | 'readJSON', 31 | 'outputJSON', 32 | 'writeJSON' 33 | ] 34 | thenifyAll.withCallback(fsExtra, exports, fsExtraKeys) 35 | 36 | // Delegate all normal fs to mz/fs 37 | // (this overwrites anything proxies directly above) 38 | var mzKeys = [ 39 | 'rename', 40 | 'ftruncate', 41 | 'chown', 42 | 'fchown', 43 | 'lchown', 44 | 'chmod', 45 | 'fchmod', 46 | 'stat', 47 | 'lstat', 48 | 'fstat', 49 | 'link', 50 | 'symlink', 51 | 'readlink', 52 | 'realpath', 53 | 'unlink', 54 | 'rmdir', 55 | 'mkdir', 56 | 'mkdtemp', 57 | 'readdir', 58 | 'close', 59 | 'open', 60 | 'utimes', 61 | 'futimes', 62 | 'fsync', 63 | 'fdatasync', 64 | 'write', 65 | 'read', 66 | 'readFile', 67 | 'writeFile', 68 | 'appendFile', 69 | 'access', 70 | 'truncate', 71 | 'exists' 72 | ] 73 | 74 | mzKeys.forEach(function(key){ 75 | exports[key] = mzfs[key] 76 | }) 77 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fs-promise", 3 | "version": "2.0.3", 4 | "description": "[DEPRECATED] Use mz or fs-extra^3.0 with Promise Support", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/kevinbeaty/fs-promise.git" 12 | }, 13 | "keywords": [ 14 | "promise", 15 | "fs", 16 | "file", 17 | "file system" 18 | ], 19 | "author": "Kevin Beaty", 20 | "license": "MIT", 21 | "dependencies": { 22 | "any-promise": "^1.3.0", 23 | "fs-extra": "^2.0.0", 24 | "mz": "^2.6.0", 25 | "thenify-all": "^1.6.0" 26 | }, 27 | "devDependencies": { 28 | "@types/fs-extra": "0.0.37", 29 | "@types/mz": "0.0.30", 30 | "mocha": "^3.0.0", 31 | "es6-promise": "^4.0.0", 32 | "rsvp": "^3.0.0", 33 | "bluebird": "^3.0.0", 34 | "when": "^3.0.0", 35 | "q": "^1.0.0" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /test/basic.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*globals describe, it, beforeEach, afterEach */ 3 | var fsp = require('..'), 4 | path = require('path'), 5 | assert = require('assert'), 6 | Prom = require('any-promise'), 7 | testdir = path.join(__dirname, 'tmp'), 8 | testdir2 = path.join(__dirname, 'tmp2') 9 | 10 | 11 | describe('basic', function(){ 12 | beforeEach(function(){ 13 | return fsp.mkdir(testdir).then(existstmp(true)); 14 | }); 15 | 16 | afterEach(function(){ 17 | return fsp.remove(testdir).then(existstmp(false)); 18 | }); 19 | 20 | it('should create files and readdir', function(){ 21 | return fsp.ensureFile(file('hello')).then(readtmp).then(function(files){ 22 | assert.deepEqual(files.sort(), ['hello']); 23 | return fsp.ensureFile(file('world')); 24 | }).then(readtmp).then(function(files){ 25 | assert.deepEqual(files.sort(), ['hello', 'world']); 26 | return fsp.exists(testdir2) 27 | }).then(function(exists){ 28 | assert.equal(exists, false); 29 | return fsp.move(testdir, testdir2) 30 | }).then(function(){ 31 | return Prom.all([fsp.exists(testdir), fsp.exists(testdir2)]) 32 | }).then(function(exists){ 33 | return assert.deepEqual(exists, [false, true]) 34 | }).then(function(){ 35 | return fsp.copy(testdir2, testdir) 36 | }).then(function(){ 37 | return Prom.all([fsp.exists(testdir), fsp.exists(testdir2)]) 38 | }).then(function(exists){ 39 | return assert.deepEqual(exists, [true, true]) 40 | }).then(readtmps).then(function(files){ 41 | assert.deepEqual(files[0].sort(), files[1].sort()); 42 | return fsp.emptyDir(testdir2); 43 | }).then(readtmp2).then(function(files){ 44 | assert.deepEqual(files, []); 45 | }).then(function(){ 46 | fsp.remove(testdir2); 47 | }) 48 | }); 49 | 50 | it('should pass through Sync as value', function(){ 51 | return fsp.ensureFile(file('hello')).then(function(files){ 52 | assert(fsp.existsSync(file('hello'))); 53 | assert(!fsp.existsSync(file('world'))); 54 | return fsp.ensureFile(file('world')); 55 | }).then(readtmp).then(function(files){ 56 | assert(fsp.existsSync(file('hello'))); 57 | assert(fsp.existsSync(file('world'))); 58 | }); 59 | }); 60 | 61 | it('should copy with pipe read/write stream', function(){ 62 | return fsp.writeFile(file('hello1'), 'hello world').then(function(){ 63 | return fsp.readFile(file('hello1'), {encoding:'utf8'}); 64 | }).then(function(contents){ 65 | assert.equal(contents, 'hello world'); 66 | var read = fsp.createReadStream(file('hello1')), 67 | write = fsp.createWriteStream(file('hello2')), 68 | promise = new Prom(function(resolve, reject){ 69 | read.on('end', resolve); 70 | write.on('error', reject); 71 | read.on('error', reject); 72 | }); 73 | read.pipe(write); 74 | return promise; 75 | }).then(function(){ 76 | return fsp.readFile(file('hello2'), {encoding:'utf8'}); 77 | }).then(function(contents){ 78 | assert.equal(contents, 'hello world'); 79 | }); 80 | }); 81 | 82 | it('should pass third argument from write #7', function testWriteFsp() { 83 | return fsp.open(file('some.txt'), 'w+').then(function (fd){ 84 | return fsp.write(fd, "hello fs-promise").then(function(result) { 85 | var written = result[0]; 86 | var text = result[1]; 87 | assert.equal(text.substring(0, written), "hello fs-promise".substring(0, written)) 88 | return fsp.close(fd); 89 | }) 90 | }) 91 | }); 92 | 93 | }); 94 | 95 | function file(){ 96 | var args = [].slice.call(arguments); 97 | args.unshift(testdir); 98 | return path.join.apply(path, args); 99 | } 100 | 101 | function existstmp(shouldExist){ 102 | return function(){ 103 | return fsp.exists(testdir).then(function(exists){ 104 | assert.equal(exists, shouldExist); 105 | }); 106 | }; 107 | } 108 | 109 | function readtmp(){ 110 | return fsp.readdir(testdir); 111 | } 112 | 113 | function readtmp2(){ 114 | return fsp.readdir(testdir2); 115 | } 116 | 117 | function readtmps(){ 118 | return Prom.all([readtmp(), readtmp2()]); 119 | } 120 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --no-colors 2 | --ui bdd 3 | --reporter spec 4 | --require test/register 5 | test/basic.js 6 | test/mz.js 7 | -------------------------------------------------------------------------------- /test/mz.js: -------------------------------------------------------------------------------- 1 | // Tests from mz. License MIT 2 | // https://github.com/normalize/mz 3 | var assert = require('assert') 4 | 5 | describe('fs', function () { 6 | var fs = require('../') 7 | 8 | it('.stat()', function (done) { 9 | fs.stat(__filename).then(function (stats) { 10 | assert.equal(typeof stats.size, 'number') 11 | done() 12 | }).catch(done) 13 | }) 14 | 15 | it('.mkdtemp()', function (done) { 16 | if (!require('fs').mkdtemp) this.skip() 17 | fs.mkdtemp('/tmp/').then(function (folder) { 18 | fs.rmdirSync(folder) 19 | done() 20 | }).catch(done) 21 | }) 22 | 23 | it('.statSync()', function () { 24 | var stats = fs.statSync(__filename) 25 | assert.equal(typeof stats.size, 'number') 26 | }) 27 | 28 | it('.exists()', function (done) { 29 | fs.exists(__filename).then(function (exists) { 30 | assert(exists) 31 | done() 32 | }).catch(done) 33 | }) 34 | 35 | it('.existsSync()', function () { 36 | var exists = fs.existsSync(__filename) 37 | assert(exists) 38 | }) 39 | 40 | describe('callback support', function () { 41 | it('.stat()', function (done) { 42 | fs.stat(__filename, function (err, stats) { 43 | assert(!err) 44 | assert.equal(typeof stats.size, 'number') 45 | done() 46 | }) 47 | }) 48 | 49 | it('.exists()', function (done) { 50 | fs.exists(__filename, function (err, exists) { 51 | assert(!err) 52 | assert(exists) 53 | done() 54 | }) 55 | }) 56 | }) 57 | }) 58 | -------------------------------------------------------------------------------- /test/register.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var expectedImpl 4 | if(process.env.ANY_PROMISE){ 5 | // should load registration regardless 6 | expectedImpl = process.env.ANY_PROMISE 7 | require('any-promise/register')(expectedImpl) 8 | } else { 9 | expectedImpl = 'global.Promise' 10 | } 11 | 12 | var impl = require('any-promise/implementation') 13 | 14 | if(impl !== expectedImpl){ 15 | throw new Error('Expecting '+expectedImpl+' got '+impl) 16 | } 17 | --------------------------------------------------------------------------------