1 |
|
2 |
|
3 |
|
4 |
|
5 | module.exports = submodule
|
6 |
|
7 | var 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 |
|
14 | submodule.usage = "npm submodule <pkg>"
|
15 |
|
16 | submodule.completion = require("./docs.js").completion
|
17 |
|
18 | function 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 |
|
36 | function 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 |
|
44 | pkg.repository.url = pkg.repository.url
|
45 | .replace(/^(git:\/\/)?(git@)?github.com[:\/]/, "https://github.com/")
|
46 |
|
47 |
|
48 | getSubmodules(function (er, modules) {
|
49 | if (er) return cb(er)
|
50 |
|
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 |
|
58 | function updateSubmodule (name, cb) {
|
59 | var git = npm.config.get("git")
|
60 | var args = [ "submodule", "update", "--init", "node_modules/", name ]
|
61 |
|
62 |
|
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 |
|
73 | function addSubmodule (name, url, cb) {
|
74 | var git = npm.config.get("git")
|
75 | var args = [ "submodule", "add", url, "node_modules/", name ]
|
76 |
|
77 |
|
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 |
|
92 | var getSubmodules = function getSubmodules (cb) {
|
93 | var git = npm.config.get("git")
|
94 | var args = [ "submodule", "status" ]
|
95 |
|
96 |
|
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 |
|
108 | return line && line.match(/^node_modules\//)
|
109 | }).map(function (line) {
|
110 | return line.replace(/^node_modules\//g, "")
|
111 | })
|
112 |
|
113 |
|
114 | getSubmodules = function (cb) { return cb(null, res) }
|
115 |
|
116 | cb(null, res)
|
117 | })
|
118 | })
|
119 | }
|