Skip to content

Commit

Permalink
feat: use optional template for environment.development.ts if avalable (
Browse files Browse the repository at this point in the history
#1639)

* a `environment.development.ts.template` file can be added to projects to provide a common base development configuration (instead of adapting the init script)
  • Loading branch information
dhhyi authored Apr 25, 2024
1 parent bb952bd commit d3c2249
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
3 changes: 3 additions & 0 deletions docs/guides/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ This environment file references two more files:
- `environment.model.ts` where "ENVIRONMENT_DEFAULTS" are taken from
- `environment.development.ts` where you can add your own configuration only for the local development environment, see overrides explanation above.
To provide a common `environment.development.ts` base configuration for projects one can add the file `src/environments/environment.development.ts.template` to the project sources and pre-configure it for the projects context.
This template will then be used by the initial `npm install` instead of our standard development configuration.
## Development Server
Run `ng serve` or `ng s` for a development server.
Expand Down
58 changes: 32 additions & 26 deletions scripts/init-development-environment.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
const force = process.argv.length > 2 && process.argv.slice(2).find(arg => arg === '-f' || arg === '--force');
const empty = process.argv.length > 2 && process.argv.slice(2).find(arg => arg === '--empty');

const envDevFile = 'src/environments/environment.development.ts';

const fs = require('fs');

if (empty) {
console.log('writing empty ' + envDevFile);

fs.writeFileSync(
envDevFile,
`import { Environment } from "./environment.model";
export const overrides: Partial<Environment> = {};
`
);
} else if (!fs.existsSync(envDevFile) || force) {
if (fs.existsSync(envDevFile)) {
const environmentLocalBackupPath = envDevFile + '.bak';
console.log('creating backup ' + environmentLocalBackupPath);
fs.renameSync(envDevFile, environmentLocalBackupPath);
}
const force = process.argv.length > 2 && process.argv.slice(2).find(arg => arg === '-f' || arg === '--force');
const empty = process.argv.length > 2 && process.argv.slice(2).find(arg => arg === '--empty');

console.log('writing ' + envDevFile);
const envDevPath = 'src/environments/environment.development.ts';
const envDevTemplatePath = 'src/environments/environment.development.ts.template';

fs.writeFileSync(
envDevFile,
`import { Environment } from "./environment.model";
/** @type string */
let envDevTemplate;
if (fs.existsSync(envDevTemplatePath)) {
envDevTemplate = fs.readFileSync('src/environments/environment.development.ts.template', 'utf8');
} else {
envDevTemplate = `import { Environment } from "./environment.model";
/* eslint-disable */
Expand All @@ -40,8 +25,29 @@ export const overrides: Partial<Environment> = {
// features: ['compare'],
};
`;
}

if (empty) {
console.log('writing empty ' + envDevPath);

fs.writeFileSync(
envDevPath,
`import { Environment } from "./environment.model";
export const overrides: Partial<Environment> = {};
`
);
} else if (!fs.existsSync(envDevPath) || force) {
if (fs.existsSync(envDevPath)) {
const environmentLocalBackupPath = envDevPath + '.bak';
console.log('creating backup ' + environmentLocalBackupPath);
fs.renameSync(envDevPath, environmentLocalBackupPath);
}

console.log('writing ' + envDevPath);

fs.writeFileSync(envDevPath, envDevTemplate);
} else {
console.log('not overwriting existing ' + envDevFile);
console.log('not overwriting existing ' + envDevPath);
}

0 comments on commit d3c2249

Please sign in to comment.