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
@@ -0,0 +1,151 @@
|
||||
/**
|
||||
* `toolctrl add permission <name>` — scaffold a new permission key plus
|
||||
* a frontend-mapper stub.
|
||||
*
|
||||
* The canonical permission list lives in
|
||||
* `packages/db/src/permissions-seed.server.ts` as a string-array
|
||||
* literal. We deliberately do NOT patch that file from the CLI — the
|
||||
* array is hand-curated and each entry has an associated `inRoles`
|
||||
* mapping a few lines below. Instead the generator writes a single
|
||||
* "fragment" file under
|
||||
* `packages/db/src/_generated-permissions/<name>.ts` that the caller
|
||||
* can paste into the canonical list, plus a frontend permission-mapper
|
||||
* stub under `apps/web/app/utils/permissions/_generated/`.
|
||||
*
|
||||
* Refs: docs/improvement-plan/08-mobile-cli-storybook-sdk/cli-scaffolding.md
|
||||
*/
|
||||
import { defineCommand } from "citty";
|
||||
import consola from "consola";
|
||||
import path from "node:path";
|
||||
import {
|
||||
createScaffoldResult,
|
||||
pascalCase,
|
||||
resolveRepoRoot,
|
||||
writeScaffoldFile,
|
||||
type ScaffoldResult,
|
||||
} from "../_lib/scaffold.js";
|
||||
|
||||
export type GeneratePermissionOptions = {
|
||||
/** Dotted permission key, e.g. "app.inventory.view". */
|
||||
name: string;
|
||||
repoRoot?: string;
|
||||
dryRun?: boolean;
|
||||
};
|
||||
|
||||
const PERMISSION_PATTERN = /^[a-z][a-z0-9]*(\.[a-z][a-z0-9_]*)+$/;
|
||||
|
||||
export async function generatePermission(options: GeneratePermissionOptions): Promise<ScaffoldResult> {
|
||||
const permission = options.name.trim();
|
||||
if (!permission) throw new Error("permission name is required.");
|
||||
if (!PERMISSION_PATTERN.test(permission)) {
|
||||
throw new Error(
|
||||
`Invalid permission key: "${permission}". Use dotted lowercase, e.g. "app.inventory.view".`,
|
||||
);
|
||||
}
|
||||
|
||||
const dryRun = options.dryRun ?? false;
|
||||
const repoRoot = resolveRepoRoot(options.repoRoot);
|
||||
const fileSlug = permission.replace(/\./g, "_");
|
||||
const constName = pascalCase(permission.replace(/\./g, " "));
|
||||
const isAdmin = permission.startsWith("admin.");
|
||||
|
||||
const result = createScaffoldResult(dryRun);
|
||||
|
||||
await writeScaffoldFile({
|
||||
targetPath: path.join(repoRoot, "packages/db/src/_generated-permissions", `${fileSlug}.ts`),
|
||||
contents: renderSeedFragment({ permission, constName, isAdmin }),
|
||||
dryRun,
|
||||
result,
|
||||
});
|
||||
|
||||
await writeScaffoldFile({
|
||||
targetPath: path.join(repoRoot, "apps/web/app/utils/permissions/_generated", `${fileSlug}.ts`),
|
||||
contents: renderMapperFragment({ permission, constName }),
|
||||
dryRun,
|
||||
result,
|
||||
});
|
||||
|
||||
result.notes.push(
|
||||
`Permission key: "${permission}"`,
|
||||
`Manual follow-up:`,
|
||||
` 1. Add "${permission}" to defaultPermissionSeeds in packages/db/src/permissions-seed.server.ts`,
|
||||
` 2. Add the role-mapping entry in the same file (rolePermissionMatrix)`,
|
||||
` 3. Wire the frontend check via the helper in apps/web/app/utils/helpers/PermissionsHelper.ts`,
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function renderSeedFragment({
|
||||
permission,
|
||||
constName,
|
||||
isAdmin,
|
||||
}: {
|
||||
permission: string;
|
||||
constName: string;
|
||||
isAdmin: boolean;
|
||||
}): string {
|
||||
return `/**
|
||||
* Permission seed fragment for "${permission}".
|
||||
* Generated by toolctrl-cli.
|
||||
*
|
||||
* Paste the constant into the \`defaultPermissionSeeds\` array in
|
||||
* \`packages/db/src/permissions-seed.server.ts\` and add a matching
|
||||
* entry to the \`rolePermissionMatrix\` further down in that file.
|
||||
*/
|
||||
export const ${constName}Permission = "${permission}" as const;
|
||||
|
||||
export const ${constName}PermissionType: "admin" | "app" = ${JSON.stringify(isAdmin ? "admin" : "app")};
|
||||
|
||||
export const ${constName}DefaultRoles: readonly string[] = [
|
||||
${isAdmin ? ' "SuperAdmin",' : ' "SuperUser",\n "Admin",'}
|
||||
];
|
||||
`;
|
||||
}
|
||||
|
||||
function renderMapperFragment({ permission, constName }: { permission: string; constName: string }): string {
|
||||
return `/**
|
||||
* Frontend permission-mapper stub for "${permission}".
|
||||
* Generated by toolctrl-cli.
|
||||
*
|
||||
* Wire this into your route loaders via the project's permission
|
||||
* helper (typically apps/web/app/utils/helpers/PermissionsHelper).
|
||||
*/
|
||||
export const ${constName}PermissionKey = "${permission}" as const;
|
||||
|
||||
export function require${constName}(allowedPermissions: ReadonlyArray<string>): boolean {
|
||||
return allowedPermissions.includes(${constName}PermissionKey);
|
||||
}
|
||||
`;
|
||||
}
|
||||
|
||||
export const addPermissionCommand = defineCommand({
|
||||
meta: {
|
||||
name: "permission",
|
||||
description: "Scaffold a permission key + frontend mapper stub.",
|
||||
},
|
||||
args: {
|
||||
name: {
|
||||
type: "positional",
|
||||
required: true,
|
||||
description: "Dotted permission key, e.g. \"app.inventory.view\".",
|
||||
},
|
||||
"dry-run": {
|
||||
type: "boolean",
|
||||
required: false,
|
||||
description: "Print the would-be writes without touching the filesystem.",
|
||||
},
|
||||
},
|
||||
async run({ args }) {
|
||||
const result = await generatePermission({ name: args.name, dryRun: args["dry-run"] === true });
|
||||
consola.success(
|
||||
`${result.dryRun ? "[dry-run] " : ""}Generated permission "${args.name}" — ${result.files.length} file(s).`,
|
||||
);
|
||||
for (const file of result.files) {
|
||||
consola.info(` ${result.dryRun ? "would write" : "wrote"} ${file.path}`);
|
||||
}
|
||||
for (const note of result.notes) {
|
||||
consola.info(note);
|
||||
}
|
||||
},
|
||||
});
|
||||
Reference in new issue
Block a user