completion.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * Copyright 2013-2022 the PM2 project authors. All rights reserved.
  3. * Use of this source code is governed by a license that
  4. * can be found in the LICENSE file.
  5. */
  6. var fs = require('fs'),
  7. pth = require('path');
  8. // hacked from node-tabtab 0.0.4 https://github.com/mklabs/node-tabtab.git
  9. // Itself based on npm completion by @isaac
  10. exports.complete = function complete(name, completer, cb) {
  11. // cb not there, assume callback is completer and
  12. // the completer is the executable itself
  13. if(!cb) {
  14. cb = completer;
  15. completer = name;
  16. }
  17. var env = parseEnv();
  18. // if not a complete command, return here.
  19. if(!env.complete) return cb();
  20. // if install cmd, add complete script to either ~/.bashrc or ~/.zshrc
  21. if(env.install) return install(name, completer, function(err, state) {
  22. console.log(state || err.message);
  23. if(err) return cb(err);
  24. cb(null, null, state);
  25. });
  26. // if install cmd, add complete script to either ~/.bashrc or ~/.zshrc
  27. if(env.uninstall) return uninstall(name, completer, function(err, state) {
  28. console.log(state || err.message);
  29. if(err) return cb(err);
  30. cb(null, null, state);
  31. });
  32. // if the COMP_* are not in the env, then dump the install script.
  33. if(!env.words || !env.point || !env.line) return script(name, completer, function(err, content) {
  34. if(err) return cb(err);
  35. process.stdout.write(content, function (n) { cb(null, null, content); });
  36. process.stdout.on("error", function (er) {
  37. // Darwin is a real dick sometimes.
  38. //
  39. // This is necessary because the "source" or "." program in
  40. // bash on OS X closes its file argument before reading
  41. // from it, meaning that you get exactly 1 write, which will
  42. // work most of the time, and will always raise an EPIPE.
  43. //
  44. // Really, one should not be tossing away EPIPE errors, or any
  45. // errors, so casually. But, without this, `. <(npm completion)`
  46. // can never ever work on OS X.
  47. // -- isaacs
  48. // https://github.com/isaacs/npm/blob/master/lib/completion.js#L162
  49. if (er.errno === "EPIPE") er = null
  50. cb(er, null, content);
  51. });
  52. cb(null, null, content);
  53. });
  54. var partial = env.line.substr(0, env.point),
  55. last = env.line.split(' ').slice(-1).join(''),
  56. lastPartial = partial.split(' ').slice(-1).join(''),
  57. prev = env.line.split(' ').slice(0, -1).slice(-1)[0];
  58. cb(null, {
  59. line: env.line,
  60. words: env.words,
  61. point: env.point,
  62. partial: partial,
  63. last: last,
  64. prev: prev,
  65. lastPartial: lastPartial
  66. });
  67. };
  68. // simple helper function to know if the script is run
  69. // in the context of a completion command. Also mapping the
  70. // special `<pkgname> completion` cmd.
  71. exports.isComplete = function isComplete() {
  72. var env = parseEnv();
  73. return env.complete || (env.words && env.point && env.line);
  74. };
  75. exports.parseOut = function parseOut(str) {
  76. var shorts = str.match(/\s-\w+/g);
  77. var longs = str.match(/\s--\w+/g);
  78. return {
  79. shorts: shorts.map(trim).map(cleanPrefix),
  80. longs: longs.map(trim).map(cleanPrefix)
  81. };
  82. };
  83. // specific to cake case
  84. exports.parseTasks = function(str, prefix, reg) {
  85. var tasks = str.match(reg || new RegExp('^' + prefix + '\\s[^#]+', 'gm')) || [];
  86. return tasks.map(trim).map(function(s) {
  87. return s.replace(prefix + ' ', '');
  88. });
  89. };
  90. exports.log = function log(arr, o, prefix) {
  91. prefix = prefix || '';
  92. arr = Array.isArray(arr) ? arr : [arr];
  93. arr.filter(abbrev(o)).forEach(function(v) {
  94. console.log(prefix + v);
  95. });
  96. }
  97. function trim (s) {
  98. return s.trim();
  99. }
  100. function cleanPrefix(s) {
  101. return s.replace(/-/g, '');
  102. }
  103. function abbrev(o) { return function(it) {
  104. return new RegExp('^' + o.last.replace(/^--?/g, '')).test(it);
  105. }}
  106. // output the completion.sh script to the console for install instructions.
  107. // This is actually a 'template' where the package name is used to setup
  108. // the completion on the right command, and properly name the bash/zsh functions.
  109. function script(name, completer, cb) {
  110. var p = pth.join(__dirname, 'completion.sh');
  111. fs.readFile(p, 'utf8', function (er, d) {
  112. if (er) return cb(er);
  113. cb(null, d);
  114. });
  115. }
  116. function install(name, completer, cb) {
  117. var markerIn = '###-begin-' + name + '-completion-###',
  118. markerOut = '###-end-' + name + '-completion-###';
  119. var rc, scriptOutput;
  120. readRc(completer, function(err, file) {
  121. if(err) return cb(err);
  122. var part = file.split(markerIn)[1];
  123. if(part) {
  124. return cb(null, ' ✗ ' + completer + ' tab-completion has been already installed. Do nothing.');
  125. }
  126. rc = file;
  127. next();
  128. });
  129. script(name, completer, function(err, file) {
  130. scriptOutput = file;
  131. next();
  132. });
  133. function next() {
  134. if(!rc || !scriptOutput) return;
  135. writeRc(rc + scriptOutput, function(err) {
  136. if(err) return cb(err);
  137. return cb(null, ' ✓ ' + completer + ' tab-completion installed.');
  138. });
  139. }
  140. }
  141. function uninstall(name, completer, cb) {
  142. var markerIn = '\n\n###-begin-' + name + '-completion-###',
  143. markerOut = '###-end-' + name + '-completion-###\n';
  144. readRc(completer, function(err, file) {
  145. if(err) return cb(err);
  146. var part = file.split(markerIn)[1];
  147. if(!part) {
  148. return cb(null, ' ✗ ' + completer + ' tab-completion has been already uninstalled. Do nothing.');
  149. }
  150. part = markerIn + part.split(markerOut)[0] + markerOut;
  151. writeRc(file.replace(part, ''), function(err) {
  152. if(err) return cb(err);
  153. return cb(null, ' ✓ ' + completer + ' tab-completion uninstalled.');
  154. });
  155. });
  156. }
  157. function readRc(completer, cb) {
  158. var file = '.' + process.env.SHELL.match(/\/bin\/(\w+)/)[1] + 'rc',
  159. filepath = pth.join(process.env.HOME, file);
  160. fs.lstat(filepath, function (err, stats) {
  161. if(err) return cb(new Error("No " + file + " file. You'll have to run instead: " + completer + " completion >> ~/" + file));
  162. fs.readFile(filepath, 'utf8', cb);
  163. });
  164. }
  165. function writeRc(content, cb) {
  166. var file = '.' + process.env.SHELL.match(/\/bin\/(\w+)/)[1] + 'rc',
  167. filepath = pth.join(process.env.HOME, file);
  168. fs.lstat(filepath, function (err, stats) {
  169. if(err) return cb(new Error("No " + file + " file. You'll have to run instead: " + completer + " completion >> ~/" + file));
  170. fs.writeFile(filepath, content, cb);
  171. });
  172. }
  173. function installed (marker, completer, cb) {
  174. readRc(completer, function(err, file) {
  175. if(err) return cb(err);
  176. var installed = file.match(marker);
  177. return cb(!!installed);
  178. });
  179. }
  180. function parseEnv() {
  181. var args = process.argv.slice(2),
  182. complete = args[0] === 'completion';
  183. return {
  184. args: args,
  185. complete: complete,
  186. install: complete && args[1] === 'install',
  187. uninstall: complete && args[1] === 'uninstall',
  188. words: +process.env.COMP_CWORD,
  189. point: +process.env.COMP_POINT,
  190. line: process.env.COMP_LINE
  191. }
  192. };