From 33566a3e195f877e11a1b038f63acc37a10723af Mon Sep 17 00:00:00 2001 From: Jared Wray Date: Thu, 4 Jan 2024 14:49:51 -0800 Subject: [PATCH] adding in robots.txt support --- src/builder.ts | 12 ++++++++ test/builder.test.ts | 39 +++++++++++++++++++++++- test/fixtures/multi-page-site/robots.txt | 2 ++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/multi-page-site/robots.txt diff --git a/src/builder.ts b/src/builder.ts index ee57d38..e45bb4e 100644 --- a/src/builder.ts +++ b/src/builder.ts @@ -47,6 +47,8 @@ export class WritrBuilder { // build the sitemap (/sitemap.xml) // build the robots.txt (/robots.txt) + await this.buildRobotsPage(this.options); + console.log('build'); } @@ -114,4 +116,14 @@ export class WritrBuilder { return result; } + + public async buildRobotsPage(options: WritrOptions): Promise { + const {sitePath} = options; + const {outputPath} = options; + const robotsPath = `${outputPath}/robots.txt`; + + await fs.ensureDir(outputPath); + + await (await fs.pathExists(`${sitePath}/robots.txt`) ? fs.copy(`${sitePath}/robots.txt`, robotsPath) : fs.writeFile(robotsPath, 'User-agent: *\nDisallow:')); + } } diff --git a/test/builder.test.ts b/test/builder.test.ts index 301225a..573a14f 100644 --- a/test/builder.test.ts +++ b/test/builder.test.ts @@ -1,4 +1,5 @@ import {expect, it, describe} from 'vitest'; +import * as fs from 'fs-extra'; import {WritrBuilder} from '../src/builder.js'; import {WritrOptions} from '../src/options.js'; @@ -16,12 +17,18 @@ describe('WritrBuilder', () => { it('should build', async () => { const builder = new WritrBuilder(); const consoleLog = console.log; + const options = new WritrOptions(); + options.outputPath = 'test/temp-build-test'; let consoleMessage = ''; console.log = message => { consoleMessage = message as string; }; - await builder.build(); + try { + await builder.build(); + } finally { + await fs.remove(builder.options.outputPath); + } expect(consoleMessage).toBe('build'); @@ -99,4 +106,34 @@ describe('WritrBuilder', () => { expect(error.message).toBe('No template path found'); } }); + it('should build the robots.txt (/robots.txt)', async () => { + const builder = new WritrBuilder(); + const options = new WritrOptions(); + options.sitePath = 'test/fixtures/single-page-site'; + options.outputPath = 'test/temp-robots-test'; + + await fs.remove(options.outputPath); + try { + await builder.buildRobotsPage(options); + const robots = await fs.readFile(`${options.outputPath}/robots.txt`, 'utf8'); + expect(robots).toBe('User-agent: *\nDisallow:'); + } finally { + await fs.remove(options.outputPath); + } + }); + it('should copy the robots.txt (/robots.txt)', async () => { + const builder = new WritrBuilder(); + const options = new WritrOptions(); + options.sitePath = 'test/fixtures/multi-page-site'; + options.outputPath = 'test/temp-robots-test-copy'; + + await fs.remove(options.outputPath); + try { + await builder.buildRobotsPage(options); + const robots = await fs.readFile(`${options.outputPath}/robots.txt`, 'utf8'); + expect(robots).toBe('User-agent: *\nDisallow: /meow'); + } finally { + await fs.remove(options.outputPath); + } + }); }); diff --git a/test/fixtures/multi-page-site/robots.txt b/test/fixtures/multi-page-site/robots.txt new file mode 100644 index 0000000..5f1ca7f --- /dev/null +++ b/test/fixtures/multi-page-site/robots.txt @@ -0,0 +1,2 @@ +User-agent: * +Disallow: /meow \ No newline at end of file