-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
73 lines (70 loc) · 2.22 KB
/
index.ts
File metadata and controls
73 lines (70 loc) · 2.22 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
#!/usr/bin/env node
import { Error, Uri } from "@cogneco/mend"
import * as dom from "@typeup/dom"
import * as parser from "@typeup/parser"
import * as renderer from "@typeup/renderer"
import * as fs from "fs"
import * as cp from "child_process"
import * as p from "./package.json"
export class Program {
private defaultCommand = "html"
constructor(private commands: string[]) {
this.commands = this.commands.slice(2)
if (this.commands.length == 0) {
this.commands.push(this.defaultCommand)
this.commands.push(".")
}
}
private open(path: string | undefined): dom.Document | undefined {
return parser.open(path, new Error.ConsoleHandler())
}
private async runHelper(command: string | undefined, commands: string[]): Promise<void> {
switch (command) {
case "json":
case "html":
case "pdf":
case "typeup":
const path = this.commands.shift()
const document = this.open(path)
if (!document)
console.log(`Unable to open document "${ path }".`)
else if (!document.region)
console.log(`Document lacks a region "${ path }".`)
else
switch (command) {
case "json":
console.log(document.toJson(" "))
break
case "html":
fs.writeFileSync(document.region.resource.toString().replace(/\.tup$/, ".html"), await renderer.render(document))
break
case "pdf":
fs.writeFileSync(document.region.resource.toString().replace(/\.tup$/, ".pdf"), cp.execFileSync("prince", ["--javascript", "-", "-o", "-"], { input: await renderer.render(document), cwd: (document.region.resource || new Uri.Locator()).folder.toString() }))
break
case "typeup":
console.log(document.toString())
break
}
break
case "version": console.log("TypeUp " + this.getVersion()); break
case "help": console.log("help"); break
default:
if (command)
commands.push(command)
command = undefined
await this.runHelper(this.defaultCommand, commands)
break
}
if (command)
this.defaultCommand = command
}
async run(): Promise<void> {
let command: string | undefined
while (command = this.commands.shift())
await this.runHelper(command, this.commands)
}
getVersion(): string {
return p.version
}
}
new Program(process.argv).run()