Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Sep 24, 2015
0 parents commit 70165f6
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
node_modules
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
git:
depth: 1
branches:
except: /^v\d/
language: node_js
node_js: stable
after_script:
- npm install istanbul-coveralls
- npm run-script coverage
- node node_modules/.bin/istanbul-coveralls
notifications:
email: false
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# empty-file

[![NPM version](https://img.shields.io/npm/v/empty-file.svg)](https://www.npmjs.com/package/empty-file)
[![Build Status](https://travis-ci.org/shinnn/empty-file.svg?branch=master)](https://travis-ci.org/shinnn/empty-file)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/empty-file.svg)](https://coveralls.io/r/shinnn/empty-file)
[![Dependency Status](https://david-dm.org/shinnn/empty-file.svg)](https://david-dm.org/shinnn/empty-file)
[![devDependency Status](https://david-dm.org/shinnn/empty-file/dev-status.svg)](https://david-dm.org/shinnn/empty-file#info=devDependencies)

A [Node](https://nodejs.org/) module to write an empty file asynchronously

```javascript
const emptyFile = require('empty-file');
const fs = require('fs');

emptyFile('file/path').then(() => {
fs.readFileSync('file/path', 'utf8'); //=> ''
});
```

## Installation

[Use npm.](https://docs.npmjs.com/cli/install)

```
npm install empty-file
```

## API

```javascript
const emptyFile = require('empty-file');
```

### emptyFile(*filePath*[, *options*])

*filePath*: `String`
*options*: `Object` ([`fs.writeFile`](https://nodejs.org/api/fs.html#fs_fs_writesync_fd_data_position_encoding) options except for `encoding`)
Return: `Object` ([Promise](https://promisesaplus.com/) instance)

It writes `new Buffer(0)` to a file.

When it finish writing a file, it will be [fulfilled](https://promisesaplus.com/#point-26) with no arguments.

When it fails, it will be [rejected](https://promisesaplus.com/#point-30) with an error object.

```javascript
const emptyFile = require('empty-file');
const fs = require('fs');

function onFulfilled() {
fs.readFileSync('tmp', 'utf8'); //=> ''
fs.statSync('tmp').mode; //=> 33261
}

function onRejected(err) {
console.error(err.message);
}

emptyFile('tmp', {mode: 33261}).then(onFulfilled, onRejected);
```

## Related project

* [empty-file-callback](https://github.com/shinnn/empty-file-callback) ([callback](http://thenodeway.io/posts/understanding-error-first-callbacks/)-style version)

## License

[The Unlicense](./LICENSE)
21 changes: 21 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

var emptyFileCallback = require('empty-file-callback');
var PinkiePromise = require('pinkie-promise');

module.exports = function emptyFile(filePath, options) {
return new PinkiePromise(function promisify(resolve, reject) {
emptyFileCallback.validateOptions(options, module.exports.ENCODING_ERROR_MESSAGE);

emptyFileCallback(filePath, options, function callback(err, version) {
if (err) {
reject(err);
return;
}

resolve(version);
});
});
};

module.exports.ENCODING_ERROR_MESSAGE = emptyFileCallback.ENCODING_ERROR_MESSAGE.replace('-callback', '');
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "empty-file",
"version": "1.0.0",
"description": "Write an empty file asynchronously",
"repository": "shinnn/empty-file",
"author": "Shinnosuke Watanabe (https://github.com/shinnn)",
"scripts": {
"pretest": "eslint --config @shinnn/node-legacy index.js test/test.js",
"test": "node --strong_mode --harmony_destructuring --throw-deprecation --track-heap-objects test.js",
"coverage": "node --strong_mode --harmony_destructuring node_modules/.bin/istanbul cover test.js"
},
"license": "MIT",
"files": [
"index.js"
],
"keywords": [
"fs",
"file",
"write",
"blank",
"clean",
"empty",
"promise",
"promises",
"async",
"asynchronous",
"asynchronously"
],
"dependencies": {
"empty-file-callback": "^1.0.0",
"pinkie-promise": "^1.0.0"
},
"devDependencies": {
"@shinnn/eslint-config-node-legacy": "^1.0.0",
"eslint": "^1.7.3",
"graceful-fs": "^4.1.2",
"istanbul": "^0.4.0",
"pify": "^2.2.0",
"tape": "^4.2.2"
}
}
78 changes: 78 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strong';

const emptyFile = require('.');
const pify = require('pify');
const test = require('tape');

const {
readFile: readFilePromise,
stat: statPromise,
unlink: unlinkPromise
} = pify.all(require('graceful-fs'));

test('emptyFile()', t => {
t.plan(9);

t.equal(emptyFile.name, 'emptyFile', 'should have a function name.');

emptyFile('tmp0')
.then(() => readFilePromise('tmp0', 'utf8'))
.then(content => t.strictEqual(content, '', 'should write an empty file.'))
.then(() => unlinkPromise('tmp0'))
.catch(t.fail);

emptyFile('tmp1', {mode: 33261})
.then(() => statPromise('tmp1'))
.then(stats => t.strictEqual(stats.mode, 33261, 'should support fs.writeFile options.'))
.then(() => unlinkPromise('tmp1'))
.catch(t.fail);

emptyFile('node_modules', null)
.then(t.fail, err => t.ok(err, 'should fail when it cannot write a file.'))
.catch(t.fail);

emptyFile('__', 'utf8')
.then(t.fail, err => {
t.ok(
/TypeError.*Encoding string is not supported since empty-file writes an empty file\./.test(err),
'should not accept encoding string.'
);
})
.catch(t.fail);

emptyFile('__', 'utf8')
.then(t.fail, err => {
t.ok(
/TypeError.*Encoding string is not supported since empty-file writes an empty file\./.test(err),
'should not accept encoding string.'
);
})
.catch(t.fail);

emptyFile('___', {encoding: 'base64'})
.then(t.fail, err => {
t.ok(
/TypeError.*Encoding option is not supported since empty-file writes an empty file\./.test(err),
'should not accept encoding option.'
);
})
.catch(t.fail);

emptyFile(null)
.then(t.fail, err => {
t.ok(
/TypeError.*path/.test(err),
'should fail when the first argument is not a string.'
);
})
.catch(t.fail);

emptyFile('foo', 123)
.then(t.fail, err => {
t.ok(
/TypeError.*Expected options to be either an object or a string/.test(err),
'should fail when the second argument is neither an object nor a string.'
);
})
.catch(t.fail);
});

0 comments on commit 70165f6

Please sign in to comment.