hazo_env 0.10.4 → 0.11.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/CHANGE_LOG.md +69 -0
- package/config/hazo_env_config.ini.sample +61 -0
- package/dist/cli.js +315 -0
- package/dist/deploy/build.d.ts +90 -0
- package/dist/deploy/build.d.ts.map +1 -0
- package/dist/deploy/build.js +299 -0
- package/dist/deploy/clean.d.ts +63 -0
- package/dist/deploy/clean.d.ts.map +1 -0
- package/dist/deploy/clean.js +135 -0
- package/dist/deploy/index.d.ts +6 -0
- package/dist/deploy/index.d.ts.map +1 -0
- package/dist/deploy/index.js +6 -0
- package/dist/deploy/init.d.ts +20 -0
- package/dist/deploy/init.d.ts.map +1 -0
- package/dist/deploy/init.js +80 -0
- package/dist/deploy/peer.d.ts +18 -0
- package/dist/deploy/peer.d.ts.map +1 -0
- package/dist/deploy/peer.js +48 -0
- package/dist/deploy/pm2-start.d.ts +88 -0
- package/dist/deploy/pm2-start.d.ts.map +1 -0
- package/dist/deploy/pm2-start.js +196 -0
- package/dist/deploy/rollback.d.ts +18 -0
- package/dist/deploy/rollback.d.ts.map +1 -0
- package/dist/deploy/rollback.js +167 -0
- package/dist/deploy/runner.d.ts +8 -0
- package/dist/deploy/runner.d.ts.map +1 -0
- package/dist/deploy/runner.js +35 -0
- package/dist/deploy/state.d.ts +19 -0
- package/dist/deploy/state.d.ts.map +1 -0
- package/dist/deploy/state.js +52 -0
- package/dist/deploy/status.d.ts +54 -0
- package/dist/deploy/status.d.ts.map +1 -0
- package/dist/deploy/status.js +182 -0
- package/dist/deploy/switch.d.ts +27 -0
- package/dist/deploy/switch.d.ts.map +1 -0
- package/dist/deploy/switch.js +152 -0
- package/dist/deploy/types.d.ts +96 -0
- package/dist/deploy/types.d.ts.map +1 -0
- package/dist/deploy/types.js +16 -0
- package/dist/deploy/verify.d.ts +41 -0
- package/dist/deploy/verify.d.ts.map +1 -0
- package/dist/deploy/verify.js +214 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/resolve/connect.d.ts.map +1 -1
- package/dist/resolve/connect.js +15 -1
- package/dist/resolve/deploy.d.ts +8 -0
- package/dist/resolve/deploy.d.ts.map +1 -0
- package/dist/resolve/deploy.js +121 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// hazo_env/src/deploy/verify.ts — `deploy verify`: re-runnable checks against
|
|
2
|
+
// the already-running standby app
|
|
3
|
+
//
|
|
4
|
+
// Unlike `deploy build`, this does NOT rebuild or restart anything. It's the
|
|
5
|
+
// "operator fixes something, re-runs verify, iterates" loop: three
|
|
6
|
+
// independent checks against whatever is already on disk/running on the
|
|
7
|
+
// staging port in the current (standby) directory.
|
|
8
|
+
//
|
|
9
|
+
// 1. Health check — HTTP GET to the staging app's health endpoint.
|
|
10
|
+
// 2. TypeScript — `npx tsc --noEmit` in the standby dir (via CommandRunner).
|
|
11
|
+
// 3. Migration parity — compare the live dir's and the standby dir's sqlite
|
|
12
|
+
// `_migrations` table name-sets for the current DB env (skipped for a
|
|
13
|
+
// shared Postgres DB, where parity trivially holds).
|
|
14
|
+
//
|
|
15
|
+
// Each check is independent: one failing never prevents the others from
|
|
16
|
+
// running — the whole point of `verify` is to surface everything that's
|
|
17
|
+
// still broken in one pass, not to short-circuit at the first problem (that
|
|
18
|
+
// linear-abort behavior belongs to `deploy build`, not here).
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import { createRequire } from 'node:module';
|
|
21
|
+
import { checkHealth, reroot, toAbsolute } from './build.js';
|
|
22
|
+
import { readState } from './state.js';
|
|
23
|
+
import { resolveConnectConfig } from '../resolve/connect.js';
|
|
24
|
+
// `doctor.ts`'s own _migrations-parity check (§7a) opens better-sqlite3 via a
|
|
25
|
+
// bare `require(...)`, which only works there because that file is loaded in
|
|
26
|
+
// a context where `require` happens to be in scope. This package builds to
|
|
27
|
+
// real ESM (`"type": "module"`), and under this package's own ESM/ts-jest
|
|
28
|
+
// test setup a bare `require` throws `ReferenceError: require is not
|
|
29
|
+
// defined` (verified directly while building this file). `createRequire`
|
|
30
|
+
// is the correct ESM-safe way to get a working `require` here, so this file
|
|
31
|
+
// uses it rather than copying doctor.ts's literal (and here, broken) form —
|
|
32
|
+
// see this task's report for the full note.
|
|
33
|
+
const require = createRequire(import.meta.url);
|
|
34
|
+
const DEFAULT_VERIFY_HEALTH_ATTEMPTS = 2;
|
|
35
|
+
const DEFAULT_VERIFY_HEALTH_DELAY_MS = 500;
|
|
36
|
+
const DEFAULT_TSC_CMD = 'npx tsc --noEmit';
|
|
37
|
+
/**
|
|
38
|
+
* Open `dbPath` read-only and return its `_migrations` table's name set.
|
|
39
|
+
* Mirrors doctor.ts's §7a shape (open via better-sqlite3, `SELECT name FROM
|
|
40
|
+
* _migrations`, treat a missing table as zero migrations applied) but
|
|
41
|
+
* returns a typed ok/error result instead of pushing directly into a
|
|
42
|
+
* DoctorCheck list, since this is a single live-vs-standby comparison rather
|
|
43
|
+
* than doctor's own N-env matrix.
|
|
44
|
+
*
|
|
45
|
+
* `fileMustExist: true` deliberately deviates from doctor.ts's plain
|
|
46
|
+
* `Database(path)` call: better-sqlite3 silently creates a new empty sqlite
|
|
47
|
+
* file at `path` if none exists, which would turn "the live db path doesn't
|
|
48
|
+
* actually exist" into a false "0 migrations, matches!" instead of a loud
|
|
49
|
+
* error.
|
|
50
|
+
*/
|
|
51
|
+
function readMigrationNames(dbPath) {
|
|
52
|
+
let Database;
|
|
53
|
+
try {
|
|
54
|
+
Database = require('better-sqlite3');
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
return { error: `better-sqlite3 is not available: ${e instanceof Error ? e.message : String(e)}` };
|
|
58
|
+
}
|
|
59
|
+
let db;
|
|
60
|
+
try {
|
|
61
|
+
db = Database(dbPath, { readonly: true, fileMustExist: true });
|
|
62
|
+
}
|
|
63
|
+
catch (e) {
|
|
64
|
+
return { error: `could not open ${dbPath}: ${e instanceof Error ? e.message : String(e)}` };
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
const names = db.prepare('SELECT name FROM _migrations').all().map((r) => r.name);
|
|
68
|
+
return { names: new Set(names) };
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
// _migrations table doesn't exist yet — treat as zero migrations applied
|
|
72
|
+
// (same lenient behavior as doctor.ts's try/catch around the same query).
|
|
73
|
+
return { names: new Set() };
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
db.close();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Compare the live dir's and the standby (current) dir's sqlite
|
|
81
|
+
* `_migrations` name-sets for the resolved current-env DB config.
|
|
82
|
+
*
|
|
83
|
+
* Scoped deliberately narrower than doctor.ts's own §7a check: doctor
|
|
84
|
+
* compares N *named envs* against each other (e.g. dev ↔ staging) for
|
|
85
|
+
* `--all --probe`, whereas this compares the SAME env's db as seen from two
|
|
86
|
+
* different directories (live vs standby) in a blue/green pair — the
|
|
87
|
+
* question `deploy verify` needs answered is "did the standby's migrations
|
|
88
|
+
* actually get applied/match the one currently serving traffic", not
|
|
89
|
+
* "do dev and staging happen to agree".
|
|
90
|
+
*/
|
|
91
|
+
function checkMigrationParity(cwd, liveDir) {
|
|
92
|
+
let dbConfig;
|
|
93
|
+
try {
|
|
94
|
+
dbConfig = resolveConnectConfig();
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
return {
|
|
98
|
+
name: 'migration-parity',
|
|
99
|
+
status: 'fail',
|
|
100
|
+
detail: `could not resolve DB config: ${e instanceof Error ? e.message : String(e)}`,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
if (dbConfig.type !== 'sqlite' || !dbConfig.sqlite) {
|
|
104
|
+
return {
|
|
105
|
+
name: 'migration-parity',
|
|
106
|
+
status: 'skipped',
|
|
107
|
+
detail: 'skipped (shared Postgres DB — parity trivially holds)',
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
const standbyDbPath = toAbsolute(cwd, dbConfig.sqlite.database_path);
|
|
111
|
+
const liveDbPath = reroot(cwd, liveDir, standbyDbPath);
|
|
112
|
+
if (liveDbPath === standbyDbPath) {
|
|
113
|
+
// Same reasoning as build.ts's db-setup step: the configured path isn't
|
|
114
|
+
// nested under the standby dir at all (e.g. an absolute [data] root
|
|
115
|
+
// shared by both live and standby), so live and standby are reading the
|
|
116
|
+
// exact same file — parity holds by construction.
|
|
117
|
+
return {
|
|
118
|
+
name: 'migration-parity',
|
|
119
|
+
status: 'pass',
|
|
120
|
+
detail: `sqlite db path is outside the live/standby split (shared) — parity holds by construction: ${standbyDbPath}`,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
const liveResult = readMigrationNames(liveDbPath);
|
|
124
|
+
if ('error' in liveResult) {
|
|
125
|
+
return { name: 'migration-parity', status: 'fail', detail: `live db (${liveDbPath}): ${liveResult.error}` };
|
|
126
|
+
}
|
|
127
|
+
const standbyResult = readMigrationNames(standbyDbPath);
|
|
128
|
+
if ('error' in standbyResult) {
|
|
129
|
+
return { name: 'migration-parity', status: 'fail', detail: `standby db (${standbyDbPath}): ${standbyResult.error}` };
|
|
130
|
+
}
|
|
131
|
+
const onlyInLive = [...liveResult.names].filter((n) => !standbyResult.names.has(n));
|
|
132
|
+
const onlyInStandby = [...standbyResult.names].filter((n) => !liveResult.names.has(n));
|
|
133
|
+
if (onlyInLive.length === 0 && onlyInStandby.length === 0) {
|
|
134
|
+
return {
|
|
135
|
+
name: 'migration-parity',
|
|
136
|
+
status: 'pass',
|
|
137
|
+
detail: `${standbyResult.names.size} migration(s) match between live (${liveDbPath}) and standby (${standbyDbPath})`,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
const detail = [
|
|
141
|
+
onlyInLive.length ? `only in live: ${onlyInLive.join(', ')}` : '',
|
|
142
|
+
onlyInStandby.length ? `only in standby: ${onlyInStandby.join(', ')}` : '',
|
|
143
|
+
]
|
|
144
|
+
.filter(Boolean)
|
|
145
|
+
.join('; ');
|
|
146
|
+
return { name: 'migration-parity', status: 'fail', detail };
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Run all three verify checks against `cwd` (the standby directory, which
|
|
150
|
+
* should already be running on `config.staging_port`). Every check runs
|
|
151
|
+
* regardless of whether an earlier one failed — see this file's header for
|
|
152
|
+
* why `verify` doesn't abort-on-first-failure the way `deploy build` does.
|
|
153
|
+
*/
|
|
154
|
+
export function runVerify(config, runner, opts = {}) {
|
|
155
|
+
const cwd = path.resolve(opts.cwd ?? process.cwd());
|
|
156
|
+
const attempts = opts.healthCheckAttempts ?? DEFAULT_VERIFY_HEALTH_ATTEMPTS;
|
|
157
|
+
const delayMs = opts.healthCheckDelayMs ?? DEFAULT_VERIFY_HEALTH_DELAY_MS;
|
|
158
|
+
const tscCmd = opts.tscCmd ?? DEFAULT_TSC_CMD;
|
|
159
|
+
const checks = [];
|
|
160
|
+
// 1. Health check.
|
|
161
|
+
const healthUrl = `http://localhost:${config.staging_port}${config.health_url}`;
|
|
162
|
+
try {
|
|
163
|
+
const result = checkHealth(runner, healthUrl, attempts, delayMs);
|
|
164
|
+
if (result.ok) {
|
|
165
|
+
checks.push({
|
|
166
|
+
name: 'health',
|
|
167
|
+
status: 'pass',
|
|
168
|
+
detail: `${healthUrl} -> 200 (attempt ${result.attempts}/${attempts})`,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
checks.push({
|
|
173
|
+
name: 'health',
|
|
174
|
+
status: 'fail',
|
|
175
|
+
detail: `${healthUrl} returned "${result.lastCode}" (expected 200) after ${attempts} attempt(s).`,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
catch (e) {
|
|
180
|
+
checks.push({ name: 'health', status: 'fail', detail: e instanceof Error ? e.message : String(e) });
|
|
181
|
+
}
|
|
182
|
+
// 2. TypeScript check.
|
|
183
|
+
try {
|
|
184
|
+
const result = runner.spawn(tscCmd, { cwd });
|
|
185
|
+
if (result.code === 0) {
|
|
186
|
+
checks.push({ name: 'typescript', status: 'pass', detail: `${tscCmd} — no errors` });
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
const output = [result.stdout, result.stderr].filter((s) => s.trim().length > 0).join('\n').trim();
|
|
190
|
+
checks.push({
|
|
191
|
+
name: 'typescript',
|
|
192
|
+
status: 'fail',
|
|
193
|
+
detail: output || `${tscCmd} exited with code ${result.code}`,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
catch (e) {
|
|
198
|
+
checks.push({ name: 'typescript', status: 'fail', detail: e instanceof Error ? e.message : String(e) });
|
|
199
|
+
}
|
|
200
|
+
// 3. Migration parity check.
|
|
201
|
+
const state = readState(config.app_name);
|
|
202
|
+
if (!state) {
|
|
203
|
+
checks.push({
|
|
204
|
+
name: 'migration-parity',
|
|
205
|
+
status: 'skipped',
|
|
206
|
+
detail: 'no deploy state found — run "deploy init" first',
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
checks.push(checkMigrationParity(cwd, path.resolve(state.live_dir)));
|
|
211
|
+
}
|
|
212
|
+
const allPassed = checks.every((c) => c.status !== 'fail');
|
|
213
|
+
return { checks, allPassed };
|
|
214
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ export * from './types/index.js';
|
|
|
4
4
|
export * from './resolve/secrets.js';
|
|
5
5
|
export * from './resolve/connect.js';
|
|
6
6
|
export * from './resolve/files.js';
|
|
7
|
+
export * from './resolve/deploy.js';
|
|
8
|
+
export * from './deploy/index.js';
|
|
7
9
|
export * from './doctor.js';
|
|
8
10
|
export * from './lib/index.js';
|
|
9
11
|
export { runSsh, assertSafeSshField } from './migrate/ssh-exec.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAE7E,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAEvG,cAAc,kBAAkB,CAAC;AAEjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAE7E,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAEvG,cAAc,kBAAkB,CAAC;AAEjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AAEpC,cAAc,mBAAmB,CAAC;AAElC,cAAc,aAAa,CAAC;AAE5B,cAAc,gBAAgB,CAAC;AAE/B,OAAO,EAAE,MAAM,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -9,6 +9,9 @@ export * from './types/index.js';
|
|
|
9
9
|
export * from './resolve/secrets.js';
|
|
10
10
|
export * from './resolve/connect.js';
|
|
11
11
|
export * from './resolve/files.js';
|
|
12
|
+
export * from './resolve/deploy.js';
|
|
13
|
+
// Deploy module (blue-green orchestration foundation: types, state, runner, peer)
|
|
14
|
+
export * from './deploy/index.js';
|
|
12
15
|
// Doctor
|
|
13
16
|
export * from './doctor.js';
|
|
14
17
|
// Lib
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../../src/resolve/connect.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,WAAW,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAmB5E;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,GAAE,qBAA0B,GAAG,WAAW,
|
|
1
|
+
{"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../../src/resolve/connect.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,WAAW,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAmB5E;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,GAAE,qBAA0B,GAAG,WAAW,CA4ElF"}
|
package/dist/resolve/connect.js
CHANGED
|
@@ -68,7 +68,21 @@ export function resolveConnectConfig(opts = {}) {
|
|
|
68
68
|
});
|
|
69
69
|
}
|
|
70
70
|
const database_path = substitutePlaceholders(rawPath, extraVars);
|
|
71
|
-
|
|
71
|
+
// Optional `driver = better-sqlite3` in [db.<env>] — passed straight through to
|
|
72
|
+
// hazo_connect's sqlite config. Validated here (not left for hazo_connect to reject)
|
|
73
|
+
// so a typo in the ini fails at config-resolve time with a hazo_env error that names
|
|
74
|
+
// the bad value, instead of surfacing later as hazo_connect's generic "Unsupported
|
|
75
|
+
// connection type" from deep inside the adapter factory.
|
|
76
|
+
const rawDriver = section['driver'];
|
|
77
|
+
if (rawDriver !== undefined && rawDriver !== 'sql.js' && rawDriver !== 'better-sqlite3') {
|
|
78
|
+
throw new HazoConfigError({
|
|
79
|
+
code: 'HAZO_ENV_CONFIG_UNKNOWN_SQLITE_DRIVER',
|
|
80
|
+
pkg: 'hazo_env',
|
|
81
|
+
message: `[db.${targetEnv}] has unknown driver "${rawDriver}". Expected "sql.js" or "better-sqlite3".`,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
const driver = rawDriver;
|
|
85
|
+
return { type: 'sqlite', sqlite: driver ? { database_path, driver } : { database_path } };
|
|
72
86
|
}
|
|
73
87
|
if (type === 'postgrest') {
|
|
74
88
|
const rawUrl = section['base_url'] ?? '';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { DeployConfig } from '../deploy/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Resolve the `[deploy]` section of hazo_env_config.ini into a typed
|
|
4
|
+
* DeployConfig. `app_name` and `dir_suffix` are the only fields without a
|
|
5
|
+
* default — every other field falls back to a sane value when omitted.
|
|
6
|
+
*/
|
|
7
|
+
export declare function resolveDeployConfig(): DeployConfig;
|
|
8
|
+
//# sourceMappingURL=deploy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deploy.d.ts","sourceRoot":"","sources":["../../src/resolve/deploy.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AA6DvD;;;;GAIG;AACH,wBAAgB,mBAAmB,IAAI,YAAY,CAuDlD"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// hazo_env/src/resolve/deploy.ts — Server-only deploy config resolver
|
|
2
|
+
//
|
|
3
|
+
// Reads the [deploy] section from hazo_env_config.ini and applies defaults.
|
|
4
|
+
// Follows the pattern of resolve/connect.ts / resolve/files.ts: load the INI
|
|
5
|
+
// via HazoConfig, read one section, throw a typed HazoConfigError on missing
|
|
6
|
+
// required fields.
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { HazoConfig } from 'hazo_config/server';
|
|
9
|
+
import { HazoConfigError } from 'hazo_core';
|
|
10
|
+
const DEFAULTS = {
|
|
11
|
+
prod_port: 3000,
|
|
12
|
+
staging_port: 3001,
|
|
13
|
+
health_url: '/api/health',
|
|
14
|
+
min_disk_mb: 1024,
|
|
15
|
+
min_memory_mb: 512,
|
|
16
|
+
branch: 'main',
|
|
17
|
+
db_strategy: 'auto',
|
|
18
|
+
build_cmd: 'npm run build',
|
|
19
|
+
install_cmd: 'npm ci',
|
|
20
|
+
pre_build_cmd: 'rm -rf .next',
|
|
21
|
+
migrate_cmd: '',
|
|
22
|
+
};
|
|
23
|
+
function findConfigFile() {
|
|
24
|
+
return path.resolve(process.cwd(), 'config', 'hazo_env_config.ini');
|
|
25
|
+
}
|
|
26
|
+
function loadConfig() {
|
|
27
|
+
const filePath = findConfigFile();
|
|
28
|
+
try {
|
|
29
|
+
return new HazoConfig({ filePath });
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
throw new HazoConfigError({
|
|
33
|
+
code: 'HAZO_ENV_CONFIG_FILE_MISSING',
|
|
34
|
+
pkg: 'hazo_env',
|
|
35
|
+
message: `Config file not found: ${filePath}. Create config/hazo_env_config.ini.`,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function parseIntOr(value, fallback) {
|
|
40
|
+
if (value === undefined)
|
|
41
|
+
return fallback;
|
|
42
|
+
const parsed = parseInt(value, 10);
|
|
43
|
+
return Number.isFinite(parsed) ? parsed : fallback;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Collects `post_switch_cmd`, `post_switch_cmd_2`, `post_switch_cmd_3`, ...
|
|
47
|
+
* from the [deploy] section, in numeric order. Numbering starts implicitly
|
|
48
|
+
* at the unsuffixed key (`post_switch_cmd`) and continues with `_2`, `_3`, etc.
|
|
49
|
+
*/
|
|
50
|
+
function collectPostSwitchCmds(section) {
|
|
51
|
+
const cmds = [];
|
|
52
|
+
const first = section['post_switch_cmd'];
|
|
53
|
+
if (typeof first === 'string' && first.trim())
|
|
54
|
+
cmds.push(first.trim());
|
|
55
|
+
let n = 2;
|
|
56
|
+
for (;;) {
|
|
57
|
+
const value = section[`post_switch_cmd_${n}`];
|
|
58
|
+
if (typeof value !== 'string' || !value.trim())
|
|
59
|
+
break;
|
|
60
|
+
cmds.push(value.trim());
|
|
61
|
+
n += 1;
|
|
62
|
+
}
|
|
63
|
+
return cmds;
|
|
64
|
+
}
|
|
65
|
+
const VALID_DB_STRATEGIES = new Set(['auto', 'copy', 'shared']);
|
|
66
|
+
/**
|
|
67
|
+
* Resolve the `[deploy]` section of hazo_env_config.ini into a typed
|
|
68
|
+
* DeployConfig. `app_name` and `dir_suffix` are the only fields without a
|
|
69
|
+
* default — every other field falls back to a sane value when omitted.
|
|
70
|
+
*/
|
|
71
|
+
export function resolveDeployConfig() {
|
|
72
|
+
const config = loadConfig();
|
|
73
|
+
const section = config.getSection('deploy');
|
|
74
|
+
if (!section) {
|
|
75
|
+
throw new HazoConfigError({
|
|
76
|
+
code: 'HAZO_ENV_CONFIG_MISSING_DEPLOY_SECTION',
|
|
77
|
+
pkg: 'hazo_env',
|
|
78
|
+
message: 'No [deploy] section found in hazo_env_config.ini.',
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
const app_name = section['app_name'];
|
|
82
|
+
if (!app_name) {
|
|
83
|
+
throw new HazoConfigError({
|
|
84
|
+
code: 'HAZO_ENV_CONFIG_MISSING_DEPLOY_APP_NAME',
|
|
85
|
+
pkg: 'hazo_env',
|
|
86
|
+
message: '[deploy] section is missing required field "app_name".',
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
const dir_suffix = section['dir_suffix'];
|
|
90
|
+
if (!dir_suffix) {
|
|
91
|
+
throw new HazoConfigError({
|
|
92
|
+
code: 'HAZO_ENV_CONFIG_MISSING_DEPLOY_DIR_SUFFIX',
|
|
93
|
+
pkg: 'hazo_env',
|
|
94
|
+
message: '[deploy] section is missing required field "dir_suffix".',
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
const db_strategy_raw = section['db_strategy'] ?? DEFAULTS.db_strategy;
|
|
98
|
+
if (!VALID_DB_STRATEGIES.has(db_strategy_raw)) {
|
|
99
|
+
throw new HazoConfigError({
|
|
100
|
+
code: 'HAZO_ENV_CONFIG_INVALID_DB_STRATEGY',
|
|
101
|
+
pkg: 'hazo_env',
|
|
102
|
+
message: `[deploy] db_strategy "${db_strategy_raw}" is invalid. Expected "auto", "copy", or "shared".`,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
app_name,
|
|
107
|
+
dir_suffix,
|
|
108
|
+
prod_port: parseIntOr(section['prod_port'], DEFAULTS.prod_port),
|
|
109
|
+
staging_port: parseIntOr(section['staging_port'], DEFAULTS.staging_port),
|
|
110
|
+
health_url: section['health_url'] ?? DEFAULTS.health_url,
|
|
111
|
+
min_disk_mb: parseIntOr(section['min_disk_mb'], DEFAULTS.min_disk_mb),
|
|
112
|
+
min_memory_mb: parseIntOr(section['min_memory_mb'], DEFAULTS.min_memory_mb),
|
|
113
|
+
branch: section['branch'] ?? DEFAULTS.branch,
|
|
114
|
+
db_strategy: db_strategy_raw,
|
|
115
|
+
build_cmd: section['build_cmd'] ?? DEFAULTS.build_cmd,
|
|
116
|
+
install_cmd: section['install_cmd'] ?? DEFAULTS.install_cmd,
|
|
117
|
+
pre_build_cmd: section['pre_build_cmd'] ?? DEFAULTS.pre_build_cmd,
|
|
118
|
+
migrate_cmd: section['migrate_cmd'] ?? DEFAULTS.migrate_cmd,
|
|
119
|
+
post_switch_cmds: collectPostSwitchCmds(section),
|
|
120
|
+
};
|
|
121
|
+
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -13,6 +13,16 @@ export interface DbEnvConfig {
|
|
|
13
13
|
type: 'sqlite' | 'postgrest';
|
|
14
14
|
sqlite?: {
|
|
15
15
|
database_path: string;
|
|
16
|
+
/** Which SQLite engine hazo_connect should use. Defaults to 'sql.js' when
|
|
17
|
+
* omitted — matches hazo_connect's own default, see its HazoConnectConfig.
|
|
18
|
+
* 'sql.js' keeps one in-memory copy per process and overwrites the whole
|
|
19
|
+
* file on every write, which corrupts the on-disk file when more than
|
|
20
|
+
* one process (or, in Next.js dev, more than one webpack bundle —
|
|
21
|
+
* middleware.ts compiles separately from route handlers) holds its own
|
|
22
|
+
* copy open against the same path. 'better-sqlite3' opens the file
|
|
23
|
+
* natively with real OS-level WAL locking and is the one that's safe
|
|
24
|
+
* under concurrent access. */
|
|
25
|
+
driver?: 'sql.js' | 'better-sqlite3';
|
|
16
26
|
};
|
|
17
27
|
postgrest?: {
|
|
18
28
|
base_url: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAEA,sDAAsD;AACtD,MAAM,MAAM,OAAO,GAAG,KAAK,GAAG,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,CAAC;AAEnE,mCAAmC;AACnC,MAAM,MAAM,WAAW,GAAG,aAAa,GAAG,MAAM,GAAG,SAAS,GAAG,YAAY,CAAC;AAE5E,iFAAiF;AACjF,MAAM,MAAM,UAAU,GAAG,MAAM,EAAE,CAAC;AAElC,0EAA0E;AAC1E,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AAErD,2CAA2C;AAC3C,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,QAAQ,GAAG,SAAS,CAAC;AAEtD,yEAAyE;AACzE,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,QAAQ,GAAG,WAAW,CAAC;IAC7B,MAAM,CAAC,EAAE;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAEA,sDAAsD;AACtD,MAAM,MAAM,OAAO,GAAG,KAAK,GAAG,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,CAAC;AAEnE,mCAAmC;AACnC,MAAM,MAAM,WAAW,GAAG,aAAa,GAAG,MAAM,GAAG,SAAS,GAAG,YAAY,CAAC;AAE5E,iFAAiF;AACjF,MAAM,MAAM,UAAU,GAAG,MAAM,EAAE,CAAC;AAElC,0EAA0E;AAC1E,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AAErD,2CAA2C;AAC3C,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,QAAQ,GAAG,SAAS,CAAC;AAEtD,yEAAyE;AACzE,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,QAAQ,GAAG,WAAW,CAAC;IAC7B,MAAM,CAAC,EAAE;QACP,aAAa,EAAE,MAAM,CAAC;QACtB;;;;;;;;uCAQ+B;QAC/B,MAAM,CAAC,EAAE,QAAQ,GAAG,gBAAgB,CAAC;KACtC,CAAC;IACF,SAAS,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;CACnD;AAED,yDAAyD;AACzD,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,OAAO,CAAC;IAClB,KAAK,EAAE;QACL,QAAQ,EAAE,MAAM,CAAC;QACjB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;KAC9B,CAAC;CACH;AAED,8BAA8B;AAC9B,MAAM,WAAW,cAAc;IAC7B,GAAG,EAAE,OAAO,CAAC;IACb,IAAI,EAAE,WAAW,CAAC;IAClB,OAAO,EAAE,UAAU,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,QAAQ,CAAC;IACnB,KAAK,CAAC,EAAE,UAAU,CAAC;CACpB;AAED,uCAAuC;AACvC,MAAM,WAAW,qBAAqB;IACpC,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hazo_env",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.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",
|
|
@@ -90,9 +90,9 @@
|
|
|
90
90
|
"@types/node": "^22.10.0",
|
|
91
91
|
"@types/react": "^19.0.0",
|
|
92
92
|
"@types/react-dom": "^19.0.0",
|
|
93
|
-
"hazo_core": "^2.
|
|
93
|
+
"hazo_core": "^2.5.0",
|
|
94
94
|
"hazo_config": "^2.6.0",
|
|
95
|
-
"hazo_connect": "^4.3.
|
|
95
|
+
"hazo_connect": "^4.3.5",
|
|
96
96
|
"hazo_files": "^3.4.0",
|
|
97
97
|
"next": "^16.0.10",
|
|
98
98
|
"react": "^19.0.0",
|