Skip to content

Commit

Permalink
refactor(ES2015): rewrite in ES2015 + improved build process
Browse files Browse the repository at this point in the history
- rewrite in ES2015
- only build for LTS versions of node
- updated semantic-release no longer needing after_all script
- set package version to 0.0.0-development to avoid local build issues
- ignore building new greenkeeper branches

BREAKING CHANGE: this is re-write, though the API has not changed, check the README for install instructions
  • Loading branch information
Ahmad Nassri committed Nov 19, 2016
1 parent 5d9007e commit df068e7
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 100 deletions.
28 changes: 28 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"env": {
"test": {
"presets": [ ["env", { "targets": { "node": "current" } }] ],
"plugins": []
},

"browsers": {
"presets": [ ["env", { "targets": { "browsers": ["last 2 versions"] } }] ],
"plugins": ["babel-plugin-add-module-exports"]
},

"v4": {
"presets": [ ["env", { "targets": { "node": 4.0 } }] ],
"plugins": ["babel-plugin-add-module-exports"]
},

"v6": {
"presets": [ ["env", { "targets": { "node": 6.0 } }] ],
"plugins": ["babel-plugin-add-module-exports"]
},

"v7": {
"presets": [ ["env", { "targets": { "node": 7.0 } }] ],
"plugins": ["babel-plugin-add-module-exports"]
}
}
}
17 changes: 17 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
engines:
fixme:
enabled: true

duplication:
enabled: true
config:
languages:
- javascript

eslint:
enabled: true

ratings:
paths:
- src/**
- test/**
1 change: 0 additions & 1 deletion .gitattributes

This file was deleted.

5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.log
node_modules
coverage
/lib
/node_modules
/.nyc_output
4 changes: 0 additions & 4 deletions .jshintrc

This file was deleted.

4 changes: 0 additions & 4 deletions .npmignore

This file was deleted.

25 changes: 22 additions & 3 deletions .travis.yml
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
sudo: false

language: node_js

node_js:
- 4
- 6
- 7

env:
- BABEL_ENV=test

matrix:
fast_finish: true

cache:
directories:
- node_modules

after_script:
before_script:
- npm prune

after_success:
- npm run coverage
- npm run codeclimate
- BABEL_ENV=v4 npm run compile -- -d lib
- BABEL_ENV=v6 npm run compile -- -d lib/node6
- BABEL_ENV=v7 npm run compile -- -d lib/node7
- BABEL_ENV=browsers npm run compile -- -d lib/browsers
- npm run semantic-release

branches:
except:
- /^v\d+\.\d+\.\d+$/
- /^greenkeeper\/.+/
45 changes: 35 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,44 @@
[![Downloads][npm-downloads]][npm-url]
[![Code Climate][codeclimate-quality]][codeclimate-url]
[![Coverage Status][codeclimate-coverage]][codeclimate-url]
[![Dependency Status][dependencyci-image]][dependencyci-url]
[![Dependencies][david-image]][david-url]

## Install

```sh
npm install --save fs-writefile-promise
```bash
npm install --only=production --save fs-writefile-promise
```

## API
## Usage

I recommend using an optimized build matching your Node.js environment version, otherwise, the standard `require` would work just fine with any version of Node `>= v4.0` .

```js
/*
* Node 7
*/
const write = require('fs-writefile-promise/lib/node7')

/*
* Node 6
*/
const write = require('fs-writefile-promise/lib/node6')

/*
* Node 4 (Default)
* Note: additional ES2015 polyfills may be required
*/
var write = require('fs-writefile-promise')
```

## API

### write(filename, data [, options])

*filename*: `String`
*data* `String` or `Buffer`
*options*: `Object`
*filename*: `String`
*data* `String` or `Buffer`
*options*: `Object`
Return: `Object` ([Promise])

When it finishes, it will be [*fulfilled*](http://promisesaplus.com/#point-26) with the file name that was written to.
Expand All @@ -42,17 +61,20 @@ write('/tmp/foo', 'bar')
.catch(function (err) {
console.error(err)
})
})
```

#### options

