-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirstamp.ts
executable file
·178 lines (150 loc) · 4.49 KB
/
dirstamp.ts
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
#!/usr/bin/env node
"use strict";
import * as klaw from "klaw";
import * as path from "path";
import * as fs from "fs";
import * as chalk from "chalk";
import { EOL } from "os";
import strip = require("strip-ansi");
const opts: {
depthLimit: number,
preserveSymlinks: boolean,
queueMethod: klaw.QueueMethod
} = {
depthLimit: -1,
preserveSymlinks: false,
queueMethod: "shift"
},
custom: {
json: boolean;
check: string;
watch: boolean;
nocolors: boolean; //OBS
} = {
json: false,
check: null,
watch: false,
nocolors: false
};
function ensure(pt: klaw.Item): void {
pt.path = path.relative(path.resolve(pth), pt.path);
let pths: string[] = pt.path.split(path.sep).slice(0, -1),
obj: { } = struct,
par: { } = struct;
for (var i of pths) {
par = obj;
obj = (obj[i] = obj[i] || { });
}
if (i && par[i]) {
par[i] = (par[i] = par[i] || { });
par[i][pt.path.split(path.sep).pop()] = pt.stats.isDirectory() ? { } : '';
} else {
par[pt.path.split(path.sep).pop()] = pt.stats.isDirectory() ? { } : '';
}
} //ensure
function breakdown(root: { } = struct, offset: number = 0, key: string): string {
let tb: string = '\t'.repeat(offset),
ret: string = EOL;
if (typeof root === "string") {
ret += custom.nocolors ? tb + key : chalk["cyan"]["dim"](tb + key);
} else {
//@ts-ignore
ret += custom.nocolors ? tb + key : chalk[strip(key).trim().endsWith("G:") ? "red" : (strip(key).trim().endsWith("S:") ? "green" : "yellow")]["dim"](tb + key);
for (let i in root) {
ret += breakdown(root[i], offset + 1, i);
}
}
return ret;
} //breakdown
function checkValidity(struct: { }, base: { } = check, missing: { } = { }): object[] {
missing = missing || { };
for (let i in struct) {
if (typeof struct[i] === "string") {
if (!(i in base)) {
missing[i] = struct[i];
}
delete base[i];
} else {
checkValidity(struct[i], base[i] = base[i] || { }, missing[i] = missing[i] || { });
if (!Object.keys(base[i]).length) {
delete base[i];
}
}
}
return [prune("missing", { missing: missing }), base];
} //checkValidity
function prune(prop: string, targ: {}): { } {
if (!Object.keys(targ[prop]).length && typeof targ[prop] !== "string") {
delete targ[prop];
} else if (typeof targ[prop] !== "string") {
for (let i in targ[prop]) {
prune(i, targ[prop]);
}
}
return targ;
} //prune
let copy: string[] = Array.from(process.argv.slice(2)),
struct: {} = { },
pth: string = process.cwd(),
check: {} = {};
if (copy.includes("-h")) {
console.info("Run 'man dirstamp'.");
process.exit(0);
}
opts.preserveSymlinks = /-.*s/.test(copy.join(' '));
custom.json = /-.*j/.test(copy.join(' '));
(custom.nocolors = /-.*C/.test(copy.join(' '))) && (chalk["level"] = 0);
custom.watch = /-.*w/.test(copy.join(' '));
opts.queueMethod = <klaw.QueueMethod>(/-.*p/.test(copy.join(' ')) ? "pop" : "shift");
if (copy.includes("-d")) {
let idx: number;
opts.depthLimit = Number.parseInt(copy[(idx = copy.findIndex((val: string): boolean => val === "-d")) + 1]);
if (isNaN(opts.depthLimit)) {
console.error(chalk["italic"]("INVALID DEPTH!\t-d"), chalk["red"](copy[idx + 1]));
process.exit(1);
}
copy.splice(idx, 2);
}
if (copy.includes("-c")) {
let idx: number;
custom.check = path.normalize(copy[(idx = copy.findIndex((val: string): boolean => val === "-c")) + 1]);
try {
check = JSON.parse(fs.readFileSync(custom.check, "utf8"));
} catch (err) {
console.error(chalk["redBright"](err.message));
process.exit(err.code);
}
copy.splice(idx, 2);
}
/*
* RESERVED:
* -p, -s, -j, -C, -c, -w, -d, -h
*/
copy = copy.filter((v: string): boolean => !v.startsWith('-'));
if (custom.watch) {
fs.watch(pth, {
persistent: true,
recursive: true
}, (evt: string, file: string): void => {
console.log(chalk["bold"](evt), chalk["underline"](file), chalk["gray"](Date()));
});
} else {
klaw(pth = copy.join(' ') || pth, <klaw.Options>opts)
.on("data", (item: klaw.Item): void => {
ensure(item);
}).once("end", (): void => {
delete struct[''];
if (custom.check) {
let out: object[] = checkValidity(struct);
//@ts-ignore
process.stdout.write(breakdown(out[0].missing, 0, chalk["underline"]("EXTRANEOUS:") + EOL));
process.stdout.write(breakdown(out[1], 0, chalk["underline"]("MISSING:") + EOL) + EOL);
} else if (custom.json) {
process.stdout.write(JSON.stringify(struct, null, 0));
} else {
process.stdout.write(breakdown(struct, 0, path.basename(pth)) + EOL);
}
}).once("error", (err: Error): void => {
console.error(chalk["red"](err.message));
});
}