Allow theme config name and path to be configurable
This commit is contained in:
@@ -69,7 +69,9 @@ export default defineConfig({
|
|||||||
Logger: 1,
|
Logger: 1,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
astrowind(),
|
astrowind({
|
||||||
|
config: "./src/config.yaml"
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
|
|
||||||
image: {
|
image: {
|
||||||
|
@@ -1,12 +1,11 @@
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import os from 'node:os';
|
import os from 'node:os';
|
||||||
|
|
||||||
import yaml from 'js-yaml';
|
|
||||||
|
|
||||||
import configBuilder from "./utils/configBuilder"
|
import configBuilder from "./utils/configBuilder"
|
||||||
|
import loadConfig from './utils/loadConfig';
|
||||||
|
|
||||||
const tasksIntegration = () => {
|
const tasksIntegration = ({ config: _themeConfig = 'src/config.yaml' } = {}) => {
|
||||||
let config;
|
let cfg;
|
||||||
return {
|
return {
|
||||||
name: 'AstroWind:tasks',
|
name: 'AstroWind:tasks',
|
||||||
|
|
||||||
@@ -26,8 +25,8 @@ const tasksIntegration = () => {
|
|||||||
const virtualModuleId = 'astrowind:config';
|
const virtualModuleId = 'astrowind:config';
|
||||||
const resolvedVirtualModuleId = '\0' + virtualModuleId;
|
const resolvedVirtualModuleId = '\0' + virtualModuleId;
|
||||||
|
|
||||||
const fileConfig = yaml.load(fs.readFileSync('src/config.yaml', 'utf8'));
|
const rawJsonConfig = await loadConfig(_themeConfig);
|
||||||
const { SITE, I18N, METADATA, APP_BLOG, UI, ANALYTICS } = configBuilder(fileConfig);
|
const { SITE, I18N, METADATA, APP_BLOG, UI, ANALYTICS } = configBuilder(rawJsonConfig);
|
||||||
|
|
||||||
updateConfig({
|
updateConfig({
|
||||||
site: SITE.site,
|
site: SITE.site,
|
||||||
@@ -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 }) => {
|
'astro:config:done': async ({ config }) => {
|
||||||
config = cfg;
|
cfg = config;
|
||||||
},
|
},
|
||||||
|
|
||||||
'astro:build:done': async ({ logger }) => {
|
'astro:build:done': async ({ logger }) => {
|
||||||
@@ -75,21 +78,21 @@ const tasksIntegration = () => {
|
|||||||
buildLogger.info("Updating `robots.txt` with `sitemap-index.xml` ...")
|
buildLogger.info("Updating `robots.txt` with `sitemap-index.xml` ...")
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const outDir = config.outDir;
|
const outDir = cfg.outDir;
|
||||||
const publicDir = config.publicDir;
|
const publicDir = cfg.publicDir;
|
||||||
const sitemapName = 'sitemap-index.xml';
|
const sitemapName = 'sitemap-index.xml';
|
||||||
const sitemapFile = new URL(sitemapName, outDir);
|
const sitemapFile = new URL(sitemapName, outDir);
|
||||||
const robotsTxtFile = new URL('robots.txt', publicDir);
|
const robotsTxtFile = new URL('robots.txt', publicDir);
|
||||||
const robotsTxtFileInOut = new URL('robots.txt', outDir);
|
const robotsTxtFileInOut = new URL('robots.txt', outDir);
|
||||||
|
|
||||||
const hasIntegration =
|
const hasIntegration =
|
||||||
Array.isArray(config?.integrations) &&
|
Array.isArray(cfg?.integrations) &&
|
||||||
config.integrations?.find((e) => e?.name === '@astrojs/sitemap') !== undefined;
|
cfg.integrations?.find((e) => e?.name === '@astrojs/sitemap') !== undefined;
|
||||||
const sitemapExists = fs.existsSync(sitemapFile);
|
const sitemapExists = fs.existsSync(sitemapFile);
|
||||||
|
|
||||||
if (hasIntegration && sitemapExists) {
|
if (hasIntegration && sitemapExists) {
|
||||||
const robotsTxt = fs.readFileSync(robotsTxtFile, { encoding: 'utf8', flags: 'a+' });
|
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;
|
const pattern = /^Sitemap:(.*)$/m;
|
||||||
|
|
||||||
if (!pattern.test(robotsTxt)) {
|
if (!pattern.test(robotsTxt)) {
|
||||||
|
16
src/integration/utils/loadConfig.ts
Normal file
16
src/integration/utils/loadConfig.ts
Normal file
@@ -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;
|
Reference in New Issue
Block a user