UNPKG

@mc-resource/cli

Version:

a Resource Manager for Minecraft

122 lines (121 loc) 4.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConcreteConfig = exports.Concrete = void 0; const fs_1 = __importDefault(require("fs")); class Concrete { constructor() { if (this.checkForConcreteFile()) { this.loadConcreteFile(); } this.manifest = Object.assign(new ConcreteConfig(), this.manifest); } checkForConcreteFile() { if (fs_1.default.existsSync(`${process.cwd()}/concrete.json`)) { return true; } return false; } loadConcreteFile() { return (this.manifest = JSON.parse(fs_1.default.readFileSync(`${process.cwd()}/concrete.json`, 'utf8'))); } // TODO: we need to get rid of modrinth classes and types here // and put them on its own registry folder addResource(project, version, filename) { var _a, _b; this.loadConcreteFile(); this.removeUnresolvedResourcesFiles(); if (this.checkForResource(project)) { return this; } (_b = (_a = this.manifest) === null || _a === void 0 ? void 0 : _a.dependencies) === null || _b === void 0 ? void 0 : _b.push(project); this.save(); return this; } checkForResource(project) { var _a; this.loadConcreteFile(); this.removeUnresolvedResourcesFiles(); if ((_a = this.manifest) === null || _a === void 0 ? void 0 : _a.dependencies) { if (typeof project === 'object' && 'name' in project) { for (const resource of this.manifest.dependencies) { if (resource.name == project.name) { return resource; } } } else { for (const resource of this.manifest.dependencies) { if (resource.name == project) { return resource; } } } } return undefined; } checkForResourceVersion(version) { var _a, _b, _c, _d, _e; this.loadConcreteFile(); this.removeUnresolvedResourcesFiles(); if ((_a = this.manifest) === null || _a === void 0 ? void 0 : _a.dependencies) { if (typeof version === 'object' && 'name' in version) { for (const resource of (_b = this.manifest) === null || _b === void 0 ? void 0 : _b.dependencies) { if (((_c = resource.version) === null || _c === void 0 ? void 0 : _c.name) == version.name) { return resource; } } } else { for (const resource of (_d = this.manifest) === null || _d === void 0 ? void 0 : _d.dependencies) { if (((_e = resource.version) === null || _e === void 0 ? void 0 : _e.name) == version) { return resource; } } } } } removeUnresolvedResourcesFiles() { var _a, _b, _c; if ((_a = this.manifest) === null || _a === void 0 ? void 0 : _a.dependencies) { for (const resource of (_b = this.manifest) === null || _b === void 0 ? void 0 : _b.dependencies) { if (!fs_1.default.existsSync(`${process.cwd()}/${resource.type}s/${(_c = resource.version) === null || _c === void 0 ? void 0 : _c.filename}`)) { this.removeResource(resource.name || resource); } } } } removeResource(project) { var _a, _b; const concrete = this.loadConcreteFile(); if (typeof project === 'object' && 'name' in project) { concrete.dependencies = (_a = concrete.dependencies) === null || _a === void 0 ? void 0 : _a.filter((resource) => { return resource.name !== project.name; }); } else { concrete.dependencies = (_b = concrete.dependencies) === null || _b === void 0 ? void 0 : _b.filter((resource) => { return resource.name !== project; }); } this.save(); this.removeUnresolvedResourcesFiles(); return this; } save() { this.checkForConcreteFile(); const concrete = this.manifest; if (concrete && 'dependencies' in concrete) this.writeToConcreteFile(concrete); } writeToConcreteFile(config) { this.checkForConcreteFile(); fs_1.default.writeFileSync(`${process.cwd()}/concrete.json`, JSON.stringify(config, null, 2)); } } exports.Concrete = Concrete; class ConcreteConfig { } exports.ConcreteConfig = ConcreteConfig;