-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
84 lines (78 loc) · 2.49 KB
/
index.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
#!/usr/bin/env node
import yargs from "yargs";
import chalk from "chalk";
import * as path from "path";
import * as fs from "fs/promises";
import { hideBin } from "yargs/helpers";
import YAML from "yaml";
import { xml2js } from "xml-js";
import { cleanYaml, processAction } from "./functions.js";
import { confirm } from "@inquirer/prompts";
const argv = yargs(hideBin(process.argv))
.command("$0 <xml> [output]", "Migrate XML to YAML", (yargs) =>
yargs
.option("force", {
alias: "f",
describe: "Force overwrite of the generated file",
boolean: true,
})
.option("redundancy", {
alias: "r",
describe: "Bypass default layer checks and place all maps in the hidden layer, no matter what",
boolean: true,
})
.positional("xml", {
describe: "Path of the XML to convert",
normalize: true,
})
.positional("output", {
describe: "Directory path where the yaml file willl be generated",
default: ".",
normalize: true,
})
.demandOption("xml")
)
.help()
.version(false).argv;
const invalidXML = "Invalid XML layout file";
const output = path.resolve(argv.output, `${path.parse(argv.xml).name}.yaml`);
async function checkOutput() {
try {
const stat = await fs.stat(output);
if (!argv.force && stat.isFile()) {
const answer = await confirm({
message: `${output} already exists. Do you want to overwrite it`,
});
if (!answer) process.exit(0);
}
} catch (error) {}
}
try {
await checkOutput();
console.log(chalk.yellow(`Migrating ${argv.xml} to ${output}`));
const yaml = { layers: {} };
const xml = await fs.readFile(argv.xml, "utf8");
const result = xml2js(xml, { ignoreComment: true });
if (
!result.elements.length ||
result.elements[0].type !== "element" ||
result.elements[0].name !== "keyboardData"
)
throw new Error(invalidXML);
const actionMap = result.elements[0].elements.find(
({ name }) => name === "keyboardActionMap"
);
if (!actionMap) throw new Error(invalidXML);
actionMap.elements
.filter(({ name, type }) => name === "keyboardAction" && type === "element")
.forEach(({ elements }) => {
if (elements) processAction(elements, yaml, argv.redundancy);
});
cleanYaml(yaml);
await fs.writeFile(output, YAML.stringify(yaml));
console.log(chalk.green("Migration successful"));
} catch (e) {
console.error(chalk.bold.red(e));
console.error(chalk.bold.red(e.stack));
process.exit(1);
}