1 | 'use strict'
|
2 |
|
3 | const glob = require('glob')
|
4 |
|
5 | const CUSTOM = ['']
|
6 | const BOOLEAN = false
|
7 |
|
8 | const 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 |
|
37 | function 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 |
|
45 | function 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 |
|
54 | function 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 |
|
66 | function 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 |
|
84 | function 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 |
|
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 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | if (error.errno === 'EPIPE') {
|
114 | error = null
|
115 | }
|
116 | })
|
117 | })
|
118 | }
|
119 |
|
120 |
|
121 | exports.completion = completion
|
122 |
|
123 |
|
124 | exports.opositeWord = opositeWord
|
125 | exports.sendCompletion = sendCompletion
|
126 | exports.complete = complete
|