@rivinia/env 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,16 +8,30 @@ Typed application environments for Angular: dev/staging/prod file layout, a
8
8
  files and wires the build configurations. Zero runtime code — environments are
9
9
  build-time constants; this package ships types plus setup tooling only.
10
10
 
11
- - Version: **0.1.0**
11
+ - Version: **0.2.0**
12
12
  - Requires: nothing at runtime (types only). Schematic runs under Angular CLI 22+.
13
13
 
14
14
  ## Install
15
15
 
16
16
  ```bash
17
17
  npm install @rivinia/env
18
- ng add @rivinia/env
18
+ ng add @rivinia/env # multi-select: development / staging / production
19
19
  ```
20
20
 
21
+ Interactive runs ask which environments to scaffold (checkbox, all checked by
22
+ default); non-interactive runs take all. From flags, comma-separate (short
23
+ aliases work: `dev`, `prod`, `stag`):
24
+
25
+ ```bash
26
+ ng add @rivinia/env --environments staging,production
27
+ ```
28
+
29
+ Only the selection is scaffolded and wired (model + base file always — the
30
+ base is the replacement source). Each wired configuration gets its output
31
+ directory (`builds/dev`, `builds/stag`, `builds/prod`) unless it already
32
+ defines one, and `/builds` is appended to `.gitignore` once. Re-runs never
33
+ overwrite files or duplicate configuration.
34
+
21
35
  `ng add` creates (never overwrites) `src/environments/`:
22
36
 
