-
Notifications
You must be signed in to change notification settings - Fork 6
/
cli.js
executable file
·118 lines (106 loc) · 3.27 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
#!/usr/bin/env node
var turf = require('turf'),
chalk = require('chalk'),
fs = require('fs'),
getStdin = require('get-stdin'),
defs = require('./definitions.json'),
argv = require('minimist')(process.argv.slice(2), {
boolean: ['h', 'help']
});
function isGeoJsonArgument(type) {
return type.type === 'NameExpression' &&
['FeatureCollection', 'Feature', 'Point', 'GeoJSON', 'Geometry',
'LineString', 'Polygon', 'MultiPolygon', 'MultiPoint']
.indexOf(type.name) !== -1;
}
function isJsonArgument(type) {
return (type.type === 'NameExpression' &&
type.name === 'Object') ||
(type.type === 'TypeApplication' &&
type.expression.name === 'Array');
}
function isBooleanArgument(type) {
return type.type === 'NameExpression' && type.name === 'boolean';
}
function parseArguments(def, argv, stdin) {
if ((argv._.length - 1) !== def.params.length) {
console.log('Definition %s requires %s arguments, given %s',
def.name, def.params.length, argv._.length - 1);
showParams(def.params);
throw new Error('invalid argument length given');
}
var args = [];
def.params.forEach(function(param, i) {
var arg = argv._[i + 1];
if (isGeoJsonArgument(param.type) || isJsonArgument(param.type)) {
args.push(getJsonFromArg(def, arg, stdin));
} else if (isBooleanArgument(param.type)) {
args.push(JSON.parse(arg)); // parses 'false' to false
} else {
args.push(arg);
}
});
return args;
}
var usedStdin = false
function getJsonFromArg(def, arg, stdin) {
if (arg === '-' && usedStdin) {
console.log('STDIN ("-") can only be used once for JSON/GeoJSON input');
showParams(def.params);
throw new Error('stdin used for multiple file arguments');
}
var raw
if (arg === '-') {
raw = stdin
usedStdin = true
} else {
try {
// throws for a nonexistent file
raw = fs.readFileSync(arg)
} catch (e) {
// if `arg` doesn't point to a file, fall back to treating it as literal JSON
raw = arg
}
}
try {
return JSON.parse(raw);
} catch (e) {
console.log("Could not parse JSON: ", raw);
}
}
function showParams(params) {
params.forEach(function(p) {
console.log(' %s\t%s', chalk.bold(p.name), p.description);
});
}
function getDef(name) {
return defs.filter(function(def) {
return def.name === name;
})[0];
}
function help() {
console.log(chalk.green('turf'));
defs.forEach(function(def) {
console.log('');
console.log(' ' + chalk.bold(def.name));
console.log(' ' + def.description);
console.log('');
showParams(def.params);
});
}
(function() {
if (argv.h || argv.help || !argv._.length) {
help();
process.exit(0);
}
var op = argv._[0];
if (!turf[op]) {
help();
throw new Error('turf operation ' + op + ' not found');
}
var def = getDef(argv._[0]);
getStdin().then(function (stdin) {
var args = parseArguments(def, argv, stdin);
console.log(JSON.stringify(turf[op].apply(null, args), null, 2));
});
})();