UNPKG

3.06 kBJavaScriptView Raw
1// npm submodule <pkg>
2// Check the package contents for a git repository url.
3// If there is one, then create a git submodule in the node_modules folder.
4
5module.exports = submodule
6
7var npm = require("./npm.js")
8 , exec = require("child_process").execFile
9 , cache = require("./cache.js")
10 , asyncMap = require("slide").asyncMap
11 , chain = require("slide").chain
12 , which = require("which")
13
14submodule.usage = "npm submodule <pkg>"
15
16submodule.completion = require("./docs.js").completion
17
18function submodule (args, cb) {
19 if (npm.config.get("global")) {
20 return cb(new Error("Cannot use submodule command in global mode."))
21 }
22
23 if (args.length === 0) return cb(submodule.usage)
24
25 asyncMap(args, function (arg, cb) {
26 cache.add(arg, cb)
27 }, function (er, pkgs) {
28 if (er) return cb(er)
29 chain(pkgs.map(function (pkg) { return function (cb) {
30 submodule_(pkg, cb)
31 }}), cb)
32 })
33
34}
35
36function submodule_ (pkg, cb) {
37 if (!pkg.repository
38 || pkg.repository.type !== "git"
39 || !pkg.repository.url) {
40 return cb(new Error(pkg._id + ": No git repository listed"))
41 }
42
43 // prefer https:// github urls
44 pkg.repository.url = pkg.repository.url
45 .replace(/^(git:\/\/)?(git@)?github.com[:\/]/, "https://github.com/")
46
47 // first get the list of submodules, and update if it's already there.
48 getSubmodules(function (er, modules) {
49 if (er) return cb(er)
50 // if there's already a submodule, then just update it.
51 if (modules.indexOf(pkg.name) !== -1) {
52 return updateSubmodule(pkg.name, cb)
53 }
54 addSubmodule(pkg.name, pkg.repository.url, cb)
55 })
56}
57
58function updateSubmodule (name, cb) {
59 var git = npm.config.get("git")
60 var args = [ "submodule", "update", "--init", "node_modules/", name ]
61
62 // check for git
63 which(git, function (err) {
64 if (err) {
65 err.code = "ENOGIT"
66 return cb(err)
67 }
68
69 exec(git, args, cb)
70 })
71}
72
73function addSubmodule (name, url, cb) {
74 var git = npm.config.get("git")
75 var args = [ "submodule", "add", url, "node_modules/", name ]
76
77 // check for git
78 which(git, function (err) {
79 if (err) {
80 err.code = "ENOGIT"
81 return cb(err)
82 }
83
84 exec(git, args, function (er) {
85 if (er) return cb(er)
86 updateSubmodule(name, cb)
87 })
88 })
89}
90
91
92var getSubmodules = function getSubmodules (cb) {
93 var git = npm.config.get("git")
94 var args = [ "submodule", "status" ]
95
96 // check for git
97 which(git, function (err) {
98 if (err) {
99 err.code = "ENOGIT"
100 return cb(err)
101 }
102 exec(git, args, function (er, stdout, stderr) {
103 if (er) return cb(er)
104 res = stdout.trim().split(/\n/).map(function (line) {
105 return line.trim().split(/\s+/)[1]
106 }).filter(function (line) {
107 // only care about submodules in the node_modules folder.
108 return line && line.match(/^node_modules\//)
109 }).map(function (line) {
110 return line.replace(/^node_modules\//g, "")
111 })
112
113 // memoize.
114 getSubmodules = function (cb) { return cb(null, res) }
115
116 cb(null, res)
117 })
118 })
119}