-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A moderately type safe config system
- Loading branch information
1 parent
92d7fa0
commit 4fcfafd
Showing
13 changed files
with
154 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
const settingsPath = path.join(import.meta.dirname, "assets", "settings.json"); | ||
const dtsPath = path.join(import.meta.dirname, "src", "configTypes.d.ts"); | ||
|
||
function generateType(settings) { | ||
const lines = []; | ||
lines.push("// This file is auto-generated. Any changes will be lost. See genSettingsTypes.mjs script"); | ||
lines.push("export interface Config {"); | ||
|
||
for (const category in settings) { | ||
const categorySettings = settings[category]; | ||
|
||
for (const settingKey in categorySettings) { | ||
const setting = categorySettings[settingKey]; | ||
let type; | ||
if (setting["outputType"]) { | ||
type = setting["outputType"]; | ||
} else { | ||
type = inferType(setting.inputType); | ||
} | ||
lines.push(` "${settingKey}": ${type};`); | ||
} | ||
} | ||
|
||
lines.push("}"); | ||
lines.push("\nexport type ConfigKey = keyof Config;"); | ||
|
||
return lines.join("\n"); | ||
} | ||
|
||
function inferType(inputType) { | ||
switch (inputType) { | ||
case "checkbox": | ||
return "boolean"; | ||
case "textfield": | ||
case "dropdown": | ||
case "file": | ||
return "string"; | ||
case "textarea": | ||
case "dropdown-multiselect": | ||
return "string[]"; | ||
default: | ||
return "unknown"; | ||
} | ||
} | ||
|
||
export function generateDTSFile() { | ||
const settings = JSON.parse(fs.readFileSync(settingsPath, "utf-8")); | ||
const dtsContent = generateType(settings); | ||
fs.writeFileSync(dtsPath, dtsContent, "utf-8"); | ||
console.log(`Generated settings.d.ts at ${dtsPath}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// This file is auto-generated. Any changes will be lost. See genSettingsTypes.mjs script | ||
export interface Config { | ||
"customTitlebar": boolean; | ||
"minimizeToTray": boolean; | ||
"startMinimized": boolean; | ||
"dynamicIcon": boolean; | ||
"customIconPath": string; | ||
"discordUrl": string; | ||
"modNames": string[]; | ||
"customJsBundle": string; | ||
"customCssBundle": string; | ||
"noBundleUpdates": boolean; | ||
"firewall": boolean; | ||
"customFirewallRules": boolean; | ||
"blocklist": string[]; | ||
"blockedStrings": string[]; | ||
"allowedStrings": string[]; | ||
"installDefaultShelterPlugins": boolean; | ||
"invidiousEmbeds": boolean; | ||
"messageEncryption": boolean; | ||
"encryptionPasswords": string[]; | ||
"encryptionCover": string; | ||
"encryptionMark": string; | ||
"arrpc": boolean; | ||
"scriptLoading": boolean; | ||
"launchWithOsBoot": boolean; | ||
"spellcheck": boolean; | ||
"popoutWindowAlwaysOnTop": boolean; | ||
"updateNotification": boolean; | ||
"autoscroll": boolean; | ||
"transparency": boolean; | ||
"screensharePreviousSettings": [number, number, boolean, string]; | ||
"cloudHost": string; | ||
"cloudEncryptionKey": string; | ||
"cloudToken": string; | ||
"windowState:main": unknown; | ||
} | ||
|
||
export type ConfigKey = keyof Config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters