From 3de7f8a143b7adf24b64cfe134ad270cd2da45ab Mon Sep 17 00:00:00 2001 From: Jonathan Stockdill Date: Sun, 21 Jul 2024 13:08:22 -0400 Subject: [PATCH] Dynamic Sitemap - linting (#1232) --- packages/plugin-adapter-sitemap/src/index.js | 16 ++++++--------- .../cases/build.default/build.default.spec.js | 17 ++++++---------- .../cases/build.default/src/sitemap.xml.js | 20 +++++++++---------- .../develop.default/develop.default.spec.js | 4 ++-- .../cases/develop.default/src/sitemap.xml.js | 19 +++++++++--------- 5 files changed, 32 insertions(+), 44 deletions(-) diff --git a/packages/plugin-adapter-sitemap/src/index.js b/packages/plugin-adapter-sitemap/src/index.js index 5e203d87d..bc45d10e0 100644 --- a/packages/plugin-adapter-sitemap/src/index.js +++ b/packages/plugin-adapter-sitemap/src/index.js @@ -1,6 +1,5 @@ import fs from 'fs/promises'; - async function sitemapAdapter(compilation) { try { const { outputDir, projectDirectory } = compilation.context; @@ -27,7 +26,6 @@ const greenwoodPluginAdapterSitemap = (options = {}) => [{ } }]; - /* * * Sitemap @@ -40,16 +38,14 @@ class SitemapResource extends ResourceInterface { constructor(compilation, options) { super(compilation, options); } - + async shouldServe(url) { - return url.pathname.endsWith('sitemap.xml') + return url.pathname.endsWith('sitemap.xml'); } - + // eslint-disable-next-line no-unused-vars async serve(url) { - //TODO: check if module exists - const { projectDirectory } = this.compilation.context; try { @@ -58,10 +54,10 @@ class SitemapResource extends ResourceInterface { return new Response(sitemap, { headers: { 'Content-Type': 'text/xml' } }); } catch (error) { - console.error('Error loading module: ./sitemap.xml.js', error); - return new Response("Sitemap oops.", { headers: { 'Content-Type': 'text/xml' } }); + console.error('Error loading module: ./sitemap.xml.js Does it exist?', error); + return new Response('Sitemap oops.', { headers: { 'Content-Type': 'text/xml' } }); } - + } } diff --git a/packages/plugin-adapter-sitemap/test/cases/build.default/build.default.spec.js b/packages/plugin-adapter-sitemap/test/cases/build.default/build.default.spec.js index 3c14d2835..1d91dbee4 100644 --- a/packages/plugin-adapter-sitemap/test/cases/build.default/build.default.spec.js +++ b/packages/plugin-adapter-sitemap/test/cases/build.default/build.default.spec.js @@ -10,7 +10,7 @@ * * User Config * import { greenwoodPluginAdapterSitemap } from '../../../src/index.js'; -* +* * export default { * plugins: [ * greenwoodPluginAdapterSitemap() @@ -22,12 +22,9 @@ */ import chai from 'chai'; import fs from 'fs/promises'; -import glob from 'glob-promise'; -import { JSDOM } from 'jsdom'; import path from 'path'; import { checkResourceExists } from '../../../../cli/src/lib/resource-utils.js'; -import { getSetupFiles, getOutputTeardownFiles } from '../../../../../test/utils.js'; -import { normalizePathnameForWindows } from '../../../../cli/src/lib/resource-utils.js'; +import { getSetupFiles } from '../../../../../test/utils.js'; import { Runner } from 'gallinago'; import { fileURLToPath } from 'url'; @@ -37,8 +34,8 @@ describe('Build Greenwood With: ', function() { const LABEL = 'Sitemap Adapter plugin output'; const cliPath = path.join(process.cwd(), 'packages/cli/src/index.js'); const outputPath = fileURLToPath(new URL('.', import.meta.url)); - const publicDir = path.join(outputPath, 'public') - + const publicDir = path.join(outputPath, 'public'); + let runner; before(function() { @@ -56,7 +53,7 @@ describe('Build Greenwood With: ', function() { describe('sitemap.xml', function() { it('should be present', async function() { - const sitemapPath = path.join(publicDir, 'sitemap.xml') + const sitemapPath = path.join(publicDir, 'sitemap.xml'); const itExists = await checkResourceExists(new URL(`file://${sitemapPath}`)); expect(itExists).to.be.equal(true); @@ -66,15 +63,13 @@ describe('Build Greenwood With: ', function() { it('should have the correct first element in the list', async function() { const sitemapPath = path.join(publicDir, 'sitemap.xml'); const text = await fs.readFile(sitemapPath, 'utf8'); - - + const regex = /(http:\/\/www\.example\.com\/about\/)<\/loc>/; const match = text.match(regex); expect(match[1]).to.equal('http://www.example.com/about/'); }); - }); }); diff --git a/packages/plugin-adapter-sitemap/test/cases/build.default/src/sitemap.xml.js b/packages/plugin-adapter-sitemap/test/cases/build.default/src/sitemap.xml.js index d6b779b4d..7fbac0ba1 100644 --- a/packages/plugin-adapter-sitemap/test/cases/build.default/src/sitemap.xml.js +++ b/packages/plugin-adapter-sitemap/test/cases/build.default/src/sitemap.xml.js @@ -1,17 +1,15 @@ -async function generateSitemap(compilation){ - const urls = compilation.graph.map((page) => { - return ` - http://www.example.com${page.route} +async function generateSitemap(compilation) { + const urls = compilation.graph.map((page) => { + return ` + http://www.example.com${page.route} `; - }); - return ` + }); + return ` ${urls.join('\n')} - `; - } +`; +} - - - export { generateSitemap }; \ No newline at end of file +export { generateSitemap }; \ No newline at end of file diff --git a/packages/plugin-adapter-sitemap/test/cases/develop.default/develop.default.spec.js b/packages/plugin-adapter-sitemap/test/cases/develop.default/develop.default.spec.js index ee01ba63b..3e9efcee1 100644 --- a/packages/plugin-adapter-sitemap/test/cases/develop.default/develop.default.spec.js +++ b/packages/plugin-adapter-sitemap/test/cases/develop.default/develop.default.spec.js @@ -9,7 +9,7 @@ const expect = chai.expect; describe('Develop Sitemap With: ', function() { const LABEL = 'Sitemap Resource plugin output'; - + const cliPath = path.join(process.cwd(), 'packages/cli/src/index.js'); const outputPath = fileURLToPath(new URL('.', import.meta.url)); const hostname = 'http://localhost'; @@ -59,7 +59,7 @@ describe('Develop Sitemap With: ', function() { const match = text.match(regex); expect(match[1]).to.equal('http://www.example.com/about/'); - + }); }); }); diff --git a/packages/plugin-adapter-sitemap/test/cases/develop.default/src/sitemap.xml.js b/packages/plugin-adapter-sitemap/test/cases/develop.default/src/sitemap.xml.js index d6b779b4d..2b1f8eba0 100644 --- a/packages/plugin-adapter-sitemap/test/cases/develop.default/src/sitemap.xml.js +++ b/packages/plugin-adapter-sitemap/test/cases/develop.default/src/sitemap.xml.js @@ -1,17 +1,16 @@ -async function generateSitemap(compilation){ - const urls = compilation.graph.map((page) => { - return ` - http://www.example.com${page.route} +async function generateSitemap(compilation) { + const urls = compilation.graph.map((page) => { + return ` + http://www.example.com${page.route} `; - }); - return ` + }); + + return ` ${urls.join('\n')} `; - } - - +} - export { generateSitemap }; \ No newline at end of file +export { generateSitemap }; \ No newline at end of file