Skip to content

Commit

Permalink
Allow theme config name and path to be configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
prototypa committed Apr 12, 2024
1 parent 40abd5b commit 4dff61a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
4 changes: 3 additions & 1 deletion astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export default defineConfig({
Logger: 1,
}),

astrowind(),
astrowind({
config: "./src/config.yaml"
}),
],

image: {
Expand Down
33 changes: 18 additions & 15 deletions src/integration/index.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import fs from 'node:fs';
import os from 'node:os';

import yaml from 'js-yaml';

import configBuilder from "./utils/configBuilder"
import loadConfig from './utils/loadConfig';

const tasksIntegration = () => {
let config;
const tasksIntegration = ({ config: _themeConfig = 'src/config.yaml' } = {}) => {
let cfg;
return {
name: 'AstroWind:tasks',

Expand All @@ -26,8 +25,8 @@ const tasksIntegration = () => {
const virtualModuleId = 'astrowind:config';
const resolvedVirtualModuleId = '\0' + virtualModuleId;

const fileConfig = yaml.load(fs.readFileSync('src/config.yaml', 'utf8'));
const { SITE, I18N, METADATA, APP_BLOG, UI, ANALYTICS } = configBuilder(fileConfig);
const rawJsonConfig = await loadConfig(_themeConfig);
const { SITE, I18N, METADATA, APP_BLOG, UI, ANALYTICS } = configBuilder(rawJsonConfig);

updateConfig({
site: SITE.site,
Expand Down Expand Up @@ -61,12 +60,16 @@ const tasksIntegration = () => {
},
});

addWatchFile(new URL('./src/config.yaml', config.root));
if (typeof _themeConfig === "string") {
addWatchFile(new URL(_themeConfig, config.root));

buildLogger.info("Astrowind `src/config.yaml` has been loaded.")
buildLogger.info(`Astrowind \`${_themeConfig}\` has been loaded.`)
} else {
buildLogger.info(`Astrowind config has been loaded.`)
}
},
'astro:config:done': async ({ config: cfg }) => {
config = cfg;
'astro:config:done': async ({ config }) => {
cfg = config;
},

'astro:build:done': async ({ logger }) => {
Expand All @@ -75,21 +78,21 @@ const tasksIntegration = () => {
buildLogger.info("Updating `robots.txt` with `sitemap-index.xml` ...")

try {
const outDir = config.outDir;
const publicDir = config.publicDir;
const outDir = cfg.outDir;
const publicDir = cfg.publicDir;
const sitemapName = 'sitemap-index.xml';
const sitemapFile = new URL(sitemapName, outDir);
const robotsTxtFile = new URL('robots.txt', publicDir);
const robotsTxtFileInOut = new URL('robots.txt', outDir);

const hasIntegration =
Array.isArray(config?.integrations) &&
config.integrations?.find((e) => e?.name === '@astrojs/sitemap') !== undefined;
Array.isArray(cfg?.integrations) &&
cfg.integrations?.find((e) => e?.name === '@astrojs/sitemap') !== undefined;
const sitemapExists = fs.existsSync(sitemapFile);

if (hasIntegration && sitemapExists) {
const robotsTxt = fs.readFileSync(robotsTxtFile, { encoding: 'utf8', flags: 'a+' });
const sitemapUrl = new URL(sitemapName, String(new URL(config.base, config.site)));
const sitemapUrl = new URL(sitemapName, String(new URL(cfg.base, cfg.site)));
const pattern = /^Sitemap:(.*)$/m;

if (!pattern.test(robotsTxt)) {
Expand Down
16 changes: 16 additions & 0 deletions src/integration/utils/loadConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import fs from 'node:fs';
import yaml from 'js-yaml';

const loadConfig = async (configPathOrData: string | object) => {
if (typeof configPathOrData === 'string') {
const content = fs.readFileSync(configPathOrData, 'utf8');
if (configPathOrData.endsWith(".yaml") || configPathOrData.endsWith(".yml")) {
return yaml.load(content);
}
return content;
}

return configPathOrData;
};

export default loadConfig;

0 comments on commit 4dff61a

Please sign in to comment.