Skip to content

Commit

Permalink
chore: Removes AU Legacy tenant (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
amir-zahedi authored Jan 27, 2025
1 parent 48c1db0 commit b10adcc
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 94 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-avocados-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gdu': major
---

GDU: AU Legacy tenant is removed
77 changes: 48 additions & 29 deletions packages/gdu/commands/global-configs/config-tenants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import * as dotenv from 'dotenv';

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

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

const environmentOffsets: Record<string, number> = {
dev: 1,
Expand All @@ -17,25 +17,34 @@ const environmentOffsets: Record<string, number> = {
preprod: 3,
prod: 1,
tokens: 0,
shared: 1,
};

const scopeOffsets: Record<string, number> = {
'global': 1,
'au': 2,
'nz': 3,
'au-legacy': 2,
global: 1,
au: 2,
nz: 3,
shared: 0,
};

const mfeApplicationOffset = 0;

const mapLBPriority = (value: string, env: ENV, tenant?: TENANT) => {
const envOffset = environmentOffsets[env.toLowerCase()] ?? 0;
const scopeOffset = tenant ? scopeOffsets[tenant.toLowerCase()] ?? 0 : 0;
return (envOffset * 10000) + (scopeOffset * 1000) + (mfeApplicationOffset + parseInt(value, 10) || 0);
}
return (
envOffset * 10_000 +
scopeOffset * 1000 +
(mfeApplicationOffset + Number.parseInt(value, 10) || 0)
);
};

const tokenMap: Record<string, (value: string, env: ENV, tenant?: TENANT) => any> = {
appListenerPriority: (value, env, tenant) => mapLBPriority(value, env, tenant),
const tokenMap: Record<
string,
(value: string, env: ENV, tenant?: TENANT) => any
> = {
appListenerPriority: (value, env, tenant) =>
mapLBPriority(value, env, tenant),
};

export default async () => {
Expand Down Expand Up @@ -91,12 +100,11 @@ export default async () => {
}
};

// force delete destination folder if it exists
// TODO: Add this feautre back after AUL retirement is complete.
//if (fs.existsSync(destinationFolder)) {
// fs.rmSync(destinationFolder, { recursive: true });
//}
// create destination folder
// force delete destination folder if it exists
if (fs.existsSync(destinationFolder)) {
fs.rmSync(destinationFolder, { recursive: true });
}
// create destination folder
if (!fs.existsSync(destinationFolder)) {
fs.mkdirSync(destinationFolder, { recursive: true });
}
Expand All @@ -118,18 +126,26 @@ export default async () => {
);

const defaultEnvFile = path.join(locationFolder, `.env.defaults`);
const envFile = path.join(locationFolder, tenant ? `.env.${env}_${tenant}` : `.env.${env}`);
const envFile = path.join(
locationFolder,
tenant ? `.env.${env}_${tenant}` : `.env.${env}`,
);

return [fs.existsSync(defaultEnvFile)?defaultEnvFile:null, envFile];
}
return [fs.existsSync(defaultEnvFile) ? defaultEnvFile : null, envFile];
};

