diff --git a/packages/cli/lib/util/browser.js b/packages/cli/lib/util/browser.js index 10789d82e..468fcfb4c 100644 --- a/packages/cli/lib/util/browser.js +++ b/packages/cli/lib/util/browser.js @@ -17,6 +17,14 @@ module.exports = async (url, label, route, outputDirectory) => { const html = dom.serialize(); const target = path.join(outputDirectory, route); + // Exception for index file in root public directory + const endOfPathFolder = target.substring(target.lastIndexOf('/public/'), target.length); + const isRootPublicDirectoryException = endOfPathFolder === '/public/index'; + + if (isRootPublicDirectoryException) { + return await fs.writeFileSync(path.join(outputDirectory, 'index.html'), html); + } + await fs.mkdirSync(target, { recursive: true }); return await fs.writeFileSync(path.join(target, 'index.html'), html); }; \ No newline at end of file diff --git a/test/cli.spec.js b/test/cli.spec.js index 98f8915cd..5b8e67a2b 100644 --- a/test/cli.spec.js +++ b/test/cli.spec.js @@ -27,18 +27,44 @@ describe('building greenwood with default context (no user workspace)', () => { it('should output one JS bundle file', async () => { expect(await glob.promise(path.join(CONTEXT.publicDir, './index.*.bundle.js'))).to.have.lengthOf(1); }); - + + describe('default generated index page in public directory', () => { + const indexPageHeading = 'Greenwood'; + const indexPageBody = 'This is the home page built by Greenwood. Make your own pages in src/pages/index.js!'; + let dom; + + beforeEach(async() => { + dom = await JSDOM.fromFile(path.resolve(CONTEXT.publicDir, 'index.html')); + }); + + it('should output an index.html file within the root public directory', () => { + expect(fs.existsSync(path.join(CONTEXT.publicDir, './index.html'))).to.be.true; + }); + + it('should have the expected heading text within the index page in the public directory', async() => { + const heading = dom.window.document.querySelector('h3.wc-md-index').textContent; + + expect(heading).to.equal(indexPageHeading); + }); + + it('should have the expected paragraph text within the index page in the public directory', async() => { + let paragraph = dom.window.document.querySelector('p.wc-md-index').textContent; + + expect(paragraph).to.equal(indexPageBody); + }); + }); + it('should create a default hello page directory', () => { expect(fs.existsSync(path.join(CONTEXT.publicDir, './hello'))).to.be.true; }); describe('default generated hello page directory', () => { - const defaultHeading = 'Hello World'; - const defaultBody = 'This is an example page built by Greenwood. Make your own in src/pages!'; + const helloPageHeading = 'Hello World'; + const helloPageBody = 'This is an example page built by Greenwood. Make your own in src/pages!'; let dom; beforeEach(async() => { - dom = await JSDOM.fromFile(path.resolve(CONTEXT.publicDir, 'hello/index.html')); + dom = await JSDOM.fromFile(path.resolve(CONTEXT.publicDir, './hello', './index.html')); }); it('should output an index.html file within the default hello page directory', () => { @@ -48,13 +74,13 @@ describe('building greenwood with default context (no user workspace)', () => { it('should have the expected heading text within the hello example page in the hello directory', async() => { const heading = dom.window.document.querySelector('h3.wc-md-hello').textContent; - expect(heading).to.equal(defaultHeading); + expect(heading).to.equal(helloPageHeading); }); it('should have the expected paragraph text within the hello example page in the hello directory', async() => { let paragraph = dom.window.document.querySelector('p.wc-md-hello').textContent; - expect(paragraph).to.equal(defaultBody); + expect(paragraph).to.equal(helloPageBody); }); }); });