-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
166 lines (154 loc) · 5.31 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
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
'use strict';
var fs = require('fs');
var ffs = require('final-fs');
var path = require('path');
var cli = require('commander');
const colors=require('colors');
colors.disable(); // apb
var XQLint = require('../xqlint').XQLint;
var XQueryLexer = require('../lexers/xquery_lexer').XQueryLexer;
var ANSIOutput = require('./outputs/ansi').ANSIOutput;
var CodeFormatter = require('../formatter/formatter').CodeFormatter;
var pkg = require('../../package.json');
var getFiles = function(p){
p = path.resolve(path.normalize(p));
var files = [];
if(fs.statSync(p).isFile()){
files.push(p);
} else {
var list = ffs.readdirRecursiveSync(p, true, p);
list.forEach((file) => {
if (['.xq', '.xqm'].indexOf(path.extname(file)) !== -1) {
files.push(file);
}
});
}
return files;
};
var formatCmd = cli.command('format <path>');
formatCmd.description('Format queries')
.action(function(p){
var files = getFiles(p);
files.forEach(function(file){
var source = fs.readFileSync(file, 'utf-8');
var linter = new XQLint( source, { styleCheck: false });
var formatter = new CodeFormatter(linter.getAST());
var formatted = formatter.format();
console.log(formatted.trim());
});
});
var highlightCmd = cli.command('highlight <path>');
/* highlightCmd.option(
'-o, --output <html, ansi>',
function(value){
return value.toLowerCase();
}
) */
highlightCmd
.description('Highlight queries. ANSI terminal o/p')
.action(function(p){
var files = getFiles(p);
files.forEach(function(file){
var source = fs.readFileSync(file, 'utf-8');
var lines = source.split('\n');
var lexer = new XQueryLexer();
var result = [], tokens, state;
lines.forEach(function(line){
tokens = lexer.getLineTokens(line, state);
state = tokens.state;
result.push(tokens);
});
ANSIOutput(result);
});
});
var astCmd = cli.command('ast <path>');
astCmd
.description('Print AST (abstract syntax tree) as XML')
.action(function(p){
var files = getFiles(p);
files.forEach(function(file){
var source = fs.readFileSync(file, 'utf-8');
var linter = new XQLint(source, { fileName: file, styleCheck: false });
console.log(linter.printAST());
});
});
var xqdocCmd = cli.command('xqdoc <path>');
xqdocCmd
.description('Print Module XQDoc. A custom json format, not the XML format.')
.action(function(p){
var files = getFiles(p);
files.forEach(function(file){
var source = fs.readFileSync(file, 'utf-8');
var linter = new XQLint(source, { fileName: file, styleCheck: false });
var xqdoc = linter.getXQDoc();
console.log(JSON.stringify(xqdoc, null, 2));
});
});
var lintCmd = cli.command('lint <path>');
lintCmd.option(
'-e, --emacs',
'Format for Gnu/Emacs flymake.'
)
.option(
'-s, --style-check <yes, no>',
'Check for code formatting.',
function(value){
return value === 'yes';
}
)
.option(
'-p, --processor <name>',
'XQuery Processor (e.g. basex-10.6)',
function(value) {
return value.toLowerCase();
}
)
.description('Check queries')
.action(function(p) {
var files = getFiles(p);
var errors = 0;
var warnings = 0;
var spaces = function(count){
var result = '';
for(var i = 1; i <= count; i++){
result += ' ';
}
return result;
};
files.forEach(function(file){
var source = fs.readFileSync(file, 'utf-8').replace(/^\uFEFF/, '');
var linter = new XQLint(source, { fileName: file, styleCheck: lintCmd.styleCheck, processor: lintCmd.processor });
var markers = linter.getMarkers().sort(function(a, b){ return a.sl - b.sl; });
var lines = source.split('\n');
if(markers.length !== 0) {
if (!lintCmd.emacs) {
console.log(('\n' + file).bold);
linter.getErrors().forEach(function(error){
errors++;
console.log('\t' + (error.pos.sl + 1) + ' |' + (lines[error.pos.sl].grey));
console.log('\t' + spaces((error.pos.sl + 1 + '').length + 1) + spaces(error.pos.sc + 1) + ('^ ' + error.message).red);
});
linter.getWarnings().forEach(function(error){
warnings++;
console.log('\t' + (error.pos.sl + 1) + ' |' + (lines[error.pos.sl].grey));
console.log('\t' + spaces((error.pos.sl + 1 + '').length + 1) + spaces(error.pos.sc + 1) + ('^ ' + error.message).yellow);
});
} else { // --emacs
markers.forEach(function(marker){
console.log(marker.type.toUpperCase() + ': ' + file + ':' + (marker.pos.sl + 1) + ':' + (marker.pos.sc) + ':' + marker.type + ' ' + marker.message);
});
}
}
});
if (!lintCmd.emacs) {
if(errors === 0 && warnings === 0) {
console.log(('Linted ' + files.length + ' files').green);
} else if(errors > 0) {
throw new Error(('Linted ' + files.length + ' files. Found ' + errors + ' errors and ' + warnings + ' warnings.').red);
} else if(warnings > 0) {
throw new Error(('Linted ' + files.length + ' files. Found ' + errors + ' errors and ' + warnings + ' warnings.').yellow);
}
}
});
cli.version(pkg.version);
module.exports = cli;