Skip to content

Commit

Permalink
chore: Filters none existing tenant/env combinations out before gener…
Browse files Browse the repository at this point in the history
…ating config (#290)
  • Loading branch information
amir-zahedi authored Jan 29, 2025
1 parent 313f389 commit d1e0bb7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/gold-days-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'gdu': minor
---

GDU: Filters none existing tenant/env combinations out before generating config
json files
43 changes: 38 additions & 5 deletions packages/gdu/commands/global-configs/config-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import * as fs from 'fs';
import * as path from 'path';

import * as dotenv from 'dotenv';
import { blue, dim } from 'kleur';


import { getTokens } from '../../lib/globalConfigs';

const envs = ['uat', 'preprod', 'dev', 'prod_build', 'test', 'tokens'];
const envs = ['uat', 'preprod', 'dev', 'prod_build', 'test', 'tokens', 'shared'];
const tenants = ['au', 'nz', 'global'];
type ENV = (typeof envs)[number];
type TENANT = (typeof tenants)[number];
type TENANT = (typeof tenants)[number] ;


export default async () => {
console.log('Global config tokens started');
Expand Down Expand Up @@ -37,13 +40,26 @@ export default async () => {
);

const copyTokens = () => {
const prodFile = path.join(process.cwd(), '.gdu_config', '.env.prod');
const prodFile = path.join(
process.cwd(),
'.gdu_config',
'.env.prod',
);
const tokensFile = path.join(
process.cwd(),
'.gdu_config',
'.env.tokens',
);

// Create .gdu_config directory if it doesn't exist
fs.mkdirSync(path.dirname(tokensFile), { recursive: true });

// Check if prod file exists
if (!fs.existsSync(prodFile)) {
console.log(`${dim('Info:')} Production file ${blue(prodFile)} not found, skipping...`);
return;
}

// Extract text from prod file
const prodText = fs.readFileSync(prodFile, 'utf8');
// replace #{*****} with "#{*****}" where ***** can be any value with any length
Expand All @@ -64,10 +80,18 @@ export default async () => {
'.gdu_config',
tenant ? `.env.${env}_${tenant}` : `.env.${env}`,
);

// Show informative message if the env file does not exist
if (!fs.existsSync(envFile)) {
console.log(`${dim('Info:')} Environment file ${blue(envFile)} not found, skipping...`);
return;
}

dotenv.config({ path: [defaultsFile, envFile], override: true });

const FILTERED_TOKENS = Object.keys(TOKENS).reduce((acc, key) => {
if (process.env[key]) acc[key] = process.env[key];
if (process.env[key])
acc[key] = process.env[key];
return acc;
}, {});

Expand All @@ -84,6 +108,13 @@ export default async () => {
);
};

// Create directories if they don't exist
fs.mkdirSync(destinationFolder, { recursive: true });
// clear all files in the destination folder
fs.readdirSync(destinationFolder).forEach((file) => {
fs.unlinkSync(path.join(destinationFolder, file))
});

envs.forEach((env: ENV) => {
if (env !== 'tokens') {
tenants.forEach((tenant: TENANT) => {
Expand All @@ -92,6 +123,8 @@ export default async () => {
} else {
generateTokens(env);
}

});
console.log('Global config tokens finished');
};

}

0 comments on commit d1e0bb7

Please sign in to comment.