zhangnaisong
2023-08-05 24d66c8d82b628a06e93dbb1abfea2049b3d45ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Generated by CoffeeScript 1.12.7
 
/*
 * Omelette Simple Auto Completion for Node
 */
 
(function() {
  var EventEmitter, Omelette, depthOf, fs, os, path,
    hasProp = {}.hasOwnProperty,
    extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
    slice = [].slice;
 
  EventEmitter = require("events").EventEmitter;
 
  path = require("path");
 
  fs = require("fs");
 
  os = require("os");
 
  depthOf = function(object) {
    var depth, key, level;
    level = 1;
    for (key in object) {
      if (!hasProp.call(object, key)) continue;
      if (typeof object[key] === 'object') {
        depth = depthOf(object[key]) + 1;
        level = Math.max(depth, level);
      }
    }
    return level;
  };
 
  Omelette = (function(superClass) {
    var log;
 
    extend(Omelette, superClass);
 
    log = console.log;
 
    function Omelette() {
      var isFish, isZsh, ref, ref1;
      this.compgen = process.argv.indexOf("--compgen");
      this.install = process.argv.indexOf("--completion") > -1;
      this.installFish = process.argv.indexOf("--completion-fish") > -1;
      isZsh = process.argv.indexOf("--compzsh") > -1;
      isFish = process.argv.indexOf("--compfish") > -1;
      this.isDebug = process.argv.indexOf("--debug") > -1;
      this.fragment = parseInt(process.argv[this.compgen + 1]) - (isZsh ? 1 : 0);
      this.line = process.argv[this.compgen + 3];
      this.word = (ref = this.line) != null ? ref.trim().split(/\s+/).pop() : void 0;
      ref1 = process.env, this.HOME = ref1.HOME, this.SHELL = ref1.SHELL;
    }
 
    Omelette.prototype.setProgram = function(programs) {
      programs = programs.split('|');
      this.program = programs[0];
      return this.programs = programs.map(function(program) {
        return program.replace(/[^A-Za-z0-9\.\_\-]/g, '');
      });
    };
 
    Omelette.prototype.setFragments = function() {
      var fragments1;
      fragments1 = 1 <= arguments.length ? slice.call(arguments, 0) : [];
      this.fragments = fragments1;
    };
 
    Omelette.prototype.generate = function() {
      var data;
      data = {
        before: this.word,
        fragment: this.fragment,
        line: this.line,
        reply: this.reply
      };
      this.emit("complete", this.fragments[this.fragment - 1], data);
      this.emit(this.fragments[this.fragment - 1], data);
      this.emit("$" + this.fragment, data);
      return process.exit();
    };
 
    Omelette.prototype.reply = function(words) {
      if (words == null) {
        words = [];
      }
      console.log(typeof words.join === "function" ? words.join(os.EOL) : void 0);
      return process.exit();
    };
 
    Omelette.prototype.tree = function(objectTree) {
      var depth, i, level, ref;
      if (objectTree == null) {
        objectTree = {};
      }
      depth = depthOf(objectTree);
      for (level = i = 1, ref = depth; 1 <= ref ? i <= ref : i >= ref; level = 1 <= ref ? ++i : --i) {
        this.on("$" + level, function(arg) {
          var accessor, fragment, line, replies, reply;
          fragment = arg.fragment, reply = arg.reply, line = arg.line;
          accessor = new Function('_', "return _['" + (line.split(/\s+/).slice(1).filter(Boolean).join("']['")) + "']");
          replies = fragment === 1 ? Object.keys(objectTree) : accessor(objectTree);
          return reply((function(replies) {
            if (replies instanceof Function) {
              return replies();
            }
            if (replies instanceof Array) {
              return replies;
            }
            if (replies instanceof Object) {
              return Object.keys(replies);
            }
          })(replies));
        });
      }
      return this;
    };
 
    Omelette.prototype.generateCompletionCode = function() {
      var completions;
      completions = this.programs.map((function(_this) {
        return function(program) {
          var completion;
          completion = "_" + program + "_completion";
          return "### " + program + " completion - begin. generated by omelette.js ###\nif type compdef &>/dev/null; then\n  " + completion + "() {\n    compadd -- `" + _this.program + " --compzsh --compgen \"${CURRENT}\" \"${words[CURRENT-1]}\" \"${BUFFER}\"`\n  }\n  compdef " + completion + " " + program + "\nelif type complete &>/dev/null; then\n  " + completion + "() {\n    local cur prev nb_colon\n    _get_comp_words_by_ref -n : cur prev\n    nb_colon=$(grep -o \":\" <<< \"$COMP_LINE\" | wc -l)\n\n    COMPREPLY=( $(compgen -W '$(" + _this.program + " --compbash --compgen \"$((COMP_CWORD - (nb_colon * 2)))\" \"$prev\" \"${COMP_LINE}\")' -- \"$cur\") )\n\n    __ltrim_colon_completions \"$cur\"\n  }\n  complete -F " + completion + " " + program + "\nfi\n### " + program + " completion - end ###";
        };
      })(this));
      if (this.isDebug) {
        completions.push(this.generateTestAliases());
      }
      return completions.join(os.EOL);
    };
 
    Omelette.prototype.generateCompletionCodeFish = function() {
      var completions;
      completions = this.programs.map((function(_this) {
        return function(program) {
          var completion;
          completion = "_" + program + "_completion";
          return "### " + program + " completion - begin. generated by omelette.js ###\nfunction " + completion + "\n  " + _this.program + " --compfish --compgen (count (commandline -poc)) (commandline -pt) (commandline -pb)\nend\ncomplete -f -c " + program + " -a '(" + completion + ")'\n### " + program + " completion - end ###";
        };
      })(this));
      if (this.isDebug) {
        completions.push(this.generateTestAliases());
      }
      return completions.join(os.EOL);
    };
 
    Omelette.prototype.generateTestAliases = function() {
      var debugAliases, debugUnaliases, fullPath;
      fullPath = path.join(process.cwd(), this.program);
      debugAliases = this.programs.map(function(program) {
        return "  alias " + program + "=" + fullPath;
      }).join(os.EOL);
      debugUnaliases = this.programs.map(function(program) {
        return "  unalias " + program;
      }).join(os.EOL);
      return "### test method ###\nomelette-debug-" + this.program + "() {\n" + debugAliases + "\n}\nomelette-nodebug-" + this.program + "() {\n" + debugUnaliases + "\n}\n### tests ###";
    };
 
    Omelette.prototype.checkInstall = function() {
      if (this.install) {
        log(this.generateCompletionCode());
        process.exit();
      }
      if (this.installFish) {
        log(this.generateCompletionCodeFish());
        return process.exit();
      }
    };
 
    Omelette.prototype.getActiveShell = function() {
      var SHELL;
      SHELL = process.env.SHELL;
      if (SHELL.match(/bash/)) {
        return 'bash';
      } else if (SHELL.match(/zsh/)) {
        return 'zsh';
      } else if (SHELL.match(/fish/)) {
        return 'fish';
      }
    };
 
    Omelette.prototype.getDefaultShellInitFile = function() {
      var fileAt, fileAtHome;
      fileAt = function(root) {
        return function(file) {
          return path.join(root, file);
        };
      };
      fileAtHome = fileAt(this.HOME);
      switch (this.shell = this.getActiveShell()) {
        case 'bash':
          return fileAtHome('.bash_profile');
        case 'zsh':
          return fileAtHome('.zshrc');
        case 'fish':
          return fileAtHome('.config/fish/config.fish');
      }
    };
 
    Omelette.prototype.setupShellInitFile = function(initFile) {
      var completionPath, programFolder, template;
      if (initFile == null) {
        initFile = this.getDefaultShellInitFile();
      }
      template = (function(_this) {
        return function(command) {
          return "\n# begin " + _this.program + " completion\n" + command + "\n# end " + _this.program + " completion\n";
        };
      })(this);
      switch (this.shell) {
        case 'bash':
          programFolder = path.join(this.HOME, "." + this.program);
          completionPath = path.join(programFolder, 'completion.sh');
          if (!fs.existsSync(programFolder)) {
            fs.mkdirSync(programFolder);
          }
          fs.writeFileSync(completionPath, this.generateCompletionCode());
          fs.appendFileSync(initFile, template("source " + completionPath));
          break;
        case 'zsh':
          fs.appendFileSync(initFile, template(". <(" + this.program + " --completion)"));
          break;
        case 'fish':
          fs.appendFileSync(initFile, template(this.program + " --completion-fish | source"));
      }
      return process.exit();
    };
 
    Omelette.prototype.init = function() {
      if (this.compgen > -1) {
        return this.generate();
      }
    };
 
    return Omelette;
 
  })(EventEmitter);
 
  module.exports = function() {
    var _omelette, args, callback, callbacks, fn, fragment, fragments, i, index, len, program, ref, ref1, template;
    template = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
    if (template instanceof Array && args.length > 0) {
      ref = [template[0].trim(), args], program = ref[0], callbacks = ref[1];
      fragments = callbacks.map(function(callback, index) {
        return "arg" + index;
      });
    } else {
      ref1 = template.split(/\s+/), program = ref1[0], fragments = 2 <= ref1.length ? slice.call(ref1, 1) : [];
      callbacks = [];
    }
    fragments = fragments.map(function(fragment) {
      return fragment.replace(/^\<+|\>+$/g, '');
    });
    _omelette = new Omelette;
    _omelette.setProgram(program);
    _omelette.setFragments.apply(_omelette, fragments);
    _omelette.checkInstall();
    fn = function(callback) {
      return _omelette.on(fragment, function() {
        var args;
        args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
        return this.reply(callback instanceof Array ? callback : callback.apply(null, args));
      });
    };
    for (index = i = 0, len = callbacks.length; i < len; index = ++i) {
      callback = callbacks[index];
      fragment = "arg" + index;
      fn(callback);
    }
    return _omelette;
  };
 
}).call(this);