The option object will be directly passed to [fs.writefile](https://nodejs.org/api/fs.html#fs_fs_writefile_filename_data_options_callback).

## License

[ISC License](LICENSE) © [Ahmad Nassri](https://www.ahmadnassri.com/)
----
> :copyright: [ahmadnassri.com](https://www.ahmadnassri.com/)  · 
> License: [ISC][license-url]  · 
> Github: [@ahmadnassri](https://github.com/ahmadnassri)  · 
> Twitter: [@ahmadnassri](https://twitter.com/ahmadnassri)
[license-url]: https://github.com/ahmadnassri/fs-writefile-promise/blob/master/LICENSE
[license-url]: http://choosealicense.com/licenses/isc/

[travis-url]: https://travis-ci.org/ahmadnassri/fs-writefile-promise
[travis-image]: https://img.shields.io/travis/ahmadnassri/fs-writefile-promise.svg?style=flat-square
Expand All @@ -69,5 +91,8 @@ The option object will be directly passed to [fs.writefile](https://nodejs.org/a
[david-url]: https://david-dm.org/ahmadnassri/fs-writefile-promise
[david-image]: https://img.shields.io/david/ahmadnassri/fs-writefile-promise.svg?style=flat-square

[dependencyci-url]: https://dependencyci.com/github/ahmadnassri/fs-writefile-promise
[dependencyci-image]: https://dependencyci.com/github/ahmadnassri/fs-writefile-promise/badge?style=flat-square

[fs.writefile]: https://nodejs.org/api/fs.html#fs_fs_writefile_filename_data_options_callback
[Promise]: http://promisesaplus.com/
15 changes: 0 additions & 15 deletions lib/index.js

This file was deleted.

58 changes: 38 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,48 +1,66 @@
{
"version": "1.0.3",
"version": "0.0.0-development",
"name": "fs-writefile-promise",
"description": "Promise version of fs.writefile",
"author": "Ahmad Nassri <[email protected]> (https://www.ahmadnassri.com/)",
"homepage": "https://github.com/ahmadnassri/fs-writefile-promise",
"repository": "ahmadnassri/fs-writefile-promise",
"repository": {
"type": "git",
"url": "https://github.com/ahmadnassri/fs-writefile-promise.git"
},
"license": "ISC",
"main": "lib/index",
"main": "lib/index.js",
"keywords": [
"fs-writefile",
"promise"
],
"engines": {
"node": ">= 4"
"node": ">=4"
},
"files": [
"lib"
"lib",
"src"
],
"bugs": {
"url": "https://github.com/ahmadnassri/fs-writefile-promise/issues"
},
"scripts": {
"pretest": "standard && echint --verbose",
"test": "mocha",
"posttest": "npm run coverage",
"coverage": "istanbul cover --dir coverage _mocha -- -R dot",
"codeclimate": "codeclimate-test-reporter < coverage/lcov.info"
"compile": "babel -q src",
"test": "BABEL_ENV=test tap test/*.js --reporter spec --node-arg=--require --node-arg=babel-register",
"pretest": "snazzy && echint",
"coverage": "BABEL_ENV=test tap test/*.js --coverage --nyc-arg=--require --nyc-arg=babel-register",
"codeclimate": "nyc report --reporter=text-lcov | codeclimate-test-reporter",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"standard": {
"ignore": [
"lib/**"
]
},
"echint": {
"ignore": [
"coverage/**"
"lib/**"
]
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"devDependencies": {
"codeclimate-test-reporter": "0.1.1",
"echint": "^1.2.0",
"istanbul": "^0.3.15",
"mocha": "^3.0.1",
"rimraf": "^2.4.0",
"should": "^11.0.0",
"standard": "^8.0.0"
"babel-cli": "^6.18.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-env": "0.0.8",
"babel-register": "^6.18.0",
"codeclimate-test-reporter": "^0.4.0",
"cz-conventional-changelog": "^1.2.0",
"echint": "^1.5.3",
"rimraf": "^2.5.4",
"semantic-release": "^6.3.2",
"snazzy": "^5.0.0",
"tap": "^8.0.1"
},
"dependencies": {
"mkdirp-promise": "^3.0.1",
"pinkie-promise": "^1.0.0"
"mkdirp-promise": "^4.0.1"
}
}
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import fs from 'fs'

export default function (filename, data, options) {
return new Promise(function (resolve, reject) {
fs.writeFile(filename, data, options, (err) => err === null ? resolve(filename) : reject(err))
})
}
68 changes: 27 additions & 41 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,43 @@
/* global afterEach, describe, it */
import fs from 'fs'
import mkdirp from 'mkdirp-promise'
import path from 'path'
import rimraf from 'rimraf'
import write from '../src'
import { test } from 'tap'

'use strict'
const tmp = path.join('test', 'tmp')

var fs = require('fs')
var mkdirp = require('mkdirp-promise')
var path = require('path')
var rimraf = require('rimraf')
var write = require('..')
test('fs-writefile-promise', (tap) => {
tap.plan(3)
tap.afterEach((done) => rimraf(tmp, done))

require('should')
tap.test('successfully write file', (assert) => {
assert.plan(2)

var tmp = path.join('test', 'tmp')

afterEach(function (done) {
rimraf(tmp, done)
})

describe('node module', function () {
it('should successfully write file', function (done) {
var target = path.join(tmp, 'foo')
let target = path.join(tmp, 'foo')

mkdirp(tmp)
.then(function () {
return write(target, 'bar')
})
.then(function (filename) {
return fs.readFileSync(filename)
})
.then(function (content) {
content.should.be.a.Buffer
content.toString().should.eql('bar')

done()
.then(() => write(target, 'bar'))
.then((filename) => fs.readFileSync(filename))
.then((content) => {
assert.type(content, Buffer)
assert.equal(content.toString(), 'bar')
})
})

it('should throw a type error when the path isn\'t a string', function (done) {
write(false, 'foo')
.catch(function (err) {
err.message.should.equal('path must be a string')
tap.test("throw a type error when the path isn't a string", (assert) => {
assert.plan(1)

done()
})
write(false, 'foo')
.catch((err) => assert.match(err.message, 'path must be a string'))
})

it('should throw an error when the encoding is not supported', function (done) {
var target = path.join(tmp, 'fake', 'path')
tap.test('throw an error when the encoding is not supported', (assert) => {
assert.plan(1)

write(target, 'foo')
.catch(function (err) {
err.code.should.equal('ENOENT')
let target = path.join(tmp, 'fake', 'path')

done()
})
write(target, 'foo')
.catch((err) => assert.equal(err.code, 'ENOENT'))
})
})

0 comments on commit df068e7

Please sign in to comment.