-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-env.js
More file actions
executable file
·101 lines (89 loc) · 3.01 KB
/
generate-env.js
File metadata and controls
executable file
·101 lines (89 loc) · 3.01 KB
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
#!/usr/bin/env node
const {
getContentstackEndpoints,
getRegionForString,
} = require("@timbenniks/contentstack-endpoints");
const fs = require("fs");
const path = require("path");
// Read .env file
const envPath = path.join(__dirname, ".env");
let envVars = {};
if (fs.existsSync(envPath)) {
// Development: read from .env file
const envContent = fs.readFileSync(envPath, "utf8");
envContent.split("\n").forEach((line) => {
const [key, value] = line.split("=");
if (key && value) {
envVars[key.trim()] = value.trim();
}
});
} else {
// Production: read from process.env
Object.keys(process.env).forEach((key) => {
if (key.startsWith('NG_APP_CONTENTSTACK_')) {
envVars[key] = process.env[key];
}
});
}
const region = getRegionForString(envVars.NG_APP_CONTENTSTACK_REGION);
const endpoints = getContentstackEndpoints(region, true);
// Generate environment.ts content
const environmentContent = `export const environment = {
production: false,
contentstack: {
apiKey: '${envVars.NG_APP_CONTENTSTACK_API_KEY || ""}',
deliveryToken: '${envVars.NG_APP_CONTENTSTACK_DELIVERY_TOKEN || ""}',
previewToken: '${envVars.NG_APP_CONTENTSTACK_PREVIEW_TOKEN || ""}',
environment: '${envVars.NG_APP_CONTENTSTACK_ENVIRONMENT || "preview"}',
region: '${region ? region : envVars.NG_APP_CONTENTSTACK_REGION}',
preview: ${envVars.NG_APP_CONTENTSTACK_PREVIEW === "true"},
contentDelivery: '${
envVars.NG_APP_CONTENTSTACK_CONTENT_DELIVERY ||
(endpoints && endpoints.contentDelivery)
}',
previewHost: '${
envVars.NG_APP_CONTENTSTACK_PREVIEW_HOST ||
(endpoints && endpoints.preview)
}',
applicationHost: '${
envVars.NG_APP_CONTENTSTACK_CONTENT_APPLICATION ||
(endpoints && endpoints.application)
}'
}
};
`;
// Generate environment.production.ts content
const environmentProdContent = `export const environment = {
production: true,
contentstack: {
apiKey: '${envVars.NG_APP_CONTENTSTACK_API_KEY || ""}',
deliveryToken: '${envVars.NG_APP_CONTENTSTACK_DELIVERY_TOKEN || ""}',
previewToken: '${envVars.NG_APP_CONTENTSTACK_PREVIEW_TOKEN || ""}',
environment: '${envVars.NG_APP_CONTENTSTACK_ENVIRONMENT || "preview"}',
region: '${region ? region : envVars.NG_APP_CONTENTSTACK_REGION}',
preview: ${envVars.NG_APP_CONTENTSTACK_PREVIEW === "true"},
contentDelivery: '${
envVars.NG_APP_CONTENTSTACK_CONTENT_DELIVERY ||
(endpoints && endpoints.contentDelivery)
}',
previewHost: '${
envVars.NG_APP_CONTENTSTACK_PREVIEW_HOST ||
(endpoints && endpoints.preview)
}',
applicationHost: '${
envVars.NG_APP_CONTENTSTACK_CONTENT_APPLICATION ||
(endpoints && endpoints.application)
}'
}
};
`;
// Write the files
fs.writeFileSync(
path.join(__dirname, "src/environments/environment.ts"),
environmentContent
);
fs.writeFileSync(
path.join(__dirname, "src/environments/environment.production.ts"),
environmentProdContent
);
console.log("Environment files generated successfully!");