diff options
Diffstat (limited to 'source')
| -rw-r--r-- | source/blog.js | 76 | ||||
| -rw-r--r-- | source/controller.js | 42 |
2 files changed, 66 insertions, 52 deletions
diff --git a/source/blog.js b/source/blog.js index cc654dd..56f76a4 100644 --- a/source/blog.js +++ b/source/blog.js @@ -16,52 +16,54 @@ class Blog { async ReadBlogs() {return this.#LoadBlogs().then(output => {return output})} + // ! Check if file is a markdown file async #LoadBlogs() { return new Promise((res, rej) => { - fs.readdir('./blog/', (err, files) => { - if (err) { - logger.Error(`Error reading directory ${err}`) - rej(null) - } else { - this.blog = new Map() - files.forEach(file => { - try { + if (fs.existsSync('./blog/')) { + fs.readdir('./blog/', (err, files) => { + if (err) { + logger.Error(`${err.code}: Error reading directory`) + rej(`${err.code}: Error reading directory`) + } else { + this.blog = new Map() + files.forEach(file => { this.blog.set(file.replace('.md', ''), fs.readFileSync(path.resolve(`./blog/${file}`), 'utf-8', (err, data) => { if (err) { - logger.Error(err) + logger.Error(`${err.code}: Failed to read ${file}`) + rej(`${err.code}: Failed to read ${file}`) return } return data })) - } catch (err) { - logger.Error(err) - rej(null) - } - }) - this.blog.forEach((value, key) => { - const regex = /# (.*)\nDate: (.*)\n---(.*?)---/s - const match = value.match(regex) + }) + this.blog.forEach((value, key) => { + const regex = /# (.*)\nDate: (.*)\n---(.*?)---/s + const match = value.match(regex) - if (match) { - const [, title, date, description] = match - this.blog.set(key, { - title: title, - date: new Date(date), - short: description, - body: md.render(value) - }) - } else { - this.blog.set(key, { - title: undefined, - date: undefined, - short: undefined, - body: md.render(value) - }) - } - }) - res(this.#OrderByDate(this.blog)) - } - }) + if (match) { + const [, title, date, description] = match + this.blog.set(key, { + title: title, + date: new Date(date), + short: description, + body: md.render(value) + }) + } else { + this.blog.set(key, { + title: undefined, + date: undefined, + short: undefined, + body: md.render(value) + }) + } + }) + res(this.#OrderByDate(this.blog)) + } + }) + } else { + logger.Error('ERROR: blog directory does not exist') + rej('ERROR: blog directory does not exist') + } }) } diff --git a/source/controller.js b/source/controller.js index c66df01..889e868 100644 --- a/source/controller.js +++ b/source/controller.js @@ -1,4 +1,5 @@ const fs = require('fs') +const path = require('path') const Method = require('./method') const logger = require('./logger')() @@ -6,23 +7,33 @@ const logger = require('./logger')() class Controller { constructor() { this.main = new Promise((res, rej) => { - fs.readdir('./scripts/', (err, files) => { - if (err) { - logger.Error(`${err.code}: Failed to read directory`) - rej(`${err.code}: Failed to read directory`) - } + if (fs.existsSync('./scripts/')) { + fs.readdir('./scripts/', (err, files) => { + if (err) { + logger.Error(`${err.code}: Failed to read directory`) + rej(`${err.code}: Failed to read directory`) + } - this.require = new Map() - files.forEach(file => { - try { - this.require.set(file.replace('.js', ''), require(`../scripts/${file}`)()) - } catch (err) { - logger.Error(`${err.code}: Failed to read ${file}`) - rej(`${err.code}: Failed to read ${file}`) + this.require = new Map() + files.forEach(file => { + try { + this.require.set(file.replace('.js', ''), require(`../scripts/${file}`)()) + } catch (err) { + logger.Error(`${err.code}: Failed to read ${file}`) + rej(`${err.code}: Failed to read ${file}`) + } + }) + res(this.require) + }) + } else { + fs.mkdirSync('./scripts/') + fs.writeFile(path.join('./scripts/', 'index.js'), fs.readFileSync('./examples/index.js', 'utf-8'), (err) => { + if (err) { + logger.Error(`${err.code}: Error writing to file`) + rej(`${err.code}: Error writing to file`) } }) - res(this.require) - }) + } }).then(output => { return new Promise((res, rej) => { const map = output @@ -95,4 +106,5 @@ class Controller { } } -module.exports = Controller +module.exports = Controller + |
