forked from potherca-abandoned/liquid-linter-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·214 lines (177 loc) · 7.22 KB
/
index.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
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
#!/usr/bin/env node
'use strict';
const Chalk = require('chalk');
const Commander = require('commander');
const FileHound = require('filehound');
const FileSystem = require('fs');
const Linter = require('@unicorns/liquid-linter');
const Readline = require('readline');
const Util = require('util');
const EXIT_ERROR_NO_PATH_GIVEN = 65;
const EXIT_ERROR_NOT_EXISTS = 66;
const EXIT_ERROR_LINTER = 67;
var g_aFileExtensions = ['md', 'html', 'lqd', 'liquid'];
var g_aInput = [];
function addValueToCollection(p_sValue, p_aCollection) {
p_aCollection.push(p_sValue);
return p_aCollection;
}
function showFinalMessage(p_iErrorCount, p_iWarningCount) {
var sErrorCount, sWarningCount;
sWarningCount = Chalk.yellow('⚠') + ' ' + p_iWarningCount + ' warning' + (p_iWarningCount > 1?'s':'');
sErrorCount = Chalk.red('✘') + ' ' + p_iErrorCount + ' error' + (p_iErrorCount > 1?'s':'');
if (p_iErrorCount > 0 && p_iWarningCount > 0) {
console.error((p_iErrorCount + p_iWarningCount) + ' messages (' + sErrorCount + ', ' + sWarningCount + ')');
} else if (p_iWarningCount > 0) {
console.error(sWarningCount);
} else if (p_iErrorCount > 0) {
console.error(sErrorCount);
} else {
console.info(Chalk.green('✔') + ' no errors');
}
}
/**
* @NOTE: This function is only called by Commander if at least one argument is present
*
* @param p_aPaths Array
*/
function action (p_aPaths) {
var iErrorCount = 0, iWarningCount = 0;
Linter.loadTags({
blocks: Commander.customBlock,
tags: Commander.customTag
});
p_aPaths.forEach(function (p_sPath) {
var aFiles, oFileHound, oStat, sMessage;
if (FileSystem.existsSync(p_sPath) === false) {
process.exitCode = EXIT_ERROR_NOT_EXISTS;
sMessage = Util.format(
' %d:%d %s %s',
0,
0,
Chalk.red('error'),
'No such file or directory'
);
console.error(Chalk.red.underline(p_sPath));
console.error(sMessage);
console.log('');
iErrorCount++;
} else {
oStat = FileSystem.statSync(p_sPath);
if (oStat.isDirectory() === false) {
aFiles = [p_sPath];
} else {
oFileHound = FileHound.create()
.paths(p_sPath)
.ext(g_aFileExtensions)
.ignoreHiddenDirectories()
.ignoreHiddenFiles()
;
aFiles = oFileHound.findSync();
}
aFiles.forEach(function (p_sFile) {
var sMessage;
const sExtension = p_sFile.split('.').pop();
/* @FIXME: Individual hidden files could be given, these still need to be filtered. 2017/03/09/BMP */
/* @NOTE: Individual files could be given that do not adhere to the extensions list */
if (g_aFileExtensions.indexOf(sExtension) === -1) {
sMessage = Util.format('(file extension "%s" does not match one of "%s")',
sExtension,
g_aFileExtensions.join(', ')
);
sMessage = Util.format(
'%s: not scanning %s',
Chalk.green(p_sFile),
Chalk.dim(sMessage)
);
console.info(sMessage);
} else {
Linter.lintFilePromise(p_sFile).then(function(p_aErrors) {
// @FIXME: Use warnings if not errors
// console.error(Chalk.yellow.underline(p_sPath));
// console.error(' ' + Chalk.yellow('warning') + sMessage);
if (p_aErrors.length > 0) {
process.exitCode = EXIT_ERROR_LINTER;
console.error(Chalk.red.underline(p_sFile));
//@TODO: add nice padding to give different lines the same format -> ' '.repeat(4 - (p_oError.location.line + '' +p_oError.location.col).length);
p_aErrors.reverse().forEach(function (p_oError) {
var sMessage = Util.format(
' %d:%d-%d:%d %s %s',
p_oError.location.line,
p_oError.location.col,
p_oError.location.line,
p_oError.location.col + p_oError.location.length,
Chalk.red('error'),
p_oError.message.split('\n')[0]
);
console.error(sMessage);
iErrorCount++;
});
console.log('');
} else {
console.info(Chalk.green(p_sFile) + ': no issues found');
}
});
}
});
}
});
// @TODO: Figure out how to run showFinalMessage as last command
// showFinalMessage(iErrorCount, iWarningCount);
}
/**
* Parses the command-line arguments from STDIN/pipe or argument vector (argv)
*/
function parseArguments() {
var aFileList;
if (g_aInput.length > 0) {
aFileList = g_aInput;
} else {
aFileList = process.argv;
}
Commander.parse(aFileList);
if (g_aInput.length === 0 && (Commander.args && Commander.args.length === 0)) {
process.exitCode = EXIT_ERROR_NO_PATH_GIVEN;
console.error(Chalk.red('Error: ') + Chalk.bold(' no path given'));
Commander.outputHelp();
}
}
/* @NOTE: There's a problem in the linter with {% include %} tags. For now related errors are simply ignored. */
process.on('unhandledRejection', function(error/*, promise*/) {
if (error.name !== 'Liquid.FileSystemError' && error.message === 'This file system doesn\'t allow includes') {
throw error;
}
});
Commander
.version('1.0.2')
.description('Linter for Liquid template files (Unicorn Edition)\n\n**This version is for the Shopify extended version of liquid**')
.arguments('<paths...>')
// @TODO: Figure out how to accept multiple ignore paths
// .option('-x, --exclude <ignore-path...>', 'Paths to ignore')
.option('-b, --custom-block <custom-block>', 'custom blocks to ignore (can be used multiple times)', addValueToCollection, [])
.option('-t, --custom-tag <custom-tag>', 'custom tags to ignore (can be used multiple times)', addValueToCollection, [])
.action(function(p_sInput) {
if(g_aInput.length > 0) {
p_sInput = g_aInput;
}
action(p_sInput)
})
;
if(process.stdin.isTTY) {
parseArguments();
} else {
var readlineInterface = Readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
readlineInterface.on('line', function(p_sLine){
if (p_sLine !== null) {
g_aInput.push(p_sLine);
}
});
readlineInterface.on('close', function(){
parseArguments();
});
}
/*EOF*/