ConceptCreator/app/SaveProject.js

52 lines
2.2 KiB
JavaScript
Raw Normal View History

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SaveProject = void 0;
const fs = require("fs");
const path = require("node:path");
2024-02-17 08:45:22 +01:00
const ModelComponentType_1 = require("../src/app/game-model/ModelComponentType");
const LoadedProject_1 = require("./LoadedProject");
class SaveProject {
static saveProject(projectDir, storageModels) {
2024-02-17 08:45:22 +01:00
if (!fs.existsSync(projectDir)) {
fs.mkdirSync(projectDir, { recursive: true });
}
storageModels.forEach(storageModel => {
2024-02-17 08:45:22 +01:00
const modelDir = path.join(projectDir, storageModel.storageDir);
if (!fs.existsSync(modelDir)) {
fs.mkdirSync(modelDir, { recursive: true });
}
2024-02-17 08:45:22 +01:00
const filePath = path.join(modelDir, storageModel.fileName + ".json");
fs.writeFile(filePath, storageModel.jsonString, 'utf-8', (err) => {
if (err) {
console.error('Error writing JSON to file:', err);
}
else {
console.log('JSON file saved successfully:', filePath);
}
});
});
}
2024-02-17 08:45:22 +01:00
static loadProject(projectDir) {
const loadedScriptAccounts = SaveProject.loadScriptAccounts(projectDir);
return new LoadedProject_1.LoadedProject(path.basename(projectDir), loadedScriptAccounts);
}
static loadScriptAccounts(projectDir) {
const scriptAccountDir = path.join(projectDir, "script-accounts");
if (!fs.existsSync(scriptAccountDir)) {
return [];
}
const loadedScriptAccounts = [];
const scriptAccountFileNames = fs.readdirSync(scriptAccountDir);
scriptAccountFileNames.forEach(scriptAccountFileName => {
const scriptAccountFile = path.join(scriptAccountDir, scriptAccountFileName);
const scriptAccountData = fs.readFileSync(scriptAccountFile, 'utf-8');
loadedScriptAccounts.push({
modelType: ModelComponentType_1.ModelComponentType.SCRIPTACCOUNT,
jsonString: scriptAccountData
});
});
return loadedScriptAccounts;
}
}
exports.SaveProject = SaveProject;
//# sourceMappingURL=SaveProject.js.map