-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·192 lines (164 loc) · 5.72 KB
/
cli.js
File metadata and controls
executable file
·192 lines (164 loc) · 5.72 KB
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
#!/usr/bin/env node
var chalk = require('chalk'),
cjson = require('cjson'),
Liftoff = require('liftoff'),
fs = require('fs'),
glob = require('glob'),
path = require('path'),
Promise = require('bluebird');
var readFilePromise = Promise.promisify(fs.readFile),
globPromise = Promise.promisify(glob);
var app = new Liftoff({
processTitle: 'htmllint',
moduleName: 'htmllint',
configName: '.htmllint',
extensions: {
'rc': null
}
});
var argv = require('yargs')
.usage([
'Lints html files with htmllint.',
'Usage: $0 [OPTIONS] [ARGS]'
].join('\n'))
.example('$0', 'lints all html files in the cwd and all child directories')
.example('$0 init', 'creates a default .htmllintrc in the cwd')
.example('$0 *.html', 'lints all html files in the cwd')
.example('$0 public/*.html', 'lints all html files in the public directory')
.default('rc', null)
.describe('rc', 'path to a htmllintrc file to use (json)')
.option('format', {
describe: 'format to output',
choices: ['simple', 'json'],
default: 'simple'
})
.default('cwd', null)
.describe('cwd', 'path to use for the current working directory')
.default('stdin', false)
.describe('stdin', 'accept input from stdin (using option value for filename in output)')
.argv;
var STDIN_FILENO = 0;
var args = argv._;
app.launch({
cwd: argv.cwd,
configPath: argv.rc
}, function (env) {
var cwd = argv.cwd || process.cwd();
var htmllintPath = 'htmllint';
if (env.modulePath) {
var cliPackage = require('../package.json'),
semver = require('semver');
var acceptedRange = cliPackage.dependencies.htmllint,
localVersion = env.modulePackage.version;
if (semver.satisfies(localVersion, acceptedRange)) {
htmllintPath = env.modulePath;
} else {
console.log(
chalk.red('local htmllint version is not supported:'),
chalk.magenta(localVersion, '!=', acceptedRange)
);
console.log('using builtin version of htmllint');
}
}
var htmllint = require(htmllintPath);
var formatters = require('../lib/formatters');
if (args[0] === 'init') {
// copy .htmllintrc file
var srcPath = path.join(__dirname, '../lib/default_cfg.json'),
outputPath = path.join(env.cwd, '.htmllintrc');
var opts = htmllint.Linter.getOptions('default'),
config = JSON.stringify(opts, null, 4);
config = '{\n "plugins": [], // npm modules to load\n'
+ config.slice(1);
fs.writeFile(outputPath, config, function (err) {
if (err) {
console.error('error writing config file: ', err);
}
});
return;
}
var cfg;
if (env.configPath) {
cfg = cjson.load(env.configPath);
}else{
cfg = cjson.load('package.json').htmllint;
}
if (!cfg) {
console.log(
chalk.red('No configuration found (local .htmllintrc file not found and no htmllint config in package.json)'),
'(you can create one using "htmllint init")'
);
process.exit(1);
}
htmllint.use(cfg.plugins || []);
delete cfg.plugins;
if (argv.stdin) {
args.unshift(STDIN_FILENO);
}
if (!args.length) {
args = ['**/*.html'];
}
function lintFile(filename) {
var p;
if (filename === STDIN_FILENO) {
filename = argv.stdin === true ? 'stdin' : argv.stdin;
p = new Promise(function (resolve, reject) {
process.stdin.resume();
process.stdin.setEncoding('utf8');
var content = '';
process.stdin.on('data', function (chunk) {
content += chunk;
});
process.stdin.on('end', function () {
resolve(content);
});
process.stdin.on('error', function (err) {
reject(err);
});
});
} else {
var filepath = path.resolve(cwd, filename);
p = readFilePromise(filepath, 'utf8');
}
return p.then(function (src) {
return htmllint(src, cfg);
})
.then(function (issues) {
formatters.formatMessage.call(htmllint, argv.format, filename, issues);
return { errorCount: issues.length };
})
.catch(function (err) {
// MC: muahahahahah :D
throw ('[htmllint error in ' + filename + ' ] ' + err);
});
}
Promise.all(
args.map(function (pattern) {
if (pattern === STDIN_FILENO) {
return STDIN_FILENO;
}
return globPromise(pattern, { cwd: cwd });
})
).then(function (filesArr) {
var files = Array.prototype.concat.apply([], filesArr);
return Promise.settle(
files.map(lintFile)
);
}, function (err) {
console.error(chalk.red.bold('error during glob expansion:'), err);
}).done(function (results) {
var errorCount = 0;
results.forEach(function (result) {
if (result.isFulfilled()) {
var resultValue = result.value();
errorCount += resultValue.errorCount;
} else {
console.error(chalk.bold.red(result.reason()));
}
});
formatters.done.call(htmllint, argv.format, errorCount, results.length);
if (errorCount > 0) {
process.exit(1);
}
});
});