diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..765ba8b --- /dev/null +++ b/.babelrc @@ -0,0 +1,23 @@ +{ + "env": { + "test": { + "presets": [ ["env", { "targets": { "node": "current" } }] ], + "plugins": [] + }, + + "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/.editorconfig b/.editorconfig index e717f5e..435e3c7 100644 --- a/.editorconfig +++ b/.editorconfig @@ -2,12 +2,12 @@ root = true [*] -indent_style = space -indent_size = 2 -end_of_line = lf charset = utf-8 -trim_trailing_whitespace = true +end_of_line = lf +indent_size = 2 +indent_style = space insert_final_newline = true +trim_trailing_whitespace = true [*.md] trim_trailing_whitespace = false 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 6044f8b..1bfd37a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.log -node_modules -coverage* -test/build +/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/.travis.yml b/.travis.yml index 59f23c0..cc70a14 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,30 @@ language: node_js node_js: - 4 - 6 + - 7 -after_script: +env: + - BABEL_ENV=test + +matrix: + fast_finish: true + +cache: + directories: + - node_modules + +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 + - npm run semantic-release + +branches: + except: + - /^v\d+\.\d+\.\d+$/ + - /^greenkeeper\/.+/ diff --git a/LICENSE b/LICENSE index d527871..ca55c91 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,13 @@ -The MIT License (MIT) - -Copyright (c) 2015 Ahmad Nassri (https://www.ahmadnassri.com) - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -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 OR COPYRIGHT HOLDERS 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. +Copyright (c) 2015, Ahmad Nassri + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md index cce560a..3189857 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# Metalsmith Request [![version][npm-version]][npm-url] [![License][npm-license]][license-url] [![Actively Maintained][active-image]][active-url] +# Metalsmith Request [![version][npm-version]][npm-url] [![License][npm-license]][license-url] -[Metalsmith](http://www.metalsmith.io/) plugin to grab content from the web and expose the results to metadata. +> [Metalsmith](http://www.metalsmith.io/) plugin to grab content from the web and expose the results to metadata. uses [got](https://github.com/sindresorhus/got) under the hood to make HTTP calls. @@ -8,23 +8,43 @@ uses [got](https://github.com/sindresorhus/got) under the hood to make HTTP call [![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 metalsmith-request +```bash +npm install --production --save metalsmith-request ``` -## API +## Usage -Pass `targets` to the plugin with the `use` method: +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 -var Metalsmith = require('metalsmith') +/* + * Node 7 + */ +const request = require('metalsmith-request/lib/node7') + +/* + * Node 6 + */ +const request = require('metalsmith-request/lib/node6') + +/* + * Node 4 (Default) + * Note: additional ES2015 polyfills may be required + */ var request = require('metalsmith-request') +``` + +## API -var metalsmith = new Metalsmith(__dirname) +Pass `targets` to the plugin with the `use` method: + +```js +const metalsmith = new Metalsmith(__dirname) .use(request({ foo: 'http://domain.com/foo' })) @@ -33,12 +53,9 @@ var metalsmith = new Metalsmith(__dirname) You can also pass `options` as the second argument to use with the [got](https://github.com/sindresorhus/got) module ```js -var Metalsmith = require('metalsmith') -var request = require('metalsmith-request') - -var metalsmith = new Metalsmith(__dirname) +const metalsmith = new Metalsmith(__dirname) .use(request( - { + { foo: 'http://domain.com/foo.json' }, { @@ -61,20 +78,13 @@ You can also use the plugin with the Metalsmith CLI by adding a key to your `met } ``` -## Support - -Donations are welcome to help support the continuous development of this project. - -[![Gratipay][gratipay-image]][gratipay-url] -[![PayPal][paypal-image]][paypal-url] -[![Flattr][flattr-image]][flattr-url] -[![Bitcoin][bitcoin-image]][bitcoin-url] - -## License - -[MIT](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/metalsmith-request/blob/master/LICENSE +[license-url]: http://choosealicense.com/licenses/isc/ [travis-url]: https://travis-ci.org/ahmadnassri/metalsmith-request [travis-image]: https://img.shields.io/travis/ahmadnassri/metalsmith-request.svg?style=flat-square @@ -91,17 +101,5 @@ Donations are welcome to help support the continuous development of this project [david-url]: https://david-dm.org/ahmadnassri/metalsmith-request [david-image]: https://img.shields.io/david/ahmadnassri/metalsmith-request.svg?style=flat-square -[gratipay-url]: https://www.gratipay.com/ahmadnassri/ -[gratipay-image]: https://img.shields.io/gratipay/ahmadnassri.svg?style=flat-square - -[paypal-url]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=UJ2B2BTK9VLRS&on0=project&os0=metalsmith-request -[paypal-image]: http://img.shields.io/badge/paypal-donate-green.svg?style=flat-square - -[flattr-url]: https://flattr.com/submit/auto?user_id=codeinchaos&url=https://github.com/ahmadnassri/metalsmith-request&title=metalsmith-request&language=&tags=github&category=software -[flattr-image]: http://img.shields.io/badge/flattr-donate-green.svg?style=flat-square - -[bitcoin-image]: http://img.shields.io/badge/bitcoin-1Nb46sZRVG3or7pNaDjthcGJpWhvoPpCxy-green.svg?style=flat-square -[bitcoin-url]: https://www.coinbase.com/checkouts/ae383ae6bb931a2fa5ad11cec115191e?name=metalsmith-request - -[active-image]: https://maintained.tech/badge.svg -[active-url]: https://maintained.tech/ +[dependencyci-url]: https://dependencyci.com/github/ahmadnassri/metalsmith-request +[dependencyci-image]: https://dependencyci.com/github/ahmadnassri/metalsmith-request/badge?style=flat-square diff --git a/package.json b/package.json index 1aa4a79..83427ec 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,15 @@ { - "version": "1.0.0", + "version": "0.0.0-development", "name": "metalsmith-request", "description": "Metalsmith plugin to grab content from the web and expose the results to metadata", "author": "Ahmad Nassri (https://www.ahmadnassri.com/)", - "repository": "ahmadnassri/metalsmith-request", - "license": "MIT", - "main": "src/index.js", + "homepage": "https://github.com/ahmadnassri/metalsmith-request", + "repository": { + "type": "git", + "url": "https://github.com/ahmadnassri/metalsmith-request.git" + }, + "license": "ISC", + "main": "lib/index.js", "keywords": [ "metalsmith", "plugin", @@ -15,33 +19,53 @@ "got" ], "engines": { - "node": ">= 4" + "node": ">=4" }, "files": [ + "lib", "src" ], "bugs": { "url": "https://github.com/ahmadnassri/metalsmith-request/issues" }, "scripts": { - "pretest": "standard && echint", - "test": "mocha", - "posttest": "npm run coverage", - "coverage": "istanbul cover --dir coverage _mocha -- -R dot", - "codeclimate": "codeclimate < coverage/lcov.info" + "compile": "babel -q src", + "test": "BABEL_ENV=test tap test/*.js --node-arg=--require --node-arg=babel-register | tap-mocha-reporter spec", + "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": [ + "lib/**" + ] + }, + "config": { + "commitizen": { + "path": "./node_modules/cz-conventional-changelog" + } }, "devDependencies": { - "codeclimate-test-reporter": "0.3.2", - "echint": "^1.1.1", - "istanbul": "^0.4.3", - "metalsmith": "^1.7.0", - "mocha": "^3.0.1", - "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", + "metalsmith": "^2.3.0", + "semantic-release": "^6.3.2", + "snazzy": "^5.0.0", + "tap": "^8.0.1", + "tap-mocha-reporter": "^3.0.0" }, "dependencies": { - "async": "^1.0.0", - "debug": "^2.2.0", - "got": "^6.3.0" + "got": "^6.6.3" } } diff --git a/src/index.js b/src/index.js index 67604b0..c87a49c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,9 @@ -'use strict' +import { debuglog } from 'util' +import got from 'got' -var async = require('async') -var debug = require('debug')('metalsmith-request') -var got = require('got') -var util = require('util') +const debug = debuglog('metalsmith-request') -var defaults = { +const defaults = { json: false, headers: { 'user-agent': 'https://github.com/ahmadnassri/metalsmith-request' @@ -14,27 +12,26 @@ var defaults = { module.exports = function (targets, options) { targets = targets || {} - options = util._extend(defaults, options || {}) + options = Object.assign({}, defaults, options) - return function (files, metalsmith, done) { - var metadata = metalsmith.metadata() - - function get (key, done) { - var url = targets[key] + return (files, metalsmith, done) => { + Promise.all(Object.keys(targets).map((key) => { + let metadata = metalsmith.metadata() + let url = targets[key] debug('[%s](%s) >', key, url) - got.get(url, options, function (err, data, res) { - debug('[%s](%s) < %s: %s', key, url, res.statusCode, res.statusMessage) - - if (err) return done(err) + return got(url, options) + .then(response => { + debug('[%s](%s) < %s: %s', key, url, response.statusCode, response.statusMessage) - metadata[key] = data + return response.body + }) - done() - }) - } + .then(body => (metadata[key] = body)) + })) - async.each(Object.keys(targets), get, done) + .then(() => done()) + .catch(err => done(err)) } } diff --git a/test/index.js b/test/index.js index 5624af7..961f42e 100644 --- a/test/index.js +++ b/test/index.js @@ -1,42 +1,34 @@ -/* global describe, it */ -'use strict' +import Metalsmith from 'metalsmith' +import request from '../src' +import { test } from 'tap' -var Metalsmith = require('metalsmith') -var request = require('..') +test('send request and update metadata', { timeout: 5000 }, (assert) => { + let smith = new Metalsmith('test') -require('should') + let options = { + json: true + } -describe('metalsmith-request', function () { - this.timeout(5000) + let targets = { + foo: 'http://mockbin.com/request?foo=bar', + bar: 'http://mockbin.com/request?bar=baz' + } - it('should send request and update metadata', function (done) { - var smith = new Metalsmith('test') + smith.use(request(targets, options)) - var options = { - json: true - } + smith.build((err) => { + assert.equal(err, null) - var targets = { - foo: 'http://mockbin.com/request?foo=bar', - bar: 'http://mockbin.com/request?bar=baz' - } + let metadata = smith.metadata() - smith.use(request(targets, options)) + assert.type(metadata.foo, Object) + assert.type(metadata.foo.queryString, Object) + assert.same(metadata.foo.queryString, { foo: 'bar' }) - smith.build(function (err) { - if (err) return done(err) + assert.type(metadata.bar, Object) + assert.type(metadata.bar.queryString, Object) + assert.same(metadata.bar.queryString, { bar: 'baz' }) - var metadata = smith.metadata() - - metadata.foo.should.be.an.Object - metadata.foo.should.have.property('queryString') - metadata.foo.queryString.should.eql({ foo: 'bar' }) - - metadata.bar.should.be.an.Object - metadata.bar.should.have.property('queryString') - metadata.bar.queryString.should.eql({ bar: 'baz' }) - - done() - }) + assert.end() }) })