From ab796326f11f6c8424faab4824e2dc764ddb5b97 Mon Sep 17 00:00:00 2001 From: Simon C Date: Wed, 23 Feb 2022 16:14:51 +0100 Subject: [PATCH] feat: Update filename with path field --- index.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 73b944d..5c0489d 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ import { Directus } from '@directus/sdk' import yaml from 'js-yaml' import fs from 'fs' import Axios from 'axios' +import path from 'path' export default class DirectusToMarkdown { constructor(config) { @@ -25,12 +26,16 @@ export default class DirectusToMarkdown { const frontMatter = this._formatFrontMatter(item, deleteFields) const content = item[this.contentKey] ? item[this.contentKey].toString() : '' const itemContent = `${frontMatter}${content}\r\n` - const indexName = 'index' // TODO: index or _index ? - fs.writeFileSync(`${itemPath}/${indexName}.md`, itemContent) + const filePath = `${itemPath}/${item.path ? item.path : 'index.md'}` + const fileDirname = path.dirname(filePath) + if (!fs.existsSync(fileDirname)) { + fs.mkdirSync(fileDirname, { recursive: true }); + } + fs.writeFileSync(filePath, itemContent) } - async _writeFile(response, path) { - const writer = fs.createWriteStream(path) + async _writeFile(response, filePath) { + const writer = fs.createWriteStream(filePath) response.data.pipe(writer)