69 lines
2.2 KiB
JavaScript
69 lines
2.2 KiB
JavaScript
import fs from 'node:fs';
|
|
const templates = JSON.parse(fs.readFileSync('./templates.json'));
|
|
|
|
/**
|
|
* Checks whether a specific directory is a valid MyFactory Addon
|
|
* @param {*} dir
|
|
*/
|
|
export async function isMyFactoryAddon(dir) {
|
|
try {
|
|
// Check if directory exists
|
|
await fs.access(directoryPath, fs.constants.F_OK);
|
|
|
|
// Check for "myfactory.xml" file
|
|
const fileStats = await fs.stat(`${directoryPath}/myfactory.xml`);
|
|
if (!fileStats.isFile()) {
|
|
throw new Error(`"myfactory.xml" is not a file.`);
|
|
}
|
|
|
|
// Check for "Install" directory
|
|
await fs.access(`${directoryPath}/Install`, fs.constants.F_OK);
|
|
|
|
// Check for nested directories "DB" and "Files" inside "Install"
|
|
const installDirStats = await fs.stat(`${directoryPath}/Install`);
|
|
if (!installDirStats.isDirectory()) {
|
|
throw new Error(`"Install" is not a directory.`);
|
|
}
|
|
|
|
const dbDirStats = await fs.stat(`${directoryPath}/Install/DB`);
|
|
if (!dbDirStats.isDirectory()) {
|
|
throw new Error(`"DB" directory is missing inside "Install".`);
|
|
}
|
|
|
|
const filesDirStats = await fs.stat(`${directoryPath}/Install/Files`);
|
|
if (!filesDirStats.isDirectory()) {
|
|
throw new Error(`"Files" directory is missing inside "Install".`);
|
|
}
|
|
|
|
console.log("Directory structure is valid!");
|
|
} catch (error) {
|
|
console.error("Error verifying directory structure:", error.message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* If a directory does not exist, create it.
|
|
* @param {string} path Path to directory
|
|
*/
|
|
export async function ensure_directory(path) {
|
|
if (!fs.existsSync(path)) {
|
|
fs.mkdirSync(path);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fill out and return a template string from ./templates.json
|
|
* @param {string} template The field name of the template
|
|
* @param {object<any>} data Data to fill out
|
|
*/
|
|
export function template(template, data) {
|
|
/** @type {string} */
|
|
var templateText = templates[template];
|
|
if(!templateText) throw new Error("Unknown template");
|
|
|
|
Object.keys(data).forEach(key => {
|
|
templateText = templateText.replaceAll(`{${key}}`, data[key]);
|
|
});
|
|
|
|
return templateText;
|
|
} |