const generateTokens = (envFiles: string[], mfe, env: ENV, tenant?: TENANT) => {
if(!Array.isArray(envFiles)) {
const generateTokens = (
envFiles: string[],
mfe,
env: ENV,
tenant?: TENANT,
) => {
if (!Array.isArray(envFiles)) {
throw new TypeError('envFile should be an array');
}
const fileStats = fs.statSync(envFiles[1]);// if the config file is empty then also ignore the defaults
const fileStats = fs.statSync(envFiles[1]); // if the config file is empty then also ignore the defaults
const dirPathMfeApp = path.join(destinationFolder, `${mfe}`);
const filteredEnvFiles = envFiles.filter(Boolean)
const filteredEnvFiles = envFiles.filter(Boolean);
if (!fs.existsSync(dirPathMfeApp)) {
fs.mkdirSync(dirPathMfeApp);
}
Expand All @@ -144,11 +160,15 @@ export default async () => {
);
const FILTERED_TOKENS = Object.keys(TOKENS).reduce((acc, key) => {
if (fileContent.includes(key)) {
const mapperEntry = Object.keys(tokenMap).find(
mapKey => key.toLowerCase().startsWith(mapKey.toLowerCase()),
const mapperEntry = Object.keys(tokenMap).find((mapKey) =>
key.toLowerCase().startsWith(mapKey.toLowerCase()),
);
if (mapperEntry) {
acc[key] = tokenMap[mapperEntry](process.env[key]!, env, tenant);
acc[key] = tokenMap[mapperEntry](
process.env[key]!,
env,
tenant,
);
} else {
acc[key] = process.env[key];
}
Expand All @@ -165,7 +185,6 @@ export default async () => {
}
};


mfeProjects.forEach((mfe) => {
envs.forEach((env) => {
if (env === 'tokens') {
Expand All @@ -185,4 +204,4 @@ export default async () => {
});

console.log('MFE app config tokens finished');
}
};
17 changes: 5 additions & 12 deletions packages/gdu/commands/global-configs/config-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import * as dotenv from 'dotenv';
import { getTokens } from '../../lib/globalConfigs';

const envs = ['uat', 'preprod', 'dev', 'prod_build', 'test', 'tokens'];
const tenants = ['au', 'nz', 'au-legacy', 'global'];
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 @@ -38,11 +37,7 @@ 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',
Expand Down Expand Up @@ -72,8 +67,7 @@ export default async () => {
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 @@ -100,5 +94,4 @@ export default async () => {
}
});
console.log('Global config tokens finished');

}
};
47 changes: 33 additions & 14 deletions packages/gdu/commands/global-configs/generate-configs-files.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,54 @@
import fs from 'fs';
import * as path from 'path';

import { camelCaseToUpperSnakeCase, getInterfaceKeys } from '../../lib/globalConfigs';
import {
camelCaseToUpperSnakeCase,
getInterfaceKeys,
} from '../../lib/globalConfigs';

export default async () => {
console.log('Global config generate configs files started');
const tokensFile = path.resolve( 'packages', 'global-configs', 'processEnvs.d.ts');
const tokensFile = path.resolve(
'packages',
'global-configs',
'processEnvs.d.ts',
);
const keys = await getInterfaceKeys(tokensFile);
if(!keys) {
if (!keys) {
console.log('No keys found');
return;
}

const createTokenFile = (key: string) => {
const fileName = `${camelCaseToUpperSnakeCase(key)}.ts`;
const fileContent = `//Exported value for process.env.${key}\n` +
const fileContent =
`//Exported value for process.env.${key}\n` +
`//Always use values exported from this package instead of process.env.XXX directly\n` +
`export default process.env.${key};\n`;
const filePath = path.join('packages', 'global-configs', 'configs', fileName);
const filePath = path.join(
'packages',
'global-configs',
'configs',
fileName,
);

fs.writeFileSync(filePath, fileContent, 'utf8');

}
};

const createIndexFile = (keys: string[]) => {
const content = keys.map(key => {
const upperSnakeCaseKey = camelCaseToUpperSnakeCase(key);
return `export { default as ${upperSnakeCaseKey} } from './${upperSnakeCaseKey}';`;
}).join('\n');

const indexFilePath = path.join('packages', 'global-configs', 'configs', 'index.ts');
const content = keys
.map((key) => {
const upperSnakeCaseKey = camelCaseToUpperSnakeCase(key);
return `export { default as ${upperSnakeCaseKey} } from './${upperSnakeCaseKey}';`;
})
.join('\n');

const indexFilePath = path.join(
'packages',
'global-configs',
'configs',
'index.ts',
);

fs.writeFileSync(indexFilePath, content, 'utf8');
};
Expand All @@ -39,4 +58,4 @@ export default async () => {
}
createIndexFile(keys as string[]);
console.log('Global config generate configs files finished');
}
};
32 changes: 24 additions & 8 deletions packages/gdu/commands/global-configs/generate-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,35 @@ import { getInterfaceKeys } from '../../lib/globalConfigs';
export default async () => {
console.log('Global config generate tokens started');

const typeFilePath = path.resolve( 'packages', 'global-configs', 'processEnvs.d.ts');
const typeFilePath = path.resolve(
'packages',
'global-configs',
'processEnvs.d.ts',
);
const keys = await getInterfaceKeys(typeFilePath);

type ProcessEnvs = any;

const TOKENS: Record<keyof ProcessEnvs, string | undefined> = keys.reduce((acc, key) => {
acc[key] = `process.env.${key.toString()}`;
return acc;
}, {} as Record<keyof ProcessEnvs, string | undefined>);
const TOKENS: Record<keyof ProcessEnvs, string | undefined> = keys.reduce(
(acc, key) => {
acc[key] = `process.env.${key.toString()}`;
return acc;
},
{} as Record<keyof ProcessEnvs, string | undefined>,
);

const tokensContent: string = `export const TOKENS = ${JSON.stringify(TOKENS, null, 2).replace(/"process\.env\.(\w+)"/g, 'process.env.$1')};\n`;
const tokensFilePath = path.resolve('packages', 'global-configs', '__generated__', 'tokens.ts');
const tokensContent: string = `export const TOKENS = ${JSON.stringify(
TOKENS,
null,
2,
).replace(/"process\.env\.(\w+)"/g, 'process.env.$1')};\n`;
const tokensFilePath = path.resolve(
'packages',
'global-configs',
'__generated__',
'tokens.ts',
);
fs.writeFileSync(tokensFilePath, tokensContent);

console.log('tokens.ts has been created with the TOKENS object.');
}
};
5 changes: 2 additions & 3 deletions packages/gdu/commands/global-configs/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

import configTenants from './config-tenants';
import configTokents from './config-tokens';
import generateConfigFiles from './generate-configs-files';
import configGenerateTokens from './generate-tokens';

export default async ({tokens}) => {
if(tokens) {
export default async ({ tokens }) => {
if (tokens) {
await generateConfigFiles();
}
await configGenerateTokens();
Expand Down
4 changes: 3 additions & 1 deletion packages/gdu/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ export default (app: Sade) => {
app.command('generate-global-configs')
.describe('Generate global configs')
.option('-t, --tokens', 'Generate tokens files', '')
.action(deferredAction(async () => import('./global-configs'), IS_NOT_ROOT));
.action(
deferredAction(async () => import('./global-configs'), IS_NOT_ROOT),
);
};

const NOT_READY = wrapAction(() => {
Expand Down
28 changes: 14 additions & 14 deletions packages/gdu/commands/mfe-list/generate-mfe-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,22 @@ import fs from 'fs';
import * as path from 'path';
const appsDir = path.resolve('apps');

const tenantsList = [
'au',
'au-legacy',
'nz',
'global'
]
const tenantsList = ['au', 'nz', 'global'];
const envs = ['dev', 'uat', 'test', 'preprod', 'prod', 'shared'];

function checkTenantsEnvs(appsDir, directory) {
const appConfig = path.resolve(appsDir, directory, '.gdu_app_config');
const tenants = []
const tenants = [];
for (const tenant of tenantsList) {
const envsList = []
const envsList = [];
for (const env of envs) {
const tenantEnvFile = path.join(appConfig, `.env.${env}_${tenant}`);
if (fs.existsSync(tenantEnvFile)) {
envsList.push(env);
}
}
if (envsList.length > 0) {
tenants[tenant] = envsList ;
tenants[tenant] = envsList;
}
}
return tenants;
Expand All @@ -45,7 +40,9 @@ async function generateMfeLists() {
if (fs.existsSync(configPath)) {
const tenants = checkTenantsEnvs(appsDir, directory);
// const { type, octopusProjectName } = require(configPath);
const { type, octopusProjectName } = (await import(configPath)) as OctopusConfig;
const { type, octopusProjectName } = (await import(
configPath
)) as OctopusConfig;

const mfe = {
name: directory,
Expand Down Expand Up @@ -74,15 +71,18 @@ async function generateMfeLists() {

// extract tenants into tenants-list.json
async function generateTenants() {
const dirPath = path.resolve( '.mfe-data');
const dirPath = path.resolve('.mfe-data');
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
fs.writeFileSync(path.join(dirPath, 'tenants-list.json'), JSON.stringify(tenantsList, null, 2));
fs.writeFileSync(
path.join(dirPath, 'tenants-list.json'),
JSON.stringify(tenantsList, null, 2),
);
return tenantsList;
}

export default async () => {
await generateTenants();
await generateMfeLists()
}
await generateMfeLists();
};
Loading

0 comments on commit b10adcc

Please sign in to comment.