hazo_env 0.15.3 → 0.17.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.
Files changed (67) hide show
  1. package/CHANGE_LOG.md +120 -0
  2. package/README.md +333 -0
  3. package/dist/bin.d.ts +3 -0
  4. package/dist/bin.d.ts.map +1 -0
  5. package/dist/bin.js +17 -0
  6. package/dist/cli.js +178 -17
  7. package/dist/deploy/build.d.ts +1 -1
  8. package/dist/deploy/build.d.ts.map +1 -1
  9. package/dist/deploy/build.js +51 -14
  10. package/dist/deploy/clean.d.ts +2 -1
  11. package/dist/deploy/clean.d.ts.map +1 -1
  12. package/dist/deploy/clean.js +36 -6
  13. package/dist/deploy/generate-readme-cli.d.ts +3 -0
  14. package/dist/deploy/generate-readme-cli.d.ts.map +1 -0
  15. package/dist/deploy/generate-readme-cli.js +40 -0
  16. package/dist/deploy/help.d.ts +55 -0
  17. package/dist/deploy/help.d.ts.map +1 -0
  18. package/dist/deploy/help.js +545 -0
  19. package/dist/deploy/idle-slots.d.ts +42 -0
  20. package/dist/deploy/idle-slots.d.ts.map +1 -0
  21. package/dist/deploy/idle-slots.js +136 -0
  22. package/dist/deploy/index.d.ts +5 -0
  23. package/dist/deploy/index.d.ts.map +1 -1
  24. package/dist/deploy/index.js +5 -0
  25. package/dist/deploy/init.d.ts +14 -0
  26. package/dist/deploy/init.d.ts.map +1 -1
  27. package/dist/deploy/init.js +14 -1
  28. package/dist/deploy/preflight.d.ts +26 -0
  29. package/dist/deploy/preflight.d.ts.map +1 -0
  30. package/dist/deploy/preflight.js +91 -0
  31. package/dist/deploy/rollback.d.ts +10 -1
  32. package/dist/deploy/rollback.d.ts.map +1 -1
  33. package/dist/deploy/rollback.js +25 -1
  34. package/dist/deploy/share.d.ts +28 -0
  35. package/dist/deploy/share.d.ts.map +1 -0
  36. package/dist/deploy/share.js +53 -0
  37. package/dist/deploy/shared-dirs.d.ts +128 -0
  38. package/dist/deploy/shared-dirs.d.ts.map +1 -0
  39. package/dist/deploy/shared-dirs.js +486 -0
  40. package/dist/deploy/status.d.ts +8 -0
  41. package/dist/deploy/status.d.ts.map +1 -1
  42. package/dist/deploy/status.js +6 -0
  43. package/dist/deploy/switch.d.ts.map +1 -1
  44. package/dist/deploy/switch.js +25 -0
  45. package/dist/deploy/types.d.ts +18 -0
  46. package/dist/deploy/types.d.ts.map +1 -1
  47. package/dist/deploy/verify.d.ts.map +1 -1
  48. package/dist/deploy/verify.js +7 -1
  49. package/dist/envsync/engine.d.ts.map +1 -1
  50. package/dist/envsync/engine.js +21 -7
  51. package/dist/envsync/paths.d.ts +22 -0
  52. package/dist/envsync/paths.d.ts.map +1 -1
  53. package/dist/envsync/paths.js +55 -0
  54. package/dist/peer-hint.d.ts +8 -0
  55. package/dist/peer-hint.d.ts.map +1 -0
  56. package/dist/peer-hint.js +49 -0
  57. package/dist/resolve/config-hint.d.ts +2 -0
  58. package/dist/resolve/config-hint.d.ts.map +1 -0
  59. package/dist/resolve/config-hint.js +23 -0
  60. package/dist/resolve/connect.d.ts.map +1 -1
  61. package/dist/resolve/connect.js +2 -1
  62. package/dist/resolve/deploy.d.ts.map +1 -1
  63. package/dist/resolve/deploy.js +167 -1
  64. package/dist/resolve/envsync.d.ts +16 -0
  65. package/dist/resolve/envsync.d.ts.map +1 -1
  66. package/dist/resolve/envsync.js +19 -0
  67. package/package.json +4 -3
