UNPKG

@knapsack/app

Version:

Build Design Systems with Knapsack

85 lines (83 loc) 3.35 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomPages = void 0; /** * Copyright (C) 2018 Basalt This file is part of Knapsack. Knapsack is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Knapsack is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Knapsack; if not, see <https://www.gnu.org/licenses>. */ const globby_1 = __importDefault(require("globby")); const path_1 = require("path"); const file_db_1 = require("../../server/dbs/file-db"); const log_1 = require("../../cli/log"); const createFileName = (id) => `knapsack.custom-page.${id}.yml`; class CustomPages { dataDir; constructor({ dataDir }) { this.dataDir = dataDir; } getData = async () => { const data = { pages: {}, }; await (0, globby_1.default)((0, path_1.join)(this.dataDir, createFileName('*'))).then(async (configFilePaths) => { if (!configFilePaths) return; return Promise.all(configFilePaths.map(async (configFilePath) => { const [, id] = configFilePath.split('.').reverse(); const db = new file_db_1.FileDb({ filePath: configFilePath, type: 'yml', writeFileIfAbsent: false, }); const config = await db.getData(); if (id === config.id) { data.pages[config.id] = config; } else { log_1.log.warn(`id mismatch ${id} in ${(0, path_1.relative)(process.cwd(), configFilePath)} : filename does not match id, will delete`); } })); }); return data; }; savePrep = async (data) => { const ogData = await this.getData(); const idsToDelete = new Set(Object.keys(ogData.pages)); const allFiles = []; await Promise.all(Object.keys(data.pages).map(async (id) => { idsToDelete.delete(id); const page = data.pages[id]; const db = new file_db_1.FileDb({ filePath: (0, path_1.join)(this.dataDir, createFileName(id)), type: 'yml', writeFileIfAbsent: false, }); const files = await db.savePrep(page); files.forEach((file) => allFiles.push(file)); })); idsToDelete.forEach((id) => { allFiles.push({ isDeleted: true, contents: '', encoding: 'utf8', path: (0, path_1.join)(this.dataDir, createFileName(id)), }); }); return allFiles; }; } exports.CustomPages = CustomPages; //# sourceMappingURL=custom-pages.js.map