-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
67 lines (56 loc) · 1.89 KB
/
index.ts
File metadata and controls
67 lines (56 loc) · 1.89 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
import * as fs from "fs"
import * as path from "path"
import { log_preset, LoggingFormat } from "./lib/logging"
import parseBuffer from "./lib/bplistParser"
import { parseFile, detectFileType } from "./lib/ParserFactory"
function show_help(): never {
console.log(
"Toneparse - Parse Neural DSP, Logic Pro preset, and binary plist files"
)
console.log(
"Usage: toneparse PRESET_FILE.[xml|patch|cst|plist] -f=[md|json]"
)
console.log(
"-f logging format. Values: md=markdown(default) | json=JSON"
)
process.exit(0)
}
function read_file(file: string) {
try {
const stats = fs.statSync(file)
// .patch is a directory, find the .cst file inside
if (stats.isDirectory() && path.extname(file) === ".patch") {
const cstFile = path.join(file, "#Root.cst")
return fs.readFileSync(cstFile)
}
return fs.readFileSync(file)
} catch (e) {
console.error("Error reading file:", e)
process.exit(1)
}
}
function parse_file(filename: string): Preset {
const fileType = detectFileType(filename)
if (fileType === "plist") {
// if plist just dump it back to user (useful for debugging)
const buffer = read_file(filename)
if (buffer.toString("ascii", 0, 6) != "bplist") {
throw new Error("Invalid Binary Plist file provided")
}
const parsedPlist = parseBuffer(buffer)
const result = parsedPlist[0]
log_preset(result, LoggingFormat.JSON)
process.exit(0)
}
return parseFile(filename)
}
function main() {
if (!process.argv[2] || process.argv[2].includes("-h")) show_help()
let format = LoggingFormat.MARKDOWN
if (process.argv[3] && process.argv[3].includes("json")) {
format = LoggingFormat.JSON
}
const preset = parse_file(process.argv[2])
log_preset(preset, format)
}
main()