UNPKG

mongoose-management

Version:
74 lines (73 loc) 3.17 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = require("path"); const util_1 = require("util"); const prettier_1 = require("prettier"); const groups_1 = __importDefault(require("./cli/dataset/groups")); const converter_1 = __importDefault(require("./converter")); exports.writeFile = util_1.promisify(fs_1.default.writeFile); exports.readFile = util_1.promisify(fs_1.default.readFile); class Storage { constructor(pathProject, pathData, prompts, prettier) { this.pathProject = pathProject; this.prompts = prompts; this.prettier = prettier; this.path = './schemas-mongodb.json'; this.path = path_1.join(pathData || path_1.join(pathProject, this.path)); this.data = new groups_1.default({ groups: [] }, pathProject); this.data.setReference(); } load() { return __awaiter(this, void 0, void 0, function* () { const spinner = this.prompts.getSpinner(); spinner.start('Database schemata are loaded!'); try { const json = yield exports.readFile(this.path, { encoding: 'utf8' }); const data = JSON.parse(json); converter_1.default(data); this.data = new groups_1.default(data, this.pathProject); this.data.setReference(); } catch (err) { if (err.code !== 'ENOENT') { spinner.fail(); throw err; } } spinner.succeed(); return this.data; }); } write(withPressKey = true) { return __awaiter(this, void 0, void 0, function* () { const spinner = this.prompts.getSpinner(); spinner.start('Database schemata are saved!'); try { const json = JSON.stringify(this.data.getObject()); const pretty = prettier_1.format(json, Object.assign({}, this.prettier, { parser: 'json' })); yield exports.writeFile(this.path, pretty, { encoding: 'utf8' }); } catch (err) { spinner.fail(); throw err; } spinner.succeed(); if (withPressKey) { yield this.prompts.pressKey(); } }); } } exports.default = Storage;