source: trip-planner-front/node_modules/karma/lib/completion.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.4 KB
Line 
1'use strict'
2
3const glob = require('glob')
4
5const CUSTOM = ['']
6const BOOLEAN = false
7
8const options = {
9 start: {
10 '--port': CUSTOM,
11 '--auto-watch': BOOLEAN,
12 '--no-auto-watch': BOOLEAN,
13 '--log-level': ['disable', 'debug', 'info', 'warn', 'error'],
14 '--colors': BOOLEAN,
15 '--no-colors': BOOLEAN,
16 '--reporters': ['dots', 'progress'],
17 '--no-reporters': BOOLEAN,
18 '--browsers': ['Chrome', 'ChromeHeadless', 'ChromeCanary', 'Firefox', 'PhantomJS', 'Safari', 'Opera'],
19 '--no-browsers': BOOLEAN,
20 '--single-run': BOOLEAN,
21 '--no-single-run': BOOLEAN,
22 '--help': BOOLEAN
23 },
24 init: {
25 '--log-level': ['disable', 'debug', 'info', 'warn', 'error'],
26 '--colors': BOOLEAN,
27 '--no-colors': BOOLEAN,
28 '--help': BOOLEAN
29 },
30 run: {
31 '--no-refresh': BOOLEAN,
32 '--port': CUSTOM,
33 '--help': BOOLEAN
34 }
35}
36
37function opositeWord (word) {
38 if (word.startsWith('-')) {
39 return word.startsWith('--no-') ? `--${word.substr(5)}` : `--no-${word.substr(2)}`
40 } else {
41 return null
42 }
43}
44
45function sendCompletion (possibleWords, env) {
46 const regexp = new RegExp('^' + env.last)
47 possibleWords
48 .filter((word) => regexp.test(word) && !env.words.includes(word) && !env.words.includes(opositeWord(word)))
49 .forEach((word) => {
50 console.log(word)
51 })
52}
53
54function sendCompletionFiles (env) {
55 glob(env.last + '*', { mark: true, nocase: true }, (err, files) => {
56 if (err) return console.error(err)
57
58 if (files.length === 1 && files[0].endsWith('/')) {
59 sendCompletionFiles({ last: files[0] })
60 } else {
61 console.log(files.join('\n'))
62 }
63 })
64}
65
66function complete (env) {
67 if (env.count === 1) {
68 return sendCompletion(env.words[0].startsWith('-') ? ['--help', '--version'] : Object.keys(options), env)
69 } else if (env.count === 2 && !env.words[1].startsWith('-')) {
70 return sendCompletionFiles(env)
71 }
72
73 const cmdOptions = options[env.words[0]]
74
75 if (cmdOptions) {
76 if (cmdOptions[env.prev] === CUSTOM && env.last) {
77 console.log(env.last)
78 } else {
79 return sendCompletion(cmdOptions[env.prev] || Object.keys(cmdOptions), env)
80 }
81 }
82}
83
84function completion () {
85 if (process.argv[3] === '--') {
86 return complete({
87 words: process.argv.slice(5),
88 count: parseInt(process.env.COMP_CWORD, 10),
89 last: process.argv[process.argv.length - 1],
90 prev: process.argv[process.argv.length - 2]
91 })
92 }
93
94 // just print out the karma-completion.sh
95 const fs = require('graceful-fs')
96 const path = require('path')
97
98 fs.readFile(path.resolve(__dirname, '../scripts/karma-completion.sh'), 'utf8', function (err, data) {
99 if (err) return console.error(err)
100
101 process.stdout.write(data)
102 process.stdout.on('error', function (error) {
103 // Darwin is a real dick sometimes.
104 //
105 // This is necessary because the "source" or "." program in
106 // bash on OS X closes its file argument before reading
107 // from it, meaning that you get exactly 1 write, which will
108 // work most of the time, and will always raise an EPIPE.
109 //
110 // Really, one should not be tossing away EPIPE errors, or any
111 // errors, so casually. But, without this, `. <(karma completion)`
112 // can never ever work on OS X.
113 if (error.errno === 'EPIPE') {
114 error = null
115 }
116 })
117 })
118}
119
120// PUBLIC API
121exports.completion = completion
122
123// for testing
124exports.opositeWord = opositeWord
125exports.sendCompletion = sendCompletion
126exports.complete = complete
Note: See TracBrowser for help on using the repository browser.