Files
blogger-nodejs/src/initialise.js

43 lines
1.4 KiB
JavaScript

import·{·createRequire·}·from·'module';
const·require·=·createRequire(import.meta.url)
// Initialise the program by creating users.js, comments.js, posts.js and config.js
// All require default content in them to start off with
// Then exit successfully
// returns nothing
export function initialise() {
const fs = require("fs");
try {
const users = require("../data/users.js");
}
catch (error) {
console.log("Creating users file")
fs.writeFileSync(`../data/users.json`, `{\n"users": []\n}`)
}
try {
const posts = require("../data/posts.json");
}
catch (error) {
console.log("Creating posts file")
fs.writeFileSync(`../data/posts.json`, `{\n"posts": []\n}`)
}
try {
const comments = require("../data/comments.json");
}
catch (error) {
console.log("Creating comments file")
fs.writeFileSync(`../data/comments.json`, `{\n"comments": [],\n"counter": 0}`)
}
try {
const config = require("../data/config.js");
}
catch (error) {
console.log("Copying the example config to config.js")
console.log("!!! PLEASE MODIFY config.js TO YOUR NEEDS !!!")
fs.copyFile('example-config.js', 'config.js', (err) => {
console.log("Error copying file")
})
}
console.log("Successfully initialised")
process.exit(0)
}