UNPKG

@jorsek/ezd-client

Version:
74 lines 3.81 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = require("fs"); const path_1 = require("path"); const rimraf = require("rimraf"); const _1 = require("."); const core = new _1.Core(Object.assign({ org: process.env.EZD_ORG, token: process.env.EZD_TOKEN, rootMapId: process.env.ROOT_MAP_ID }, (process.env.EZD_SCHEME ? { scheme: process.env.EZD_SCHEME } : {}), (process.env.EZD_HOSTNAME ? { hostname: process.env.EZD_HOSTNAME } : {}), (process.env.EZD_ENV ? { env: process.env.EZD_ENV } : {}))); const content_serializer = () => __awaiter(this, void 0, void 0, function* () { const content = yield content_generator(); const rootPath = path_1.resolve(".", "ezd_content"); try { rimraf.sync(rootPath); } catch (e) { console.error(e); } fs_1.mkdirSync(rootPath); fs_1.writeFileSync(path_1.resolve(rootPath, "homepage.json"), JSON.stringify(content.homepage, null, 4)); fs_1.writeFileSync(path_1.resolve(rootPath, "sections.json"), JSON.stringify(content.sections, null, 4)); for (const [route, pageData] of Object.entries(content.pages)) { const components = route.split("/"); const filename = `${components.slice(-1)}.json`; const folderPath = path_1.join("pages", ...components.slice(0, -1)); fs_1.mkdirSync(path_1.resolve(rootPath, folderPath), { recursive: true }); fs_1.writeFileSync(path_1.resolve(rootPath, folderPath, filename), JSON.stringify(pageData, null, 4)); console.log(`Wrote ${filename} for: ${pageData.title}`); } }); const content_generator = () => __awaiter(this, void 0, void 0, function* () { const homepageData = yield core.getHomePageData(); const export_data = { homepage: homepageData, pages: {}, sections: [], }; const sections = homepageData.sections; const sectionTrees = yield Promise.all(sections.map((section) => __awaiter(this, void 0, void 0, function* () { return (yield core.client.content.getNavTree(section.href)); }))); /* name: string; route: string; id: string; children: Array<INavTree>; */ const process_page = (tree, section) => __awaiter(this, void 0, void 0, function* () { try { const nodeContent = yield core.client.content.getContent(tree.href); console.log(`Loaded page: ${tree.title} at ${tree.href}`); export_data.pages[tree.href] = nodeContent; return Promise.all(tree.children.map(child => process_page(child, section))); } catch (e) { console.log(`Failed to load page: ${tree.title} at ${tree.href}: ${e}`); } }); // await Promise.all(reduced_paths.map(tree => process_page(tree, tree.href, null))); for (let id = 0; id < sections.length; id++) { const section = sections[id]; const tree = sectionTrees[id]; yield process_page(tree, section.href); export_data.sections.push({ section, tree }); export_data.pages[section.href] = yield core.client.content.getContent(section.href); } return export_data; }); content_serializer(); //# sourceMappingURL=StaticExporter.js.map