124 lines
5.8 KiB
JavaScript
124 lines
5.8 KiB
JavaScript
import { Database } from 'sqlite3';
|
|
import readline from 'readline';
|
|
import fs from 'fs';
|
|
import figlet from 'figlet';
|
|
|
|
const version = "v1.0.0";
|
|
|
|
const tables = [
|
|
{
|
|
name: 'users',
|
|
schema: `
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
username TEXT,
|
|
email TEXT,
|
|
password TEXT,
|
|
key TEXT
|
|
);
|
|
`
|
|
},
|
|
{
|
|
name: 'posts',
|
|
schema: `
|
|
CREATE TABLE IF NOT EXISTS posts (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
title TEXT,
|
|
description TEXT,
|
|
category TEXT,
|
|
slug TEXT,
|
|
content TEXT,
|
|
date TEXT
|
|
);
|
|
`
|
|
},
|
|
{
|
|
name: 'categories',
|
|
schema: `
|
|
CREATE TABLE IF NOT EXISTS categories (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT,
|
|
description TEXT,
|
|
slug TEXT
|
|
);
|
|
`
|
|
}
|
|
];
|
|
|
|
figlet('BlogPop', (err, data) => {
|
|
if (err) {
|
|
console.log('BLOGPOP SETUP | ' + version + "\n");
|
|
console.dir(err);
|
|
return;
|
|
}
|
|
console.log(data);
|
|
console.log("SETUP | VERSION: " + version + "\n");
|
|
|
|
if (fs.existsSync('./db.sqlite')) {
|
|
console.log("[!] You've already run the setup before!");
|
|
process.exit(0);
|
|
}
|
|
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout
|
|
});
|
|
|
|
console.log("┌───────────────────────────────────────────────────────────────────────────────────────────┐");
|
|
console.log("│ Let's start by setting up an admin user for you. Please enter your desired details below. │");
|
|
console.log("├────────────────┬──────────────────────────────────────────────────────────────────────────┘");
|
|
|
|
rl.question('│ ADMIN USERNAME │ ', (username) => {
|
|
if (!username) {
|
|
console.log("└────────────────┴──────────────────────────────────────────────────────────────────────────┘\n");
|
|
console.log('[!] The username field cannot be blank.');
|
|
rl.close();
|
|
process.exit(1);
|
|
}
|
|
console.log("├────────────────┼──────────────────────────────────────────────────────────────────────────┘");
|
|
rl.question('│ ADMIN EMAIL │ ', (email) => {
|
|
if (!email) {
|
|
console.log("└────────────────┴──────────────────────────────────────────────────────────────────────────┘\n");
|
|
console.log('[!] The email field cannot be blank.');
|
|
rl.close();
|
|
process.exit(1);
|
|
}
|
|
console.log("├────────────────┼──────────────────────────────────────────────────────────────────────────┘");
|
|
rl.question('│ ADMIN PASSWORD │ ', (password) => {
|
|
if (!password) {
|
|
console.log("└────────────────┴──────────────────────────────────────────────────────────────────────────┘\n");
|
|
console.log('[!] The password field cannot be blank.');
|
|
rl.close();
|
|
process.exit(1);
|
|
}
|
|
console.log("└────────────────┴──────────────────────────────────────────────────────────────────────────┘\n");
|
|
console.log("[TASK] Creating tables...\n");
|
|
const db = new Database('./db.sqlite');
|
|
|
|
for (const table of tables) {
|
|
db.exec(table.schema);
|
|
console.log(`[✓] Created table ${table.name} successfully.`);
|
|
}
|
|
console.log('\n[✓ TASK] Tables created.\n');
|
|
|
|
console.log("[TASK] Creating admin user...");
|
|
const insertStmt = db.prepare(`INSERT INTO users (username, email, password) VALUES (?, ?, ?)`);
|
|
insertStmt.run(username, email, password);
|
|
insertStmt.finalize();
|
|
console.log('[✓ TASK] Admin user created.\n');
|
|
|
|
console.log("┌────────────────────────────────────────────────────┐");
|
|
console.log("│ ✓ SETUP COMPLETE │");
|
|
console.log("├─────────────────────┬──────────────────────────────┤");
|
|
console.log("│ START SERVER (NODE) │ $ node index.js │");
|
|
console.log("│ START SERVER (BUN) │ $ bun index.js │")
|
|
console.log("└─────────────────────┴──────────────────────────────┘")
|
|
|
|
db.close();
|
|
rl.close();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|