UNPKG

@knapsack/app

Version:

Build Design Systems on top of knapsack, by Basalt

95 lines (82 loc) 2.83 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomPages = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _globby = _interopRequireDefault(require("globby")); var _path = require("path"); var _fileDb = require("./dbs/file-db"); /** * 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>. */ class CustomPages { constructor({ dataDir }) { (0, _defineProperty2.default)(this, "dataDir", void 0); this.dataDir = dataDir; } async getData() { const data = { pages: {} }; await (0, _globby.default)((0, _path.join)(this.dataDir, 'knapsack.custom-page.*.yml')).then(async configFilePaths => { if (!configFilePaths) return; return Promise.all(configFilePaths.map(async configFilePath => { const db = new _fileDb.FileDb2({ filePath: configFilePath, type: 'yml', watch: false, writeFileIfAbsent: false }); const config = await db.getData(); data.pages[config.id] = config; return { db, config: await db.getData() }; })); }); return data; } async savePrep(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 _fileDb.FileDb2({ filePath: (0, _path.join)(this.dataDir, `knapsack.custom-page.${id}.yml`), type: 'yml', watch: false, 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.join)(this.dataDir, 'knapsack.custom-page.*.yml') }); }); return allFiles; } } exports.CustomPages = CustomPages;