forked from mantoni/eslint_d.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
119 lines (97 loc) · 2.58 KB
/
cli.js
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
/**
* Created using eslint/lib/cli.js as example
*/
/*
* The CLI object should *not* call process.exit() directly. It should only return
* exit codes. This allows other programs to use the CLI object and still control
* when the program exits.
*/
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const options = require('./options');
const launcher = require('./launcher');
const client = require('./client');
//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
/**
* Encapsulates all CLI behavior for eslint. Makes it easier to test as well as
* for other Node.js programs to effectively run the CLI.
*/
const cli = {
/**
* Executes the CLI based on an array of arguments that is passed in.
* @param {string|Array|Object} args The arguments to process.
* @param {string} [text] The text to lint (used for TTY).
* @returns {int} The exit code for the operation.
*/
execute(args, text) {
let currentOptions;
try {
currentOptions = options.parse(args);
}
catch (error) {
console.error(error.message);
return 1;
}
// const files = currentOptions._;
if (currentOptions.version) {
this.version();
}
else if (currentOptions.help) {
this.help();
}
else if (currentOptions.start) {
this.startServer();
}
else if (currentOptions.stop) {
client.stop();
}
else if (currentOptions.restart) {
this.restart();
}
else if (currentOptions.status) {
client.status();
}
else {
const commandArgs = args.slice(2);
if (text) {
return this.lintStdIn(text, commandArgs);
}
else {
return this.lint(commandArgs);
}
}
return 0;
},
startServer() {
launcher();
},
restart() {
const self = this;
client.stop(() => {
process.nextTick(() => this.startServer());
});
},
version() {
console.log('v%s (eslint_d v%s)',
require('eslint/package.json').version,
require('../package.json').version);
},
help() {
console.info(options.generateHelp());
},
lint(commandArgs) {
return client.lint(commandArgs);
},
/**
* Lints the passed text
* @param {string} text
* @param {Array<string>} commandArgs
*/
lintStdIn(text, commandArgs) {
return client.lint(commandArgs, text);
}
};
module.exports = cli;