-
Notifications
You must be signed in to change notification settings - Fork 0
/
aq.ts
92 lines (73 loc) · 2.29 KB
/
aq.ts
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
import { parse ,ArgParsingOptions, lines} from "./deps.ts";
import {i18n} from './i18n.ts'
import {IFormat, TOMLFormat, JSONFormat, YAMLFormat} from './Formats/index.ts'
import { filter } from "./filter.ts";
import {color, makeStyle} from "./color.ts"
const arguementParserOptions : ArgParsingOptions = {
unknown: (i) => {
if((i as string)[0] != '-') return true
console.error(i18n('unknownOption', {flag: i}))
console.error(i18n('useHelp'))
Deno.exit(-1)
return false;
},
boolean: [
'compact',
'help',
'json',
'toml',
'yaml',
'color',
],
alias: {
'compact': 'c',
'help': 'h',
'toml': 't',
'yaml': 'y',
'json': 'j',
'color': 'C'
}
}
const argments = parse(Deno.args, arguementParserOptions);
if(argments['help']){
console.error(i18n('helpMessage'))
Deno.exit(0);
}
if(argments["_"].length == 0)
{
console.error(i18n('helpMessage'))
Deno.exit(-4)
}
const userFilter = argments["_"].shift() as string
let inputFiles: Deno.File[] = []
if(argments["_"].length == 0)
inputFiles = [Deno.stdin]
else
inputFiles = argments["_"].map(z => Deno.openSync(z as string, 'r'))
let outputFormat: IFormat = new JSONFormat();
if(argments['json']) outputFormat = new JSONFormat()
else if(argments['toml']) outputFormat = new TOMLFormat()
else if(argments['yaml']) outputFormat = new YAMLFormat()
inputFiles.forEach(async file => {
const file_lines: string[] = [];
for await(const line of lines(file)){
file_lines.push(line)
}
const input = file_lines.join('\n')
let format: IFormat;
var detectedFormat = [new JSONFormat(), new YAMLFormat(), new TOMLFormat() ].find(f => f.IsOfType(input))
if(detectedFormat)
format = detectedFormat
else{
console.error(i18n('formatNotDetected'))
Deno.exit(-2)
}
if(!format.IsOfType(input)) {
console.error(i18n('formatNoMatch', {format: format.constructor.name}))
Deno.exit(-3)
}
var object:any = format.Unmarshal(input)
var filtered:any = filter(object, userFilter)
var marshaled = outputFormat.Marshal(filtered, argments['color'], argments['compact'])
console.log(marshaled)
});