@archer-edu/dxp-directus-cli
Version:
289 lines • 12.8 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(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 });
exports.getParentPath = void 0;
const api_1 = require("./support/api");
const config_1 = require("./support/config");
const utils_1 = require("./support/utils");
const js_yaml_1 = __importDefault(require("js-yaml"));
const register = (program) => {
program
.command("pull-pages <siteId>")
.alias("p")
.description("Pull content pages from directus into the 'content' directory")
.option("-f, --force", "Force-delete the content directory before pulling")
.option("-d, --debug", "Enable debug mode")
.option("-c, --continue", "Continue on error (lax-mode for previews)")
.option("-i, --download-images <imageDir>", "Download images to a directory relative to the src folders (ie 'assets/images'). If omitted, images use the CDN")
.option("-v, --version <version>", "Version of the blogs to pull")
.option("-t, --tag <tag>", "Version tag of the page to pull")
.action((siteId, options) => __awaiter(void 0, void 0, void 0, function* () {
const { downloadImages, force, debug, continue: cont, tag } = options;
if (debug)
console.log(`Pulling pages from site ${siteId}`);
if (downloadImages && debug)
console.log(`Downloading images to ${downloadImages}`);
yield pullPages({ siteId, downloadImages, force, debug, cont, tag });
}));
};
exports.default = register;
const pullPages = (_a) => __awaiter(void 0, [_a], void 0, function* ({ siteId, downloadImages, force, debug, cont, tag }) {
const imagePath = downloadImages ? (0, utils_1.fixRelativePath)(downloadImages) : null;
const pages = yield getPages(siteId, tag, imagePath, debug, cont);
const writtenFiles = pages.map((page) => writePage(page));
console.log(`Pulled ${pages.length} pages from directus`);
if (force) {
const fileFormat = new RegExp("^.*/_.*.md$");
const deleted = (0, utils_1.deleteFiles)(config_1.contentPath, (f) => !writtenFiles.includes(f) &&
!fileFormat.test(f) &&
!f.includes("blog") &&
!f.includes("resources"));
console.log(`Deleted ${deleted.length} files`);
if (debug) {
console.table(deleted);
}
}
});
const pagesQuery = (siteId, limit, offset) => `{
pages: site_pages (
filter: { site: { key: { _eq: "${siteId}"}}},
sort:["sort"],
limit: ${limit},
offset: ${offset}
) {
id
slug
parent_page {
id
slug
}
}
}`;
const pageVersionQuery = (id, tag) => `{
page: site_pages_by_id(
id: "${id}",
version: "${tag}"
) {
id
title
description
slug
content
sources
json_ld
status
sort
date_created
front_matter
parent_page {
id
slug
}
sections (
sort: ["sort"]
){
type
style
section
content
align
data
source
path
classes
sort
images {
id
}
image {
id
title
type
filename_download
filename_disk
description
}
items (
sort: ["sort"]
){
item
style
align
classes
content
data
sort
image {
id
title
type
filename_download
filename_disk
description
}
}
}
}
}`;
function getPages(siteId_1, tag_1, downloadImages_1) {
return __awaiter(this, arguments, void 0, function* (siteId, tag, downloadImages, debug = false, cont = false) {
let allPages = [];
let offset = 0;
const limit = 100; // Adjust this based on your needs
let hasMore = true;
while (hasMore) {
try {
const { pages = [] } = yield (0, api_1.graphRequest)(config_1.queryPath, pagesQuery(siteId, limit, offset), debug);
allPages = allPages.concat(pages);
if (debug)
console.log(`Fetched ${pages.length} pages, offset ${offset}`);
// If the number of pages fetched is less than the limit, it means we have fetched all pages
if (pages.length < limit)
hasMore = false;
else
offset += limit; // Increase the offset for the next batch
}
catch (e) {
console.error(`An error occurred while fetching pages: ${e.message}`);
if (!cont)
throw e;
hasMore = false;
}
}
// Process each page
return yield Promise.all(allPages.map((item) => __awaiter(this, void 0, void 0, function* () {
try {
// pull each item with the version param
// if there's a version with this tag, it will be pulled
// otherwise, the main version will be pulled
if (debug)
console.log(`Fetching versioned page ${item.id} with tag ${tag}`);
const { page } = yield (0, api_1.graphRequest)(config_1.queryPath, pageVersionQuery(item.id, tag), debug);
const mappedPage = yield mapPage(page, allPages, downloadImages, cont); // Ensure mapPage can handle this logic
if (debug)
console.dir({ slug: page.slug, parent: page.parent_page, path: mappedPage.file_path, original_slug: mappedPage.metadata.slug });
return mappedPage;
}
catch (e) {
console.error(`Error in page ${item.slug}`, e);
if (!cont)
throw e;
return null;
}
}))).then(pages => pages.filter(p => p != null));
});
}
function getParentPath(page, pages) {
if (page.parent_page == null) {
return '';
}
const parent = pages.find((p) => p.id == page.parent_page.id);
if (parent == null) {
return '';
}
const ancestors = getParentPath(parent, pages);
return ancestors + `${parent.slug}`;
}
exports.getParentPath = getParentPath;
const yamlLog = (data, page) => {
return {
onWarning: (e) => {
console.warn(e, {
page,
data
});
}
};
};
function mapPage(item_1, items_1, downloadImages_1) {
return __awaiter(this, arguments, void 0, function* (item, items, downloadImages, cont = false) {
const { id, title, description, slug, json_ld, status, content, date_created: published, front_matter, sources, sections, sort } = item;
const parent_path = getParentPath(item, items);
const has_children = items.some((p) => p.parent_page && id == p.parent_page.id);
const page_name = (slug == "/" || slug == "/index") ? "/_index" : has_children ? `${slug}/_index` : slug;
const file_path = `${config_1.contentPath}${parent_path}${page_name}.md`;
let parsedMetadata = {};
try {
parsedMetadata = (front_matter ? js_yaml_1.default.load((0, utils_1.cleanFrontMatter)(front_matter), yamlLog(front_matter, parent_path + slug)) : { sections: [] });
if (!parsedMetadata.sections)
parsedMetadata.sections = [];
if ((sections === null || sections === void 0 ? void 0 : sections.length) > 0) {
const mappedSections = yield Promise.all(sections.map((section) => __awaiter(this, void 0, void 0, function* () {
var _a;
try {
const sectionData = (section === null || section === void 0 ? void 0 : section.data) ? js_yaml_1.default.load((0, utils_1.cleanFrontMatter)(section.data), yamlLog(section.data, parent_path + slug)) : {};
delete section.data;
return Object.assign(Object.assign(Object.assign({}, section), sectionData), { name: section.name || section.id, images: yield Promise.all(((_a = section.images) === null || _a === void 0 ? void 0 : _a.map((image) => __awaiter(this, void 0, void 0, function* () {
try {
return {
src: yield (0, utils_1.fixImage)(image.id, downloadImages),
alt: image.description || image.title
};
}
catch (e) {
console.error(`Error in image ${image.id} for ${slug} in section ${section.id}`, e);
return image;
}
}))) || []), image: section.image ? Object.assign(Object.assign({}, section.image), { src: yield (0, utils_1.fixImage)(section.image.id, downloadImages), alt: section.image.description || section.image.title }) : null, items: (section === null || section === void 0 ? void 0 : section.items) ? (yield Promise.all((section.items.map((item) => __awaiter(this, void 0, void 0, function* () {
try {
const itemData = (item === null || item === void 0 ? void 0 : item.data) ? js_yaml_1.default.load((0, utils_1.cleanFrontMatter)(item.data), yamlLog(item.data, parent_path + slug)) : {};
delete item.data;
return Object.assign(Object.assign(Object.assign({}, item), itemData), { image: item.image ? Object.assign(Object.assign({}, item.image), { src: yield (0, utils_1.fixImage)(item.image.id, downloadImages), alt: item.image.description || item.image.title }) : null });
}
catch (e) {
console.error(`Error in item ${item.id} for ${slug} in section ${section.id}`, e);
return item;
}
})) || []))).sort((a, b) => (a.sort || 0) - (b.sort || 0)) : [] });
}
catch (e) {
console.error(`Error parsing section ${section.id} for ${slug}`, e);
if (!cont)
throw e;
return section;
}
})));
parsedMetadata.sections = [...mappedSections, ...parsedMetadata.sections]
.sort((a, b) => a.sort - b.sort);
}
}
catch (e) {
console.error(`Error parsing frontmatter for ${slug}`, e);
if (!cont)
throw e;
}
const page = {
metadata: (0, utils_1.cleanObject)(Object.assign({ id,
title, description: (0, utils_1.stripNewLines)(description), json_ld,
// truncate published date to drop time info
published: status == 'published' ? published.slice(0, 'YYYY-MM-DD'.length) : undefined, slug: slug.split('/').slice(-1)[0] || 'index', draft: status == 'draft' || undefined, weight: sort }, parsedMetadata)),
sources,
file_path,
content: yield (0, utils_1.fixImages)(content, downloadImages),
};
return page;
});
}
function writePage(page) {
var _a;
const { file_path, content, metadata, sources } = page;
const renderedSources = (sources === null || sources === void 0 ? void 0 : sources.map(utils_1.renderSource).join('\n')) || '';
if (!((_a = metadata.sections) === null || _a === void 0 ? void 0 : _a.length))
delete metadata.sections;
return (0, utils_1.writeFile)(file_path, `---\n${js_yaml_1.default.dump(metadata, {
skipInvalid: true,
lineWidth: -1,
})}\n---\n${content}${renderedSources}`.trim());
}
//# sourceMappingURL=pull-pages.js.map