@basaltkit/deploy 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/LICENSE +21 -0
- package/README.md +112 -2
- package/dist/apache.d.ts +13 -0
- package/dist/apache.js +179 -0
- package/dist/backup.d.ts +13 -0
- package/dist/backup.js +51 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +3 -0
- package/dist/checks.d.ts +13 -0
- package/dist/checks.js +99 -0
- package/dist/cli.d.ts +8 -0
- package/dist/cli.js +133 -0
- package/dist/config.d.ts +231 -0
- package/dist/config.js +163 -0
- package/dist/context.d.ts +29 -0
- package/dist/context.js +44 -0
- package/dist/diff.d.ts +2 -0
- package/dist/diff.js +33 -0
- package/dist/dotenv.d.ts +47 -0
- package/dist/dotenv.js +145 -0
- package/dist/env-check.d.ts +18 -0
- package/dist/env-check.js +95 -0
- package/dist/env-step.d.ts +29 -0
- package/dist/env-step.js +126 -0
- package/dist/host.d.ts +85 -0
- package/dist/host.js +198 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +10 -0
- package/dist/infra.d.ts +42 -0
- package/dist/infra.js +242 -0
- package/dist/pipeline.d.ts +35 -0
- package/dist/pipeline.js +352 -0
- package/dist/pm2.d.ts +23 -0
- package/dist/pm2.js +106 -0
- package/dist/ports.d.ts +10 -0
- package/dist/ports.js +27 -0
- package/dist/state.d.ts +20 -0
- package/dist/state.js +34 -0
- package/package.json +48 -3
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `deploy.config.ts` — committed with the app, holds no secrets (RFC 0006 §3, §11.2).
|
|
3
|
+
*
|
|
4
|
+
* Targets are plain data (`{ kind: 'pm2', … }`); what each kind does lives in
|
|
5
|
+
* its own module. A config can therefore be printed, diffed and validated
|
|
6
|
+
* without running anything.
|
|
7
|
+
*/
|
|
8
|
+
export interface DeployConfig {
|
|
9
|
+
/** Owner label: names every process, container, volume, vhost and backup. `[a-z][a-z0-9-]{1,30}`. */
|
|
10
|
+
app: string;
|
|
11
|
+
/** The apex the app answers on, e.g. `lexfirma.aipro.ao`. */
|
|
12
|
+
domain: string;
|
|
13
|
+
/** `subdomain` → tenants live on `<id>.<domain>`: the wildcard certificate is checked too. */
|
|
14
|
+
tenants?: 'subdomain' | 'none';
|
|
15
|
+
branch?: string;
|
|
16
|
+
remote?: string;
|
|
17
|
+
env: EnvConfig;
|
|
18
|
+
infra?: InfraConfig;
|
|
19
|
+
build?: BuildConfig;
|
|
20
|
+
migrate?: MigrateConfig;
|
|
21
|
+
backup?: BackupConfig;
|
|
22
|
+
runtime: RuntimeConfig;
|
|
23
|
+
proxy?: ProxyConfig;
|
|
24
|
+
health?: HealthConfig;
|
|
25
|
+
/**
|
|
26
|
+
* Migration directory names (e.g. `20261011_drop_legacy`) that cannot run
|
|
27
|
+
* under the previous release's code. A release that applies one is never
|
|
28
|
+
* rolled back automatically: the health failure prints the restore command
|
|
29
|
+
* for the pre-migration dump instead (RFC 0006 §8).
|
|
30
|
+
*/
|
|
31
|
+
destructive?: string[];
|
|
32
|
+
}
|
|
33
|
+
export interface EnvConfig {
|
|
34
|
+
/** Relative to the app directory. Written 0600, backed up before every change. */
|
|
35
|
+
file?: string;
|
|
36
|
+
/** The module whose import runs the app's `defineEnv(...)`, e.g. `src/env.ts`. */
|
|
37
|
+
schema?: string;
|
|
38
|
+
/** Always these values — a different one in the file is corrected. */
|
|
39
|
+
fixed?: Record<string, string>;
|
|
40
|
+
/** Generated once when absent, then kept. */
|
|
41
|
+
generate?: Record<string, SecretSpec>;
|
|
42
|
+
/** Asked once when absent or empty, then kept. */
|
|
43
|
+
ask?: Record<string, Question>;
|
|
44
|
+
/** Computed from the deploy's own values on every run. */
|
|
45
|
+
derive?: (context: DeriveContext) => Record<string, string>;
|
|
46
|
+
/** Must not exist in the file at all (e.g. a flag whose "0" would read as true). */
|
|
47
|
+
forbid?: string[];
|
|
48
|
+
}
|
|
49
|
+
export interface DeriveContext {
|
|
50
|
+
app: string;
|
|
51
|
+
domain: string;
|
|
52
|
+
port: number;
|
|
53
|
+
}
|
|
54
|
+
export interface SecretSpec {
|
|
55
|
+
kind: 'secret';
|
|
56
|
+
/** Random bytes, written as hex. */
|
|
57
|
+
bytes: number;
|
|
58
|
+
}
|
|
59
|
+
export interface Question {
|
|
60
|
+
kind: 'question';
|
|
61
|
+
prompt: string;
|
|
62
|
+
secret: boolean;
|
|
63
|
+
/** May be left empty (the app then runs without it, e.g. mail to the log). */
|
|
64
|
+
optional: boolean;
|
|
65
|
+
default?: string;
|
|
66
|
+
}
|
|
67
|
+
export type InfraConfig = ExternalInfra | DockerInfra;
|
|
68
|
+
export interface ExternalInfra {
|
|
69
|
+
kind: 'external';
|
|
70
|
+
}
|
|
71
|
+
export interface DockerInfra {
|
|
72
|
+
kind: 'docker';
|
|
73
|
+
postgres?: PostgresService;
|
|
74
|
+
redis?: RedisService;
|
|
75
|
+
minio?: MinioService;
|
|
76
|
+
}
|
|
77
|
+
export interface PostgresService {
|
|
78
|
+
image: string;
|
|
79
|
+
/** Host port on 127.0.0.1; `auto` keeps the running container's, else the `.env` URL's, else the first free from 5433. */
|
|
80
|
+
port: number | 'auto';
|
|
81
|
+
user: string;
|
|
82
|
+
database: string;
|
|
83
|
+
/** The `.env` key that receives the connection URL. */
|
|
84
|
+
urlKey: string;
|
|
85
|
+
/** Query parameters appended to the URL (e.g. `{ schema: 'public' }` for Prisma). */
|
|
86
|
+
urlParams: Record<string, string>;
|
|
87
|
+
}
|
|
88
|
+
export interface RedisService {
|
|
89
|
+
image: string;
|
|
90
|
+
port: number | 'auto';
|
|
91
|
+
urlKey: string;
|
|
92
|
+
}
|
|
93
|
+
export interface MinioService {
|
|
94
|
+
image: string;
|
|
95
|
+
port: number | 'auto';
|
|
96
|
+
consolePort: number | 'auto';
|
|
97
|
+
user: string;
|
|
98
|
+
buckets: string[];
|
|
99
|
+
/** The `.env` keys that receive the endpoint and the credentials. */
|
|
100
|
+
keys: {
|
|
101
|
+
endpoint: string;
|
|
102
|
+
accessKey: string;
|
|
103
|
+
secretKey: string;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
export interface BuildConfig {
|
|
107
|
+
/** Prisma config files, each generated with `prisma generate --config <file>`. */
|
|
108
|
+
prisma?: string[];
|
|
109
|
+
/** Frontend directory built with `<pm> --dir <web> build`. */
|
|
110
|
+
web?: string;
|
|
111
|
+
}
|
|
112
|
+
export interface MigrateConfig {
|
|
113
|
+
/** `prisma migrate deploy` with the root Prisma config. */
|
|
114
|
+
central?: boolean;
|
|
115
|
+
/** `<pm> basalt tenant:migrate` (or the given package.json script arguments). */
|
|
116
|
+
tenants?: boolean | string[];
|
|
117
|
+
}
|
|
118
|
+
export interface BackupConfig {
|
|
119
|
+
beforeMigrate?: boolean;
|
|
120
|
+
keep?: number;
|
|
121
|
+
/** Relative to the app directory. Default `backups/predeploy`. */
|
|
122
|
+
dir?: string;
|
|
123
|
+
/** With external infra: the `.env` key holding the database URL. Default `DATABASE_URL`. */
|
|
124
|
+
urlKey?: string;
|
|
125
|
+
}
|
|
126
|
+
export type RuntimeConfig = Pm2Runtime;
|
|
127
|
+
export interface Pm2Runtime {
|
|
128
|
+
kind: 'pm2';
|
|
129
|
+
/** Default `<app>-api`. Must start with `<app>-` (RFC 0006 §5, C4). */
|
|
130
|
+
name?: string;
|
|
131
|
+
/** package.json script the process runs. */
|
|
132
|
+
script: string;
|
|
133
|
+
port: number | 'auto';
|
|
134
|
+
/** Ports never picked by `auto` (e.g. a port the proxy itself listens on). */
|
|
135
|
+
avoid: number[];
|
|
136
|
+
}
|
|
137
|
+
export type ProxyConfig = ApacheProxy | NoProxy;
|
|
138
|
+
export interface ApacheProxy {
|
|
139
|
+
kind: 'apache';
|
|
140
|
+
/** File name under `sites-available/`. Default `<app>.conf`. */
|
|
141
|
+
vhost?: string;
|
|
142
|
+
/** Directory served as the site's root, relative to the app directory. */
|
|
143
|
+
webRoot: string;
|
|
144
|
+
/** Path prefix proxied to the API (stripped). */
|
|
145
|
+
apiPrefix: string;
|
|
146
|
+
/** Let's Encrypt certificate name (`/etc/letsencrypt/live/<name>/`). Default: the domain. */
|
|
147
|
+
certName?: string;
|
|
148
|
+
/** Default `/etc/apache2`. */
|
|
149
|
+
root?: string;
|
|
150
|
+
}
|
|
151
|
+
export interface NoProxy {
|
|
152
|
+
kind: 'none';
|
|
153
|
+
}
|
|
154
|
+
export interface HealthConfig {
|
|
155
|
+
/** Path on the API itself (without the proxy prefix). */
|
|
156
|
+
path?: string;
|
|
157
|
+
timeoutSeconds?: number;
|
|
158
|
+
/** Also check `https://<domain><apiPrefix><path>` — what users reach. */
|
|
159
|
+
public?: boolean;
|
|
160
|
+
}
|
|
161
|
+
export declare function defineDeploy(config: DeployConfig): DeployConfig;
|
|
162
|
+
export declare const secret: (bytes?: number) => SecretSpec;
|
|
163
|
+
export declare const ask: {
|
|
164
|
+
secret: (prompt: string, options?: {
|
|
165
|
+
optional?: boolean;
|
|
166
|
+
}) => Question;
|
|
167
|
+
text: (prompt: string, options?: {
|
|
168
|
+
optional?: boolean;
|
|
169
|
+
default?: string;
|
|
170
|
+
}) => Question;
|
|
171
|
+
};
|
|
172
|
+
export declare const pm2: (options?: {
|
|
173
|
+
name?: string;
|
|
174
|
+
script?: string;
|
|
175
|
+
port?: number | 'auto';
|
|
176
|
+
avoid?: number[];
|
|
177
|
+
}) => Pm2Runtime;
|
|
178
|
+
export declare const apache: (options?: {
|
|
179
|
+
vhost?: string;
|
|
180
|
+
webRoot?: string;
|
|
181
|
+
apiPrefix?: string;
|
|
182
|
+
certName?: string;
|
|
183
|
+
root?: string;
|
|
184
|
+
}) => ApacheProxy;
|
|
185
|
+
export declare const none: () => NoProxy;
|
|
186
|
+
export declare const external: () => ExternalInfra;
|
|
187
|
+
export declare const MINIO_IMAGE = "minio/minio:RELEASE.2025-03-12T18-04-18Z";
|
|
188
|
+
export interface DockerInfraOptions {
|
|
189
|
+
postgres?: Partial<PostgresService> | false;
|
|
190
|
+
redis?: Partial<RedisService> | false;
|
|
191
|
+
minio?: Partial<MinioService> | false;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Dedicated containers on 127.0.0.1 for this app only. Names are fixed by the
|
|
195
|
+
* app label — `<app>-pg`, `<app>-redis`, `<app>-minio` — so a server that
|
|
196
|
+
* already runs them (created by hand or by an older script) is adopted, not
|
|
197
|
+
* duplicated.
|
|
198
|
+
*/
|
|
199
|
+
export declare function dockerInfra(app: string, options?: DockerInfraOptions): DockerInfra;
|
|
200
|
+
export declare const APP_NAME: RegExp;
|
|
201
|
+
export declare class DeployConfigError extends Error {
|
|
202
|
+
readonly problems: string[];
|
|
203
|
+
constructor(problems: string[]);
|
|
204
|
+
}
|
|
205
|
+
export declare const processName: (config: DeployConfig) => string;
|
|
206
|
+
export declare const vhostName: (config: DeployConfig) => string | undefined;
|
|
207
|
+
export declare function validateConfig(config: DeployConfig): DeployConfig;
|
|
208
|
+
/**
|
|
209
|
+
* `/etc/basalt/<app>.json` — what is true of ONE server and so cannot be
|
|
210
|
+
* committed: a port taken there by something else, a branch it tracks. Data
|
|
211
|
+
* only; functions stay in deploy.config.ts.
|
|
212
|
+
*/
|
|
213
|
+
export interface ServerOverrides {
|
|
214
|
+
domain?: string;
|
|
215
|
+
branch?: string;
|
|
216
|
+
port?: number;
|
|
217
|
+
infra?: {
|
|
218
|
+
postgres?: {
|
|
219
|
+
port?: number;
|
|
220
|
+
};
|
|
221
|
+
redis?: {
|
|
222
|
+
port?: number;
|
|
223
|
+
};
|
|
224
|
+
minio?: {
|
|
225
|
+
port?: number;
|
|
226
|
+
consolePort?: number;
|
|
227
|
+
};
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
export declare function applyOverrides(config: DeployConfig, overrides: ServerOverrides): DeployConfig;
|
|
231
|
+
export declare function parseOverrides(text: string, path: string): ServerOverrides;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `deploy.config.ts` — committed with the app, holds no secrets (RFC 0006 §3, §11.2).
|
|
3
|
+
*
|
|
4
|
+
* Targets are plain data (`{ kind: 'pm2', … }`); what each kind does lives in
|
|
5
|
+
* its own module. A config can therefore be printed, diffed and validated
|
|
6
|
+
* without running anything.
|
|
7
|
+
*/
|
|
8
|
+
// ── helpers used in deploy.config.ts ───────────────────────────────────────
|
|
9
|
+
export function defineDeploy(config) {
|
|
10
|
+
return config;
|
|
11
|
+
}
|
|
12
|
+
export const secret = (bytes = 32) => ({ kind: 'secret', bytes });
|
|
13
|
+
export const ask = {
|
|
14
|
+
secret: (prompt, options = {}) => ({
|
|
15
|
+
kind: 'question', prompt, secret: true, optional: options.optional ?? false,
|
|
16
|
+
}),
|
|
17
|
+
text: (prompt, options = {}) => ({
|
|
18
|
+
kind: 'question', prompt, secret: false, optional: options.optional ?? false,
|
|
19
|
+
...(options.default === undefined ? {} : { default: options.default }),
|
|
20
|
+
}),
|
|
21
|
+
};
|
|
22
|
+
export const pm2 = (options = {}) => ({
|
|
23
|
+
kind: 'pm2',
|
|
24
|
+
...(options.name === undefined ? {} : { name: options.name }),
|
|
25
|
+
script: options.script ?? 'start',
|
|
26
|
+
port: options.port ?? 'auto',
|
|
27
|
+
avoid: options.avoid ?? [],
|
|
28
|
+
});
|
|
29
|
+
export const apache = (options = {}) => ({
|
|
30
|
+
kind: 'apache',
|
|
31
|
+
...(options.vhost === undefined ? {} : { vhost: options.vhost }),
|
|
32
|
+
webRoot: options.webRoot ?? 'web/dist',
|
|
33
|
+
apiPrefix: options.apiPrefix ?? '/api',
|
|
34
|
+
...(options.certName === undefined ? {} : { certName: options.certName }),
|
|
35
|
+
...(options.root === undefined ? {} : { root: options.root }),
|
|
36
|
+
});
|
|
37
|
+
export const none = () => ({ kind: 'none' });
|
|
38
|
+
export const external = () => ({ kind: 'external' });
|
|
39
|
+
export const MINIO_IMAGE = 'minio/minio:RELEASE.2025-03-12T18-04-18Z';
|
|
40
|
+
/**
|
|
41
|
+
* Dedicated containers on 127.0.0.1 for this app only. Names are fixed by the
|
|
42
|
+
* app label — `<app>-pg`, `<app>-redis`, `<app>-minio` — so a server that
|
|
43
|
+
* already runs them (created by hand or by an older script) is adopted, not
|
|
44
|
+
* duplicated.
|
|
45
|
+
*/
|
|
46
|
+
export function dockerInfra(app, options = {}) {
|
|
47
|
+
const infra = { kind: 'docker' };
|
|
48
|
+
if (options.postgres !== false) {
|
|
49
|
+
infra.postgres = {
|
|
50
|
+
image: 'postgres:16', port: 'auto', user: app, database: app, urlKey: 'DATABASE_URL', urlParams: {},
|
|
51
|
+
...options.postgres,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
if (options.redis !== false) {
|
|
55
|
+
infra.redis = { image: 'redis:7-alpine', port: 'auto', urlKey: 'REDIS_URL', ...options.redis };
|
|
56
|
+
}
|
|
57
|
+
if (options.minio !== false && options.minio !== undefined) {
|
|
58
|
+
infra.minio = {
|
|
59
|
+
image: MINIO_IMAGE, port: 'auto', consolePort: 'auto', user: app, buckets: [],
|
|
60
|
+
keys: { endpoint: 'S3_ENDPOINT', accessKey: 'S3_ACCESS_KEY', secretKey: 'S3_SECRET_KEY' },
|
|
61
|
+
...options.minio,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return infra;
|
|
65
|
+
}
|
|
66
|
+
// ── validation and server-local overrides ──────────────────────────────────
|
|
67
|
+
export const APP_NAME = /^[a-z][a-z0-9-]{1,30}$/;
|
|
68
|
+
const DOMAIN = /^(?=.{1,253}$)([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/;
|
|
69
|
+
export class DeployConfigError extends Error {
|
|
70
|
+
problems;
|
|
71
|
+
constructor(problems) {
|
|
72
|
+
super(`deploy.config.ts:\n${problems.map((p) => ` - ${p}`).join('\n')}`);
|
|
73
|
+
this.problems = problems;
|
|
74
|
+
this.name = 'DeployConfigError';
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
export const processName = (config) => config.runtime.name ?? `${config.app}-api`;
|
|
78
|
+
export const vhostName = (config) => config.proxy?.kind === 'apache' ? (config.proxy.vhost ?? `${config.app}.conf`) : undefined;
|
|
79
|
+
export function validateConfig(config) {
|
|
80
|
+
const problems = [];
|
|
81
|
+
if (!APP_NAME.test(config.app))
|
|
82
|
+
problems.push(`app "${config.app}" must match ${APP_NAME}`);
|
|
83
|
+
if (!DOMAIN.test(config.domain))
|
|
84
|
+
problems.push(`domain "${config.domain}" is not a lowercase host name`);
|
|
85
|
+
// C4: the deploy only ever names what carries the app's label. `pm2 restart
|
|
86
|
+
// all` once was one keystroke away from a production app that is not ours.
|
|
87
|
+
const name = processName(config);
|
|
88
|
+
if (!name.startsWith(`${config.app}-`))
|
|
89
|
+
problems.push(`runtime.name "${name}" must start with "${config.app}-" — the deploy only touches what the app owns`);
|
|
90
|
+
if (!/^[a-z0-9-]+$/.test(name))
|
|
91
|
+
problems.push(`runtime.name "${name}" may only use a-z, 0-9 and -`);
|
|
92
|
+
const vhost = vhostName(config);
|
|
93
|
+
if (vhost !== undefined && !/^[a-z0-9.-]+\.conf$/.test(vhost))
|
|
94
|
+
problems.push(`proxy.vhost "${vhost}" must be a plain *.conf file name`);
|
|
95
|
+
const keys = [
|
|
96
|
+
...Object.keys(config.env.fixed ?? {}),
|
|
97
|
+
...Object.keys(config.env.generate ?? {}),
|
|
98
|
+
...Object.keys(config.env.ask ?? {}),
|
|
99
|
+
];
|
|
100
|
+
for (const key of keys)
|
|
101
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key))
|
|
102
|
+
problems.push(`env key "${key}" is not a valid variable name`);
|
|
103
|
+
for (const key of config.env.forbid ?? []) {
|
|
104
|
+
if (keys.includes(key))
|
|
105
|
+
problems.push(`env key "${key}" is both forbidden and set`);
|
|
106
|
+
}
|
|
107
|
+
if (config.infra?.kind === 'docker') {
|
|
108
|
+
const minio = config.infra.minio;
|
|
109
|
+
for (const bucket of minio?.buckets ?? []) {
|
|
110
|
+
if (!/^[a-z0-9][a-z0-9.-]{2,62}$/.test(bucket))
|
|
111
|
+
problems.push(`bucket "${bucket}" is not a valid S3 bucket name`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (config.backup?.keep !== undefined && (!Number.isInteger(config.backup.keep) || config.backup.keep < 1)) {
|
|
115
|
+
problems.push('backup.keep must be a whole number ≥ 1');
|
|
116
|
+
}
|
|
117
|
+
if (problems.length > 0)
|
|
118
|
+
throw new DeployConfigError(problems);
|
|
119
|
+
return config;
|
|
120
|
+
}
|
|
121
|
+
export function applyOverrides(config, overrides) {
|
|
122
|
+
const next = { ...config };
|
|
123
|
+
if (overrides.domain !== undefined)
|
|
124
|
+
next.domain = overrides.domain;
|
|
125
|
+
if (overrides.branch !== undefined)
|
|
126
|
+
next.branch = overrides.branch;
|
|
127
|
+
if (overrides.port !== undefined)
|
|
128
|
+
next.runtime = { ...config.runtime, port: overrides.port };
|
|
129
|
+
if (overrides.infra && config.infra?.kind === 'docker') {
|
|
130
|
+
const infra = { ...config.infra };
|
|
131
|
+
const o = overrides.infra;
|
|
132
|
+
if (infra.postgres && o.postgres?.port !== undefined)
|
|
133
|
+
infra.postgres = { ...infra.postgres, port: o.postgres.port };
|
|
134
|
+
if (infra.redis && o.redis?.port !== undefined)
|
|
135
|
+
infra.redis = { ...infra.redis, port: o.redis.port };
|
|
136
|
+
if (infra.minio && o.minio) {
|
|
137
|
+
infra.minio = {
|
|
138
|
+
...infra.minio,
|
|
139
|
+
...(o.minio.port === undefined ? {} : { port: o.minio.port }),
|
|
140
|
+
...(o.minio.consolePort === undefined ? {} : { consolePort: o.minio.consolePort }),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
next.infra = infra;
|
|
144
|
+
}
|
|
145
|
+
return next;
|
|
146
|
+
}
|
|
147
|
+
export function parseOverrides(text, path) {
|
|
148
|
+
let value;
|
|
149
|
+
try {
|
|
150
|
+
value = JSON.parse(text);
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
throw new DeployConfigError([`${path} is not valid JSON: ${error.message}`]);
|
|
154
|
+
}
|
|
155
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
156
|
+
throw new DeployConfigError([`${path} must be a JSON object`]);
|
|
157
|
+
}
|
|
158
|
+
const known = new Set(['domain', 'branch', 'port', 'infra']);
|
|
159
|
+
const unknown = Object.keys(value).filter((key) => !known.has(key));
|
|
160
|
+
if (unknown.length > 0)
|
|
161
|
+
throw new DeployConfigError([`${path}: unknown keys ${unknown.join(', ')} (known: ${[...known].join(', ')})`]);
|
|
162
|
+
return value;
|
|
163
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { DeployConfig } from './config.js';
|
|
2
|
+
import type { Host } from './host.js';
|
|
3
|
+
export interface DeployContext {
|
|
4
|
+
config: DeployConfig;
|
|
5
|
+
/** The app's checkout. */
|
|
6
|
+
dir: string;
|
|
7
|
+
host: Host;
|
|
8
|
+
/** The package manager the project declares (`packageManager`, else its lockfile). */
|
|
9
|
+
pm: 'pnpm' | 'npm' | 'yarn' | 'bun';
|
|
10
|
+
envPath: string;
|
|
11
|
+
/** Releases, generated infra files. `/var/lib/basalt/<app>` once `install` ran, else `<dir>/.basalt`. */
|
|
12
|
+
stateDir: string;
|
|
13
|
+
}
|
|
14
|
+
export interface Finding {
|
|
15
|
+
/** The RFC 0006 §5 check id (`C1`…`C10`) or a step name. */
|
|
16
|
+
id: string;
|
|
17
|
+
level: 'error' | 'warning';
|
|
18
|
+
message: string;
|
|
19
|
+
/** One line the operator can act on. */
|
|
20
|
+
fix?: string;
|
|
21
|
+
}
|
|
22
|
+
export declare const errors: (findings: Finding[]) => Finding[];
|
|
23
|
+
export declare function report(host: Host, findings: Finding[]): void;
|
|
24
|
+
export declare function detectPackageManager(host: Host, dir: string): Promise<DeployContext['pm']>;
|
|
25
|
+
export declare function resolveStateDir(host: Host, config: DeployConfig, dir: string): Promise<string>;
|
|
26
|
+
export declare class DeployError extends Error {
|
|
27
|
+
readonly findings: Finding[];
|
|
28
|
+
constructor(message: string, findings?: Finding[]);
|
|
29
|
+
}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
export const errors = (findings) => findings.filter((f) => f.level === 'error');
|
|
3
|
+
export function report(host, findings) {
|
|
4
|
+
for (const finding of findings) {
|
|
5
|
+
const line = `[${finding.id}] ${finding.message}${finding.fix ? `\n → ${finding.fix}` : ''}`;
|
|
6
|
+
if (finding.level === 'error')
|
|
7
|
+
host.out.error(line);
|
|
8
|
+
else
|
|
9
|
+
host.out.warn(line);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export async function detectPackageManager(host, dir) {
|
|
13
|
+
const text = await host.readFile(join(dir, 'package.json'));
|
|
14
|
+
if (text) {
|
|
15
|
+
try {
|
|
16
|
+
const declared = String(JSON.parse(text).packageManager ?? '');
|
|
17
|
+
const name = declared.split('@')[0];
|
|
18
|
+
if (name === 'pnpm' || name === 'npm' || name === 'yarn' || name === 'bun')
|
|
19
|
+
return name;
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
// An unreadable package.json is the build step's error to report.
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (await host.exists(join(dir, 'pnpm-lock.yaml')))
|
|
26
|
+
return 'pnpm';
|
|
27
|
+
if (await host.exists(join(dir, 'yarn.lock')))
|
|
28
|
+
return 'yarn';
|
|
29
|
+
if (await host.exists(join(dir, 'bun.lock')))
|
|
30
|
+
return 'bun';
|
|
31
|
+
return 'npm';
|
|
32
|
+
}
|
|
33
|
+
export async function resolveStateDir(host, config, dir) {
|
|
34
|
+
const system = `/var/lib/basalt/${config.app}`;
|
|
35
|
+
return (await host.stat(system))?.kind === 'dir' ? system : join(dir, '.basalt');
|
|
36
|
+
}
|
|
37
|
+
export class DeployError extends Error {
|
|
38
|
+
findings;
|
|
39
|
+
constructor(message, findings = []) {
|
|
40
|
+
super(message);
|
|
41
|
+
this.findings = findings;
|
|
42
|
+
this.name = 'DeployError';
|
|
43
|
+
}
|
|
44
|
+
}
|
package/dist/diff.d.ts
ADDED
package/dist/diff.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** A line diff (LCS) for audit output: `-` installed, `+` would be written. Small files only (vhosts). */
|
|
2
|
+
export function lineDiff(before, after) {
|
|
3
|
+
const a = before.replace(/\r\n/g, '\n').split('\n');
|
|
4
|
+
const b = after.replace(/\r\n/g, '\n').split('\n');
|
|
5
|
+
const n = a.length;
|
|
6
|
+
const m = b.length;
|
|
7
|
+
const lcs = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));
|
|
8
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
9
|
+
for (let j = m - 1; j >= 0; j--) {
|
|
10
|
+
const row = lcs[i];
|
|
11
|
+
row[j] = a[i] === b[j] ? lcs[i + 1][j + 1] + 1 : Math.max(lcs[i + 1][j], row[j + 1]);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
const out = [];
|
|
15
|
+
let i = 0;
|
|
16
|
+
let j = 0;
|
|
17
|
+
while (i < n && j < m) {
|
|
18
|
+
if (a[i] === b[j]) {
|
|
19
|
+
i++;
|
|
20
|
+
j++;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (lcs[i + 1][j] >= lcs[i][j + 1])
|
|
24
|
+
out.push(`- ${a[i++]}`);
|
|
25
|
+
else
|
|
26
|
+
out.push(`+ ${b[j++]}`);
|
|
27
|
+
}
|
|
28
|
+
while (i < n)
|
|
29
|
+
out.push(`- ${a[i++]}`);
|
|
30
|
+
while (j < m)
|
|
31
|
+
out.push(`+ ${b[j++]}`);
|
|
32
|
+
return out;
|
|
33
|
+
}
|
package/dist/dotenv.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `.env` as a document: read without a shell (no `$` expansion, no command
|
|
3
|
+
* substitution), edited in place (a key keeps its line, comments survive), and
|
|
4
|
+
* written in a form Node's `--env-file`, `dotenv` and `docker compose
|
|
5
|
+
* --env-file` all read the same way.
|
|
6
|
+
*
|
|
7
|
+
* Problems are reported by line number, never by value: the file holds secrets
|
|
8
|
+
* and the report ends up in terminals, CI logs and screenshots.
|
|
9
|
+
*/
|
|
10
|
+
export declare const ENV_KEY: RegExp;
|
|
11
|
+
export type EnvLine = {
|
|
12
|
+
kind: 'pair';
|
|
13
|
+
key: string;
|
|
14
|
+
value: string;
|
|
15
|
+
raw: string;
|
|
16
|
+
} | {
|
|
17
|
+
kind: 'other';
|
|
18
|
+
raw: string;
|
|
19
|
+
};
|
|
20
|
+
export interface EnvProblem {
|
|
21
|
+
line: number;
|
|
22
|
+
message: string;
|
|
23
|
+
}
|
|
24
|
+
export interface EnvDocument {
|
|
25
|
+
lines: EnvLine[];
|
|
26
|
+
problems: EnvProblem[];
|
|
27
|
+
}
|
|
28
|
+
export declare function parseEnv(text: string): EnvDocument;
|
|
29
|
+
export declare function envValues(document: EnvDocument): Map<string, string>;
|
|
30
|
+
export declare class UnwritableValueError extends Error {
|
|
31
|
+
readonly key: string;
|
|
32
|
+
constructor(key: string);
|
|
33
|
+
}
|
|
34
|
+
/** A value in the one form every reader agrees on, or {@link UnwritableValueError}. */
|
|
35
|
+
export declare function formatValue(value: string, key?: string): string;
|
|
36
|
+
export type EnvChange = {
|
|
37
|
+
kind: 'set';
|
|
38
|
+
key: string;
|
|
39
|
+
value: string;
|
|
40
|
+
reason: string;
|
|
41
|
+
} | {
|
|
42
|
+
kind: 'remove';
|
|
43
|
+
key: string;
|
|
44
|
+
reason: string;
|
|
45
|
+
};
|
|
46
|
+
/** Applies changes: a set key keeps its line, a new key is appended, a removed key's line goes. */
|
|
47
|
+
export declare function applyEnvChanges(document: EnvDocument, changes: EnvChange[]): string;
|