@rivinia/env 0.0.0-stage → 0.1.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 +54 -2
- package/fesm2022/rivinia-env.mjs +0 -0
- package/package.json +26 -3
- package/schematics/collection.json +10 -0
- package/schematics/ng-add/index.cjs +183 -0
- package/schematics/ng-add/schema.json +14 -0
- package/types/rivinia-env.d.ts +15 -0
package/README.md
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @rivinia/env
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@rivinia/env)
|
|
4
|
+
[](https://www.npmjs.com/package/@rivinia/env)
|
|
5
|
+
|
|
6
|
+
Typed application environments for Angular: dev/staging/prod file layout, a
|
|
7
|
+
`BaseEnvironment` contract, and an `ng-add` schematic that scaffolds the
|
|
8
|
+
files and wires the build configurations. Zero runtime code — environments are
|
|
9
|
+
build-time constants; this package ships types plus setup tooling only.
|
|
10
|
+
|
|
11
|
+
- Version: **0.1.0**
|
|
12
|
+
- Requires: nothing at runtime (types only). Schematic runs under Angular CLI 22+.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @rivinia/env
|
|
18
|
+
ng add @rivinia/env
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`ng add` creates (never overwrites) `src/environments/`:
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
environment.model.ts # AppEnvironment extends BaseEnvironment — extend it
|
|
25
|
+
environment.ts # development defaults (used as-is by `development`)
|
|
26
|
+
environment.staging.ts # staging values (production: true)
|
|
27
|
+
environment.prod.ts # production values
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
and wires `angular.json`: `production` gains its replacement if missing, and a
|
|
31
|
+
`staging` configuration (copied from `production`) is created if absent.
|
|
32
|
+
`development` intentionally gets no replacement (CLI standard). The app version
|
|
33
|
+
is snapshotted from your `package.json` into each file (`0.0.0` fallback).
|
|
34
|
+
|
|
35
|
+
## Usage pattern (app-owned)
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
// environment.model.ts
|
|
39
|
+
import type { BaseEnvironment } from '@rivinia/env';
|
|
40
|
+
|
|
41
|
+
export interface AppEnvironment extends BaseEnvironment {
|
|
42
|
+
readonly apiUrl: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// anywhere:
|
|
46
|
+
import { environment } from '../environments/environment';
|
|
47
|
+
fetch(`${environment.apiUrl}/trips`);
|
|
48
|
+
console.log(environment.version); // e.g. footer, about page, cache keys
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Re-run the schematic (or edit by hand) when the app version changes.
|
|
52
|
+
|
|
53
|
+
## License
|
|
54
|
+
|
|
55
|
+
MIT.
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rivinia/env",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Typed application environments for Angular (dev/staging/prod) with an ng-add schematic.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"angular",
|
|
7
|
+
"environment",
|
|
8
|
+
"configuration",
|
|
9
|
+
"typescript"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"schematics": "./schematics/collection.json",
|
|
14
|
+
"module": "fesm2022/rivinia-env.mjs",
|
|
15
|
+
"typings": "types/rivinia-env.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
"./package.json": {
|
|
18
|
+
"default": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./types/rivinia-env.d.ts",
|
|
22
|
+
"default": "./fesm2022/rivinia-env.mjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"tslib": "^2.3.0"
|
|
28
|
+
}
|
|
6
29
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "../../../node_modules/@angular-devkit/schematics/collection-schema.json",
|
|
3
|
+
"schematics": {
|
|
4
|
+
"ng-add": {
|
|
5
|
+
"description": "Scaffold typed environment files and wire dev/staging/production configurations.",
|
|
6
|
+
"factory": "./ng-add/index.cjs#ngAdd",
|
|
7
|
+
"schema": "./ng-add/schema.json"
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ngAdd = ngAdd;
|
|
4
|
+
const schematics_1 = require("@angular-devkit/schematics");
|
|
5
|
+
const MODEL_FILE = `import type { BaseEnvironment } from '@rivinia/env';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Application environment contract. Extend with your own fields
|
|
9
|
+
* (apiUrl, keys, feature flags, …) — nothing else in the app should
|
|
10
|
+
* import environment values except through this type.
|
|
11
|
+
*/
|
|
12
|
+
export interface AppEnvironment extends BaseEnvironment {
|
|
13
|
+
/** Base URL for API calls (per-environment value below). */
|
|
14
|
+
readonly apiUrl: string;
|
|
15
|
+
}
|
|
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
|
+
];
|
|
49
|
+
/**
|
|
50
|
+
* `ng add @rivinia/env` — scaffolds typed environment files and wires
|
|
51
|
+
* development/staging/production configurations with file replacements.
|
|
52
|
+
*
|
|
53
|
+
* - Never overwrites existing files (each file skipped individually).
|
|
54
|
+
* - Never removes user configuration: only ADDS missing fileReplacements
|
|
55
|
+
* (matched by replace+with pair) and creates a `staging` configuration
|
|
56
|
+
* (copied from `production` when present) if absent.
|
|
57
|
+
* - `development` intentionally gets no replacement (CLI standard: it uses
|
|
58
|
+
* `environment.ts` directly).
|
|
59
|
+
* - App version is snapshotted from the app's `package.json` (`0.0.0` fallback).
|
|
60
|
+
*/
|
|
61
|
+
function ngAdd(options) {
|
|
62
|
+
return (tree, context) => {
|
|
63
|
+
const workspace = readWorkspace(tree, context, options.project);
|
|
64
|
+
if (!workspace)
|
|
65
|
+
return tree;
|
|
66
|
+
const { name, project, angular } = workspace;
|
|
67
|
+
const sourceRoot = project.sourceRoot ?? (project.root ? `${project.root}/src` : 'src');
|
|
68
|
+
const version = readAppVersion(tree);
|
|
69
|
+
const configsSnapshot = () => JSON.stringify(project.architect?.build?.configurations ?? null);
|
|
70
|
+
const configsBefore = configsSnapshot();
|
|
71
|
+
let created = 0;
|
|
72
|
+
for (const file of fileDefs(sourceRoot)) {
|
|
73
|
+
const path = `/${file.name}`;
|
|
74
|
+
if (tree.exists(path)) {
|
|
75
|
+
context.logger.info(`[rivinia/env] exists, skipped: ${path}`);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
tree.create(path, file.content(version));
|
|
79
|
+
created++;
|
|
80
|
+
}
|
|
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);
|
|
86
|
+
if (created > 0 || configsSnapshot() !== configsBefore) {
|
|
87
|
+
tree.overwrite('/angular.json', JSON.stringify(angular, null, 2) + '\n');
|
|
88
|
+
}
|
|
89
|
+
context.logger.info(`[rivinia/env] ${created} file(s) created, configurations wired for '${name}'.`);
|
|
90
|
+
return tree;
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function readWorkspace(tree, context, requested) {
|
|
94
|
+
let angular;
|
|
95
|
+
try {
|
|
96
|
+
const raw = tree.read('/angular.json')?.toString('utf-8');
|
|
97
|
+
if (!raw)
|
|
98
|
+
throw new schematics_1.SchematicsException('angular.json not found.');
|
|
99
|
+
angular = JSON.parse(raw);
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
context.logger.warn(`[rivinia/env] cannot read angular.json (${error instanceof Error ? error.message : error}) — skipping.`);
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
const names = Object.keys(angular.projects ?? {});
|
|
106
|
+
if (names.length === 0) {
|
|
107
|
+
context.logger.warn('[rivinia/env] no projects in angular.json — skipping.');
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
// NOTE: the schema default for `project` is "" (engine-filled), so only a
|
|
111
|
+
// non-empty value counts as an explicit request. (`??` alone would honor "".)
|
|
112
|
+
const name = requested && requested.length > 0
|
|
113
|
+
? requested
|
|
114
|
+
: angular.defaultProject && typeof angular.defaultProject === 'string'
|
|
115
|
+
? angular.defaultProject
|
|
116
|
+
: names[0];
|
|
117
|
+
const project = angular.projects?.[name];
|
|
118
|
+
if (!project) {
|
|
119
|
+
context.logger.warn(`[rivinia/env] project '${name}' not found — skipping.`);
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
return { name, project, angular };
|
|
123
|
+
}
|
|
124
|
+
function readAppVersion(tree) {
|
|
125
|
+
try {
|
|
126
|
+
const raw = tree.read('/package.json')?.toString('utf-8');
|
|
127
|
+
if (!raw)
|
|
128
|
+
return '0.0.0';
|
|
129
|
+
const parsed = JSON.parse(raw);
|
|
130
|
+
return typeof parsed.version === 'string' && parsed.version.length > 0
|
|
131
|
+
? parsed.version
|
|
132
|
+
: '0.0.0';
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
return '0.0.0';
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function getConfigurations(angular, name) {
|
|
139
|
+
const project = angular.projects?.[name];
|
|
140
|
+
if (!project)
|
|
141
|
+
throw new schematics_1.SchematicsException(`[rivinia/env] project '${name}' not found.`);
|
|
142
|
+
project.architect ??= {};
|
|
143
|
+
project.architect.build ??= {};
|
|
144
|
+
const build = project.architect.build;
|
|
145
|
+
build.configurations ??= {};
|
|
146
|
+
return build.configurations;
|
|
147
|
+
}
|
|
148
|
+
function ensureReplacement(tree, angular, name, configuration, entry) {
|
|
149
|
+
if (!tree.exists('/angular.json'))
|
|
150
|
+
return;
|
|
151
|
+
const configurations = getConfigurations(angular, name);
|
|
152
|
+
const target = configurations[configuration];
|
|
153
|
+
if (!target)
|
|
154
|
+
return; // never create production/development for the user
|
|
155
|
+
const list = target['fileReplacements'];
|
|
156
|
+
if (!Array.isArray(list)) {
|
|
157
|
+
target['fileReplacements'] = [{ ...entry }];
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const exists = list.some((item) => typeof item === 'object' &&
|
|
161
|
+
item !== null &&
|
|
162
|
+
item['replace'] === entry.replace &&
|
|
163
|
+
item['with'] === entry.with);
|
|
164
|
+
if (!exists)
|
|
165
|
+
list.push({ ...entry });
|
|
166
|
+
}
|
|
167
|
+
function ensureStaging(tree, angular, name, sourceRoot, context) {
|
|
168
|
+
if (!tree.exists('/angular.json'))
|
|
169
|
+
return;
|
|
170
|
+
const configurations = getConfigurations(angular, name);
|
|
171
|
+
if (!configurations['staging']) {
|
|
172
|
+
const production = configurations['production'];
|
|
173
|
+
const copy = production && typeof production === 'object' ? { ...production } : {};
|
|
174
|
+
delete copy['fileReplacements'];
|
|
175
|
+
delete copy['outputPath'];
|
|
176
|
+
configurations['staging'] = copy;
|
|
177
|
+
context.logger.info('[rivinia/env] created `staging` configuration.');
|
|
178
|
+
}
|
|
179
|
+
ensureReplacement(tree, angular, name, 'staging', {
|
|
180
|
+
replace: `${sourceRoot}/environments/environment.ts`,
|
|
181
|
+
with: `${sourceRoot}/environments/environment.staging.ts`,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/schema",
|
|
3
|
+
"$id": "rivinia-env-ng-add",
|
|
4
|
+
"title": "Rivinia env ng-add options",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"properties": {
|
|
7
|
+
"project": {
|
|
8
|
+
"type": "string",
|
|
9
|
+
"description": "Angular CLI project name — only for multi-app workspaces. Defaults to the workspace default project.",
|
|
10
|
+
"default": ""
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"required": []
|
|
14
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal contract every Rivinia-style environment file satisfies.
|
|
3
|
+
* Applications extend it with their own fields (apiUrl, keys, …) — the
|
|
4
|
+
* library never dictates field names beyond these two.
|
|
5
|
+
*/
|
|
6
|
+
interface BaseEnvironment {
|
|
7
|
+
/** `true` for production-like builds (staging included). */
|
|
8
|
+
readonly production: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Application version, snapshotted from the app's `package.json` when the
|
|
11
|
+
* files are scaffolded. Re-run the schematic or edit by hand on release.
|
|
12
|
+
*/
|
|
13
|
+
readonly version: string;
|
|
14
|
+
}
|
|
15
|
+
export type { BaseEnvironment };
|