summary refs log tree commit diff
path: root/src/renderEnvConfigSchema.ts
blob: 492e3774b1822edd02bf4d7ed320fdf92c666013 (plain) (blame)
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
import { bgRedBright, dim, gray, white } from "picocolors";

require("dotenv").config({ quiet: true });
import moduleAlias from "module-alias";
moduleAlias();

import { initDatabase } from "@spacebar/util";
import { Config } from "@spacebar/util";
import { EnvConfig } from "@spacebar/util";

const schema = EnvConfig.schema();
const keyMap = [
	{
		name: "Name",
		selector: (v: { key: string }) => v.key,
	},
	{
		name: "Value",
		selector: (v: { type: string }) => v.type,
	},
	{
		name: "Description",
		selector: (v: { description: string }) => v.description,
	},
];
// --- separators
const startOfLine = dim("| "); // <tr>
const endOfLine = "\n"; // </tr><br/>\n
const startOfCell = ""; // <td>
const endOfCell = dim(" | "); // </td><td>
const headSeparator = dim("-");
const pad = true;

// --- do not touch
const colWidths: { [key: string]: number } = {};

console.log(bgRedBright("Calculating column widths"));
for (const key of keyMap) {
	colWidths[key.name] = key.name.length;
}

for (const key of keyMap) {
	for (const entry of schema) {
		const len = key.selector(entry).toString().length;
		if (!colWidths[key.name] || len > colWidths[key.name]) {
			console.log(gray(`ColW ${key.name} ${colWidths[key.name] || 0} -> ${len} (entry: ${key.selector(entry)})`));
			colWidths[key.name] = len;
		}
	}
}

// --- render table
console.log(bgRedBright("Environment variables"));
process.stdout.write(startOfLine);
for (const key of keyMap) {
	const header = startOfCell + (pad ? key.name.padEnd(colWidths[key.name], " ") : key.name) + endOfCell;
	process.stdout.write(header);
}

process.stdout.write(endOfLine);
process.stdout.write(startOfLine);
for (const key of keyMap) {
	const separator = startOfCell + headSeparator.repeat(colWidths[key.name]) + endOfCell;
	process.stdout.write(separator);
}

process.stdout.write(endOfLine);
for (const entry of schema) {
	process.stdout.write(startOfLine);
	for (const key of keyMap) {
		const cell = startOfCell + (pad ? key.selector(entry).toString().padEnd(colWidths[key.name], " ") : key.selector(entry).toString()) + endOfCell;
		process.stdout.write(cell);
	}
	process.stdout.write(endOfLine);
}