Files
blogger-nodejs/src/initialise.js
deadvey 1ecc223433 small changes
Signed-off-by: deadvey <deadvey@deadvey.com>
2025-09-24 20:07:49 +01:00

51 lines
1.5 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.json");
}
catch (error) {
console.log("Creating users file")
fs.writeFileSync(`../data/users.json`, `[]`)
}
try {
const posts = require("../data/posts.json");
}
catch (error) {
console.log("Creating posts file")
fs.writeFileSync(`../data/posts.json`, `[]`)
}
try {
const comments = require("../data/comments.json");
}
catch (error) {
console.log("Creating comments file")
fs.writeFileSync(`../data/comments.json`, `[]`)
}
try {
const comments = require("../data/data.json");
}
catch (error) {
console.log("Creating generic data file")
fs.writeFileSync(`../data/data.json`, `{"hitcount": 0}`)
}
try {
const config = require("../config.json");
}
catch (error) {
console.log("Copying the example config to config.js")
console.log("!!! PLEASE MODIFY config.json TO YOUR NEEDS !!!")
fs.copyFile('../example-config.json', '../config.json', (err) => {
console.log("Error copying file")
})
}
console.log("Successfully initialised")
process.exit(0)
}