Skip to content

Commit

Permalink
fix: create parent directories if not exist in generate mode (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
rclement authored Jul 14, 2020
1 parent 1224ecc commit 7c8595d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { join, resolve } = require('path')
const { promisify } = require('util')
const { writeFileSync } = require('fs')
const { existsSync, mkdirSync, writeFileSync } = require('fs')
const { dirname } = require('path')
const { Feed } = require('feed')
const AsyncCache = require('async-cache')
const logger = require('./logger')
Expand Down Expand Up @@ -32,7 +33,11 @@ module.exports = async function (moduleOptions) {
}

const xmlGeneratePath = resolve(this.options.rootDir, join(this.options.generate.dir, feedOptions.path))
const xmlGenerateDirPath = dirname(xmlGeneratePath)

if (!existsSync(xmlGenerateDirPath)) {
mkdirSync(xmlGenerateDirPath, { recursive: true })
}
writeFileSync(xmlGeneratePath, await feedCache.get(index))

logger.success('Generated', feedOptions.path)
Expand Down
20 changes: 20 additions & 0 deletions test/__snapshots__/module.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ exports[`module generate simple rss 1`] = `
</rss>"
`;
exports[`module generate simple rss in subdir 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>
<rss version=\\"2.0\\">
<channel>
<title>Feed Title</title>
<link>http://example.com/</link>
<description>This is my personal feed!</description>
<lastBuildDate>Fri, 14 Jul 2000 00:00:00 GMT</lastBuildDate>
<docs>https://validator.w3.org/feed/docs/rss2.html</docs>
<generator>awesome</generator>
<image>
<title>Feed Title</title>
<url>http://example.com/image.png</url>
<link>http://example.com/</link>
</image>
<copyright>All rights reserved 2013, John Doe</copyright>
</channel>
</rss>"
`;
exports[`module multi rss 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>
<rss version=\\"2.0\\">
Expand Down
12 changes: 12 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ describe('module', () => {
expect(readFileSync(filePath, { encoding: 'utf8' })).toMatchSnapshot()
})

test('generate simple rss in subdir', async () => {
({ nuxt } = await generate({
...config,
feed: [
{ ...createFeed(), ...{ path: join('/feeds/articles', 'feed.xml') } }
]
}))

const filePath = resolve(nuxt.options.rootDir, join(nuxt.options.generate.dir, '/feeds/articles', 'feed.xml'))
expect(readFileSync(filePath, { encoding: 'utf8' })).toMatchSnapshot()
})

test('simple rss', async () => {
({ nuxt } = await setup(config))

Expand Down

0 comments on commit 7c8595d

Please sign in to comment.