Skip to content

Commit

Permalink
Revert "chore: Removes AU Legacy tenant"
Browse files Browse the repository at this point in the history
This reverts commit 1c98504.
  • Loading branch information
amir-zahedi committed Jan 27, 2025
1 parent 1c98504 commit 48c1db0
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 140 deletions.
77 changes: 29 additions & 48 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', 'shared'];
const tenants = ['au', 'nz', 'global'];
const envs = ['uat', 'preprod', 'dev', 'prod', 'test', 'tokens'];
const tenants = ['au', 'nz', 'au-legacy', '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,34 +17,25 @@ const environmentOffsets: Record<string, number> = {
preprod: 3,
prod: 1,
tokens: 0,
shared: 1,
};

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

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 * 10_000 +
scopeOffset * 1000 +
(mfeApplicationOffset + Number.parseInt(value, 10) || 0)
);
};
return (envOffset * 10000) + (scopeOffset * 1000) + (mfeApplicationOffset + 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 @@ -100,11 +91,12 @@ export default async () => {
}
};

// force delete destination folder if it exists
if (fs.existsSync(destinationFolder)) {
fs.rmSync(destinationFolder, { recursive: true });
}
// create destination folder
// 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
if (!fs.existsSync(destinationFolder)) {
fs.mkdirSync(destinationFolder, { recursive: true });
}
Expand All @@ -126,26 +118,18 @@ 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 @@ -160,15 +144,11 @@ 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 @@ -185,6 +165,7 @@ export default async () => {
}
};


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

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

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

}
47 changes: 14 additions & 33 deletions packages/gdu/commands/global-configs/generate-configs-files.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,35 @@
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 @@ -58,4 +39,4 @@ export default async () => {
}
createIndexFile(keys as string[]);
console.log('Global config generate configs files finished');
};
}
32 changes: 8 additions & 24 deletions packages/gdu/commands/global-configs/generate-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,19 @@ 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: 3 additions & 2 deletions packages/gdu/commands/global-configs/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

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: 1 addition & 3 deletions packages/gdu/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ 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,22 +2,27 @@ import fs from 'fs';
import * as path from 'path';
const appsDir = path.resolve('apps');

const tenantsList = ['au', 'nz', 'global'];
const tenantsList = [
'au',
'au-legacy',
'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 @@ -40,9 +45,7 @@ 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 @@ -71,18 +74,15 @@ 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 48c1db0

Please sign in to comment.