Permalink
Browse files

Close npm#944 Implement --save switch to write deps to package.json

  • Loading branch information...
isaacs committed Jun 18, 2011
1 parent ff0f636 commit 2d970344545d7dfadf217365755afd0cdca5b1f1
Showing with 60 additions and 2 deletions.
  1. +10 −0 doc/config.md
  2. +47 −2 lib/install.js
  3. +3 −0 lib/utils/config-defs.js
@@ -90,6 +90,7 @@ The following shorthands are parsed on the command-line:
* `-f`: `--force`
* `-l`: `--long`
* `-desc`: `--description`
* `-S`: `--save`
* `ll` and `la` commands: `ls --long`
If the specified configuration param resolves unambiguously to a known
@@ -433,6 +434,15 @@ The base URL of the npm package registry.
Remove failed installs.
### save
* Default: false
* Type: Boolean
Save installed packages to a package.json file as dependencies.
Only works if there is already a package.json file present.
### searchopts
* Default: ""
@@ -157,7 +157,8 @@ function install (args, cb_) {
, pretty = prettify(tree, installed)
output.write(pretty, function (er) {
cb_(er, installed, tree, pretty)
if (er) return cb_(er)
save(where, installed, tree, pretty, cb_)
})
}
@@ -214,6 +215,49 @@ function install (args, cb_) {
})
}
// if the -S|--save option is specified, then write installed packages
// as dependencies to a package.json file.
// This is experimental.
// save(installed, tree, pretty, cb_)
function save (where, installed, tree, pretty, cb) {
if (!npm.config.get("save")) {
return cb(null, installed, tree, pretty)
}
// each item in the tree is a top-level thing that should be saved
// to the package.json file.
// The relevant tree shape is { <folder>: {what:<pkg>} }
var saveTarget = path.resolve(where, "package.json")
, things = Object.keys(tree).map(function (k) {
//log.warn(k, "k")
return tree[k].what.split("@")
}).reduce(function (set, k) {
set[k[0]] = "~" + k[1]
return set
}, {})
//log.warn(things, "things")
// don't use readJson, because we don't want to do all the other
// tricky npm-specific stuff that's in there.
fs.readFile(saveTarget, function (er, data) {
// ignore errors here, just don't save it.
try {
data = JSON.parse(data.toString("utf8"))
} catch (ex) {
er = ex
}
if (er) return cb(null, installed, tree, pretty)
data.dependencies = data.dependencies || {}
Object.keys(things).forEach(function (t) {
data.dependencies[t] = things[t]
})
fs.writeFile(saveTarget, JSON.stringify(data, null, 2), function (er) {
cb(er, installed, tree, pretty)
})
})
}
// Outputting *all* the installed modules is a bit confusing,
// because the length of the path does not make it clear
// that the submodules are not immediately require()able.
@@ -270,12 +314,13 @@ function treeify (installed) {
//log.warn(whatWhere, "whatWhere")
return Object.keys(whatWhere).reduce(function (l, r) {
var ww = whatWhere[r]
//log.warn(ww)
//log.warn([r, ww], "r, ww")
if (!ww.parent) {
l[r] = ww
} else {
var p = whatWhere[ww.parentDir]
if (p) p.children.push(ww)
else l[r] = ww
}
return l
}, {})
@@ -83,6 +83,7 @@ Object.defineProperty(exports, "defaults", {get: function () {
, "rebuild-bundle" : true
, registry : "http://registry.npmjs.org/"
, rollback : true
, save : false
, searchopts: ""
, searchexclude: null
, shell : process.env.SHELL || "bash"
@@ -148,6 +149,7 @@ exports.types =
, "rebuild-bundle" : Boolean
, registry : url
, rollback : Boolean
, save : Boolean
, searchopts : String
, searchexclude: [null, String]
, shell : path
@@ -190,4 +192,5 @@ exports.shorthands =
, p : ["--parseable"]
, porcelain : ["--parseable"]
, g : ["--global"]
, S : ["--save"]
}

0 comments on commit 2d97034

Please sign in to comment.