Add Prettier and ESLint tooling (Refs #77)
This commit is contained in:
+137
-35
@@ -5,7 +5,12 @@ import { configFileForGame, type ArtifactKind } from '../config.js';
|
||||
|
||||
export type { ArtifactKind };
|
||||
|
||||
export const ARTIFACT_KINDS: ReadonlyArray<ArtifactKind> = ['script', 'config', 'plugin', 'baseline'];
|
||||
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);
|
||||
@@ -78,8 +83,12 @@ function sha256(buf: Buffer): string {
|
||||
|
||||
function normalizeReleaseVersion(value: string | null | undefined): string | null {
|
||||
if (value == null) return null;
|
||||
// eslint-disable-next-line no-control-regex -- Reject control characters in release versions.
|
||||
if (typeof value !== 'string' || value.length > 80 || /[\u0000-\u001f\u007f]/.test(value)) {
|
||||
throw new ScriptError('release version must be a single line of at most 80 characters', 'invalid-release-version');
|
||||
throw new ScriptError(
|
||||
'release version must be a single line of at most 80 characters',
|
||||
'invalid-release-version'
|
||||
);
|
||||
}
|
||||
return value.trim() || null;
|
||||
}
|
||||
@@ -94,7 +103,9 @@ export function getScriptByName(name: string): ScriptRow | undefined {
|
||||
|
||||
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 WHERE kind = ? ORDER BY name')
|
||||
.all(kind) as ScriptRow[];
|
||||
}
|
||||
return db().prepare('SELECT * FROM scripts ORDER BY name').all() as ScriptRow[];
|
||||
}
|
||||
@@ -104,7 +115,10 @@ export function listScripts(kind?: ArtifactKind): ScriptRow[] {
|
||||
// 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 {
|
||||
export function getBaselineForScope(
|
||||
scopeType: ScopeType,
|
||||
scopeValue: string
|
||||
): ScriptRow | undefined {
|
||||
return db()
|
||||
.prepare(
|
||||
`SELECT * FROM scripts
|
||||
@@ -137,40 +151,49 @@ export function listPromotedOverlaysForScope(
|
||||
return rows.filter((r) => !skip.has(r.id));
|
||||
}
|
||||
|
||||
export const VERSION_COLUMNS = 'id, script_id, version, release_version, sha256, size, notes, state, kind, uploaded_at';
|
||||
export const VERSION_COLUMNS =
|
||||
'id, script_id, version, release_version, sha256, size, notes, state, kind, uploaded_at';
|
||||
|
||||
export function getVersionContent(id: number): Buffer {
|
||||
const row = db().prepare(`SELECT ${blobContent('content_hash')} AS content FROM script_versions WHERE id = ?`).get(id) as { content: Buffer } | undefined;
|
||||
if (!row?.content) throw new ScriptError(`version content is unavailable (id=${id})`, 'no-such-version');
|
||||
const row = db()
|
||||
.prepare(
|
||||
`SELECT ${blobContent('content_hash')} AS content FROM script_versions WHERE id = ?`
|
||||
)
|
||||
.get(id) as { content: Buffer } | undefined;
|
||||
if (!row?.content)
|
||||
throw new ScriptError(`version content is unavailable (id=${id})`, 'no-such-version');
|
||||
return row.content;
|
||||
}
|
||||
|
||||
export function listVersions(scriptId: number): ScriptVersionRow[] {
|
||||
return db()
|
||||
.prepare(`SELECT ${VERSION_COLUMNS} FROM script_versions WHERE script_id = ? ORDER BY version DESC`)
|
||||
.prepare(
|
||||
`SELECT ${VERSION_COLUMNS} FROM script_versions WHERE script_id = ? ORDER BY version DESC`
|
||||
)
|
||||
.all(scriptId) as ScriptVersionRow[];
|
||||
}
|
||||
|
||||
export function getVersion(id: number): ScriptVersionRow | undefined {
|
||||
return db().prepare(`SELECT ${VERSION_COLUMNS} FROM script_versions WHERE id = ?`).get(id) as
|
||||
| ScriptVersionRow
|
||||
| undefined;
|
||||
ScriptVersionRow | undefined;
|
||||
}
|
||||
|
||||
export type ScriptVersionMetadata = Pick<ScriptVersionRow, 'id' | 'version' | 'sha256' | 'size'>;
|
||||
|
||||
/** Planning a launch never needs to load potentially large script or plugin bytes. */
|
||||
export function getVersionMetadata(id: number): ScriptVersionMetadata | undefined {
|
||||
return db().prepare('SELECT id, version, sha256, size FROM script_versions WHERE id = ?').get(id) as
|
||||
| ScriptVersionMetadata
|
||||
| undefined;
|
||||
return db()
|
||||
.prepare('SELECT id, version, sha256, size FROM script_versions WHERE id = ?')
|
||||
.get(id) as ScriptVersionMetadata | undefined;
|
||||
}
|
||||
|
||||
// Latest version is 'max(version)' — used everywhere we want the freshest
|
||||
// upload, whether or not it has been promoted (current_version_id).
|
||||
export function getLatestVersion(scriptId: number): ScriptVersionRow | undefined {
|
||||
return db()
|
||||
.prepare(`SELECT ${VERSION_COLUMNS} FROM script_versions WHERE script_id = ? ORDER BY version DESC LIMIT 1`)
|
||||
.prepare(
|
||||
`SELECT ${VERSION_COLUMNS} FROM script_versions WHERE script_id = ? ORDER BY version DESC LIMIT 1`
|
||||
)
|
||||
.get(scriptId) as ScriptVersionRow | undefined;
|
||||
}
|
||||
|
||||
@@ -191,7 +214,10 @@ export interface CreateScriptInput {
|
||||
|
||||
// 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 } {
|
||||
export function createScript(input: CreateScriptInput): {
|
||||
script: ScriptRow;
|
||||
version: ScriptVersionRow;
|
||||
} {
|
||||
if (!input.name.trim()) throw new ScriptError('script name is required', 'name-empty');
|
||||
const kind: ArtifactKind = input.kind ?? 'script';
|
||||
if (!isArtifactKind(kind)) throw new ScriptError(`invalid kind: ${input.kind}`, 'invalid-kind');
|
||||
@@ -200,15 +226,23 @@ export function createScript(input: CreateScriptInput): { script: ScriptRow; ver
|
||||
// the same on-disk cfg file, so the operator never picks it.
|
||||
let targetRelPath = input.targetRelPath?.trim() ?? '';
|
||||
if (!targetRelPath && (kind === 'config' || kind === 'baseline')) {
|
||||
targetRelPath = configFileForGame(input.scopeType === 'game' ? input.scopeValue ?? null
|
||||
: input.scopeValue?.startsWith('zm_') ? 't6' : 't5');
|
||||
targetRelPath = configFileForGame(
|
||||
input.scopeType === 'game'
|
||||
? (input.scopeValue ?? null)
|
||||
: input.scopeValue?.startsWith('zm_')
|
||||
? 't6'
|
||||
: 't5'
|
||||
);
|
||||
}
|
||||
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');
|
||||
throw new ScriptError(
|
||||
'baselines require a scope (game or map)',
|
||||
'baseline-requires-scope'
|
||||
);
|
||||
}
|
||||
if (getBaselineForScope(input.scopeType, input.scopeValue)) {
|
||||
throw new ScriptError(
|
||||
@@ -231,7 +265,8 @@ export function createScript(input: CreateScriptInput): { script: ScriptRow; ver
|
||||
}
|
||||
}
|
||||
validateScope(input.scopeType ?? null, input.scopeValue ?? null);
|
||||
if (getScriptByName(input.name)) throw new ScriptError(`script "${input.name}" already exists`, 'name-taken');
|
||||
if (getScriptByName(input.name))
|
||||
throw new ScriptError(`script "${input.name}" already exists`, 'name-taken');
|
||||
|
||||
const sha = sha256(input.content);
|
||||
const releaseVersion = normalizeReleaseVersion(input.releaseVersion);
|
||||
@@ -249,9 +284,23 @@ export function createScript(input: CreateScriptInput): { script: ScriptRow; ver
|
||||
let scriptId!: number;
|
||||
let versionId!: number;
|
||||
db().transaction(() => {
|
||||
const s = insertScript.run(input.name, targetRelPath, input.scopeType ?? null, input.scopeValue ?? null, kind);
|
||||
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, putFileBlob(input.content), input.notes ?? null, kind, releaseVersion);
|
||||
const v = insertVersion.run(
|
||||
scriptId,
|
||||
sha,
|
||||
size,
|
||||
putFileBlob(input.content),
|
||||
input.notes ?? null,
|
||||
kind,
|
||||
releaseVersion
|
||||
);
|
||||
versionId = Number(v.lastInsertRowid);
|
||||
})();
|
||||
|
||||
@@ -261,7 +310,12 @@ export function createScript(input: CreateScriptInput): { script: ScriptRow; ver
|
||||
};
|
||||
}
|
||||
|
||||
export function uploadNewVersion(scriptId: number, content: Buffer, notes?: string, releaseVersion?: string): ScriptVersionRow {
|
||||
export function uploadNewVersion(
|
||||
scriptId: number,
|
||||
content: Buffer,
|
||||
notes?: string,
|
||||
releaseVersion?: string
|
||||
): ScriptVersionRow {
|
||||
const script = getScript(scriptId);
|
||||
if (!script) throw new ScriptError(`no such script (id=${scriptId})`, 'no-such-script');
|
||||
|
||||
@@ -285,7 +339,16 @@ export function uploadNewVersion(scriptId: number, content: Buffer, notes?: stri
|
||||
`INSERT INTO script_versions (script_id, version, sha256, size, content_hash, notes, kind, release_version)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
|
||||
)
|
||||
.run(scriptId, nextVersion, sha, content.byteLength, putFileBlob(content), notes ?? null, script.kind, release);
|
||||
.run(
|
||||
scriptId,
|
||||
nextVersion,
|
||||
sha,
|
||||
content.byteLength,
|
||||
putFileBlob(content),
|
||||
notes ?? null,
|
||||
script.kind,
|
||||
release
|
||||
);
|
||||
return getVersion(Number(res.lastInsertRowid))!;
|
||||
}
|
||||
|
||||
@@ -293,7 +356,10 @@ function validateScope(scopeType: ScopeType | null, scopeValue: string | null):
|
||||
if (scopeType === null && scopeValue === null) return;
|
||||
if (scopeType === 'game' || scopeType === 'map') {
|
||||
if (!scopeValue || !scopeValue.trim()) {
|
||||
throw new ScriptError(`scope_value required for scope_type=${scopeType}`, 'invalid-scope');
|
||||
throw new ScriptError(
|
||||
`scope_value required for scope_type=${scopeType}`,
|
||||
'invalid-scope'
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -352,7 +418,11 @@ export function deleteVersion(versionId: number): void {
|
||||
WHERE bs.script_version_id = ? AND b.state IN ('running','ready-promote') LIMIT 1`
|
||||
)
|
||||
.get(versionId) as { id: number } | undefined;
|
||||
if (beta) throw new ScriptError(`beta ${beta.id} is still running against this version`, 'in-use-beta');
|
||||
if (beta)
|
||||
throw new ScriptError(
|
||||
`beta ${beta.id} is still running against this version`,
|
||||
'in-use-beta'
|
||||
);
|
||||
|
||||
db().prepare('DELETE FROM script_versions WHERE id = ?').run(versionId);
|
||||
}
|
||||
@@ -370,7 +440,11 @@ export function deleteScript(scriptId: number): void {
|
||||
WHERE bs.script_id = ? AND b.state IN ('running','ready-promote') LIMIT 1`
|
||||
)
|
||||
.get(scriptId) as { id: number } | undefined;
|
||||
if (beta) throw new ScriptError(`beta ${beta.id} is still running against this script`, 'has-active-beta');
|
||||
if (beta)
|
||||
throw new ScriptError(
|
||||
`beta ${beta.id} is still running against this script`,
|
||||
'has-active-beta'
|
||||
);
|
||||
|
||||
// Every FK that references scripts(id) or script_versions(id) with NO
|
||||
// ACTION (the default) will abort the delete unless we null it first.
|
||||
@@ -386,14 +460,37 @@ export function deleteScript(scriptId: number): void {
|
||||
const handle = db();
|
||||
const tx = handle.transaction((id: number) => {
|
||||
const inMyVersions = 'IN (SELECT id FROM script_versions WHERE script_id = ?)';
|
||||
const inMyBetas = 'IN (SELECT id FROM betas WHERE script_id = ? UNION SELECT beta_id FROM beta_scripts WHERE script_id = ?)';
|
||||
const inMyBetas =
|
||||
'IN (SELECT id FROM betas WHERE script_id = ? UNION SELECT beta_id FROM beta_scripts WHERE script_id = ?)';
|
||||
|
||||
handle.prepare(`UPDATE beta_servers SET prior_version_id = NULL WHERE prior_version_id ${inMyVersions}`).run(id);
|
||||
handle.prepare(`UPDATE beta_server_scripts SET prior_version_id = NULL WHERE prior_version_id ${inMyVersions}`).run(id);
|
||||
handle.prepare(`UPDATE deployment_scripts SET prior_version_id = NULL WHERE prior_version_id ${inMyVersions}`).run(id);
|
||||
handle.prepare(`UPDATE deployments SET prior_version_id = NULL WHERE prior_version_id ${inMyVersions}`).run(id);
|
||||
handle.prepare(`UPDATE deployments SET script_version_id = NULL WHERE script_version_id ${inMyVersions}`).run(id);
|
||||
handle.prepare(`UPDATE deployments SET beta_id = NULL WHERE beta_id ${inMyBetas}`).run(id, id);
|
||||
handle
|
||||
.prepare(
|
||||
`UPDATE beta_servers SET prior_version_id = NULL WHERE prior_version_id ${inMyVersions}`
|
||||
)
|
||||
.run(id);
|
||||
handle
|
||||
.prepare(
|
||||
`UPDATE beta_server_scripts SET prior_version_id = NULL WHERE prior_version_id ${inMyVersions}`
|
||||
)
|
||||
.run(id);
|
||||
handle
|
||||
.prepare(
|
||||
`UPDATE deployment_scripts SET prior_version_id = NULL WHERE prior_version_id ${inMyVersions}`
|
||||
)
|
||||
.run(id);
|
||||
handle
|
||||
.prepare(
|
||||
`UPDATE deployments SET prior_version_id = NULL WHERE prior_version_id ${inMyVersions}`
|
||||
)
|
||||
.run(id);
|
||||
handle
|
||||
.prepare(
|
||||
`UPDATE deployments SET script_version_id = NULL WHERE script_version_id ${inMyVersions}`
|
||||
)
|
||||
.run(id);
|
||||
handle
|
||||
.prepare(`UPDATE deployments SET beta_id = NULL WHERE beta_id ${inMyBetas}`)
|
||||
.run(id, id);
|
||||
handle.prepare('UPDATE deployments SET script_id = NULL WHERE script_id = ?').run(id);
|
||||
handle.prepare('DELETE FROM scripts WHERE id = ?').run(id);
|
||||
});
|
||||
@@ -402,7 +499,10 @@ export function deleteScript(scriptId: number): void {
|
||||
|
||||
// Servers that match a script's scope. Empty for unscoped scripts — those are
|
||||
// only ever pushed to explicitly chosen beta targets.
|
||||
export function serversMatchingScope(scopeType: ScopeType | null, scopeValue: string | null): string[] {
|
||||
export function serversMatchingScope(
|
||||
scopeType: ScopeType | null,
|
||||
scopeValue: string | null
|
||||
): string[] {
|
||||
if (scopeType === null || scopeValue === null) return [];
|
||||
const rows = db()
|
||||
.prepare(`SELECT name FROM servers WHERE ${scopeType} = ? ORDER BY name`)
|
||||
@@ -415,7 +515,9 @@ export function serversMatchingScope(scopeType: ScopeType | null, scopeValue: st
|
||||
// server.
|
||||
export function listDistinctGames(): string[] {
|
||||
const rows = db()
|
||||
.prepare(`SELECT DISTINCT game FROM servers WHERE game IS NOT NULL AND game <> '' ORDER BY game`)
|
||||
.prepare(
|
||||
`SELECT DISTINCT game FROM servers WHERE game IS NOT NULL AND game <> '' ORDER BY game`
|
||||
)
|
||||
.all() as Array<{ game: string }>;
|
||||
return rows.map((r) => r.game);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user