Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use template for environment.development.ts if available #1639

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading