Skip to content

Commit

Permalink
🐙 adding in support for package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
jcblw committed Jun 5, 2016
1 parent 20e8adb commit c513302
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 38 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
32 changes: 19 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
})
6 changes: 4 additions & 2 deletions lib/get-bad-deps.js
Original file line number Diff line number Diff line change
@@ -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
}, {})
Expand Down
14 changes: 7 additions & 7 deletions lib/get-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}, {}))
})
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down
8 changes: 6 additions & 2 deletions test/get-bad-deps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
2 changes: 1 addition & 1 deletion test/get-current-dep-versions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
24 changes: 18 additions & 6 deletions test/get-deps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,41 @@ 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()
})
})

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()
})
})
6 changes: 6 additions & 0 deletions test/scaffold/package-mock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"foo": "~1.0.0",
"bar": "^2.0.0"
}
}
2 changes: 1 addition & 1 deletion test/scaffold/packages/baz/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "3.0.0"
"version": "3.1.0"
}
4 changes: 2 additions & 2 deletions test/scaffold/shrinkwrap-mock.json
Original file line number Diff line number Diff line change
@@ -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"}
}
}

0 comments on commit c513302

Please sign in to comment.