@@ -4,9 +4,13 @@
4
4
  // Follows the pattern of resolve/connect.ts / resolve/files.ts: load the INI
5
5
  // via HazoConfig, read one section, throw a typed HazoConfigError on missing
6
6
  // required fields.
7
+ import os from 'node:os';
7
8
  import path from 'node:path';
8
9
  import { HazoConfig } from 'hazo_config/server';
9
10
  import { HazoConfigError } from 'hazo_core';
11
+ import { resolvePeerDir } from '../deploy/peer.js';
12
+ import { resolveConnectConfig } from './connect.js';
13
+ import { missingConfigMessage } from './config-hint.js';
10
14
  const DEFAULTS = {
11
15
  prod_port: 3000,
12
16
  staging_port: 3001,
@@ -32,7 +36,7 @@ function loadConfig() {
32
36
  throw new HazoConfigError({
33
37
  code: 'HAZO_ENV_CONFIG_FILE_MISSING',
34
38
  pkg: 'hazo_env',
35
- message: `Config file not found: ${filePath}. Create config/hazo_env_config.ini.`,
39
+ message: missingConfigMessage(filePath),
36
40
  });
37
41
  }
38
42
  }
@@ -108,6 +112,164 @@ function collectEnvFiles(section) {
108
112
  .filter((s) => s.length > 0);
109
113
  }
110
114
  const VALID_DB_STRATEGIES = new Set(['auto', 'copy', 'shared']);
