-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·149 lines (131 loc) · 4.45 KB
/
Copy pathcli.js
File metadata and controls
executable file
·149 lines (131 loc) · 4.45 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
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
#!/usr/bin/env node
'use strict';
const { compile, match, build, createRouter, paramNames, isMatch, compare } = require('./index.js');
function usage() {
console.log(`pathmatch — URL path pattern matching
Usage:
pathmatch match <pattern> <path> Match a path against a pattern
pathmatch params <pattern> List parameter names in a pattern
pathmatch build <pattern> <key=val>.. Build a URL from params
pathmatch test <pattern> <path> Boolean match check
pathmatch rank <p1> <p2> [p3..] Rank patterns by specificity
pathmatch routes <pattern> <path>.. Test multiple paths against a pattern
Options:
--sensitive Case-sensitive matching
--no-end Allow partial matches (don't require full path)
--json Output as JSON
Examples:
pathmatch match '/users/:id' '/users/42'
pathmatch build '/users/:id/posts/:postId' id=42 postId=7
pathmatch params '/users/:userId/posts/:postId'
pathmatch rank '/users/profile' '/users/:id' '/users/*'
pathmatch test '/files/*' '/files/a/b/c.txt' --json`);
}
const args = process.argv.slice(2);
if (args.length === 0) {
usage();
process.exit(0);
}
const cmd = args[0];
let rest = args.slice(1);
// Parse flags
const json = rest.includes('--json');
const sensitive = rest.includes('--sensitive');
const noEnd = rest.includes('--no-end');
rest = rest.filter(a => !a.startsWith('--'));
const opts = { sensitive, end: !noEnd };
function jsonOut(data) {
console.log(JSON.stringify(data, null, 2));
}
try {
switch (cmd) {
case 'match': {
const [pattern, path] = rest;
if (!pattern || !path) { console.error('Usage: pathmatch match <pattern> <path>'); process.exit(1); }
const c = compile(pattern, opts);
const m = match(c, path);
if (!m) {
if (json) jsonOut({ matched: false });
else console.log('no match');
process.exit(1);
}
if (json) jsonOut({ matched: true, params: m.params, path: m.matched });
else {
console.log('match ✓');
for (const [k, v] of Object.entries(m.params)) {
console.log(` ${k} = ${v}`);
}
}
break;
}
case 'params': {
const [pattern] = rest;
if (!pattern) { console.error('Usage: pathmatch params <pattern>'); process.exit(1); }
const names = paramNames(pattern);
if (json) jsonOut({ pattern, params: names });
else names.forEach(n => console.log(n));
break;
}
case 'build': {
const [pattern, ...kvArgs] = rest;
if (!pattern) { console.error('Usage: pathmatch build <pattern> <key=val>..'); process.exit(1); }
const params = {};
for (const kv of kvArgs) {
const [k, ...v] = kv.split('=');
params[k] = v.join('=');
}
const result = build(pattern, params);
if (json) jsonOut({ pattern, params, result });
else console.log(result);
break;
}
case 'test': {
const [pattern, path] = rest;
if (!pattern || !path) { console.error('Usage: pathmatch test <pattern> <path>'); process.exit(1); }
const result = isMatch(pattern, path, opts);
if (json) jsonOut({ pattern, path, matched: result });
else console.log(result ? 'true' : 'false');
process.exit(result ? 0 : 1);
break;
}
case 'rank': {
if (rest.length < 2) { console.error('Usage: pathmatch rank <p1> <p2> [p3..]'); process.exit(1); }
const ranked = rest
.map(p => ({ pattern: p, score: compile(p, opts).score }))
.sort((a, b) => a.score - b.score);
if (json) jsonOut({ ranked });
else {
console.log('Most → least specific:');
ranked.forEach((r, i) => console.log(` ${i + 1}. ${r.pattern} (score: ${r.score})`));
}
break;
}
case 'routes': {
const [pattern, ...paths] = rest;
if (!pattern || paths.length === 0) { console.error('Usage: pathmatch routes <pattern> <path>..'); process.exit(1); }
const results = paths.map(path => ({
path,
matched: isMatch(pattern, path, opts),
}));
if (json) jsonOut({ pattern, results });
else {
for (const r of results) {
console.log(`${r.matched ? '✓' : '✗'} ${r.path}`);
}
}
break;
}
case 'help':
case '--help':
case '-h':
usage();
break;
default:
console.error(`Unknown command: ${cmd}`);
usage();
process.exit(1);
}
} catch (e) {
console.error('Error: ' + e.message);
process.exit(1);
}