-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcopy-env.mts
48 lines (42 loc) · 1.47 KB
/
copy-env.mts
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
import * as fs from 'fs';
import * as path from 'path';
function copyEnvFile(targetDir: string): void {
// Ensure targetDir is trimmed
targetDir = targetDir.trim();
const examplePath: string = path.join(targetDir, ".env.example");
const envPath: string = path.join(targetDir, ".env");
console.log("Attempting to copy from:", examplePath);
console.log("To:", envPath);
// Check if .env.example exists
fs.access(examplePath, fs.constants.F_OK, (err: Error | null) => {
if (err) {
console.error(`.env.example file does not exist in ${targetDir}`);
return;
}
// .env.example exists, now check for .env
fs.access(envPath, fs.constants.F_OK, (err: Error | null) => {
if (err) {
// .env file does not exist, copy .env.example to .env
fs.copyFile(examplePath, envPath, (err: Error | null) => {
if (err) {
console.error("Error occurred:", err);
return;
}
console.log(`.env.example has been copied to ${envPath}`);
});
} else {
// .env file exists, no action needed
console.log(
`.env file already exists in ${targetDir}, no action taken.`
);
}
});
});
}
// Get the directory path from the command line argument and trim whitespace
const directoryPath: string | undefined = process.argv[2]?.trim();
if (directoryPath) {
copyEnvFile(directoryPath);
} else {
console.error("Please provide a directory path as an argument.");
}