Skip to content

Commit

Permalink
Merge pull request #8608 from ever-co/fix/ui-config-targets
Browse files Browse the repository at this point in the history
chore(packages): fix target dependencies and optimize build process
  • Loading branch information
rahul-rocket authored Dec 22, 2024
2 parents 5de0dad + adc6e96 commit b165252
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 23 deletions.
28 changes: 22 additions & 6 deletions .scripts/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// We are using dotenv (.env) for consistency with other Platform projects
// This is Angular app and all settings will be loaded into the client browser!

import { env } from './env';
import { writeFile, unlinkSync } from 'fs';
import { existsSync, mkdirSync, writeFile, unlinkSync } from 'fs';
import * as path from 'path';
import { argv } from 'yargs';
import { env } from './env';

const environment = argv.environment;
const isProd = environment === 'prod';
Expand Down Expand Up @@ -364,9 +364,25 @@ try {
const envFileDest: string = isProd ? 'environment.prod.ts' : 'environment.ts';
const envFileDestOther: string = !isProd ? 'environment.prod.ts' : 'environment.ts';

writeFile(`./packages/ui-config/src/lib/environments/${envFileDest}`, envFileContent, (error) => {
console.log(`Generating Angular environment file: ${envFileDest}`);
console.log(`Generating Second Angular environment file: ${envFileDestOther}`);

// Define the folder paths
const environmentsFolder = './packages/ui-config/src/lib/environments';

// Ensure the environments folder exists
if (!existsSync(environmentsFolder)) {
mkdirSync(environmentsFolder, { recursive: true });
console.log(`Created environments folder: ${environmentsFolder}`);
}

// Paths to environment files
const envFilePath = path.resolve(`${environmentsFolder}/${envFileDest}`);
const envFileOtherPath = path.resolve(`${environmentsFolder}/${envFileDestOther}`);

writeFile(envFilePath, envFileContent, (error) => {
if (error) {
console.log(error);
console.error(`Error writing environment file: ${error}`);
} else {
// Paths to environment files
const envFilePath = path.resolve(`./packages/ui-config/src/lib/environments/${envFileDest}`);
Expand All @@ -376,9 +392,9 @@ writeFile(`./packages/ui-config/src/lib/environments/${envFileDest}`, envFileCon

let envFileDestOtherContent = `export const environment = { production: ${!isProd} }`;

writeFile(`./packages/ui-config/src/lib/environments/${envFileDestOther}`, envFileDestOtherContent, (error) => {
writeFile(envFileOtherPath, envFileDestOtherContent, (error) => {
if (error) {
console.log(error);
console.error(`Error writing environment file: ${error}`);
} else {
const envFileOtherPath = path.resolve(`./packages/ui-config/src/lib/environments/${envFileDestOther}`);
console.log(`Generated Second Empty Angular environment file: ${envFileOtherPath}`);
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@
"build:package:ui-auth": "cross-env NODE_ENV=development NODE_OPTIONS=--max-old-space-size=12288 yarn nx build ui-auth --configuration=development",
"build:package:ui-auth:prod": "cross-env NODE_ENV=production NODE_OPTIONS=--max-old-space-size=12288 yarn nx build ui-auth --configuration=production",
"build:package:ui-auth:docker": "cross-env NODE_ENV=production NODE_OPTIONS=--max-old-space-size=60000 yarn nx build ui-auth --configuration=production",
"build:package:ui-config": "cross-env NODE_ENV=development NODE_OPTIONS=--max-old-space-size=12288 && yarn run config:dev && yarn nx build ui-config --configuration=development",
"build:package:ui-config:prod": "cross-env NODE_ENV=production NODE_OPTIONS=--max-old-space-size=12288 yarn run config:prod && yarn nx build ui-config --configuration=production",
"build:package:ui-config:docker": "cross-env NODE_ENV=production NODE_OPTIONS=--max-old-space-size=60000 yarn run config:prod && yarn nx build ui-config --configuration=production",
"build:package:ui-core": "cross-env NODE_ENV=development NODE_OPTIONS=--max-old-space-size=12288 yarn build:package:ui-config && yarn nx build ui-core --configuration=development",
"build:package:ui-core:prod": "cross-env NODE_ENV=production NODE_OPTIONS=--max-old-space-size=12288 yarn build:package:ui-config:prod && yarn nx build ui-core --configuration=production",
"build:package:ui-core:docker": "cross-env NODE_ENV=production NODE_OPTIONS=--max-old-space-size=60000 yarn build:package:ui-config:docker && yarn nx build ui-core --configuration=production",
"build:package:ui-config": "cross-env NODE_ENV=development NODE_OPTIONS=--max-old-space-size=12288 && yarn nx build ui-config --configuration=development",
"build:package:ui-config:prod": "cross-env NODE_ENV=production NODE_OPTIONS=--max-old-space-size=12288 yarn nx build ui-config --configuration=production",
"build:package:ui-config:docker": "cross-env NODE_ENV=production NODE_OPTIONS=--max-old-space-size=60000 yarn nx build ui-config --configuration=production",
"build:package:ui-core": "cross-env NODE_ENV=development NODE_OPTIONS=--max-old-space-size=12288 yarn nx build ui-core --configuration=development",
"build:package:ui-core:prod": "cross-env NODE_ENV=production NODE_OPTIONS=--max-old-space-size=12288 yarn nx build ui-core --configuration=production",
"build:package:ui-core:docker": "cross-env NODE_ENV=production NODE_OPTIONS=--max-old-space-size=60000 yarn nx build ui-core --configuration=production",
"build:package:auth": "cross-env NODE_ENV=development NODE_OPTIONS=--max-old-space-size=12288 yarn nx build auth --configuration=development",
"build:package:auth:prod": "cross-env NODE_ENV=production NODE_OPTIONS=--max-old-space-size=12288 yarn nx build auth --configuration=production",
"build:package:auth:docker": "cross-env NODE_ENV=production NODE_OPTIONS=--max-old-space-size=60000 yarn nx build auth --configuration=production",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"private": true,
"scripts": {
"lib:build": "yarn nx build ui-config --configuration=development",
"lib:build:prod": "cross-env NODE_ENV=production yarn ts-node scripts/replace-env-files.ts --environment=prod && yarn nx build ui-config --configuration=production",
"lib:build:prod": "yarn nx build ui-config --configuration=production",
"lib:watch": "yarn nx build ui-config --watch --configuration=development"
},
"peerDependencies": {
Expand Down
36 changes: 36 additions & 0 deletions packages/ui-config/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
"build": {
"executor": "@nx/angular:package",
"outputs": ["{workspaceRoot}/dist/{projectRoot}"],
"dependsOn": [
{
"target": "replace-env",
"projects": "self"
}
],
"options": {
"project": "packages/ui-config/ng-package.json",
"tsConfig": "packages/ui-config/tsconfig.json"
Expand All @@ -23,6 +29,36 @@
},
"defaultConfiguration": "production"
},
"generate-env": {
"executor": "nx:run-commands",
"configurations": {
"production": {
"commands": ["yarn run config:prod"]
},
"development": {
"commands": ["yarn run config:dev"]
}
},
"defaultConfiguration": "production"
},
"replace-env": {
"executor": "nx:run-commands",
"dependsOn": [
{
"target": "generate-env",
"projects": "self"
}
],
"configurations": {
"production": {
"command": "cross-env NODE_ENV=production yarn ts-node packages/ui-config/scripts/replace-env-files.ts"
},
"development": {
"command": "cross-env NODE_ENV=development yarn ts-node packages/ui-config/scripts/replace-env-files.ts"
}
},
"defaultConfiguration": "production"
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
Expand Down
8 changes: 5 additions & 3 deletions packages/ui-config/scripts/replace-env-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import * as fs from 'fs';
* Replaces the environment.ts file with environment.prod.ts if the environment is set to production.
*/
function replaceFilesForProduction() {
const env = process.env['NODE_ENV'] || 'development';
console.log('Replacing environment files for production build...', process.env['NODE_ENV']);

const env = process.env['NODE_ENV'] || 'development';
console.log(`Current NODE_ENV: ${process.env['NODE_ENV']}`);

if (env === 'production') {
const sourceFile = path.join(path.resolve(`./src/lib/environments`), 'environment.prod.ts');
const targetFile = path.join(path.resolve(`./src/lib/environments`), 'environment.ts');
console.log('Replacing environment files for __dirname: ' + __dirname);
const sourceFile = path.join(path.resolve(__dirname, '../src/lib/environments'), 'environment.prod.ts');
const targetFile = path.join(path.resolve(__dirname, '../src/lib/environments'), 'environment.ts');

fs.copyFileSync(sourceFile, targetFile);
console.log(`Replaced environment file with ${sourceFile}`);
Expand Down
14 changes: 10 additions & 4 deletions packages/ui-core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
"build": {
"executor": "@nx/angular:package",
"outputs": ["{workspaceRoot}/dist/{projectRoot}"],
"dependsOn": [
"^build",
{
"target": "config",
"projects": "self"
}
],
"options": {
"project": "packages/ui-core/ng-package.json",
"tsConfig": "packages/ui-core/tsconfig.json"
Expand All @@ -22,17 +29,16 @@
"tsConfig": "packages/ui-core/tsconfig.lib.json"
}
},
"defaultConfiguration": "production",
"dependsOn": ["^build", "config"]
"defaultConfiguration": "production"
},
"config": {
"executor": "nx:run-commands",
"configurations": {
"production": {
"commands": ["yarn ts-node .scripts/configure.ts --prod"]
"commands": ["yarn nx build ui-config --configuration=production"]
},
"development": {
"commands": ["yarn ts-node .scripts/configure.ts --dev"]
"commands": ["yarn nx build ui-config --configuration=development"]
}
},
"defaultConfiguration": "production"
Expand Down
4 changes: 1 addition & 3 deletions packages/ui-core/shared/src/lib/project/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './project-mutation/project-mutation.component';
export * from './project-mutation/project-mutation.module';
export * from './project-module/index';
export * from './project-module';

0 comments on commit b165252

Please sign in to comment.