Skip to content

Commit

Permalink
Close npm#944 Implement --save switch to write deps to package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jun 18, 2011
1 parent ff0f636 commit 2d97034
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
10 changes: 10 additions & 0 deletions doc/config.md
Expand Up @@ -90,6 +90,7 @@ The following shorthands are parsed on the command-line:
* `-f`: `--force` * `-f`: `--force`
* `-l`: `--long` * `-l`: `--long`
* `-desc`: `--description` * `-desc`: `--description`
* `-S`: `--save`
* `ll` and `la` commands: `ls --long` * `ll` and `la` commands: `ls --long`


If the specified configuration param resolves unambiguously to a known If the specified configuration param resolves unambiguously to a known
Expand Down Expand Up @@ -433,6 +434,15 @@ The base URL of the npm package registry.


Remove failed installs. 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 ### searchopts


* Default: "" * Default: ""
Expand Down
49 changes: 47 additions & 2 deletions lib/install.js
Expand Up @@ -157,7 +157,8 @@ function install (args, cb_) {
, pretty = prettify(tree, installed) , pretty = prettify(tree, installed)


output.write(pretty, function (er) { output.write(pretty, function (er) {
cb_(er, installed, tree, pretty) if (er) return cb_(er)
save(where, installed, tree, pretty, cb_)
}) })
} }


Expand Down Expand Up @@ -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, // Outputting *all* the installed modules is a bit confusing,
// because the length of the path does not make it clear // because the length of the path does not make it clear
// that the submodules are not immediately require()able. // that the submodules are not immediately require()able.
Expand Down Expand Up @@ -270,12 +314,13 @@ function treeify (installed) {
//log.warn(whatWhere, "whatWhere") //log.warn(whatWhere, "whatWhere")
return Object.keys(whatWhere).reduce(function (l, r) { return Object.keys(whatWhere).reduce(function (l, r) {
var ww = whatWhere[r] var ww = whatWhere[r]
//log.warn(ww) //log.warn([r, ww], "r, ww")
if (!ww.parent) { if (!ww.parent) {
l[r] = ww l[r] = ww
} else { } else {
var p = whatWhere[ww.parentDir] var p = whatWhere[ww.parentDir]
if (p) p.children.push(ww) if (p) p.children.push(ww)
else l[r] = ww
} }
return l return l
}, {}) }, {})
Expand Down
3 changes: 3 additions & 0 deletions lib/utils/config-defs.js
Expand Up @@ -83,6 +83,7 @@ Object.defineProperty(exports, "defaults", {get: function () {
, "rebuild-bundle" : true , "rebuild-bundle" : true
, registry : "http://registry.npmjs.org/" , registry : "http://registry.npmjs.org/"
, rollback : true , rollback : true
, save : false
, searchopts: "" , searchopts: ""
, searchexclude: null , searchexclude: null
, shell : process.env.SHELL || "bash" , shell : process.env.SHELL || "bash"
Expand Down Expand Up @@ -148,6 +149,7 @@ exports.types =
, "rebuild-bundle" : Boolean , "rebuild-bundle" : Boolean
, registry : url , registry : url
, rollback : Boolean , rollback : Boolean
, save : Boolean
, searchopts : String , searchopts : String
, searchexclude: [null, String] , searchexclude: [null, String]
, shell : path , shell : path
Expand Down Expand Up @@ -190,4 +192,5 @@ exports.shorthands =
, p : ["--parseable"] , p : ["--parseable"]
, porcelain : ["--parseable"] , porcelain : ["--parseable"]
, g : ["--global"] , g : ["--global"]
, S : ["--save"]
} }

0 comments on commit 2d97034

Please sign in to comment.