|
| 1 | +//============================================================================== |
| 2 | +// ■ .REPLRC (.replrc.js) |
| 3 | +//------------------------------------------------------------------------------ |
| 4 | +// Local-REPL configuration. |
| 5 | +//============================================================================== |
| 6 | +require("colors"); |
1 | 7 | const $0db = require("0db");
|
2 |
| -const { prompt, banner, cls } = require("./core"); |
| 8 | +const faker = require("faker"); |
3 | 9 |
|
| 10 | +//------------------------------------------------------------------------------ |
| 11 | +// ● REPL-Context |
| 12 | +//------------------------------------------------------------------------------ |
| 13 | +const context = { |
| 14 | + $0db, |
| 15 | + db: $0db(), |
| 16 | + faker, |
| 17 | + gen: gen(), |
| 18 | + cls, |
| 19 | + help, |
| 20 | +}; |
| 21 | +const contextHelp = { |
| 22 | + $0db: "0db object", |
| 23 | + db: "Default instance (./db.json)", |
| 24 | + faker: "Fake data generator (See: https://npmjs.com/package/faker)", |
| 25 | + gen: "Built-in example data objects generator (using faker)", |
| 26 | + cls: "Clears screen", |
| 27 | + help: "Shows this help message", |
| 28 | +}; |
| 29 | +function help(name = "") { |
| 30 | + if (!name) { |
| 31 | + _logContext(context); |
| 32 | + } |
| 33 | + _logContextHelp(context, contextHelp, name); |
| 34 | + return "=================================================="; |
| 35 | +} |
| 36 | + |
| 37 | +//------------------------------------------------------------------------------ |
| 38 | +// ● Data-Objects-Generator |
| 39 | +//------------------------------------------------------------------------------ |
| 40 | +function gen() { |
| 41 | + return { |
| 42 | + user(count = 1) { |
| 43 | + if (count < 1) count = 1; |
| 44 | + const users = []; |
| 45 | + for (let i = 0; i < count; i++) { |
| 46 | + const { name, internet, datatype } = faker; |
| 47 | + const gender = name.gender("binary"); |
| 48 | + const firstName = name.firstName(gender); |
| 49 | + const lastName = name.lastName(gender); |
| 50 | + users[i] = { |
| 51 | + name: internet.userName(firstName, lastName), |
| 52 | + firstName: name.firstName(gender), |
| 53 | + lastName: name.lastName(gender), |
| 54 | + gender, |
| 55 | + email: internet.email(firstName, lastName), |
| 56 | + password: internet.password(), |
| 57 | + points: datatype.number({ min: 0, max: 8000 }), |
| 58 | + }; |
| 59 | + } |
| 60 | + return users.length === 1 ? users[0] : users; |
| 61 | + }, |
| 62 | + note(author, count = 1) { |
| 63 | + if (count < 1) count = 1; |
| 64 | + if (!author) author = "/** Put here the $id of some existent user **/"; |
| 65 | + const notes = []; |
| 66 | + for (let i = 0; i < count; i++) { |
| 67 | + notes[i] = { |
| 68 | + author, |
| 69 | + title: faker.lorem.sentence(), |
| 70 | + content: faker.lorem.paragraph(), |
| 71 | + }; |
| 72 | + } |
| 73 | + return notes.length === 1 ? notes[0] : notes; |
| 74 | + }, |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +//------------------------------------------------------------------------------ |
| 79 | +// ● REPL-Prompt |
| 80 | +//------------------------------------------------------------------------------ |
| 81 | +function prompt(context, package) { |
| 82 | + return "[0db] ► "; |
| 83 | +} |
| 84 | + |
| 85 | +//------------------------------------------------------------------------------ |
| 86 | +// ● REPL-Banner |
| 87 | +//------------------------------------------------------------------------------ |
| 88 | +function banner(context, package) { |
| 89 | + cls(); |
| 90 | + _logPackage(package); |
| 91 | + _logContext(context); |
| 92 | + console.log(); |
| 93 | +} |
| 94 | + |
| 95 | +//------------------------------------------------------------------------------ |
| 96 | +// ● Clear-Console |
| 97 | +//------------------------------------------------------------------------------ |
| 98 | +function cls() { |
| 99 | + const readline = require("readline"); |
| 100 | + console.log("\n".repeat(process.stdout.rows)); |
| 101 | + readline.cursorTo(process.stdout, 0, 0); |
| 102 | + readline.clearScreenDown(process.stdout); |
| 103 | + console.clear(); |
| 104 | + return "Console cleared."; |
| 105 | +} |
| 106 | + |
| 107 | +//------------------------------------------------------------------------------ |
| 108 | +// ● Helpers |
| 109 | +//------------------------------------------------------------------------------ |
| 110 | +function _logContext(context) { |
| 111 | + const vars = Object.keys(context).join(", "); |
| 112 | + console.log("● Context variables:", `${vars}`.green); |
| 113 | +} |
| 114 | +function _logContextHelp(context, contextHelp, name) { |
| 115 | + const filter = name ? (key) => key === name : (key) => true; |
| 116 | + const keys = Object.keys(context).filter(filter); |
| 117 | + if (keys.length) { |
| 118 | + for (const key of keys) { |
| 119 | + console.log(`\t• ${key.green}: ${contextHelp[key]}.`); |
| 120 | + } |
| 121 | + } else { |
| 122 | + console.log(`\t${name.red} object does not exist in the built-in context.`); |
| 123 | + } |
| 124 | +} |
| 125 | +function _logPackage(package) { |
| 126 | + const { name, version, description } = package; |
| 127 | + console.log( |
| 128 | + "♛ Welcome to", |
| 129 | + `${name}@${version}`.blue, |
| 130 | + `✎ ${description}`.grey |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +//------------------------------------------------------------------------------ |
| 135 | +// ► Exports |
| 136 | +//------------------------------------------------------------------------------ |
4 | 137 | module.exports = {
|
5 |
| - context: { |
6 |
| - $0db, |
7 |
| - db: $0db(), |
8 |
| - cls, |
9 |
| - }, |
10 |
| - enableAwait: true, |
| 138 | + context, |
11 | 139 | prompt,
|
12 | 140 | banner,
|
| 141 | + cls, |
| 142 | + enableAwait: true, |
13 | 143 | };
|
0 commit comments