-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcli.js
executable file
·60 lines (59 loc) · 1.99 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
#! /usr/bin/env node
const cliArgs = require('minimist')(process.argv.slice(
process.argv[0]=="lookml-parser"
?1 //e.g. lookml-parser --bla
:2 //e.g. node cli.js --bla
))
const parser = require("./index.js")
const util = require("util")
var r,repl = cliArgs.interactive && require("repl")
var trace = (cliArgs.trace || cliArgs.t || '').split(",").reduce((idx,x)=>({...idx, [x]:true}),{})
parser.parseFiles({
source: cliArgs.input || cliArgs.i,
conditionalCommentString: cliArgs['conditional-comment'] || cliArgs.c,
fileOutput: cliArgs['file-output'] || cliArgs.f,
console,
trace
}).then((result)=>{
let transformationFlags = cliArgs.transform || cliArgs.x || 's'
if(typeof transformationFlags === 'string'){
for(let flag of transformationFlags.split('')){
const transform = parser.transformations.byCliFlags[flag]
if(!transform){
result.warnings = (result.warnings||[]).concat(`Requested transformation '${flag}' not recognized and ignored.'`)
continue
}
transform(result)
}
}
if(repl){
r = repl.start({writer:x=>
util
.inspect(x,{depth:1,colors:true})
// Truncate long strings
.replace(/: "([^"]{60})[^"]+"/,': "$1..."')
//Collapse some levels
.replace(/(\n\s+([_a-zA-Z$][_0-9a-zA-Z$]*)?:)\s+([_a-zA-Z$][_0-9a-zA-Z$]* {)/g,"$1 $2")
})
Object.assign(r.context,result)
console.info("\x1b[32mSuccess!\x1b[0m Evaluate any of the following"
+"\n\t"
+Object.keys(result||{})
.map(s=>s.match(/error|warning/)?"\x1b[33m"+s+"\x1b[0m":s)
.map(s=> typeof result[s] == "function"
? s+(result[s].toString().match(/\([^)]*\)/)||[""])[0]
: s)
.join(", ")
)
}else{
console.log(JSON.stringify(result, undefined, cliArgs.whitespace))
}
}).catch((result)=>{
if(repl){
r = repl.start()
r.context.error = result
console.info("\x1b[31mError.\x1b[0m Evaluate `error` for details")
}else{
console.error(JSON.stringify(result, undefined, cliArgs.whitespace))
}
})