115
+ /** Expand a leading `~` (home dir) in a path value. Leaves everything else unchanged. */
116
+ function expandHome(value) {
117
+ if (value === '~')
118
+ return os.homedir();
119
+ if (value.startsWith('~/'))
120
+ return path.join(os.homedir(), value.slice(2));
121
+ return value;
122
+ }
123
+ /**
124
+ * True when `child` is `parent` itself, or lives anywhere underneath it.
125
+ * Works for absolute paths (shared_root vs. slot dirs) and for slot-relative
126
+ * paths compared against each other (shared_dirs entries, the sqlite db
127
+ * path): `path.relative` resolves both against the same implicit base, so
128
+ * the relative result between them is correct either way.
129
+ */
130
+ function isInsideOrEqual(child, parent) {
131
+ const rel = path.relative(parent, child);
132
+ return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
133
+ }
134
+ /**
135
+ * Resolve `[deploy] shared_root`: default `~/<app_name>-shared`, `~`
136
+ * expanded, must resolve to an absolute path, and must sit outside both the
137
+ * current slot dir and its peer (best-effort — a peer that can't be
138
+ * resolved, e.g. no blue/green suffix yet, is simply not checked).
139
+ */
140
+ function resolveSharedRoot(section, appName, slot) {
141
+ const raw = section['shared_root']?.trim();
142
+ const expanded = raw ? expandHome(raw) : path.join(os.homedir(), `${appName}-shared`);
143
+ if (!path.isAbsolute(expanded)) {
144
+ throw new HazoConfigError({
145
+ code: 'HAZO_ENV_CONFIG_SHARED_ROOT_NOT_ABSOLUTE',
146
+ pkg: 'hazo_env',
147
+ message: `[deploy] shared_root "${raw}" must resolve to an absolute path. Use "~/..." or an absolute path.`,
148
+ });
149
+ }
150
+ const resolved = path.resolve(expanded);
151
+ const absCwd = path.resolve(slot.cwd);
152
+ if (isInsideOrEqual(resolved, absCwd)) {
153
+ throw new HazoConfigError({
154
+ code: 'HAZO_ENV_CONFIG_SHARED_ROOT_INSIDE_SLOT',
155
+ pkg: 'hazo_env',
156
+ message: `[deploy] shared_root "${resolved}" must be outside the app directory ("${absCwd}"). Point it at a sibling location instead.`,
157
+ });
158
+ }
159
+ let peerDir;
160
+ try {
161
+ peerDir = resolvePeerDir(absCwd, undefined, slot.dirSuffix);
162
+ }
163
+ catch {
164
+ peerDir = undefined; // Can't determine the peer yet (e.g. no -blue/-green suffix) — nothing to check against.
165
+ }
166
+ if (peerDir && isInsideOrEqual(resolved, peerDir)) {
167
+ throw new HazoConfigError({
168
+ code: 'HAZO_ENV_CONFIG_SHARED_ROOT_INSIDE_SLOT',
169
+ pkg: 'hazo_env',
170
+ message: `[deploy] shared_root "${resolved}" must be outside the peer directory ("${peerDir}"). Point it at a sibling location instead.`,
171
+ });
172
+ }
173
+ return resolved;
174
+ }
175
+ /**
176
+ * Parse `shared_dirs` (comma-separated, slot-relative paths, e.g.
177
+ * `shared_dirs = var/uploads, data/blog-images`) and reject anything that
178
+ * could resolve outside the slot or collide with another entry: absolute
179
+ * paths, `..` traversal, duplicates, and one entry nested inside another.
180
+ */
181
+ function collectSharedDirs(section) {
182
+ const raw = section['shared_dirs'];
183
+ if (typeof raw !== 'string' || !raw.trim())
184
+ return [];
185
+ const entries = raw
186
+ .split(',')
187
+ .map((s) => s.trim())
188
+ .filter((s) => s.length > 0);
189
+ const normalized = [];
190
+ for (const entry of entries) {
191
+ if (path.isAbsolute(entry)) {
192
+ throw new HazoConfigError({
193
+ code: 'HAZO_ENV_CONFIG_SHARED_DIR_ABSOLUTE',
194
+ pkg: 'hazo_env',
195
+ message: `[deploy] shared_dirs entry "${entry}" must be relative (slot-relative), not absolute.`,
196
+ });
197
+ }
198
+ const clean = entry.replace(/\/+$/, '');
199
+ const norm = path.normalize(clean);
200
+ if (norm.split(path.sep).includes('..')) {
201
+ throw new HazoConfigError({
202
+ code: 'HAZO_ENV_CONFIG_SHARED_DIR_TRAVERSAL',
203
+ pkg: 'hazo_env',
204
+ message: `[deploy] shared_dirs entry "${entry}" must not contain ".." path traversal.`,
205
+ });
206
+ }
207
+ normalized.push(norm);
208
+ }
209
+ const seen = new Set();
210
+ for (const dir of normalized) {
211
+ if (seen.has(dir)) {
212
+ throw new HazoConfigError({
213
+ code: 'HAZO_ENV_CONFIG_SHARED_DIR_DUPLICATE',
214
+ pkg: 'hazo_env',
215
+ message: `[deploy] shared_dirs contains a duplicate entry: "${dir}".`,
216
+ });
217
+ }
218
+ seen.add(dir);
219
+ }
220
+ for (let i = 0; i < normalized.length; i++) {
221
+ for (let j = 0; j < normalized.length; j++) {
222
+ if (i === j)
223
+ continue;
224
+ if (normalized[j] === normalized[i] || normalized[j].startsWith(normalized[i] + path.sep)) {
225
+ throw new HazoConfigError({
226
+ code: 'HAZO_ENV_CONFIG_SHARED_DIR_NESTED',
227
+ pkg: 'hazo_env',
228
+ message: `[deploy] shared_dirs entry "${normalized[j]}" is nested inside "${normalized[i]}" — list only the outermost directory.`,
229
+ });
230
+ }
231
+ }
232
+ }
233
+ return normalized;
234
+ }
235
+ /**
236
+ * Reject any `shared_dirs` entry that contains the resolved sqlite db path,
237
+ * when the db strategy resolves to "copy" (per-slot sqlite file). Best-effort:
238
+ * if the db config can't be resolved at all (no [db.<env>] section, env
239
+ * mismatch, ...) this silently skips the check — it's a safety net on top
240
+ * of an otherwise-valid config, not a required resolution step. Only runs
241
+ * when `shared_dirs` is non-empty, so a config that doesn't use the feature
242
+ * never pays for (or can be broken by) this extra resolution.
243
+ */
244
+ function checkSharedDirsAgainstSqlitePath(shared_dirs, db_strategy) {
245
+ if (shared_dirs.length === 0)
246
+ return;
247
+ let dbConfig;
248
+ try {
249
+ dbConfig = resolveConnectConfig();
250
+ }
251
+ catch {
252
+ return;
253
+ }
254
+ if (dbConfig.type !== 'sqlite' || !dbConfig.sqlite)
255
+ return;
256
+ const dbStrategyResolved = db_strategy === 'auto' ? 'copy' : db_strategy;
257
+ if (dbStrategyResolved !== 'copy')
258
+ return;
259
+ const rawDbPath = dbConfig.sqlite.database_path;
260
+ if (path.isAbsolute(rawDbPath))
261
+ return; // Shared across slots by config already — not a per-slot path to protect.
262
+ const dbPathRel = path.normalize(rawDbPath);
263
+ for (const dir of shared_dirs) {
264
+ if (isInsideOrEqual(dbPathRel, dir)) {
265
+ throw new HazoConfigError({
266
+ code: 'HAZO_ENV_CONFIG_SHARED_DIR_CONTAINS_SQLITE_DB',
267
+ pkg: 'hazo_env',
268
+ message: `[deploy] shared_dirs entry "${dir}" contains the sqlite db path "${dbPathRel}", but db_strategy resolves to "copy" (per-slot). Remove "${dir}" from shared_dirs, or change db_strategy.`,
269
+ });
270
+ }
271
+ }
272
+ }
111
273
  /**
112
274
  * Resolve the `[deploy]` section of hazo_env_config.ini into a typed
113
275
  * DeployConfig. `app_name` and `dir_suffix` are the only fields without a
@@ -147,6 +309,8 @@ export function resolveDeployConfig() {
147
309
  message: `[deploy] db_strategy "${db_strategy_raw}" is invalid. Expected "auto", "copy", or "shared".`,
148
310
  });
149
311
  }
312
+ const shared_dirs = collectSharedDirs(section);
313
+ checkSharedDirsAgainstSqlitePath(shared_dirs, db_strategy_raw);
150
314
  return {
151
315
  app_name,
152
316
  dir_suffix,
@@ -165,5 +329,7 @@ export function resolveDeployConfig() {
165
329
  sidecar_apps: collectSidecarApps(section),
166
330
  env_files: collectEnvFiles(section),
167
331
  verify_tsc_cmd: (section['verify_tsc_cmd'] ?? '').trim() || 'npx tsc --noEmit',
332
+ shared_root: resolveSharedRoot(section, app_name, { cwd: process.cwd(), dirSuffix: dir_suffix }),
333
+ shared_dirs,
168
334
  };
169
335
  }
@@ -39,6 +39,22 @@ export interface EnvsyncConfig {
39
39
  allowProd?: boolean;
40
40
  /** Every KEY=value pair from [migrate.env_overrides], ${VAR}-expanded */
