-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli.ts
More file actions
148 lines (131 loc) · 4.15 KB
/
Copy pathcli.ts
File metadata and controls
148 lines (131 loc) · 4.15 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import prompts from "prompts"
import fs from "fs"
import path from "path"
import { generateKeylayout } from "./generator/generateKeylayout"
import { generateMacBundle } from "./generator/generateMacBundle"
import { generateKlc } from "./generator/generateKlc"
import { generateXkb } from "./generator/generateXkb"
import { generateKcm } from "./generator/generateKcm"
import { generateChr_background } from "./generator/generateChr_background"
import { generateChr_manifest } from "./generator/generateChr_manifest"
import { fixUnicode } from "./utils"
import yargs from "yargs"
const { argv } = yargs
prompts.override(argv)
const filenames = fs
.readdirSync("input")
.filter((name) => name.endsWith(".json") && !name.endsWith(".json.bak"))
.sort((a, b) => a.localeCompare(b))
const choices = filenames.map((filename) => ({
title: filename,
value: filename,
}))
;(async () => {
const response = await prompts({
type: "select",
name: "input",
message: "Pick input JSON file",
choices,
})
const jsonInput = JSON.parse(
fs.readFileSync(path.join(process.cwd(), "input", response.input), "utf8"),
)
let jsonName: string
let layoutName: string
let safeLayoutName: string
let dir: string
try {
jsonName = response.input.split(".").slice(0, -1).join(".")
layoutName = jsonInput.name
// Replace spaces with underscores for safe use in IDs or filenames for some system
safeLayoutName = layoutName.replace(/\s+/g, "_").replace(/\./g, "_")
dir = `output/${jsonName}`
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
} catch (e) {
console.error(e)
process.exit(1)
}
// Keylayout
try {
const keylayoutXml = await generateKeylayout(jsonInput)
const bundleDir = `${dir}/${layoutName}.bundle`
const contentsDir = `${bundleDir}/Contents`
const resourceDir = `${contentsDir}/Resources`
const lprojDir = `${resourceDir}/th.lproj`
if (!fs.existsSync(lprojDir)) {
fs.mkdirSync(lprojDir, { recursive: true })
}
const outputFilename = `${resourceDir}/${layoutName}.keylayout`
fs.writeFileSync(outputFilename, keylayoutXml, "utf-8")
fixUnicode(outputFilename)
console.log(`Output : ${outputFilename}`)
const iconName = jsonInput.icon ? `icon_${jsonInput.icon}` : "icon"
fs.copyFileSync(
`src/iconMac/${iconName}.icns`,
path.join(resourceDir, `${layoutName}.icns`),
)
const files = await generateMacBundle(jsonInput)
for (const [filename, content] of Object.entries(files)) {
const filePath = path.join(contentsDir, filename)
fs.writeFileSync(filePath, content, "utf-8")
}
} catch (e) {
console.error(e)
process.exit(1)
}
// Klc
try {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
const outputFilename =
`${dir}/` + jsonInput.os.windows.installerName + `.klc`
await generateKlc(jsonInput, outputFilename)
console.log(`Output : ${outputFilename}`)
} catch (e) {
console.error(e)
process.exit(1)
}
// Xkb
try {
const outputFilename = `${dir}/${safeLayoutName}_xkb`
await generateXkb(jsonInput, outputFilename)
console.log(`Output : ${outputFilename}`)
} catch (e) {
console.error(e)
process.exit(1)
}
// Kmc
try {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
const outputFilename = `${dir}/${safeLayoutName}.kcm`
await generateKcm(jsonInput, outputFilename)
console.log(`Output : ${outputFilename}`)
} catch (e) {
console.error(e)
process.exit(1)
}
// Chr___OS
try {
const chrDir = `${dir}/${jsonInput.os.windows.installerName.toLowerCase()}`
if (!fs.existsSync(chrDir)) {
fs.mkdirSync(chrDir)
}
const outputManifest = chrDir + `/manifest.json`
await generateChr_manifest(jsonInput, outputManifest)
console.log(`Output : ${outputManifest}`)
const outputBackground = chrDir + `/background.js`
await generateChr_background(jsonInput, outputBackground)
console.log(`Output : ${outputBackground}`)
fs.readdirSync("src/iconChr").forEach((f) =>
fs.copyFileSync(path.join("src/iconChr", f), path.join(chrDir, f)),
)
} catch (e) {
console.error(e)
process.exit(1)
}
})()