UNPKG

1.87 kBPlain TextView Raw
1#!/usr/bin/env node
2
3var fs = require('fs'),
4 path = require('path'),
5 geojsonhint = require('../'),
6 VFile = require('vfile'),
7 vfileReporter = require('vfile-reporter'),
8 concat = require('concat-stream'),
9 argv = require('minimist')(process.argv.slice(2));
10
11if (process.stdin.isTTY && !argv._[0]) {
12 process.stdout.write(fs.readFileSync(path.join(__dirname, '/HELP.md')));
13 process.exit(1);
14}
15
16var filename;
17if (argv._[0]) {
18 filename = argv._[0];
19}
20
21(filename ? fs.createReadStream(filename) : process.stdin).pipe(concat(hint));
22
23function hint(input) {
24
25 var options = {
26 noDuplicateMembers: argv.noDuplicateMembers !== 'false'
27 };
28
29 var errors = geojsonhint.hint(input.toString(), options);
30
31 if (!errors || !errors.length) {
32 process.exit(0);
33 }
34
35 var format = argv.f || argv.format || 'pretty';
36
37 if (format === 'json') {
38 if (errors instanceof Error) {
39 console.log(JSON.stringify(errors.toString(), null, 2));
40 } else {
41 console.log(JSON.stringify(errors, null, 2));
42 }
43 } else if (format === 'pretty') {
44 var file = new VFile({
45 contents: input,
46 filename: filename
47 });
48 file.quiet = true;
49 errors.forEach(function(e) {
50 var pos = { line: e.line };
51 var fn = (e.level === 'message') ? 'message' : 'fail';
52 try {
53 file[fn](e.message, pos)
54 } catch(e) { }
55 });
56 console.log(vfileReporter(file));
57 } else if (format === 'compact') {
58 var filePart = filename ? filename + ': ' : '';
59 errors.forEach(function(e) {
60 var level = e.level ? e.level : "error";
61 console.log(filePart + 'line ' + e.line + ' - ' + e.message + " - " + level);
62 });
63 } else {
64 throw new Error('Format unknown');
65 }
66}