diff --git a/.eslintrc.json b/.eslintrc.json index b893ca0..5e8d859 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -35,7 +35,7 @@ "func-style": 2, "generator-star-spacing": 2, "getter-return": 2, - "global-require": 2, + "global-require": 0, "guard-for-in": 2, "handle-callback-err": 2, "id-blacklist": 2, @@ -183,7 +183,7 @@ "no-with": 2, "nonblock-statement-body-position": 2, "object-curly-spacing": 2, - "object-shorthand": 2, + "object-shorthand": [2, "consistent"], "one-var": 2, "operator-assignment": 2, "prefer-arrow-callback": 2, diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 7064ed7..0000000 --- a/.jshintrc +++ /dev/null @@ -1,13 +0,0 @@ -{ - "esversion": 6, - "node": true, - "globals" : { - /* MOCHA */ - "describe" : false, - "it" : false, - "before" : false, - "beforeEach" : false, - "after" : false, - "afterEach" : false - } -} diff --git a/.travis.yml b/.travis.yml index 294c54d..dfe0fad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,9 @@ node_js: addons: code_climate: repo_token: $CODE_CLIMATE_TOKEN +branches: + only: + master install: - npm install --loglevel=error - npm install --loglevel=error -g mocha diff --git a/index.js b/index.js index b01e000..ab2bd2a 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,16 @@ require('bluebird'); const StorageBase = require('ghost-storage-base'), cloudinary = require('cloudinary').v2, path = require('path'), - request = require('request').defaults({encoding: null}); + request = require('request').defaults({encoding: null}), + common = (() => { + // Tries to include GhostError helper + try { + return require(path.join(__dirname, '../../../lib/common')); + } catch (ex) { + // Use local mock instead + return require(path.join(__dirname, '/tests/errors')); + } + })(); class CloudinaryAdapter extends StorageBase { @@ -55,7 +64,10 @@ class CloudinaryAdapter extends StorageBase { return new Promise((resolve, reject) => { cloudinary.uploader.upload(image.path, uploadOptions, (err, res) => { if (err) { - return reject(new Error(`Could not upload image ${image.path}`)); + return reject(new common.errors.GhostError({ + err: err, + message: `Could not upload image ${image.path}` + })); } return resolve(cloudinary.url(res.public_id.concat('.', res.format), fetchOptions)); }); @@ -80,7 +92,10 @@ class CloudinaryAdapter extends StorageBase { return new Promise((resolve, reject) => { cloudinary.uploader.destroy(pubId, (err, res) => { if (err) { - return reject(new Error(`Could not delete image ${filename}`)); + return reject(new common.errors.GhostError({ + err: err, + message: `Could not delete image ${filename}` + })); } return resolve(res); }); @@ -95,7 +110,10 @@ class CloudinaryAdapter extends StorageBase { return new Promise((resolve, reject) => { request.get(opts.path, (err, res) => { if (err) { - return reject(new Error(`Could not read image ${opts.path}`)); + return reject(new common.errors.GhostError({ + err: err, + message: `Could not read image ${opts.path}` + })); } return resolve(res.body); }); diff --git a/package-lock.json b/package-lock.json index 2dd5e01..f5235ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -933,6 +933,11 @@ "is-property": "1.0.2" } }, + "get-caller-file": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.2.tgz", + "integrity": "sha1-9wLmMSfn4jHBYKgMFVSstw1QR+U=" + }, "get-func-name": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz", @@ -1541,6 +1546,14 @@ } } }, + "mock-require": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/mock-require/-/mock-require-3.0.1.tgz", + "integrity": "sha1-1e/YNMDaDOxzx7Z3Y9gWfTLYUd4=", + "requires": { + "get-caller-file": "1.0.2" + } + }, "moment": { "version": "2.19.1", "resolved": "https://registry.npmjs.org/moment/-/moment-2.19.1.tgz", diff --git a/package.json b/package.json index 89a5acd..4f85f0d 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,8 @@ "dependencies": { "bluebird": "3.5.1", "cloudinary": "~1.10.0", - "ghost-storage-base": "0.0.1" + "ghost-storage-base": "0.0.1", + "mock-require": "^3.0.1" }, "devDependencies": { "chai": "^4.1.2", diff --git a/tests/delete.js b/tests/delete.js index 1fd34fb..a907212 100644 --- a/tests/delete.js +++ b/tests/delete.js @@ -6,6 +6,7 @@ const CloudinaryAdapter = require('../index'), sinon = require('sinon'), cloudinary = require('cloudinary').v2, path = require('path'), + common = require(path.join(__dirname, '/errors')), fixtures = require(path.join(__dirname, '/fixtures')); let cloudinaryAdapter = null; @@ -31,7 +32,7 @@ describe('delete', function () { done('expected error'); }) .catch(function (ex) { - expect(ex).to.be.an.instanceOf(Error); + expect(ex).to.be.an.instanceOf(common.errors.GhostError); expect(ex.message).to.equal(`Could not delete image ${fixtures.mockInexistentImage.name}`); done(); }); diff --git a/tests/errors.js b/tests/errors.js new file mode 100644 index 0000000..eb1bd94 --- /dev/null +++ b/tests/errors.js @@ -0,0 +1,11 @@ +'use strict'; + +class GhostError extends Error { + constructor(options) { + super(); + this.err = options.err || null; + this.message = options.message || ""; + } +} + +module.exports = {errors: {GhostError: GhostError}}; diff --git a/tests/fixtures.js b/tests/fixtures.js index 0d8524a..c69516e 100644 --- a/tests/fixtures.js +++ b/tests/fixtures.js @@ -74,9 +74,9 @@ function generateImage(imageFile, imageName) { } module.exports = { - sampleConfig, - sampleLegacyConfig, - sampleApiResult, + sampleConfig: sampleConfig, + sampleLegacyConfig: sampleLegacyConfig, + sampleApiResult: sampleApiResult, mockImage: generateImage('favicon.png'), mockImageWithSpacesInName: generateImage('favicon.png', 'favicon with spaces.png'), mockInexistentImage: generateImage('not-found.png') diff --git a/tests/read.js b/tests/read.js index 5af6e82..bd38249 100644 --- a/tests/read.js +++ b/tests/read.js @@ -5,12 +5,14 @@ const CloudinaryAdapter = require('../index'), chai = require('chai'), expect = chai.expect, path = require('path'), + common = require(path.join(__dirname, '/errors')), fixtures = require(path.join(__dirname, '/fixtures')); let cloudinaryAdapter = null; describe('read', function () { it('should find the image', function (done) { + cloudinaryAdapter = new CloudinaryAdapter(fixtures.sampleConfig); const scope = nock('https://blog.mornati.net') .get('/myimage.png') @@ -29,7 +31,7 @@ describe('read', function () { done('expected error'); }) .catch(function (ex) { - expect(ex).to.be.an.instanceOf(Error); + expect(ex).to.be.an.instanceOf(common.errors.GhostError); expect(ex.message).to.equal('Could not read image undefined'); done(); }); @@ -47,7 +49,7 @@ describe('read', function () { done('expected error'); }) .catch(function (ex) { - expect(ex).to.be.an.instanceOf(Error); + expect(ex).to.be.an.instanceOf(common.errors.GhostError); expect(ex.message).to.equal('Could not read image https://blog.mornati.net/myimage.png'); done(scope.done()); }); diff --git a/tests/save.js b/tests/save.js index d6e0227..01dd494 100644 --- a/tests/save.js +++ b/tests/save.js @@ -6,6 +6,7 @@ const CloudinaryAdapter = require('../index'), sinon = require('sinon'), cloudinary = require('cloudinary').v2, path = require('path'), + common = require(path.join(__dirname, '/errors')), fixtures = require(path.join(__dirname, '/fixtures')); let cloudinaryAdapter = null; @@ -142,7 +143,7 @@ describe('save', function () { done('expected error'); }) .catch(function (ex) { - expect(ex).to.be.an.instanceOf(Error); + expect(ex).to.be.an.instanceOf(common.errors.GhostError); expect(ex.message).to.equal(`Could not upload image ${fixtures.mockImage.path}`); done(); });