-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
152 lines (122 loc) · 3.65 KB
/
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
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
150
151
152
#!/usr/bin/env node
import * as dotenv from "dotenv";
dotenv.config();
import { transform } from "sucrase";
import fs from "fs";
import path from "path";
const configLocation = "./env-assert.config.ts";
const getConfigFileAsJS = async () => {
// @ts-ignore
const config = await import("./env-assert.config.js");
return config;
};
const checkForConfigFile = (): boolean => {
try {
fs.readFileSync(configLocation, "utf8");
return true;
} catch (e) {
return false;
}
};
const attemptTranspileConfigFile = (): boolean => {
try {
const compiledCode = transform(fs.readFileSync(configLocation, "utf8"), {
transforms: ["typescript", "imports"],
}).code;
fs.writeFileSync(
`${path.resolve(__dirname)}/env-assert.config.js`,
compiledCode
);
return true;
} catch (e) {
// @ts-ignore
console.log(e?.stdout?.toString());
// @ts-ignore
console.log(e?.stderr?.toString());
return false;
}
};
const validateConfigFile = (file: any) => {
if (!file.default) {
console.log("Config file must export a default export");
process.exit(1);
}
if (!file.default?.required) {
console.log(
"The Default export in the config file must have a required property"
);
process.exit(1);
}
if (Array.isArray(file.default?.required) === false) {
console.log(
"The required property in the config file default export must be an array of strings"
);
process.exit(1);
}
};
const exampleConfigFileTxt = `import type { CreateEnvVarsType } from "env-assert";
const required = ["FOO"] as const;
const optional = ["BAR"] as const;
const config = {
required,
optional,
};
export default config;
export type EnvVars = CreateEnvVarsType<typeof config>;
`;
const createExampleConfigFile = () => {
fs.writeFileSync(configLocation, exampleConfigFileTxt);
console.log("Created an example config file, env-assert-config.ts 🫡");
};
//--------------------------------------------
(async () => {
if (checkForConfigFile() === false) {
console.log(
"Could not find config file, it should be env-assert.config.ts in the root"
);
createExampleConfigFile();
process.exit(1);
}
if (attemptTranspileConfigFile() === false) {
console.log(
"Failed to transpile config file, please ensure TypeScript is installed and the config file has no errors"
);
process.exit(1);
}
const configFile = await getConfigFileAsJS();
validateConfigFile(configFile);
const requiredEnvVars = configFile.default?.required as string[];
const optionalEnvVars = configFile.default?.optional as string[];
const errorsArray: string[] = [];
const verifiedArray: string[] = [];
requiredEnvVars.forEach((envVar) => {
if (!process.env[envVar]) {
errorsArray.push(`[ Required Env Var ] ${envVar} is not defined ❌`);
} else {
verifiedArray.push(`[ Required Env Var ] ${envVar} is defined ✅`);
}
});
if (verifiedArray.length) {
verifiedArray.forEach((e) => console.log(e));
}
optionalEnvVars?.forEach((optionalEnvVar) => {
if (!process.env[optionalEnvVar]) {
console.log(`[ Optional Env Var ] ${optionalEnvVar} is not defined`);
}
});
if (errorsArray.length) {
errorsArray.forEach((e) => console.log(e));
process.exit(1);
}
})();
//--------------------------------------------
export type CreateEnvVarsType<
T extends {
required: readonly string[];
optional?: readonly string[];
}
> = T extends { optional: readonly string[] }
? { [Property in T["required"][number]]: string } & {
[Property in T["optional"][number]]: string | undefined;
}
: { [Property in T["required"][number]]: string };