-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
96 lines (80 loc) · 2.38 KB
/
Copy pathutils.ts
File metadata and controls
96 lines (80 loc) · 2.38 KB
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
import * as Colors from 'https://deno.land/std@0.141.0/fmt/colors.ts';
const flags = ['-p', '-d', '-h', '--host'] as const;
type ArgType = typeof flags[number];
export function createArgumentMap() {
function isValidFlag(flag: string): flag is typeof flags[number] {
return flags.includes(flag as ArgType);
}
const argumentMap = Object.fromEntries(
flags.map((flag) => [flag, undefined]),
) as Record<typeof flags[number], string | undefined>;
Deno.args.forEach((argument, index, array) => {
if (argument === '-h') {
argumentMap[argument] = 'true';
return;
}
if (index === array.length - 1) {
return;
}
if (isValidFlag(argument)) {
argumentMap[argument] = array[index + 1];
}
});
return argumentMap;
}
export function log(...input: Parameters<typeof console.log>) {
console.log(input);
}
log.info = (...input: Parameters<typeof console.info>) => {
console.info(`${Colors.gray('>')} ${input}`);
};
log.error = (...input: Parameters<typeof console.error>) => {
console.error(`${Colors.red('!')} ${input}`);
};
log.Colors = Colors;
log.fsEvent = (event: Deno.FsEvent) => {
let color: keyof typeof Colors;
switch (event.kind) {
case 'access':
return;
case 'create':
color = 'green';
break;
case 'remove':
color = 'red';
break;
default:
color = 'yellow';
break;
}
console.log(`${Colors[color](event.kind)}: ${event.paths}`);
};
log.help = () => {
const descriptions: Record<ArgType, string> = {
'-p': 'define port for server to listen to, default is 3000',
'--host': 'define server hostname, defaults to 0.0.0.0',
'-d':
'specify directory to be watched for changes, default is current directory',
'-h': 'show help\n',
};
console.log('\nUSAGE:');
console.group();
console.log('html-dev [OPTIONS]\n');
console.groupEnd();
console.log('OPTIONS:');
console.group();
Object.entries(descriptions).forEach(([flag, description]) =>
console.log(`${flag}: ${description}`)
);
console.groupEnd();
};
// Enable syntax highlighting with bierner.lit-html extension, return input as is
export const html = (
stringSlices: TemplateStringsArray,
...interpolations: unknown[]
) => {
return stringSlices.reduce((prev, current, index) => {
const expression = interpolations[index] ?? '';
return `${prev}${current}${expression}`;
}, '');
};