From c51330288eeb55346961caab00fb0be3eca0f5d2 Mon Sep 17 00:00:00 2001 From: jcblw Date: Sun, 5 Jun 2016 10:12:39 -0700 Subject: [PATCH] :octopus: adding in support for package.json --- .editorconfig | 13 ++++++++++ index.js | 32 +++++++++++++++---------- lib/get-bad-deps.js | 6 +++-- lib/get-deps.js | 14 +++++------ package.json | 9 +++---- test/get-bad-deps.spec.js | 8 +++++-- test/get-current-dep-versions.spec.js | 2 +- test/get-deps.spec.js | 24 ++++++++++++++----- test/scaffold/package-mock.json | 6 +++++ test/scaffold/packages/baz/package.json | 2 +- test/scaffold/shrinkwrap-mock.json | 4 ++-- 11 files changed, 82 insertions(+), 38 deletions(-) create mode 100644 .editorconfig create mode 100644 test/scaffold/package-mock.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..5d12634 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/index.js b/index.js index c4b5166..6422a4b 100755 --- a/index.js +++ b/index.js @@ -5,24 +5,30 @@ const path = require('path') const getDeps = require('./lib/get-deps') const getCurrentDepVersions = require('./lib/get-current-dep-versions') +const checkIfFile = require('./lib/check-if-file') const getBadDeps = require('./lib/get-bad-deps') const removeBadDeps = require('./lib/remove-bad-deps') const shrinkwrapPath = path.resolve(process.cwd(), './npm-shrinkwrap.json') +const pkgPath = path.resolve(process.cwd(), './package.json') const packagesPath = path.resolve(process.cwd(), './node_modules/') -getDeps(shrinkwrapPath, (err, deps) => { - if (err) throw err - getCurrentDepVersions(deps, packagesPath, (err, currDeps) => { +checkIfFile(shrinkwrapPath, (swErr) => { + const usePkg = !!swErr + const fileDepPath = usePkg ? pkgPath : shrinkwrapPath + getDeps(fileDepPath, usePkg, (err, deps) => { if (err) throw err - const baddies = getBadDeps(deps, currDeps) - if (Object.keys(baddies).length) { - console.log(`cleaning out of date deps: ${Object.keys(baddies).join(',')}`) - const folders = Object.keys(baddies).map((dep) => path.resolve(packagesPath, `./${dep}/`)) - return removeBadDeps(folders, (err) => { - if (err) throw err - console.log('modules successfully removed') - }) - } - console.log('all deps up to date') + getCurrentDepVersions(deps, packagesPath, (err, currDeps) => { + if (err) throw err + const baddies = getBadDeps(deps, currDeps, usePkg) + if (Object.keys(baddies).length) { + console.log(`cleaning out of date deps: ${Object.keys(baddies).join(',')}`) + const folders = Object.keys(baddies).map((dep) => path.resolve(packagesPath, `./${dep}/`)) + return removeBadDeps(folders, (err) => { + if (err) throw err + console.log('modules successfully removed') + }) + } + console.log('all deps up to date') + }) }) }) diff --git a/lib/get-bad-deps.js b/lib/get-bad-deps.js index 411e71a..4858eba 100644 --- a/lib/get-bad-deps.js +++ b/lib/get-bad-deps.js @@ -1,9 +1,11 @@ 'use strict' -function getBadDeps (deps, currDeps) { +const semver = require('semver') + +function getBadDeps (deps, currDeps, isPkg) { return Object.keys(deps).reduce((accum, depName) => { if (deps[depName] !== currDeps[depName] && currDeps.hasOwnProperty(depName)) { - accum[depName] = true + accum[depName] = isPkg ? !semver.satisfies(currDeps[depName], deps[depName]) : true } return accum }, {}) diff --git a/lib/get-deps.js b/lib/get-deps.js index f610898..42c7c87 100644 --- a/lib/get-deps.js +++ b/lib/get-deps.js @@ -2,18 +2,18 @@ const checkIfFile = require('./check-if-file') -function getDeps (depPath, callback) { +function getDeps (depPath, isPkg, callback) { checkIfFile(depPath, (err) => { if (err) return callback(err) - const shrinkWrap = require(depPath) - - if (!shrinkWrap || typeof shrinkWrap !== 'object' || !shrinkWrap.dependencies) { - return callback(new Error('current shrink-wrap file is not formatted correctly')) + const pkgConfig = require(depPath) + const isProperlyFormatted = pkgConfig && typeof pkgConfig === 'object' && pkgConfig.dependencies + if (!isProperlyFormatted) { + return callback(new Error(`"${depPath}" is not formatted correctly`)) } - callback(null, Object.keys(shrinkWrap.dependencies).reduce((accum, key) => { - accum[key] = shrinkWrap.dependencies[key].version + callback(null, Object.keys(pkgConfig.dependencies).reduce((accum, key) => { + accum[key] = isPkg ? pkgConfig.dependencies[key] : pkgConfig.dependencies[key].version return accum }, {})) }) diff --git a/package.json b/package.json index 35db945..9f10c6f 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,9 @@ "version": "0.0.1", "description": "This is a utility that you can run before npm install to remove outdated dependencies in your node_modules file.", "main": "index.js", - "repository" : { - "type" : "git", - "url" : "https://github.com/jcblw/npm-remove-outdated.git" + "repository": { + "type": "git", + "url": "https://github.com/jcblw/npm-remove-outdated.git" }, "scripts": { "test": "standard && nyc tap ./test/*.spec.js && nyc report --reporter=text-lcov | coveralls" @@ -21,7 +21,8 @@ "author": "", "license": "ISC", "dependencies": { - "rimraf": "^2.5.2" + "rimraf": "^2.5.2", + "semver": "^5.1.0" }, "devDependencies": { "coveralls": "^2.11.9", diff --git a/test/get-bad-deps.spec.js b/test/get-bad-deps.spec.js index 7ae69a8..5c765a6 100644 --- a/test/get-bad-deps.spec.js +++ b/test/get-bad-deps.spec.js @@ -5,6 +5,10 @@ const getBadDeps = require('../lib/get-bad-deps') t.equal(typeof getBadDeps, 'function', 'module.export is a function') const deps = {'foo': '1.0.0'} -t.equal(Object.keys(getBadDeps(deps, deps)).length, 0, 'an empty object is returned if both objects passed are the same') +t.equal(Object.keys(getBadDeps(deps, deps, false)).length, 0, 'an empty object is returned if both objects passed are the same') const currentDeps = {'foo': '0.0.1'} -t.equal(getBadDeps(deps, currentDeps).foo, true, 'foo is out of date') +t.equal(getBadDeps(deps, currentDeps, false).foo, true, 'foo is out of date') +const pkgDeps = {'foo': '~1.0.0', 'bar': '2.1.x'} +const currentPkgDeps = {'foo': '1.0.1', 'bar': '2.0.0'} +t.equal(getBadDeps(pkgDeps, currentPkgDeps, true).foo, false, 'foo is up to date with the pkg') +t.equal(getBadDeps(pkgDeps, currentPkgDeps, true).bar, true, 'bar is out of date with the pkg') diff --git a/test/get-current-dep-versions.spec.js b/test/get-current-dep-versions.spec.js index 340fd77..6e37157 100644 --- a/test/get-current-dep-versions.spec.js +++ b/test/get-current-dep-versions.spec.js @@ -23,7 +23,7 @@ t.test('the deps object in the second argument to the callback', (test) => { test.equal(typeof deps.foo, 'string', 'the value of the key foo in the deps object is a string') test.equal(typeof deps.baz, 'string', 'the value of the key baz in the deps object is a string') test.equal(deps.foo, '1.0.0', 'the value foo is the correct version') - test.equal(deps.baz, '3.0.0', 'the value baz is the correct version') + test.equal(deps.baz, '3.1.0', 'the value baz is the correct version') test.end() }) }) diff --git a/test/get-deps.spec.js b/test/get-deps.spec.js index 2bb2b17..cecd28f 100644 --- a/test/get-deps.spec.js +++ b/test/get-deps.spec.js @@ -8,7 +8,7 @@ t.equal(typeof getDeps, 'function', 'type exported from module is a function') t.throws(() => { getDeps() }, 'if no path is used a exception is thrown') t.test('get deps callback error if no file is found', (test) => { - getDeps('./test/scaffold/foo', (err) => { + getDeps('./test/scaffold/foo', false, (err) => { test.equal(typeof err, 'object', 'the error from the callback is an object') test.match(err.message, 'ENOENT', 'the error message is a file not found message') test.end() @@ -16,21 +16,33 @@ t.test('get deps callback error if no file is found', (test) => { }) t.test('get deps callback error if the file given is not formatted properly', (test) => { - getDeps(path.resolve(process.cwd(), './test/scaffold/shrinkwrap-bad.json'), (err) => { + getDeps(path.resolve(process.cwd(), './test/scaffold/shrinkwrap-bad.json'), false, (err) => { test.equal(typeof err, 'object', 'the error from the callback is an object') - test.match(err.message, 'current shrink-wrap file is not formatted correctly', 'the error message is a file not formatted properly') + test.match(err.message, 'is not formatted correctly', 'the error message is a file not formatted properly') test.end() }) }) t.test('get deps callback will return an object with the key value of some dependencies from the shrinkwrap file if file is found', (test) => { - getDeps(path.resolve(process.cwd(), './test/scaffold/shrinkwrap-mock.json'), (err, deps) => { + getDeps(path.resolve(process.cwd(), './test/scaffold/shrinkwrap-mock.json'), false, (err, deps) => { if (err) test.fail('no error should be present') test.equal(typeof deps, 'object', 'the deps in second argument of the callback is an object') test.equal(deps.hasOwnProperty('foo'), true, 'the key foo is set') test.equal(deps.hasOwnProperty('bar'), true, 'the key bar is set') - test.equal(deps.foo, '1.0.0', 'the foo deps has the correct version') - test.equal(deps.bar, '2.0.0', 'the bar deps has the correct version') + test.equal(deps.foo, '1.0.1', 'the foo deps has the correct version') + test.equal(deps.bar, '2.2.0', 'the bar deps has the correct version') + test.end() + }) +}) + +t.test('get deps callback will return an object with the key value of some dependencies from the package file if file is found', (test) => { + getDeps(path.resolve(process.cwd(), './test/scaffold/package-mock.json'), true, (err, deps) => { + if (err) test.fail('no error should be present') + test.equal(typeof deps, 'object', 'the deps in second argument of the callback is an object') + test.equal(deps.hasOwnProperty('foo'), true, 'the key foo is set') + test.equal(deps.hasOwnProperty('bar'), true, 'the key bar is set') + test.equal(deps.foo, '~1.0.0', 'the foo deps has the correct version') + test.equal(deps.bar, '^2.0.0', 'the bar deps has the correct version') test.end() }) }) diff --git a/test/scaffold/package-mock.json b/test/scaffold/package-mock.json new file mode 100644 index 0000000..9f3af53 --- /dev/null +++ b/test/scaffold/package-mock.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "foo": "~1.0.0", + "bar": "^2.0.0" + } +} diff --git a/test/scaffold/packages/baz/package.json b/test/scaffold/packages/baz/package.json index f27700c..05e6f1d 100644 --- a/test/scaffold/packages/baz/package.json +++ b/test/scaffold/packages/baz/package.json @@ -1,3 +1,3 @@ { - "version": "3.0.0" + "version": "3.1.0" } diff --git a/test/scaffold/shrinkwrap-mock.json b/test/scaffold/shrinkwrap-mock.json index 2959705..fbd9c01 100644 --- a/test/scaffold/shrinkwrap-mock.json +++ b/test/scaffold/shrinkwrap-mock.json @@ -1,6 +1,6 @@ { "dependencies": { - "foo": { "version": "1.0.0" }, - "bar": { "version": "2.0.0"} + "foo": { "version": "1.0.1" }, + "bar": { "version": "2.2.0"} } }