-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcopy-env.js
49 lines (42 loc) · 1.45 KB
/
copy-env.js
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
const fs = require("fs");
const path = require("path");
function copyEnvFile(targetDir) {
// Ensure targetDir is a valid string and trim any whitespace
targetDir = typeof targetDir === "string" ? targetDir.trim() : "";
const examplePath = path.join(targetDir, ".env.example");
const envPath = 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) => {
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) => {
if (err) {
// .env file does not exist, copy .env.example to .env
fs.copyFile(examplePath, envPath, (err) => {
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 = process.argv[2]?.trim();
if (directoryPath) {
copyEnvFile(directoryPath);
} else {
console.error("Please provide a directory path as an argument.");
}