isaacscript
Version:
A command line tool for managing Isaac mods written in TypeScript.
50 lines • 2.19 kB
JavaScript
import { fatalError, getJSONC, isFile, writeFile } from "complete-node";
import path from "node:path";
import { Config } from "./classes/Config.js";
import { getModsDirectory } from "./commands/init/getModsDirectory.js";
import { getSaveSlot } from "./commands/init/getSaveSlot.js";
import { CONFIG_FILE_NAME, CONFIG_FILE_PATH, CWD } from "./constants.js";
const NUM_INDENT_SPACES = 2;
export async function getConfigFromFile() {
const existingConfig = await getExistingConfig();
if (existingConfig !== undefined) {
return existingConfig;
}
// No config file exists, so prompt the user for some information and create one.
const modsDirectory = await getModsDirectory(undefined);
const saveSlot = await getSaveSlot(undefined, false);
const config = new Config(modsDirectory, saveSlot, false);
createConfigFile(CWD, config);
return config;
}
async function getExistingConfig() {
if (!isFile(CONFIG_FILE_PATH)) {
return undefined;
}
const config = await getJSONC(CONFIG_FILE_PATH);
validateMandatoryConfigFields(config);
return config;
}
/**
* Even though some fields are always initialized in the class, it may not be necessarily exist in
* the JSON file.
*/
function validateMandatoryConfigFields(config) {
if (config["modsDirectory"] === undefined) {
errorMissing("modsDirectory", "This should be equal to the directory where Isaac mods live on your system.");
}
if (config["saveSlot"] === undefined) {
errorMissing("saveSlot", "This should be equal to the save slot that you test your mods on.");
}
}
function errorMissing(field, description) {
fatalError(`The "${CONFIG_FILE_NAME}" file is missing a "${field}" value. ${description} Please add it.`);
}
export function createConfigFile(projectPath, config) {
const configFilePath = path.join(projectPath, CONFIG_FILE_NAME);
const configContents = JSON.stringify(config, undefined, NUM_INDENT_SPACES);
// Add a newline at the end to satisfy Prettier.
const configContentsWithNewline = `${configContents}\n`;
writeFile(configFilePath, configContentsWithNewline);
}
//# sourceMappingURL=configFile.js.map