41
41
  envOverrides: Record<string, string>;
42
+ /**
43
+ * `[deploy] shared_root`, when a [deploy] section resolves successfully —
44
+ * undefined otherwise (no [deploy] section, or it fails to resolve, e.g.
45
+ * missing app_name/dir_suffix). Used by envsync/paths.ts's
46
+ * resolveFilesRoot to recognize files_root as an allowed shared-dir link
47
+ * rather than an escaping symlink. Best-effort: envsync must keep working
48
+ * for apps with no [deploy] section at all.
49
+ */
50
+ shared_root?: string;
51
+ /**
52
+ * `[deploy] shared_dirs`, or undefined/[] when unresolved — see
53
+ * shared_root above. Optional (rather than defaulted to `[]` on this
54
+ * type) so existing `EnvsyncConfig` literals built by hand in tests don't
55
+ * need updating; callers treat a missing value as `[]`.
56
+ */
57
+ shared_dirs?: string[];
42
58
  }
43
59
  /**
44
60
  * Resolve the [envsync] section from hazo_env_config.ini.
@@ -1 +1 @@
1
- {"version":3,"file":"envsync.d.ts","sourceRoot":"","sources":["../../src/resolve/envsync.ts"],"names":[],"mappings":"AASA,MAAM,WAAW,aAAa;IAC5B,gEAAgE;IAChE,SAAS,EAAE,MAAM,CAAC;IAClB,oEAAoE;IACpE,SAAS,EAAE,MAAM,CAAC;IAClB,kCAAkC;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,0EAA0E;IAC1E,UAAU,EAAE,MAAM,CAAC;IACnB,oEAAoE;IACpE,QAAQ,EAAE,MAAM,CAAC;IACjB,oEAAoE;IACpE,IAAI,EAAE,MAAM,CAAC;IACb,yEAAyE;IACzE,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,qEAAqE;IACrE,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;;;;;;OAQG;IACH,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,yEAAyE;IACzE,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACtC;AAgBD;;;;;GAKG;AACH,wBAAgB,oBAAoB,IAAI,aAAa,GAAG,IAAI,CAuE3D"}
1
+ {"version":3,"file":"envsync.d.ts","sourceRoot":"","sources":["../../src/resolve/envsync.ts"],"names":[],"mappings":"AAUA,MAAM,WAAW,aAAa;IAC5B,gEAAgE;IAChE,SAAS,EAAE,MAAM,CAAC;IAClB,oEAAoE;IACpE,SAAS,EAAE,MAAM,CAAC;IAClB,kCAAkC;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,0EAA0E;IAC1E,UAAU,EAAE,MAAM,CAAC;IACnB,oEAAoE;IACpE,QAAQ,EAAE,MAAM,CAAC;IACjB,oEAAoE;IACpE,IAAI,EAAE,MAAM,CAAC;IACb,yEAAyE;IACzE,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,qEAAqE;IACrE,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;;;;;;OAQG;IACH,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,yEAAyE;IACzE,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrC;;;;;;;OAOG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;CACxB;AAgBD;;;;;GAKG;AACH,wBAAgB,oBAAoB,IAAI,aAAa,GAAG,IAAI,CAyF3D"}
@@ -5,6 +5,7 @@
5
5
  import path from 'node:path';
6
6
  import { HazoConfig } from 'hazo_config/server';
7
7
  import { resolveFilesConfig } from './files.js';
8
+ import { resolveDeployConfig } from './deploy.js';
8
9
  const DEFAULT_KEEP = 3;
9
10
  function tryLoadEnvConfig() {
10
11
  try {
@@ -73,6 +74,22 @@ export function resolveEnvsyncConfig() {
73
74
  }
74
75
  }
75
76
  }
77
+ // Best-effort: pull shared_root/shared_dirs from [deploy] so paths.ts's
78
+ // resolveFilesRoot can recognize files_root as an allowed shared-dir link.
79
+ // Most envsync configs have no [deploy] section at all (envsync predates
80
+ // the deploy module and is used standalone in some apps) — that, or any
81
+ // other resolution failure (missing app_name/dir_suffix, ...), must never
82
+ // break envsync itself; it just means the shared-dirs allowance is empty.
83
+ let shared_root;
84
+ let shared_dirs = [];
85
+ try {
86
+ const deployConfig = resolveDeployConfig();
87
+ shared_root = deployConfig.shared_root;
88
+ shared_dirs = deployConfig.shared_dirs;
89
+ }
90
+ catch {
91
+ // No [deploy] section, or it doesn't resolve — leave shared dirs empty.
92
+ }
76
93
  return {
77
94
  source_db,
78
95
  target_db: expandEnvVars(target_db_raw, extraVars),
@@ -86,5 +103,7 @@ export function resolveEnvsyncConfig() {
86
103
  prodDbPattern: prod_db_pattern_raw !== undefined ? prod_db_pattern_raw.trim() : undefined,
87
104
  allowProd,
88
105
  envOverrides,
106
+ shared_root,
107
+ shared_dirs,
89
108
  };
90
109
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hazo_env",
3
- "version": "0.15.3",
3
+ "version": "0.17.0",
4
4
  "description": "Canonical environment resolver — typed env names, per-env DB/file/secret config, doctor and CLI for hazo apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -19,7 +19,7 @@
19
19
  "./package.json": "./package.json"
20
20
  },
21
21
  "bin": {
22
- "hazo-env": "./dist/cli.js"
22
+ "hazo-env": "./dist/bin.js"
23
23
  },
24
24
  "files": [
25
25
  "dist",
@@ -33,7 +33,8 @@
33
33
  "lint": "tsc --noEmit",
34
34
  "test": "NODE_OPTIONS=--experimental-vm-modules jest",
35
35
  "dev:test-app": "npm run build && cd test-app && npm run dev",
36
- "build:test-app": "npm run build && cd test-app && npm run build"
36
+ "build:test-app": "npm run build && cd test-app && npm run build",
37
+ "docs:deploy": "npm run build && node dist/deploy/generate-readme-cli.js"
37
38
  },
38
39
  "dependencies": {
39
40
  "dotenv": "^16.4.5",