]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/lib/shrinkwrap.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / lib / shrinkwrap.js
1 // emit JSON describing versions of all packages currently installed (for later
2 // use with shrinkwrap install)
3
4 module.exports = exports = shrinkwrap
5
6 var npm = require("./npm.js")
7   , log = require("npmlog")
8   , fs = require("fs")
9   , writeFileAtomic = require("write-file-atomic")
10   , path = require("path")
11   , readJson = require("read-package-json")
12   , sortedObject = require("sorted-object")
13
14 shrinkwrap.usage = "npm shrinkwrap"
15
16 function shrinkwrap (args, silent, cb) {
17   if (typeof cb !== "function") cb = silent, silent = false
18
19   if (args.length) {
20     log.warn("shrinkwrap", "doesn't take positional args")
21   }
22
23   // https://github.com/npm/npm/issues/7641
24   // introduced because `npm ls` can now show dev and prod depenednecy
25   // trees separately
26   if (npm.config.get("dev")) {
27     npm.config.set("production", true)
28   }
29   npm.commands.ls([], true, function (er, _, pkginfo) {
30     if (er) return cb(er)
31     shrinkwrap_(pkginfo, silent, npm.config.get("dev"), cb)
32   })
33 }
34
35 function shrinkwrap_ (pkginfo, silent, dev, cb) {
36   if (pkginfo.problems) {
37     return cb(new Error("Problems were encountered\n"
38                        +"Please correct and try again.\n"
39                        +pkginfo.problems.join("\n")))
40   }
41
42   if (!dev) {
43     // remove dev deps unless the user does --dev
44     readJson(path.resolve(npm.prefix, "package.json"), function (er, data) {
45       if (er)
46         return cb(er)
47       if (data.devDependencies) {
48         Object.keys(data.devDependencies).forEach(function (dep) {
49           if (data.dependencies && data.dependencies[dep]) {
50             // do not exclude the dev dependency if it's also listed as a dependency
51             return
52           }
53
54           log.warn("shrinkwrap", "Excluding devDependency: %s", dep, data.dependencies)
55           delete pkginfo.dependencies[dep]
56         })
57       }
58       save(pkginfo, silent, cb)
59     })
60   } else {
61     save(pkginfo, silent, cb)
62   }
63 }
64
65
66 function save (pkginfo, silent, cb) {
67   // copy the keys over in a well defined order
68   // because javascript objects serialize arbitrarily
69   pkginfo.dependencies = sortedObject(pkginfo.dependencies || {})
70   var swdata
71   try {
72     swdata = JSON.stringify(pkginfo, null, 2) + "\n"
73   } catch (er) {
74     log.error("shrinkwrap", "Error converting package info to json")
75     return cb(er)
76   }
77
78   var file = path.resolve(npm.prefix, "npm-shrinkwrap.json")
79
80   writeFileAtomic(file, swdata, function (er) {
81     if (er) return cb(er)
82     if (silent) return cb(null, pkginfo)
83     console.log("wrote npm-shrinkwrap.json")
84     cb(null, pkginfo)
85   })
86 }