-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
28 lines (24 loc) · 934 Bytes
/
index.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
export type Environ = Record<string, string | undefined>;
export function expandUser(input: string, environ: Environ = process.env) {
if (typeof input !== 'string') {
throw new Error('The input type not is string');
}
const home = environ.HOME ?? environ.USERPROFILE;
return home ? input.replace(/^~/g, home) : input;
}
expandUser.bind = (environ: Environ) => (input: string) => expandUser(input, environ);
export function expandVars(input: string, environ: Environ = process.env) {
if (typeof input !== 'string') {
throw new Error('The input type not is string');
}
if (input.indexOf('$') === -1) {
return input;
}
return input.replace(/\$(\w+|\{[^}]*\})/g, (match, p1: string) => {
if (p1[0] === '{' && p1[p1.length - 1] === '}') {
p1 = p1.slice(1, -1);
}
return environ[p1] ?? match;
});
}
expandVars.bind = (environ: Environ) => (input: string) => expandVars(input, environ);