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