-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
exist-ls
executable file
·203 lines (181 loc) · 4.91 KB
/
exist-ls
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env node
const { connect } = require("../../index")
const { argv } = require("process") // eslint-disable-line node/prefer-global/process
// read connection options from ENV
const connectionOptions = require("../connection")
const { readFileSync } = require("fs")
const { resolve, join } = require("path")
const xqueryPath = resolve(__dirname, '../../xquery/')
const query = readFileSync(join(xqueryPath, "list-resources.xq"))
const consoleColors = new Map([
["Reset", "\x1b[0m"],
["Bright", "\x1b[1m"],
["Dim", "\x1b[2m"],
["Underscore", "\x1b[4m"],
["Blink", "\x1b[5m"],
["Reverse", "\x1b[7m"],
["Hidden", "\x1b[8m"],
["FgBlack", "\x1b[30m"],
["FgRed", "\x1b[31m"],
["FgGreen", "\x1b[32m"],
["FgYellow", "\x1b[33m"],
["FgBlue", "\x1b[34m"],
["FgMagenta", "\x1b[35m"],
["FgCyan", "\x1b[36m"],
["FgWhite", "\x1b[37m"],
["BgBlack", "\x1b[40m"],
["BgRed", "\x1b[41m"],
["BgGreen", "\x1b[42m"],
["BgYellow", "\x1b[43m"],
["BgBlue", "\x1b[44m"],
["BgMagenta", "\x1b[45m"],
["BgCyan", "\x1b[46m"],
["BgWhite", "\x1b[47m"],
]);
const cc = (name) => consoleColors.get(name);
const initialPaddings = new Map([
["padOwner", 0],
["padGroup", 0],
["padSize", 4],
])
const padReducer = (res, next) => {
if (res.get("padGroup") < next.group.length) {
res.set("padGroup", next.group.length);
}
if (res.get("padOwner") < next.owner.length) {
res.set("padOwner", next.owner.length);
}
if (res.get("padSize") < next.size.toFixed().length) {
res.set("padSize", next.size.toFixed().length);
}
return res;
};
const getPaddings = (list) => list.reduce(padReducer, initialPaddings);
const timeFormat = {
hour12:false,
hour: '2-digit',
minute: '2-digit'
}
const dateFormat = {
month: "short"
};
const currentYear = (new Date()).getFullYear()
function formatDateTime(xsDateTime) {
const date = new Date(xsDateTime)
const year = date.getFullYear()
const month = date.toLocaleDateString('iso', dateFormat)
const day = date.getDate().toString().padStart(3)
if (year < currentYear) {
return month + day + year.toString().padStart(6)
}
const time = date.toLocaleTimeString('iso', timeFormat).padStart(6)
return month + day + time
}
function formatName(item, color) {
if (!color) {
return item.name;
}
if (item.type === "resource") {
return item.name;
}
return cc("Bright") + cc("FgCyan") + item.name + cc("Reset");
}
function renderList(list, color) {
for (let item of list) {
console.log(formatName(item, color));
}
}
function renderExtendedListItem(item, paddings, color) {
console.log(
item.mode,
item.owner.padStart(paddings.get("padOwner")),
item.group.padStart(paddings.get("padGroup")),
item.size.toFixed().padStart(paddings.get("padSize")),
formatDateTime(item.modified),
formatName(item, color)
);
}
function renderExtendedList(list, noColor) {
const paddings = getPaddings(list);
for (let item of list) {
renderExtendedListItem(item, paddings, noColor);
}
}
async function ls(collection, options) {
try {
const db = connect(connectionOptions);
const {glob, color, extended} = options
const result = await db.queries.readAll(query, {
variables: { collection, glob, extended },
});
const json = JSON.parse(result.pages.toString());
if (json.error) {
throw Error(json.error);
}
if (extended) {
renderExtendedList(json, color);
return;
}
renderList(json, color);
} catch (e) {
let message = e.faultString ? e.faultString : e.message;
console.error(message);
process.exit(1);
}
}
async function run() {
const cli = require("yargs")
.command("$0", "List connection contents", (yargs) => {
yargs.demandCommand(1, 1).usage(`
Usage:
exist-ls [options] collection`);
})
.option("G", {
alias: "color",
describe: "Color the output",
default: false,
type: "boolean",
})
.option("l", {
alias: "extended",
describe: "Display more information for each item",
default: false,
type: "boolean",
})
.option("g", {
alias: "glob",
describe:
"Include only collection names and resources whose name match the pattern.",
type: String,
default: "*",
})
.option("h", { alias: "help"})
.strict(false)
.exitProcess(false);
try {
const parsed = cli.parse(argv.slice(2));
if (Boolean(parsed.help)) {
return 0;
}
const { glob, color, extended } = parsed;
if (typeof glob !== "string") {
console.error('Invalid value for option "glob"; must be a string.');
return 1;
}
const collection = parsed._;
return ls(collection, { glob, color, extended });
} catch (error) {
if (error.name !== "YError") {
console.error(error);
}
return 1;
}
}
run()
.then((exitCode) => {
process.exitCode = exitCode;
})
.catch((error) => {
console.error(error);
process.exitCode = 1;
});