2 module.exports = completion
4 completion.usage = "npm completion >> ~/.bashrc\n"
5 + "npm completion >> ~/.zshrc\n"
6 + "source <(npm completion)"
8 var npm = require("./npm.js")
9 , npmconf = require("./config/core.js")
10 , configDefs = npmconf.defs
11 , configTypes = configDefs.types
12 , shorthands = configDefs.shorthands
13 , nopt = require("nopt")
14 , configNames = Object.keys(configTypes).filter(function (e) {
15 return e.charAt(0) !== "_"
17 , shorthandNames = Object.keys(shorthands)
18 , allConfs = configNames.concat(shorthandNames)
19 , once = require("once")
22 completion.completion = function (opts, cb) {
23 if (opts.w > 3) return cb()
25 var fs = require("graceful-fs")
26 , path = require("path")
29 fs.stat(path.resolve(process.env.HOME, ".bashrc"), function (er) {
33 fs.stat(path.resolve(process.env.HOME, ".zshrc"), function (er) {
38 if (zshExists === null || bashExists === null) return
40 if (zshExists) out.push("~/.zshrc")
41 if (bashExists) out.push("~/.bashrc")
42 if (opts.w === 2) out = out.map(function (m) {
49 function completion (args, cb) {
50 if (process.platform === 'win32' && !(/^MINGW(32|64)$/.test(process.env.MSYSTEM))) {
51 var e = new Error('npm completion supported only in MINGW / Git bash on Windows')
53 e.errno = require('constants').ENOTSUP
57 // if the COMP_* isn't in the env, then just dump the script.
58 if (process.env.COMP_CWORD === undefined
59 ||process.env.COMP_LINE === undefined
60 ||process.env.COMP_POINT === undefined
61 ) return dumpScript(cb)
63 console.error(process.env.COMP_CWORD)
64 console.error(process.env.COMP_LINE)
65 console.error(process.env.COMP_POINT)
67 //console.log("abracadabrasauce\nabracad cat monger")
68 //if (Math.random() * 3 < 1) console.log("man\\ bear\\ pig")
69 //else if (Math.random() * 3 < 1)
70 // console.log("porkchop\\ sandwiches\nporkman")
71 //else console.log("encephylophagy")
73 // get the partial line and partial word,
74 // if the point isn't at the end.
75 // ie, tabbing at: npm foo b|ar
76 var w = +process.env.COMP_CWORD
77 , words = args.map(unescape)
79 , line = process.env.COMP_LINE
80 , point = +process.env.COMP_POINT
81 , partialLine = line.substr(0, point)
82 , partialWords = words.slice(0, w)
84 // figure out where in that last word the point is.
85 var partialWord = args[w]
86 , i = partialWord.length
87 while (partialWord.substr(0, i) !== partialLine.substr(-1*i) && i > 0) {
90 partialWord = unescape(partialWord.substr(0, i))
91 partialWords.push(partialWord)
93 var opts = { words : words
97 , lineLength : line.length
99 , partialLine : partialLine
100 , partialWords : partialWords
101 , partialWord : partialWord
105 cb = wrapCb(cb, opts)
109 if (partialWords.slice(0, -1).indexOf("--") === -1) {
110 if (word.charAt(0) === "-") return configCompl(opts, cb)
112 && words[w - 1].charAt(0) === "-"
113 && !isFlag(words[w - 1])) {
114 // awaiting a value for a non-bool config.
115 // don't even try to do this for now
116 console.error("configValueCompl")
117 return configValueCompl(opts, cb)
121 // try to find the npm command.
122 // it's the first thing after all the configs.
123 // take a little shortcut and use npm's arg parsing logic.
124 // don't have to worry about the last arg being implicitly
125 // boolean'ed, since the last block will catch that.
126 var parsed = opts.conf =
127 nopt(configTypes, shorthands, partialWords.slice(0, -1), 0)
128 // check if there's a command already.
129 console.error(parsed)
130 var cmd = parsed.argv.remain[1]
131 if (!cmd) return cmdCompl(opts, cb)
133 Object.keys(parsed).forEach(function (k) {
134 npm.config.set(k, parsed[k])
137 // at this point, if words[1] is some kind of npm command,
138 // then complete on it.
139 // otherwise, do nothing
140 cmd = npm.commands[cmd]
141 if (cmd && cmd.completion) return cmd.completion(opts, cb)
147 function dumpScript (cb) {
148 var fs = require("graceful-fs")
149 , path = require("path")
150 , p = path.resolve(__dirname, "utils/completion.sh")
152 // The Darwin patch below results in callbacks first for the write and then
153 // for the error handler, so make sure we only call our callback once.
156 fs.readFile(p, "utf8", function (er, d) {
157 if (er) return cb(er)
158 d = d.replace(/^\#\!.*?\n/, "")
160 process.stdout.write(d, function () { cb() })
161 process.stdout.on("error", function (er) {
162 // Darwin is a pain sometimes.
164 // This is necessary because the "source" or "." program in
165 // bash on OS X closes its file argument before reading
166 // from it, meaning that you get exactly 1 write, which will
167 // work most of the time, and will always raise an EPIPE.
169 // Really, one should not be tossing away EPIPE errors, or any
170 // errors, so casually. But, without this, `. <(npm completion)`
171 // can never ever work on OS X.
172 if (er.errno === "EPIPE") er = null
179 function unescape (w) {
180 if (w.charAt(0) === "\"") return w.replace(/^"|"$/g, "")
181 else return w.replace(/\\ /g, " ")
184 function escape (w) {
185 if (!w.match(/\s+/)) return w
186 return "\"" + w + "\""
189 // The command should respond with an array. Loop over that,
190 // wrapping quotes around any that have spaces, and writing
191 // them to stdout. Use console.log, not the outfd config.
192 // If any of the items are arrays, then join them with a space.
193 // Ie, returning ["a", "b c", ["d", "e"]] would allow it to expand
194 // to: "a", "b c", or "d" "e"
195 function wrapCb (cb, opts) { return function (er, compls) {
196 if (!Array.isArray(compls)) compls = compls ? [compls] : []
197 compls = compls.map(function (c) {
198 if (Array.isArray(c)) c = c.map(escape).join(" ")
202 if (opts.partialWord) compls = compls.filter(function (c) {
203 return c.indexOf(opts.partialWord) === 0
205 console.error([er && er.stack, compls, opts.partialWord])
206 if (er || compls.length === 0) return cb(er)
208 console.log(compls.join("\n"))
212 // the current word has a dash. Return the config names,
213 // with the same number of dashes as the current word has.
214 function configCompl (opts, cb) {
216 , split = word.match(/^(-+)((?:no-)*)(.*)$/)
219 , flags = configNames.filter(isFlag)
222 return cb(null, allConfs.map(function (c) {
224 }).concat(flags.map(function (f) {
225 return dashes + (no || "no-") + f
229 // expand with the valid values of various config values.
230 // not yet implemented.
231 function configValueCompl (opts, cb) {
232 console.error("configValue", opts)
236 // check if the thing is a flag or not.
237 function isFlag (word) {
238 // shorthands never take args.
239 var split = word.match(/^(-*)((?:no-)+)?(.*)$/)
242 return no || configTypes[conf] === Boolean || shorthands[conf]
245 // complete against the npm commands
246 function cmdCompl (opts, cb) {
247 return cb(null, npm.fullList)