-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathglob.ts
More file actions
120 lines (103 loc) · 3.43 KB
/
glob.ts
File metadata and controls
120 lines (103 loc) · 3.43 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
import chalk from "chalk";
import { logger } from "./logger";
/**
* Turn expression into a valid regexp
*
* @param glob A string containing a valid wildcard expression
* @returns a string containing a valid RegExp
*/
export function globToRegExp(glob: string | undefined) {
logger.silly(`turning glob expression into valid RegExp`);
logger.silly(` - glob: ${chalk.yellow(glob)}`);
if (!glob) {
logger.silly(` - glob is empty, return empty string`);
return "";
}
if (glob === "*") {
logger.silly(` - glob is ${chalk.yellow("*")}, return ${chalk.yellow(".*")}`);
return ".*";
}
const filesExtensionMatch = glob.match(/{.*}/);
if (filesExtensionMatch) {
const filesExtensionExpression = filesExtensionMatch[0];
if (filesExtensionExpression) {
// build a regex group (png|jpg|gif)
const filesExtensionRegEx = filesExtensionExpression.replace(/\,/g, "|").replace("{", "(").replace("}", ")");
glob = glob.replace(filesExtensionExpression, filesExtensionRegEx);
}
}
return glob.replace(/\//g, "\\/").replace(".", "\\.").replace("*", ".*");
}
/**
* Check if the route rule contains a valid wildcard expression
*
* @param glob A string containing a valid wildcard expression
* @returns true if the glob expression is valid, false otherwise
* @see https://docs.microsoft.com/azure/static-web-apps/configuration#wildcards
*/
export function isValidGlobExpression(glob: string | undefined) {
logger.silly(`checking if glob expression is valid`);
logger.silly(` - glob: ${chalk.yellow(glob)}`);
if (!glob) {
logger.silly(` - glob is empty. Return false`);
return false;
}
if (glob === "*") {
logger.silly(` - glob is *`);
return true;
}
const hasWildcard = glob.includes("*");
if (hasWildcard) {
const paths = glob.split("*");
if (paths.length > 2) {
logger.silly(` - glob has more than one wildcard. Return false`);
return false;
}
const pathBeforeWildcard = paths[0];
if (pathBeforeWildcard && glob.endsWith("*")) {
logger.silly(` - glob ends with *. Return true`);
return true;
}
const pathAfterWildcard = paths[1];
if (pathAfterWildcard) {
logger.silly(` - pathAfterWildcard: ${chalk.yellow(pathAfterWildcard)}`);
if (isBalancedCurlyBrackets(glob) === false) {
logger.silly(` - pathAfterWildcard contains unbalanced { } syntax. Return false`);
return false;
}
// match exactly extensions of type:
// --> /blog/*.html
// --> /blog/*.{html,jpg}
const filesExtensionMatch = pathAfterWildcard.match(/\.(\w+|\{\w+(,\w+)*\})$/);
if (filesExtensionMatch) {
logger.silly(` - pathAfterWildcard match a file extension. Return true`);
return true;
} else {
logger.silly(` - pathAfterWildcard doesn't match a file extension. Return false`);
return false;
}
}
}
return false;
}
/**
* Checks if a string expression has balanced curly brackets
*
* @param str the string expression to be checked
* @returns true if the string expression has balanced curly brackets, false otherwise
*/
export function isBalancedCurlyBrackets(str: string) {
const stack = [];
for (let i = 0; i < str.length; i++) {
const char = str[i];
if (char === "{") {
stack.push(char);
} else if (char === "}") {
if (stack.length === 0) {
return false;
}
stack.pop();
}
}
return stack.length === 0;
}