UNPKG

@knapsack/app

Version:

Build Design Systems with Knapsack

138 lines 7.09 kB
"use strict"; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _Db_blocksDir, _Db_demosDir, _Db_dataDir, _Db_dbData; Object.defineProperty(exports, "__esModule", { value: true }); exports.Db = void 0; const types_1 = require("@knapsack/types"); const path_1 = require("path"); const file_utils_1 = require("@knapsack/file-utils"); const ks_file_utils_1 = require("@knapsack/ks-file-utils"); const immer_1 = require("immer"); const file_db_1 = require("./server/dbs/file-db"); const log_1 = require("./cli/log"); class Db extends file_db_1.FileDb { constructor({ data }) { super({ filePath: (0, path_1.join)(data, 'db.yml'), type: 'yml', writeFileIfAbsent: true, orderAlphabetically: true, defaults: { // @ts-expect-error - `blocks.byId` is missing from `db.yml` but not from `KsAppClientData['db']` – we'll add it when we read all the files blocks: { settings: {}, }, // @ts-expect-error - same as above demos: { settings: {}, }, // @ts-expect-error - same as above tabs: { settings: {}, }, // @ts-expect-error - same as above blockCollections: { settings: {}, }, settings: {}, }, }); _Db_blocksDir.set(this, void 0); _Db_demosDir.set(this, void 0); _Db_dataDir.set(this, void 0); _Db_dbData.set(this, void 0); this.init = async (opt) => { await Promise.all([ super.init(opt), (0, file_utils_1.ensureDir)(__classPrivateFieldGet(this, _Db_blocksDir, "f")), (0, file_utils_1.ensureDir)(__classPrivateFieldGet(this, _Db_demosDir, "f")), ]); // doing it this way instead of `await` so it doesn't block the `init` lifecycle super.getData().then((db) => { if ((0, types_1.isObject)(db.blocks.byId)) { log_1.log.error(`db.yml should not have "blocks.byId" - it looks like you need to run 'npx @knapsack/update@latest' to migrate your data`); process.exit(1); } }); }; /** * Ran only on `knapsack serve`. Passed in full contents of App Client Data, which was created as json file in cache dir from `knapsack build`. */ this.hydrate = async ({ appClientData }) => { __classPrivateFieldSet(this, _Db_dbData, appClientData.db, "f"); }; this.getData = async () => { const db = await (0, ks_file_utils_1.readDb)({ dataDir: __classPrivateFieldGet(this, _Db_dataDir, "f"), blocksSubDir: __classPrivateFieldGet(this, _Db_blocksDir, "f"), demosSubDir: __classPrivateFieldGet(this, _Db_demosDir, "f"), }); __classPrivateFieldSet(this, _Db_dbData, db, "f"); return db; }; this.savePrep = async (db) => { // we'll remove IDs from these sets as we go const blockIdsToDelete = new Set(Object.keys(__classPrivateFieldGet(this, _Db_dbData, "f").blocks.byId)); const demoIdsToDelete = new Set(Object.keys(__classPrivateFieldGet(this, _Db_dbData, "f").demos.byId)); const modifiedFiles = [ ...Object.values(db.blocks.byId).map((block) => { blockIdsToDelete.delete(block.id); return { path: (0, path_1.join)(__classPrivateFieldGet(this, _Db_blocksDir, "f"), `block.${block.id}.json`), contents: JSON.stringify(block, null, 2), encoding: 'utf8', }; }), ...Object.values(db.demos.byId).map((demo) => { demoIdsToDelete.delete(demo.id); return { path: (0, path_1.join)(__classPrivateFieldGet(this, _Db_demosDir, "f"), `demo.${demo.id}.json`), contents: JSON.stringify(demo, null, 2), encoding: 'utf8', }; }), ]; // Now that we've removed all the IDs that exist in the new data, we can // delete the rest - which is done by having a `KnapsackFile` object with // `isDeleted: true` const deletedFiles = [ ...Array.from(blockIdsToDelete).map((id) => ({ path: (0, path_1.join)(__classPrivateFieldGet(this, _Db_blocksDir, "f"), `block.${id}.json`), isDeleted: true, contents: '', encoding: 'utf8', })), ...Array.from(demoIdsToDelete).map((id) => ({ path: (0, path_1.join)(__classPrivateFieldGet(this, _Db_demosDir, "f"), `demo.${id}.json`), isDeleted: true, contents: '', encoding: 'utf8', })), ]; // removing the objects that come from multiple JSON files const cleanedDb = (0, immer_1.produce)(db, (dbDraft) => { delete dbDraft.blocks.byId; delete dbDraft.demos.byId; }); // it'll be an array of one const dbFiles = await super.savePrep(cleanedDb); return [...dbFiles, ...modifiedFiles, ...deletedFiles]; }; __classPrivateFieldSet(this, _Db_dataDir, data, "f"); __classPrivateFieldSet(this, _Db_blocksDir, (0, path_1.join)(data, 'blocks'), "f"); __classPrivateFieldSet(this, _Db_demosDir, (0, path_1.join)(data, 'demos'), "f"); } } exports.Db = Db; _Db_blocksDir = new WeakMap(), _Db_demosDir = new WeakMap(), _Db_dataDir = new WeakMap(), _Db_dbData = new WeakMap(); //# sourceMappingURL=db.js.map