From be3d83c892342f9a7b68475abc8272c29fbb8c64 Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Mon, 27 Apr 2020 23:34:07 +0530 Subject: [PATCH] :sparkles: Support for creating CNAME --- src/files.ts | 26 ++++++++++++++++---------- src/generator.ts | 35 ++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/files.ts b/src/files.ts index 6a652b7a..c520bd98 100644 --- a/src/files.ts +++ b/src/files.ts @@ -1,4 +1,4 @@ -import { readFile, readdir, mkdirp } from "fs-extra"; +import { readFile, readdir, mkdirp, writeFile } from "fs-extra"; import { join, parse } from "path"; import { getConfig } from "./config"; import recursiveReadDir from "recursive-readdir"; @@ -9,6 +9,12 @@ import { getTitle } from "./parse"; import { getSiteContent } from "./content"; import { filePathtoUrl } from "./helpers"; +export const createCname = async () => { + const config = await getConfig(); + const distPath = await getDistPath(); + if (config.cname) await writeFile(join(distPath, "CNAME"), config.cname); +}; + export const getContentPath = async () => { const config = await getConfig(); const path = config.contentDir @@ -82,7 +88,7 @@ export const readContentFile = async (path: string) => { export const listRootFiles = async () => { const contentPath = await getContentPath(); - let files = (await readdir(contentPath)).map(file => + let files = (await readdir(contentPath)).map((file) => parse(file).ext ? file : `${file}/index.md` ); const result = []; @@ -100,10 +106,10 @@ export const listRootFiles = async () => { export const listDirs = async () => { const contentPath = await getContentPath(); return Array.from( - new Set((await recursiveReadDir(contentPath)).map(f => parse(f).dir)) + new Set((await recursiveReadDir(contentPath)).map((f) => parse(f).dir)) ) - .filter(d => d !== contentPath) - .map(d => d.replace(`${contentPath}/`, "")); + .filter((d) => d !== contentPath) + .map((d) => d.replace(`${contentPath}/`, "")); }; export const listContentFiles = async ( @@ -117,9 +123,9 @@ export const listContentFiles = async ( let files = await recursiveReadDir(contentPath); const config = await getConfig(); if (filterContentFiles) - files = files.filter(name => + files = files.filter((name) => (config.contentFileExt || ["md"]) - .map(ext => `.${ext}`) + .map((ext) => `.${ext}`) .includes(parse(name).ext) ); let finalFiles: string[] = []; @@ -132,7 +138,7 @@ export const listContentFiles = async ( } } if (removeContentPath) - finalFiles = finalFiles.map(file => file.replace(`${contentPath}/`, "")); + finalFiles = finalFiles.map((file) => file.replace(`${contentPath}/`, "")); return finalFiles; }; @@ -158,7 +164,7 @@ export const getTemplatePartsList = async () => { const defaultTemplateFiles = await readdir(defaultTemplatesPartDir); return Array.from( new Set([...templatePartsFiles, ...defaultTemplateFiles]) - ).map(i => i.replace(".html", "")); + ).map((i) => i.replace(".html", "")); }); }; @@ -231,7 +237,7 @@ export const getNextPrevious = async ( ) => { return cached(`next-prev-${type}-${current}`, async () => { const allContent = await getSiteContent(); - const currentFile = allContent.filter(f => f.htmlPath === current); + const currentFile = allContent.filter((f) => f.htmlPath === current); if (!currentFile.length) return ""; if (type === "next") { const next = currentFile[0].next; diff --git a/src/generator.ts b/src/generator.ts index db93ba1c..b615bb6e 100644 --- a/src/generator.ts +++ b/src/generator.ts @@ -5,7 +5,7 @@ import { ensureFile, copyFile, copy, - remove + remove, } from "fs-extra"; import { getDistPath, @@ -19,7 +19,8 @@ import { getScriptPath, getBreadcrumbs, getBreadcrumbsSchema, - getNextPreviousNav + getNextPreviousNav, + createCname, } from "./files"; import { cached } from "./cache"; import { join, parse } from "path"; @@ -30,7 +31,7 @@ import { getNavbar, getSiteMeta, registerPartials, - getCustomCss + getCustomCss, } from "./data"; import { minify } from "html-minifier"; import { removeHeading, getTitle, getTags, getFilesForTag } from "./parse"; @@ -165,11 +166,11 @@ export const generate = async (customConfig?: StaartSiteConfig) => { if (!config.noHome) await generatePage("index.html", await getHomeContent()); if (!config.noSitemap) await generateSitemap(); const files = (await listContentFiles()).filter( - file => !["index.md", "sitemap.md"].includes(file) + (file) => !["index.md", "sitemap.md"].includes(file) ); const allTags = await getTags(); const tags: typeof allTags = {}; - Object.keys(allTags).forEach(tag => { + Object.keys(allTags).forEach((tag) => { tags[tag] = allTags[tag]; }); for await (const key of Object.keys(tags)) { @@ -218,8 +219,8 @@ ${filesList}`; let content = await readContentFile(file); if (parse(file).name === "index" && !config.noContentList) { const deepFiles = (await listContentFiles(join(file, ".."))) - .filter(f => f !== "index.md") - .map(f => join(file, "..", f)); + .filter((f) => f !== "index.md") + .map((f) => join(file, "..", f)); if (deepFiles.length) { content += "\n\n" + (await getNavbar(deepFiles)); } @@ -233,7 +234,7 @@ ${filesList}`; schemaVersion: 1, message: "", label: config.shieldSchemaLabel || "docs", - color: config.shieldSchemaColor || "blueviolet" + color: config.shieldSchemaColor || "blueviolet", }; await ensureDir(join(await getDistPath(), "shield-schema")); const nArticles = (await listContentFiles()).length; @@ -273,6 +274,7 @@ ${filesList}`; await remove(join(await getDistPath(), "@")); } catch (error) {} await copyAssets(); + await createCname(); }; const generateSitemap = async () => { @@ -281,7 +283,7 @@ const generateSitemap = async () => { let content = (await getSitemapContent()) + "\n\n" + (await getNavbar(files)); await generatePage("sitemap.html", content); const sitemap = new SitemapStream({ - hostname: config.baseUrl || "http://localhost:8080" + hostname: config.baseUrl || "http://localhost:8080", }); for await (const file of files) { let newFile = file; @@ -321,7 +323,7 @@ const generateSitemap = async () => { { collapseWhitespace: true, processScripts: ["application/ld+json"], - minifyCSS: true + minifyCSS: true, } ) ); @@ -347,7 +349,7 @@ const generatePage = async (path: string, content: string) => { ).toLocaleDateString("en-US", { year: "numeric", month: "long", - day: "numeric" + day: "numeric", })}. { metaTitle: path === "index.html" ? (await getSiteMeta("title", "name")) || "Staart Site" - : `${await getTitle(content, false)} · ${(await getSiteMeta( - "title", - "name" - )) || "Staart Site"}`, + : `${await getTitle(content, false)} · ${ + (await getSiteMeta("title", "name")) || "Staart Site" + }`, ...attributes, description: truncate( path === "index.html" @@ -410,7 +411,7 @@ const generatePage = async (path: string, content: string) => { "" ), 200 - ) + ), }; if (process.env.NODE_ENV) { try { @@ -444,7 +445,7 @@ const generatePage = async (path: string, content: string) => { minify(result, { collapseWhitespace: true, processScripts: ["application/ld+json", "text/javascript"], - minifyCSS: true + minifyCSS: true, }).replace("// prettier-ignore ", "") ); };