23
37
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rivinia/env",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Typed application environments for Angular (dev/staging/prod) with an ng-add schematic.",
5
5
  "keywords": [
6
6
  "angular",
@@ -14,38 +14,61 @@ export interface AppEnvironment extends BaseEnvironment {
14
14
  readonly apiUrl: string;
15
15
  }
16
16
  `;
17
- const fileDefs = (sourceRoot) => [
18
- { name: `${sourceRoot}/environments/environment.model.ts`, content: () => MODEL_FILE },
19
- {
20
- name: `${sourceRoot}/environments/environment.ts`,
21
- content: (version) => `import type { AppEnvironment } from './environment.model';\n\n` +
22
- `/** Development defaults. Used as-is by the \`development\` configuration. */\n` +
23
- `export const environment: AppEnvironment = {\n` +
24
- ` production: false,\n` +
25
- ` version: '${version}',\n` +
26
- ` apiUrl: 'http://localhost:3000/api',\n` +
27
- `};\n`,
28
- },
29
- {
30
- name: `${sourceRoot}/environments/environment.staging.ts`,
31
- content: (version) => `import type { AppEnvironment } from './environment.model';\n\n` +
32
- `/** Staging mirrors production. Wired via the \`staging\` configuration. */\n` +
33
- `export const environment: AppEnvironment = {\n` +
34
- ` production: true,\n` +
35
- ` version: '${version}',\n` +
36
- ` apiUrl: 'https://staging.example.com/api',\n` +
37
- `};\n`,
38
- },
39
- {
40
- name: `${sourceRoot}/environments/environment.prod.ts`,
41
- content: (version) => `import type { AppEnvironment } from './environment.model';\n\n` +
42
- `export const environment: AppEnvironment = {\n` +
43
- ` production: true,\n` +
44
- ` version: '${version}',\n` +
45
- ` apiUrl: 'https://api.example.com/api',\n` +
46
- `};\n`,
47
- },
48
- ];
17
+ const ALL_ENVIRONMENTS = ['development', 'staging', 'production'];
18
+ /** Short aliases users actually type (`prod`, `dev`, `stag`). */
19
+ const ENVIRONMENT_ALIASES = {
20
+ dev: 'development',
21
+ prod: 'production',
22
+ stag: 'staging',
23
+ stage: 'staging',
24
+ };
25
+ /** Output directory short names: builds/dev, builds/prod, builds/stag. */
26
+ const OUTPUT_DIR = {
27
+ development: 'builds/dev',
28
+ staging: 'builds/stag',
29
+ production: 'builds/prod',
30
+ };
31
+ const fileDefs = (sourceRoot, selected) => {
32
+ const files = [
33
+ { name: `${sourceRoot}/environments/environment.model.ts`, content: () => MODEL_FILE },
34
+ // The base file is always scaffolded: it is the `replace` source for
35
+ // prod/staging swaps (and what `development` uses directly).
36
+ {
37
+ name: `${sourceRoot}/environments/environment.ts`,
38
+ content: (version) => `import type { AppEnvironment } from './environment.model';\n\n` +
39
+ `/** Development defaults. Used as-is by the \`development\` configuration. */\n` +
40
+ `export const environment: AppEnvironment = {\n` +
41
+ ` production: false,\n` +
42
+ ` version: '${version}',\n` +
43
+ ` apiUrl: 'http://localhost:3000/api',\n` +
44
+ `};\n`,
45
+ },
46
+ ];
47
+ if (selected.includes('staging')) {
48
+ files.push({
49
+ name: `${sourceRoot}/environments/environment.staging.ts`,
50
+ content: (version) => `import type { AppEnvironment } from './environment.model';\n\n` +
51
+ `/** Staging mirrors production. Wired via the \`staging\` configuration. */\n` +
52
+ `export const environment: AppEnvironment = {\n` +
53
+ ` production: true,\n` +
54
+ ` version: '${version}',\n` +
55
+ ` apiUrl: 'https://staging.example.com/api',\n` +
56
+ `};\n`,
57
+ });
58
+ }
59
+ if (selected.includes('production')) {
60
+ files.push({
61
+ name: `${sourceRoot}/environments/environment.prod.ts`,
62
+ content: (version) => `import type { AppEnvironment } from './environment.model';\n\n` +
63
+ `export const environment: AppEnvironment = {\n` +
64
+ ` production: true,\n` +
65
+ ` version: '${version}',\n` +
66
+ ` apiUrl: 'https://api.example.com/api',\n` +
67
+ `};\n`,
68
+ });
69
+ }
70
+ return files;
71
+ };
49
72
  /**
50
73
  * `ng add @rivinia/env` — scaffolds typed environment files and wires
51
74
  * development/staging/production configurations with file replacements.
@@ -66,10 +89,31 @@ function ngAdd(options) {
66
89
  const { name, project, angular } = workspace;
67
90
  const sourceRoot = project.sourceRoot ?? (project.root ? `${project.root}/src` : 'src');
68
91
  const version = readAppVersion(tree);
92
+ // Empty selection (or engine default) means everything — same as --defaults.
93
+ // Accepts arrays (prompt/programmatic) and comma strings (CLI flags), with
94
+ // short aliases (dev/prod/stag/stage) normalized before validation.
95
+ const rawSelected = options.environments;
96
+ const asArray = Array.isArray(rawSelected)
97
+ ? [...rawSelected]
98
+ : typeof rawSelected === 'string'
99
+ ? rawSelected.split(',')
100
+ : [];
101
+ const cleaned = asArray
102
+ .map((e) => e.trim().toLowerCase())
103
+ .filter((e) => e.length > 0)
104
+ .map((e) => ENVIRONMENT_ALIASES[e] ?? e);
105
+ const unknown = [...new Set(cleaned)].filter((e) => !ALL_ENVIRONMENTS.includes(e));
106
+ if (unknown.length > 0) {
107
+ throw new schematics_1.SchematicsException(`[rivinia/env] unknown environment(s): ${unknown.join(', ')}. ` +
108
+ `Choose from development (dev), staging (stag), production (prod).`);
109
+ }
110
+ const selected = cleaned.length > 0
111
+ ? [...new Set(cleaned)]
112
+ : [...ALL_ENVIRONMENTS];
69
113
  const configsSnapshot = () => JSON.stringify(project.architect?.build?.configurations ?? null);
70
114
  const configsBefore = configsSnapshot();
71
115
  let created = 0;
72
- for (const file of fileDefs(sourceRoot)) {
116
+ for (const file of fileDefs(sourceRoot, selected)) {
73
117
  const path = `/${file.name}`;
74
118
  if (tree.exists(path)) {
75
119
  context.logger.info(`[rivinia/env] exists, skipped: ${path}`);
@@ -78,11 +122,16 @@ function ngAdd(options) {
78
122
  tree.create(path, file.content(version));
79
123
  created++;
80
124
  }
81
- ensureReplacement(tree, angular, name, 'production', {
82
- replace: `${sourceRoot}/environments/environment.ts`,
83
- with: `${sourceRoot}/environments/environment.prod.ts`,
84
- });
85
- ensureStaging(tree, angular, name, sourceRoot, context);
125
+ if (selected.includes('production')) {
126
+ ensureReplacement(tree, angular, name, 'production', {
127
+ replace: `${sourceRoot}/environments/environment.ts`,
128
+ with: `${sourceRoot}/environments/environment.prod.ts`,
129
+ });
130
+ }
131
+ if (selected.includes('staging')) {
132
+ ensureStaging(tree, angular, name, sourceRoot, context);
133
+ }
134
+ ensureOutputPaths(tree, angular, name, selected, context);
86
135
  if (created > 0 || configsSnapshot() !== configsBefore) {
87
136
  tree.overwrite('/angular.json', JSON.stringify(angular, null, 2) + '\n');
88
137
  }
@@ -164,6 +213,36 @@ function ensureReplacement(tree, angular, name, configuration, entry) {
164
213
  if (!exists)
165
214
  list.push({ ...entry });
166
215
  }
216
+ /**
217
+ * Points each selected configuration at its build directory
218
+ * (builds/dev, builds/stag, builds/prod) — but only when the app has not
219
+ * customized `outputPath` already. Never overwrites user output locations.
220
+ */
221
+ function ensureOutputPaths(tree, angular, name, selected, context) {
222
+ if (!tree.exists('/angular.json'))
223
+ return;
224
+ const configurations = getConfigurations(angular, name);
225
+ let touched = false;
226
+ for (const env of selected) {
227
+ const target = configurations[env];
228
+ if (!target || 'outputPath' in target)
229
+ continue;
230
+ target['outputPath'] = OUTPUT_DIR[env];
231
+ touched = true;
232
+ }
233
+ if (touched)
234
+ ensureGitignore(tree, context);
235
+ }
236
+ /** Appends `/builds` to the app .gitignore once (build outputs don't commit). */
237
+ function ensureGitignore(tree, context) {
238
+ if (!tree.exists('/.gitignore'))
239
+ return;
240
+ const content = tree.read('/.gitignore')?.toString('utf-8') ?? '';
241
+ if (/(^|\n)\/*builds\/?(\n|$)/.test(content))
242
+ return;
243
+ tree.overwrite('/.gitignore', content.replace(/\s*$/, '\n') + '/builds\n');
244
+ context.logger.info('[rivinia/env] added /builds to .gitignore.');
245
+ }
167
246
  function ensureStaging(tree, angular, name, sourceRoot, context) {
168
247
  if (!tree.exists('/angular.json'))
169
248
  return;
@@ -4,6 +4,22 @@
4
4
  "title": "Rivinia env ng-add options",
5
5
  "type": "object",
6
6
  "properties": {
7
+ "environments": {
8
+ "type": ["array", "string"],
9
+ "description": "Which environments to scaffold and wire: array, or comma-separated string on the CLI (--environments staging,production). Model + base file always. Omit for all three.",
10
+ "default": ["development", "staging", "production"],
11
+ "items": { "type": "string", "enum": ["development", "staging", "production", "dev", "prod", "stag", "stage"] },
12
+ "x-prompt": {
13
+ "message": "Which environments should ng-add scaffold and wire?",
14
+ "type": "list",
15
+ "multiselect": true,
16
+ "items": [
17
+ { "value": "development", "label": "development — base file, no replacement" },
18
+ { "value": "staging", "label": "staging — staging file + configuration" },
19
+ { "value": "production", "label": "production — prod file + replacement" }
20
+ ]
21
+ }
22
+ },
7
23
  "project": {
8
24
  "type": "string",
9
25
  "description": "Angular CLI project name — only for multi-app workspaces. Defaults to the workspace default project.",