forked from SnailyCAD/snaily-cadv4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy-env.mjs
91 lines (73 loc) · 2.38 KB
/
copy-env.mjs
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
import "dotenv/config";
import process from "node:process";
import { one } from "copy";
import { join } from "node:path";
import { readFile, writeFile } from "node:fs/promises";
import { EOL } from "node:os";
const DEFAULT_PORT = "3000";
const UNIX_SLASHES_REGEX = /\/apps\/client/;
const WIN_SLASHES_REGEX = /\\apps\\client/;
async function addPortToClientPackageJson() {
if (process.env.NODE_ENV === "development") return;
try {
const port = process.env.PORT_CLIENT;
if (!port) return;
let dir = join(process.cwd(), "apps", "client");
const unixMatch = process.cwd().match(UNIX_SLASHES_REGEX);
const winMatch = process.cwd().match(WIN_SLASHES_REGEX);
if (unixMatch || winMatch) {
dir = process.cwd();
}
const jsonFilePath = join(dir, "package.json");
const json = JSON.parse(await readFile(jsonFilePath, "utf8"));
if (!json.scripts.start.includes(`-p ${DEFAULT_PORT}`) && port === DEFAULT_PORT) {
json.scripts.start = "pnpm next start"; // reset the port back to default
} else {
json.scripts.start = `pnpm next start -p ${port}`;
}
await writeFile(jsonFilePath, JSON.stringify(json, null, 2) + EOL);
} catch (e) {
console.log(e);
console.log("Could not set the PORT_CLIENT. Continuing build...");
}
}
const [, , ...args] = process.argv;
const copyToClient = hasArg("--client");
const copyToApi = hasArg("--api");
let ENV_FILE_PATH = join(process.cwd(), ".env");
if (ENV_FILE_PATH.endsWith("/apps/client/.env") || ENV_FILE_PATH.endsWith("/apps/api/.env")) {
ENV_FILE_PATH = ENV_FILE_PATH.replace(/apps\/(client|api)\//, "");
}
/**
* @param {string} distDir
*/
function copyEnv(distDir) {
try {
one(ENV_FILE_PATH, distDir, (error) => {
if (error) {
console.log({ error });
return;
}
const isClient = distDir.endsWith("client");
const isApi = distDir.endsWith("api");
const type = isClient ? "client" : isApi ? "api" : null;
if (type) {
console.log(`✅ copied .env — ${type}`);
}
});
} catch (e) {
console.log({ e });
}
}
if (copyToClient) {
const CLIENT_PACKAGE_PATH = join(process.cwd(), "apps", "client");
addPortToClientPackageJson();
copyEnv(CLIENT_PACKAGE_PATH);
}
if (copyToApi) {
const API_PACKAGE_PATH = join(process.cwd(), "apps", "api");
copyEnv(API_PACKAGE_PATH);
}
function hasArg(arg) {
return args.includes(arg);
}