|
| 1 | +#!/usr/bin/env node |
| 2 | +import process from "process"; |
| 3 | +import { writeFileSync } from "fs"; |
| 4 | +import chalk from "chalk"; |
| 5 | +import { CreateFileError } from "../errors/errorHandler.js"; |
| 6 | +const args = process.argv.splice(2, 4); |
| 7 | +async function createFile(fileName, fileContent) { |
| 8 | + try { |
| 9 | + writeFileSync(fileName, fileContent); |
| 10 | + console.log(`Successfully created file ${fileName}`); |
| 11 | + } |
| 12 | + catch (error) { |
| 13 | + throw new CreateFileError(error.message); |
| 14 | + } |
| 15 | +} |
| 16 | +let exampleData = ` |
| 17 | +{ |
| 18 | + "$schema" : "http://json-schema.org/draft-04/schema#", |
| 19 | + "db" : { |
| 20 | + "users" : [ |
| 21 | + { |
| 22 | + "id" : "1" , |
| 23 | + "username" : "leerob", |
| 24 | + |
| 25 | + "password" : "robinin" |
| 26 | + } |
| 27 | + ], |
| 28 | + "posts" : [ |
| 29 | + { |
| 30 | + "id" : "1", |
| 31 | + "userId" : "1", |
| 32 | + "photo" : "https://j.co/6235jjgk", |
| 33 | + "caption" : "Look at home" |
| 34 | + } |
| 35 | + ] |
| 36 | + } |
| 37 | + |
| 38 | +} |
| 39 | +`; |
| 40 | +const jsondb = `JSONDB`; |
| 41 | +const usage = ` |
| 42 | +\t Usage of JSONDB cli |
| 43 | +\t npx jsondb <command> |
| 44 | +\t Commands : |
| 45 | +\t \t --version : Prints the npm version of jsondb |
| 46 | +\t \t --init : Initialize database.json in your project |
| 47 | +\t \t --help : Show this help message |
| 48 | +`; |
| 49 | +switch (args[0]) { |
| 50 | + case '--init': |
| 51 | + console.log(chalk.bold.white.bgBlue(`\t ${jsondb} \t`)); |
| 52 | + console.log("Initializing database.json in your project..."); |
| 53 | + createFile('database.json', exampleData); |
| 54 | + console.log(chalk.greenBright("💥 Created database.json successfully")); |
| 55 | + console.log(chalk.yellowBright("Happy coding :)")); |
| 56 | + process.exit(1); |
| 57 | + break; |
| 58 | + case '--help': |
| 59 | + case undefined: |
| 60 | + case null: |
| 61 | + default: |
| 62 | + console.log(usage); |
| 63 | + console.log(chalk.yellowBright("\t\tHappy coding :)")); |
| 64 | +} |
0 commit comments