From df068e7bff490623ce2b7670e33f7188b6bfb536 Mon Sep 17 00:00:00 2001 From: Ahmad Nassri Date: Sat, 19 Nov 2016 17:46:36 -0500 Subject: [PATCH] refactor(ES2015): rewrite in ES2015 + improved build process - 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 --- .babelrc | 28 ++++++++++++++++++++ .codeclimate.yml | 17 ++++++++++++ .gitattributes | 1 - .gitignore | 5 ++-- .jshintrc | 4 --- .npmignore | 4 --- .travis.yml | 25 +++++++++++++++--- README.md | 45 +++++++++++++++++++++++++------- lib/index.js | 15 ----------- package.json | 58 +++++++++++++++++++++++++++-------------- src/index.js | 7 +++++ test/index.js | 68 +++++++++++++++++++----------------------------- 12 files changed, 177 insertions(+), 100 deletions(-) create mode 100644 .babelrc create mode 100644 .codeclimate.yml delete mode 100644 .gitattributes delete mode 100644 .jshintrc delete mode 100644 .npmignore mode change 100644 => 100755 .travis.yml delete mode 100755 lib/index.js create mode 100755 src/index.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..6da770c --- /dev/null +++ b/.babelrc @@ -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"] + } + } +} diff --git a/.codeclimate.yml b/.codeclimate.yml new file mode 100644 index 0000000..faf3e94 --- /dev/null +++ b/.codeclimate.yml @@ -0,0 +1,17 @@ +engines: + fixme: + enabled: true + + duplication: + enabled: true + config: + languages: + - javascript + + eslint: + enabled: true + +ratings: + paths: + - src/** + - test/** diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 176a458..0000000 --- a/.gitattributes +++ /dev/null @@ -1 +0,0 @@ -* text=auto diff --git a/.gitignore b/.gitignore index 4ca5d1a..1bfd37a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.log -node_modules -coverage +/lib +/node_modules +/.nyc_output diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 086f6ba..0000000 --- a/.jshintrc +++ /dev/null @@ -1,4 +0,0 @@ -{ - "asi": true, - "node": true -} diff --git a/.npmignore b/.npmignore deleted file mode 100644 index c13bf94..0000000 --- a/.npmignore +++ /dev/null @@ -1,4 +0,0 @@ -.editorconfig -.jshintrc -.travis.yml -test diff --git a/.travis.yml b/.travis.yml old mode 100644 new mode 100755 index 1d7735e..82bd0cf --- a/.travis.yml +++ b/.travis.yml @@ -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\/.+/ diff --git a/README.md b/README.md index 31a41b4..8e7b5d4 100755 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -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/ diff --git a/lib/index.js b/lib/index.js deleted file mode 100755 index 97bdd7b..0000000 --- a/lib/index.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict' - -var Promise = require('pinkie-promise') -var fs = require('fs') - -module.exports = function (filename, data, options) { - return new Promise(function (_resolve, _reject) { - fs.writeFile(filename, data, options, function (err) { - return err === null ? _resolve(filename) : _reject(err) - }) - }) - .catch(function (err) { - throw err - }) -} diff --git a/package.json b/package.json index f681c08..47cdaf7 100644 --- a/package.json +++ b/package.json @@ -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 (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" } } diff --git a/src/index.js b/src/index.js new file mode 100755 index 0000000..3d73235 --- /dev/null +++ b/src/index.js @@ -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)) + }) +} diff --git a/test/index.js b/test/index.js index a5ae055..d533deb 100755 --- a/test/index.js +++ b/test/index.js @@ -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')) }) })