Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
+101
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* `feature-flags set-rules` — replace a flag's targeting-rule tree
|
||||
* from a JSON file (or `--rules='-'` for stdin). The replacement is
|
||||
* validated by re-evaluating against a probe context before being
|
||||
* persisted; structurally-invalid trees abort with a non-zero exit.
|
||||
*/
|
||||
import { defineCommand } from "citty";
|
||||
import consola from "consola";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { evaluateRule, type TargetingRule } from "@toolctrl/feature-flags";
|
||||
import { broadcastFlagInvalidation } from "./invalidate.js";
|
||||
|
||||
async function applyRules(name: string, rulesJson: string): Promise<void> {
|
||||
let parsed: TargetingRule | null;
|
||||
try {
|
||||
const value = JSON.parse(rulesJson);
|
||||
parsed = value === null ? null : (value as TargetingRule);
|
||||
} catch (err: unknown) {
|
||||
consola.error("Failed to parse rules JSON:", err);
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
if (parsed) {
|
||||
try {
|
||||
evaluateRule(parsed, { flagName: name, tenantId: "probe", userId: "probe" });
|
||||
} catch (err: unknown) {
|
||||
consola.error("Rule tree did not evaluate cleanly:", err);
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const prisma = getDbClient();
|
||||
const existing = await prisma.featureFlag.findFirst({ where: { name } });
|
||||
if (!existing) {
|
||||
consola.error(`Flag "${name}" not found.`);
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
await prisma.$transaction([
|
||||
prisma.featureFlag.update({
|
||||
where: { id: existing.id },
|
||||
data: { rules: (parsed ?? null) as unknown as never },
|
||||
}),
|
||||
prisma.featureFlagAudit.create({
|
||||
data: {
|
||||
flagId: existing.id,
|
||||
actorUserId: null,
|
||||
action: "update",
|
||||
oldValue: { rules: (existing.rules ?? null) as unknown as never },
|
||||
newValue: { rules: (parsed ?? null) as unknown as never },
|
||||
},
|
||||
}),
|
||||
]);
|
||||
await broadcastFlagInvalidation(name);
|
||||
consola.success(`Rules for "${name}" updated.`);
|
||||
}
|
||||
|
||||
export const featureFlagsSetRulesCommand = defineCommand({
|
||||
meta: {
|
||||
name: "set-rules",
|
||||
description: "Replace a flag's targeting-rule tree from a JSON file (or '-' for stdin).",
|
||||
},
|
||||
args: {
|
||||
name: { type: "positional", required: true, description: "Flag name." },
|
||||
file: {
|
||||
type: "string",
|
||||
description: "Path to JSON file containing the rule tree, or '-' for stdin. Use empty file to clear rules (null).",
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
async run({ args }) {
|
||||
const file = String(args.file);
|
||||
let raw: string;
|
||||
if (file === "-") {
|
||||
raw = readStdin();
|
||||
} else {
|
||||
raw = readFileSync(file, "utf-8");
|
||||
}
|
||||
raw = raw.trim() || "null";
|
||||
await applyRules(String(args.name), raw);
|
||||
},
|
||||
});
|
||||
|
||||
function readStdin(): string {
|
||||
// Synchronous read of stdin — citty CLI commands are short-lived so
|
||||
// blocking the event loop on a small input is fine.
|
||||
const chunks: Buffer[] = [];
|
||||
let chunk: Buffer | null;
|
||||
// Node's read returns null if no data available; for piped input the
|
||||
// descriptor is opened in blocking mode for the duration of the read.
|
||||
const fs = require("node:fs");
|
||||
try {
|
||||
const data = fs.readFileSync(0, "utf-8");
|
||||
return data;
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
void chunks;
|
||||
void chunk;
|
||||
}
|
||||
Reference in new issue
Block a user