Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
Sort dependencies when --save'ing.
Browse files Browse the repository at this point in the history
Uses sorted-object package to abstract away code shared by shrinkwrap and
install.
  • Loading branch information
domenic authored and robertkowalski committed Feb 20, 2014
1 parent 70fd532 commit 6fd6ff7
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 13 deletions.
5 changes: 4 additions & 1 deletion lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var npm = require("./npm.js")
, archy = require("archy")
, isGitUrl = require("./utils/is-git-url.js")
, npmInstallChecks = require("npm-install-checks")
, sortedObject = require("sorted-object")

function install (args, cb_) {
var hasArguments = !!args.length
Expand Down Expand Up @@ -378,7 +379,7 @@ function save (where, installed, tree, pretty, hasArguments, cb) {
var bundle = data.bundleDependencies || data.bundledDependencies
delete data.bundledDependencies
if (!Array.isArray(bundle)) bundle = []
data.bundleDependencies = bundle
data.bundleDependencies = bundle.sort()
}

log.verbose('saving', things)
Expand All @@ -391,6 +392,8 @@ function save (where, installed, tree, pretty, hasArguments, cb) {
}
})

data[deps] = sortedObject(data[deps])

data = JSON.stringify(data, null, 2) + "\n"
fs.writeFile(saveTarget, data, function (er) {
cb(er, installed, tree, pretty)
Expand Down
12 changes: 2 additions & 10 deletions lib/shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var npm = require("./npm.js")
, fs = require("fs")
, path = require("path")
, readJson = require("read-package-json")
, sortedObject = require("sorted-object")

shrinkwrap.usage = "npm shrinkwrap"

Expand Down Expand Up @@ -58,7 +59,7 @@ function shrinkwrap_ (pkginfo, silent, dev, cb) {
function save (pkginfo, silent, cb) {
// copy the keys over in a well defined order
// because javascript objects serialize arbitrarily
pkginfo.dependencies = copyOrder(pkginfo.dependencies)
pkginfo.dependencies = sortedObject(pkginfo.dependencies)
try {
var swdata = JSON.stringify(pkginfo, null, 2) + "\n"
} catch (er) {
Expand All @@ -75,12 +76,3 @@ function save (pkginfo, silent, cb) {
cb(null, pkginfo)
})
}

function copyOrder(obj) {
var result = {}
var keys = Object.keys(obj).sort()
keys.forEach(function (key) {
result[key] = obj[key]
})
return result
}
19 changes: 19 additions & 0 deletions node_modules/sorted-object/LICENSE.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions node_modules/sorted-object/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions node_modules/sorted-object/lib/sorted-object.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions node_modules/sorted-object/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
"ansistyles": "~0.1.3",
"path-is-inside": "~1.0.0",
"columnify": "0.1.2",
"npm-install-checks": "~1.0.0"
"npm-install-checks": "~1.0.0",
"sorted-object": "~1.0.0"
},
"bundleDependencies": [
"semver",
Expand Down Expand Up @@ -133,7 +134,8 @@
"ansistyles",
"path-is-inside",
"columnify",
"npm-install-checks"
"npm-install-checks",
"sorted-object"
],
"devDependencies": {
"ronn": "~0.3.6",
Expand Down

1 comment on commit 6fd6ff7

@XhmikosR
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 Was doing this manually all this time :)

Please sign in to comment.