From ffb04cd14b4b5eecb24473953a6fb3611b54f801 Mon Sep 17 00:00:00 2001 From: Ruslan Konviser Date: Thu, 12 Sep 2019 01:23:55 +0300 Subject: [PATCH] feat: adding environment.ts generator scripts (WIP) --- .scripts/configure.ts | 58 +++++++++++++++++++++++++++++++++++++++++++ .scripts/env.ts | 17 +++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 .scripts/configure.ts create mode 100644 .scripts/env.ts diff --git a/.scripts/configure.ts b/.scripts/configure.ts new file mode 100644 index 0000000000..5add17eacd --- /dev/null +++ b/.scripts/configure.ts @@ -0,0 +1,58 @@ +// NOTE: do NOT ever put here any secure settings! (e.g. Secret Keys) +// 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 { argv } from 'yargs'; + +const environment = argv.environment; +const isProd = environment === 'prod'; + +const envFileContent = `// NOTE: Auto-generated file +// The file contents for the current environment will overwrite these during build. +// The build system defaults to the dev environment which uses 'environment.ts', but if you do +// 'ng build --env=prod' then 'environment.prod.ts' will be used instead. +// The list of which env maps to which file can be found in '.angular-cli.json'. + +import { Environment } from './model'; + +export const environment: Environment = { + production: ${isProd} +}; +`; + +/* + +// we always want first to remove old generated files (one of them is not needed for current build) +try { + unlinkSync(`./apps/gauzy/src/environments/environment.ts`); +} catch {} +try { + unlinkSync(`./apps/gauzy/src/environments/environment.prod.ts`); +} catch {} + +const envFileDest: string = isProd ? 'environment.prod.ts' : 'environment.ts'; +const envFileDestOther: string = !isProd + ? 'environment.prod.ts' + : 'environment.ts'; + +writeFile(`./apps/gauzy/src/environments/${envFileDest}`, envFileContent, function(err) { + if (err) { + console.log(err); + } else { + console.log(`Generated Angular environment file: ${envFileDest}`); + } +}); + +writeFile(`./apps/gauzy/src/environments/${envFileDestOther}`, '', function(err) { + if (err) { + console.log(err); + } else { + console.log( + `Generated Second Empty Angular environment file: ${envFileDestOther}` + ); + } +}); + +*/ \ No newline at end of file diff --git a/.scripts/env.ts b/.scripts/env.ts new file mode 100644 index 0000000000..2a67f53592 --- /dev/null +++ b/.scripts/env.ts @@ -0,0 +1,17 @@ +// NOTE: do NOT ever put here any secure settings! (e.g. Secret Keys) +// 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 { cleanEnv, num, str, bool } from 'envalid'; + +export type Env = Readonly<{ + production: boolean; +}>; + +export const env: Env = cleanEnv( + process.env, + { + production: bool({ default: false }) + }, + { strict: true, dotEnvPath: __dirname + '/../.env' } +);