This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathext.ts
102 lines (102 loc) · 3.09 KB
/
pathext.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
import process from "node:process";
import { envDelimitation } from "./_delimitation.ts";
const isOSWindows: boolean = (
(typeof Deno !== "undefined" && Deno.build.os === "windows") ||
process.platform === "win32"
);
/**
* Cross runtime environment variables `PATHEXT` interface.
*
* > **🛡️ Permissions**
* >
* > | **Target** | **Type** | **Coverage** |
* > |:--|:--|:--|
* > | Deno | Environment Variable (`allow-env`) | `PATHEXT` (For Windows Only) |
*/
export interface CrossEnvPathExt {
/**
* Add value to the environment variable `PATHEXT`.
*
* > **🛡️ Permissions**
* >
* > | **Target** | **Type** | **Coverage** |
* > |:--|:--|:--|
* > | Deno | Environment Variable (`allow-env`) | `PATHEXT` (For Windows Only) |
* @param {...string} values Value that need to add to the environment variable `PATHEXT`.
* @returns {void}
*/
add(...values: string[]): void;
/**
* Delete value from the environment variable `PATHEXT`.
*
* > **🛡️ Permissions**
* >
* > | **Target** | **Type** | **Coverage** |
* > |:--|:--|:--|
* > | Deno | Environment Variable (`allow-env`) | `PATHEXT` (For Windows Only) |
* @param {...string} values Value that need to delete from the environment variable `PATHEXT`.
* @returns {void}
*/
delete(...values: string[]): void;
/**
* Get the values of the environment variable `PATHEXT`; Always return `null` for non-Windows operate system.
*
* > **🛡️ Permissions**
* >
* > | **Target** | **Type** | **Coverage** |
* > |:--|:--|:--|
* > | Deno | Environment Variable (`allow-env`) | `PATHEXT` (For Windows Only) |
* @returns {string[] | null} Values of the environment variable `PATHEXT`.
*/
get(): string[] | null;
}
/**
* Cross runtime environment variables `PATHEXT` interface.
*
* > **🛡️ Permissions**
* >
* > | **Target** | **Type** | **Coverage** |
* > |:--|:--|:--|
* > | Deno | Environment Variable (`allow-env`) | `PATHEXT` (For Windows Only) |
*/
export const envPathExt: CrossEnvPathExt = Object.freeze({
add(...values: string[]): void {
values.forEach((value: string): void => {
if (!value.startsWith(".")) {
throw new SyntaxError(`\`${value}\` is not an valid file extension!`);
}
});
if (isOSWindows && values.length > 0) {
const target: Set<string> = envDelimitation.get("PATHEXT");
for (const value of values) {
target.add(value.toUpperCase());
}
envDelimitation.set("PATHEXT", target);
}
},
delete(...values: string[]): void {
values.forEach((value: string): void => {
if (!value.startsWith(".")) {
throw new SyntaxError(`\`${value}\` is not an valid file extension!`);
}
});
if (isOSWindows && values.length > 0) {
const target: Set<string> = envDelimitation.get("PATHEXT");
for (const value of values) {
target.delete(value);
}
envDelimitation.set("PATHEXT", target);
}
},
get(): string[] | null {
if (!isOSWindows) {
return null;
}
const values: Set<string> = envDelimitation.get("PATHEXT");
if (values.size > 0) {
return Array.from(values.values());
}
return [".EXE", ".CMD", ".BAT", ".COM"];
}
});
export default envPathExt;