Skip to content

Commit 7cb3776

Browse files
committed
feat: add environment configuration and generation script for Contentstack
1 parent 41888d9 commit 7cb3776

File tree

7 files changed

+124
-19
lines changed

7 files changed

+124
-19
lines changed

.env.example

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Contentstack Configuration
2+
# Copy this file to .env and fill in your actual values
3+
NG_APP_CONTENTSTACK_API_KEY=your_api_key_here
4+
NG_APP_CONTENTSTACK_DELIVERY_TOKEN=your_delivery_token_here
5+
NG_APP_CONTENTSTACK_PREVIEW_TOKEN=your_preview_token_here
6+
NG_APP_CONTENTSTACK_ENVIRONMENT=preview
7+
NG_APP_CONTENTSTACK_REGION=EU
8+
NG_APP_CONTENTSTACK_PREVIEW=true

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ node_modules/
55
dist/
66
.angular/
77
.vscode/
8-
*.log
8+
*.log
9+
10+
src/environments/

README.md

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,29 @@ Go to Settings > Tokens and create a delivery token. Select the `preview` scope
6262

6363
> In the case of Angular 18, check the settings in the environment and make sure the url is: `http://localhost:4200/` instead of `http://localhost:3000/`
6464
65-
### Fill out your environment settings.
66-
67-
Now that you have a delivery token, you can fill out the `./src/environments/environment.ts` file in your codebase.
68-
69-
```js
70-
export const environment = {
71-
production: false,
72-
contentstack: {
73-
apiKey: "<YOUR_API_KEY>",
74-
deliveryToken: "<YOUR_DELIVERY_TOKEN>",
75-
previewToken: "<YOUR_PREVIEW_TOKEN>",
76-
environment: "preview",
77-
region: "EU",
78-
preview: true,
79-
},
80-
};
81-
```
65+
### Configure your environment variables
66+
67+
This project uses environment variables to keep API keys secure and out of version control.
68+
69+
1. Copy the `.env.example` file to `.env`:
70+
71+
```bash
72+
cp .env.example .env
73+
```
74+
75+
2. Fill out the `.env` file with your actual values:
76+
```env
77+
NG_APP_CONTENTSTACK_API_KEY=<YOUR_API_KEY>
78+
NG_APP_CONTENTSTACK_DELIVERY_TOKEN=<YOUR_DELIVERY_TOKEN>
79+
NG_APP_CONTENTSTACK_PREVIEW_TOKEN=<YOUR_PREVIEW_TOKEN>
80+
NG_APP_CONTENTSTACK_ENVIRONMENT=preview
81+
NG_APP_CONTENTSTACK_REGION=EU
82+
NG_APP_CONTENTSTACK_PREVIEW=true
83+
```
84+
85+
The environment files (`src/environments/environment.ts` and `src/environments/environment.production.ts`) are automatically generated from your `.env` file when you run `npm start` or `npm run build`.
86+
87+
> **Important:** Never commit the `.env` file to your repository. It's already included in `.gitignore` to prevent accidental commits of your API keys.
8288
8389
### Turn on Live Preview
8490

generate-env.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
6+
// Read .env file
7+
const envPath = path.join(__dirname, ".env");
8+
let envVars = {};
9+
10+
if (fs.existsSync(envPath)) {
11+
const envContent = fs.readFileSync(envPath, "utf8");
12+
envContent.split("\n").forEach((line) => {
13+
const [key, value] = line.split("=");
14+
if (key && value) {
15+
envVars[key.trim()] = value.trim();
16+
}
17+
});
18+
}
19+
20+
// Generate environment.ts content
21+
const environmentContent = `export const environment = {
22+
production: false,
23+
contentstack: {
24+
apiKey: '${envVars.NG_APP_CONTENTSTACK_API_KEY || ""}',
25+
deliveryToken: '${envVars.NG_APP_CONTENTSTACK_DELIVERY_TOKEN || ""}',
26+
previewToken: '${envVars.NG_APP_CONTENTSTACK_PREVIEW_TOKEN || ""}',
27+
environment: '${envVars.NG_APP_CONTENTSTACK_ENVIRONMENT || "preview"}',
28+
region: '${envVars.NG_APP_CONTENTSTACK_REGION || "EU"}',
29+
preview: ${envVars.NG_APP_CONTENTSTACK_PREVIEW === "true"},
30+
}
31+
};
32+
`;
33+
34+
// Generate environment.production.ts content
35+
const environmentProdContent = `export const environment = {
36+
production: true,
37+
contentstack: {
38+
apiKey: '${envVars.NG_APP_CONTENTSTACK_API_KEY || ""}',
39+
deliveryToken: '${envVars.NG_APP_CONTENTSTACK_DELIVERY_TOKEN || ""}',
40+
previewToken: '${envVars.NG_APP_CONTENTSTACK_PREVIEW_TOKEN || ""}',
41+
environment: '${envVars.NG_APP_CONTENTSTACK_ENVIRONMENT || "preview"}',
42+
region: '${envVars.NG_APP_CONTENTSTACK_REGION || "EU"}',
43+
preview: ${envVars.NG_APP_CONTENTSTACK_PREVIEW === "true"},
44+
}
45+
};
46+
`;
47+
48+
// Write the files
49+
fs.writeFileSync(
50+
path.join(__dirname, "src/environments/environment.ts"),
51+
environmentContent
52+
);
53+
fs.writeFileSync(
54+
path.join(__dirname, "src/environments/environment.production.ts"),
55+
environmentProdContent
56+
);
57+
58+
console.log("Environment files generated successfully!");

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kickstart-angular",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"private": false,
55
"repository": {
66
"type": "git",
@@ -12,7 +12,9 @@
1212
"license": "MIT",
1313
"scripts": {
1414
"ng": "ng",
15+
"prestart": "node generate-env.js",
1516
"start": "ng serve",
17+
"prebuild": "node generate-env.js",
1618
"build": "ng build"
1719
},
1820
"dependencies": {
@@ -35,6 +37,7 @@
3537
"@angular/compiler-cli": "20.1.6",
3638
"@types/react": "^19.1.9",
3739
"autoprefixer": "10.4.21",
40+
"dotenv": "^17.2.1",
3841
"postcss": "8.5.6",
3942
"typescript": "5.8.3"
4043
}

src/vite-env.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference types="vite/client" />
2+
3+
interface ImportMetaEnv {
4+
readonly NG_APP_CONTENTSTACK_API_KEY: string;
5+
readonly NG_APP_CONTENTSTACK_DELIVERY_TOKEN: string;
6+
readonly NG_APP_CONTENTSTACK_PREVIEW_TOKEN: string;
7+
readonly NG_APP_CONTENTSTACK_ENVIRONMENT: string;
8+
readonly NG_APP_CONTENTSTACK_REGION: string;
9+
readonly NG_APP_CONTENTSTACK_PREVIEW: string;
10+
}
11+
12+
interface ImportMeta {
13+
readonly env: ImportMetaEnv;
14+
}

0 commit comments

Comments
 (0)