config and plugin
This commit is contained in:
+117
-12
@@ -1,5 +1,14 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { db } from '../db/index.js';
|
||||
import { config, type ArtifactKind } from '../config.js';
|
||||
|
||||
export type { ArtifactKind };
|
||||
|
||||
export const ARTIFACT_KINDS: ReadonlyArray<ArtifactKind> = ['script', 'config', 'plugin', 'baseline'];
|
||||
|
||||
export function isArtifactKind(v: unknown): v is ArtifactKind {
|
||||
return typeof v === 'string' && (ARTIFACT_KINDS as readonly string[]).includes(v);
|
||||
}
|
||||
|
||||
export type ScopeType = 'game' | 'map';
|
||||
|
||||
@@ -10,6 +19,7 @@ export interface ScriptRow {
|
||||
scope_type: ScopeType | null;
|
||||
scope_value: string | null;
|
||||
current_version_id: number | null;
|
||||
kind: ArtifactKind;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
@@ -24,6 +34,7 @@ export interface ScriptVersionRow {
|
||||
content: Buffer;
|
||||
notes: string | null;
|
||||
state: ScriptVersionState;
|
||||
kind: ArtifactKind;
|
||||
uploaded_at: string;
|
||||
}
|
||||
|
||||
@@ -51,6 +62,10 @@ export class ScriptError extends Error {
|
||||
| 'in-use-beta'
|
||||
| 'has-scope'
|
||||
| 'has-active-beta'
|
||||
| 'invalid-kind'
|
||||
| 'baseline-requires-scope'
|
||||
| 'baseline-exists'
|
||||
| 'empty-overlay'
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
@@ -68,10 +83,51 @@ export function getScriptByName(name: string): ScriptRow | undefined {
|
||||
return db().prepare('SELECT * FROM scripts WHERE name = ?').get(name) as ScriptRow | undefined;
|
||||
}
|
||||
|
||||
export function listScripts(): ScriptRow[] {
|
||||
export function listScripts(kind?: ArtifactKind): ScriptRow[] {
|
||||
if (kind) {
|
||||
return db().prepare('SELECT * FROM scripts WHERE kind = ? ORDER BY name').all(kind) as ScriptRow[];
|
||||
}
|
||||
return db().prepare('SELECT * FROM scripts ORDER BY name').all() as ScriptRow[];
|
||||
}
|
||||
|
||||
// Look up the current baseline for a given scope. Baselines have exactly one
|
||||
// current_version_id after their first promote; before that they are still
|
||||
// gated through the beta pipeline. `includeUntested` returns the baseline row
|
||||
// even without a promoted version (used at beta-open time when the baseline is
|
||||
// being introduced in the same beta as the overlays).
|
||||
export function getBaselineForScope(scopeType: ScopeType, scopeValue: string): ScriptRow | undefined {
|
||||
return db()
|
||||
.prepare(
|
||||
`SELECT * FROM scripts
|
||||
WHERE kind = 'baseline' AND scope_type = ? AND scope_value = ?
|
||||
LIMIT 1`
|
||||
)
|
||||
.get(scopeType, scopeValue) as ScriptRow | undefined;
|
||||
}
|
||||
|
||||
// All promoted config overlays for a scope. `exceptScriptIds` lets callers
|
||||
// skip overlays whose new version is bundled in a beta — avoids applying the
|
||||
// prior version twice when composing.
|
||||
export function listPromotedOverlaysForScope(
|
||||
scopeType: ScopeType,
|
||||
scopeValue: string,
|
||||
exceptScriptIds: number[] = []
|
||||
): ScriptRow[] {
|
||||
const rows = db()
|
||||
.prepare(
|
||||
`SELECT * FROM scripts
|
||||
WHERE kind = 'config'
|
||||
AND scope_type = ?
|
||||
AND scope_value = ?
|
||||
AND current_version_id IS NOT NULL
|
||||
ORDER BY id`
|
||||
)
|
||||
.all(scopeType, scopeValue) as ScriptRow[];
|
||||
if (exceptScriptIds.length === 0) return rows;
|
||||
const skip = new Set(exceptScriptIds);
|
||||
return rows.filter((r) => !skip.has(r.id));
|
||||
}
|
||||
|
||||
export function listVersions(scriptId: number): ScriptVersionRow[] {
|
||||
return db()
|
||||
.prepare('SELECT * FROM script_versions WHERE script_id = ? ORDER BY version DESC')
|
||||
@@ -94,18 +150,57 @@ export function getLatestVersion(scriptId: number): ScriptVersionRow | undefined
|
||||
|
||||
export interface CreateScriptInput {
|
||||
name: string;
|
||||
targetRelPath: string;
|
||||
/** Optional: defaults to the upload filename for script/plugin, and to
|
||||
* the configured config target file for config/baseline. */
|
||||
targetRelPath?: string;
|
||||
scopeType?: ScopeType | null;
|
||||
scopeValue?: string | null;
|
||||
content: Buffer;
|
||||
notes?: string;
|
||||
/** Defaults to 'script' for back-compat with existing callers. */
|
||||
kind?: ArtifactKind;
|
||||
}
|
||||
|
||||
// Insert script + version 1 in one transaction. Scope may be set at creation
|
||||
// because the script has no current_version_id yet (nothing to gate).
|
||||
export function createScript(input: CreateScriptInput): { script: ScriptRow; version: ScriptVersionRow } {
|
||||
if (!input.name.trim()) throw new ScriptError('script name is required', 'name-empty');
|
||||
if (!input.targetRelPath.trim()) throw new ScriptError('target rel path is required', 'path-empty');
|
||||
const kind: ArtifactKind = input.kind ?? 'script';
|
||||
if (!isArtifactKind(kind)) throw new ScriptError(`invalid kind: ${input.kind}`, 'invalid-kind');
|
||||
|
||||
// Target file defaulting per kind. Configs and baselines all compose into
|
||||
// the same on-disk cfg file, so the operator never picks it.
|
||||
let targetRelPath = input.targetRelPath?.trim() ?? '';
|
||||
if (!targetRelPath && (kind === 'config' || kind === 'baseline')) {
|
||||
targetRelPath = config.configTargetFile;
|
||||
}
|
||||
if (!targetRelPath) throw new ScriptError('target rel path is required', 'path-empty');
|
||||
|
||||
// Kind-specific validation.
|
||||
if (kind === 'baseline') {
|
||||
if (!input.scopeType || !input.scopeValue) {
|
||||
throw new ScriptError('baselines require a scope (game or map)', 'baseline-requires-scope');
|
||||
}
|
||||
if (getBaselineForScope(input.scopeType, input.scopeValue)) {
|
||||
throw new ScriptError(
|
||||
`a baseline already exists for ${input.scopeType}=${input.scopeValue}`,
|
||||
'baseline-exists'
|
||||
);
|
||||
}
|
||||
}
|
||||
if (kind === 'config') {
|
||||
if (!input.scopeType || !input.scopeValue) {
|
||||
throw new ScriptError('config overlays require a scope', 'invalid-scope');
|
||||
}
|
||||
// Reject empty or comment-only overlays — nothing to apply.
|
||||
const text = input.content.toString('utf8');
|
||||
if (!/^\s*set\s+\S+\s+/m.test(text)) {
|
||||
throw new ScriptError(
|
||||
'config overlay must contain at least one `set KEY VALUE` line',
|
||||
'empty-overlay'
|
||||
);
|
||||
}
|
||||
}
|
||||
validateScope(input.scopeType ?? null, input.scopeValue ?? null);
|
||||
if (getScriptByName(input.name)) throw new ScriptError(`script "${input.name}" already exists`, 'name-taken');
|
||||
|
||||
@@ -113,20 +208,20 @@ export function createScript(input: CreateScriptInput): { script: ScriptRow; ver
|
||||
const size = input.content.byteLength;
|
||||
|
||||
const insertScript = db().prepare(
|
||||
`INSERT INTO scripts (name, target_rel_path, scope_type, scope_value)
|
||||
VALUES (?, ?, ?, ?)`
|
||||
`INSERT INTO scripts (name, target_rel_path, scope_type, scope_value, kind)
|
||||
VALUES (?, ?, ?, ?, ?)`
|
||||
);
|
||||
const insertVersion = db().prepare(
|
||||
`INSERT INTO script_versions (script_id, version, sha256, size, content, notes)
|
||||
VALUES (?, 1, ?, ?, ?, ?)`
|
||||
`INSERT INTO script_versions (script_id, version, sha256, size, content, notes, kind)
|
||||
VALUES (?, 1, ?, ?, ?, ?, ?)`
|
||||
);
|
||||
|
||||
let scriptId!: number;
|
||||
let versionId!: number;
|
||||
db().transaction(() => {
|
||||
const s = insertScript.run(input.name, input.targetRelPath, input.scopeType ?? null, input.scopeValue ?? null);
|
||||
const s = insertScript.run(input.name, targetRelPath, input.scopeType ?? null, input.scopeValue ?? null, kind);
|
||||
scriptId = Number(s.lastInsertRowid);
|
||||
const v = insertVersion.run(scriptId, sha, size, input.content, input.notes ?? null);
|
||||
const v = insertVersion.run(scriptId, sha, size, input.content, input.notes ?? null, kind);
|
||||
versionId = Number(v.lastInsertRowid);
|
||||
})();
|
||||
|
||||
@@ -140,16 +235,26 @@ export function uploadNewVersion(scriptId: number, content: Buffer, notes?: stri
|
||||
const script = getScript(scriptId);
|
||||
if (!script) throw new ScriptError(`no such script (id=${scriptId})`, 'no-such-script');
|
||||
|
||||
if (script.kind === 'config') {
|
||||
const text = content.toString('utf8');
|
||||
if (!/^\s*set\s+\S+\s+/m.test(text)) {
|
||||
throw new ScriptError(
|
||||
'config overlay must contain at least one `set KEY VALUE` line',
|
||||
'empty-overlay'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const latest = getLatestVersion(scriptId);
|
||||
const nextVersion = (latest?.version ?? 0) + 1;
|
||||
const sha = sha256(content);
|
||||
|
||||
const res = db()
|
||||
.prepare(
|
||||
`INSERT INTO script_versions (script_id, version, sha256, size, content, notes)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`
|
||||
`INSERT INTO script_versions (script_id, version, sha256, size, content, notes, kind)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
||||
)
|
||||
.run(scriptId, nextVersion, sha, content.byteLength, content, notes ?? null);
|
||||
.run(scriptId, nextVersion, sha, content.byteLength, content, notes ?? null, script.kind);
|
||||
return getVersion(Number(res.lastInsertRowid))!;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user