Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Contentstack Configuration
# Copy this file to .env and fill in your actual values
NG_APP_CONTENTSTACK_API_KEY=your_api_key_here
NG_APP_CONTENTSTACK_DELIVERY_TOKEN=your_delivery_token_here
NG_APP_CONTENTSTACK_PREVIEW_TOKEN=your_preview_token_here
NG_APP_CONTENTSTACK_ENVIRONMENT=preview
NG_APP_CONTENTSTACK_REGION=EU
NG_APP_CONTENTSTACK_PREVIEW=true
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ node_modules/
dist/
.angular/
.vscode/
*.log
*.log

src/environments/
4 changes: 4 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ fileignoreconfig:
- filename: package-lock.json
ignore_detectors:
- filecontent
- filename: generate-env.js
checksum: 04444a19d0ac887d72b234606010e52e6c315b79e21e01fb78dc3bcb62c7f5e9
- filename: src/vite-env.d.ts
checksum: 768fe1afcd0be82f8d594d60700dc7e6eab02ea3f43e1f2a32bce9cc5439449c
version: "1.0"
40 changes: 23 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,29 @@ Go to Settings > Tokens and create a delivery token. Select the `preview` scope

> 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/`

### Fill out your environment settings.

Now that you have a delivery token, you can fill out the `./src/environments/environment.ts` file in your codebase.

```js
export const environment = {
production: false,
contentstack: {
apiKey: "<YOUR_API_KEY>",
deliveryToken: "<YOUR_DELIVERY_TOKEN>",
previewToken: "<YOUR_PREVIEW_TOKEN>",
environment: "preview",
region: "EU",
preview: true,
},
};
```
### Configure your environment variables

This project uses environment variables to keep API keys secure and out of version control.

1. Copy the `.env.example` file to `.env`:

```bash
cp .env.example .env
```

2. Fill out the `.env` file with your actual values:
```env
NG_APP_CONTENTSTACK_API_KEY=<YOUR_API_KEY>
NG_APP_CONTENTSTACK_DELIVERY_TOKEN=<YOUR_DELIVERY_TOKEN>
NG_APP_CONTENTSTACK_PREVIEW_TOKEN=<YOUR_PREVIEW_TOKEN>
NG_APP_CONTENTSTACK_ENVIRONMENT=preview
NG_APP_CONTENTSTACK_REGION=EU
NG_APP_CONTENTSTACK_PREVIEW=true
```

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`.

> **Important:** Never commit the `.env` file to your repository. It's already included in `.gitignore` to prevent accidental commits of your API keys.

### Turn on Live Preview

Expand Down
93 changes: 93 additions & 0 deletions generate-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/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)) {
const envContent = fs.readFileSync(envPath, "utf8");
envContent.split("\n").forEach((line) => {
const [key, value] = line.split("=");
if (key && value) {
envVars[key.trim()] = value.trim();
}
});
}

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!");
Loading