diff options
| -rw-r--r-- | examples/index.js | 21 | ||||
| -rw-r--r-- | examples/shop.js | 51 | ||||
| -rw-r--r-- | source/blog.js | 76 | ||||
| -rw-r--r-- | source/controller.js | 42 |
4 files changed, 138 insertions, 52 deletions
diff --git a/examples/index.js b/examples/index.js new file mode 100644 index 0000000..64a619b --- /dev/null +++ b/examples/index.js @@ -0,0 +1,21 @@ +class Index { + constructor() {} + + async Main() { + return { + html: ` + <section> + <h1>Welcome to <a href='https://git.fatalmatrix.xyz/cosmic.git'>Cosmic</a>!</h1> + <p>Please read the README.md</p> + </section> + `, + meta: { + keywords: 'Cosmic', + description: 'Welcome to Cosmic!' + } + } + } +} + +module.exports = (x) => {return new Index(x)} + diff --git a/examples/shop.js b/examples/shop.js new file mode 100644 index 0000000..e75d289 --- /dev/null +++ b/examples/shop.js @@ -0,0 +1,51 @@ +const database = require('../source/database') +const estore = database('EStore') + +class Shop { + constructor() {} + + async Main() { + return estore.Query('SELECT * FROM Products').then(output => { + const data = { + start: ` + <section id='shop'> + <div> + <h1>This is the Shop page</h1> + `, + body: '', + end: ` + </div> + </section> + ` + } + if (output) { + for (let i = 0; i < output[0].length; i++) { + data.body += ` + <div class='item'> + <h2>${output[0][i].ProductName}</h2> + <p>ID: ${output[0][i].ID}</p> + </div> + ` + } + } else { + data.body = ` + There are no items in the database. + ` + } + return data.start + data.body + data.end + }) + } + Product() { + const data = ` + <section id='product'> + <div> + <h1>This is the Product page</h1> + <p>Description</p> + </div> + </section> + ` + return data + } +} + +module.exports = () => {return new Shop()} 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 + |
