Compare commits

...

2 Commits

Author SHA1 Message Date
b086825edb feat(prdy): migrate @mosaic/prdy from v0 to v1
All checks were successful
ci/woodpecker/pr/ci Pipeline was successful
ci/woodpecker/push/ci Pipeline was successful
Port full PRD wizard implementation: types, YAML persistence (js-yaml),
built-in templates (software/feature/spike), @clack/prompts interactive
wizard, and Commander.js CLI (init/list/show). Adapted for v1 conventions:
ESM .js imports, NodeNext resolution, noUncheckedIndexedAccess-safe lookups,
and consistent-type-imports lint rule.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-14 19:40:10 -05:00
cd29fc8708 feat(quality-rails): migrate @mosaic/quality-rails from v0 to v1 (#100)
Some checks failed
ci/woodpecker/push/ci Pipeline failed
Co-authored-by: Jason Woltje <jason@diversecanvas.com>
Co-committed-by: Jason Woltje <jason@diversecanvas.com>
2026-03-15 00:23:56 +00:00
15 changed files with 1270 additions and 4 deletions

View File

@@ -15,7 +15,15 @@
"typecheck": "tsc --noEmit",
"test": "vitest run --passWithNoTests"
},
"dependencies": {
"@clack/prompts": "^0.9.0",
"commander": "^12.0.0",
"js-yaml": "^4.1.0",
"zod": "^3.22.0"
},
"devDependencies": {
"@types/js-yaml": "^4.0.9",
"@types/node": "^22.0.0",
"typescript": "^5.8.0",
"vitest": "^2.0.0"
}

100
packages/prdy/src/cli.ts Normal file
View File

@@ -0,0 +1,100 @@
import { Command } from 'commander';
import { createPrd, listPrds, loadPrd } from './prd.js';
import { runPrdWizard } from './wizard.js';
interface InitCommandOptions {
readonly name: string;
readonly project: string;
readonly template?: 'software' | 'feature' | 'spike';
}
interface ListCommandOptions {
readonly project: string;
}
interface ShowCommandOptions {
readonly project: string;
readonly id?: string;
}
export function buildPrdyCli(): Command {
const program = new Command();
program.name('mosaic').description('Mosaic CLI').exitOverride();
const prdy = program.command('prdy').description('PRD wizard commands');
prdy
.command('init')
.description('Create a PRD document')
.requiredOption('--name <name>', 'PRD name')
.requiredOption('--project <path>', 'Project path')
.option('--template <template>', 'Template (software|feature|spike)')
.action(async (options: InitCommandOptions) => {
const doc = process.stdout.isTTY
? await runPrdWizard({
name: options.name,
projectPath: options.project,
template: options.template,
interactive: true,
})
: await createPrd({
name: options.name,
projectPath: options.project,
template: options.template,
interactive: false,
});
console.log(
JSON.stringify(
{
ok: true,
id: doc.id,
title: doc.title,
status: doc.status,
projectPath: doc.projectPath,
},
null,
2,
),
);
});
prdy
.command('list')
.description('List PRD documents for a project')
.requiredOption('--project <path>', 'Project path')
.action(async (options: ListCommandOptions) => {
const docs = await listPrds(options.project);
console.log(JSON.stringify(docs, null, 2));
});
prdy
.command('show')
.description('Show a PRD document')
.requiredOption('--project <path>', 'Project path')
.option('--id <id>', 'PRD document id')
.action(async (options: ShowCommandOptions) => {
if (options.id !== undefined) {
const docs = await listPrds(options.project);
const match = docs.find((doc) => doc.id === options.id);
if (match === undefined) {
throw new Error(`PRD id not found: ${options.id}`);
}
console.log(JSON.stringify(match, null, 2));
return;
}
const doc = await loadPrd(options.project);
console.log(JSON.stringify(doc, null, 2));
});
return program;
}
export async function runPrdyCli(argv: readonly string[] = process.argv): Promise<void> {
const program = buildPrdyCli();
await program.parseAsync(argv);
}

View File

@@ -1 +1,12 @@
export const VERSION = '0.0.0';
export { createPrd, loadPrd, savePrd, listPrds } from './prd.js';
export { runPrdWizard } from './wizard.js';
export { buildPrdyCli, runPrdyCli } from './cli.js';
export { BUILTIN_PRD_TEMPLATES, resolveTemplate } from './templates.js';
export type {
PrdStatus,
PrdTemplate,
PrdTemplateSection,
PrdSection,
PrdDocument,
CreatePrdOptions,
} from './types.js';

199
packages/prdy/src/prd.ts Normal file
View File

@@ -0,0 +1,199 @@
import { type Dirent, promises as fs } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import yaml from 'js-yaml';
import { z } from 'zod';
import { resolveTemplate } from './templates.js';
import type { CreatePrdOptions, PrdDocument } from './types.js';
const PRD_DIRECTORY = path.join('docs', 'prdy');
const PRD_FILE_EXTENSIONS = new Set(['.yaml', '.yml']);
const prdSectionSchema = z.object({
id: z.string().min(1),
title: z.string().min(1),
fields: z.record(z.string(), z.string()),
});
const prdDocumentSchema = z.object({
id: z.string().min(1),
title: z.string().min(1),
status: z.enum(['draft', 'review', 'approved', 'archived']),
projectPath: z.string().min(1),
template: z.string().min(1),
sections: z.array(prdSectionSchema),
createdAt: z.string().datetime(),
updatedAt: z.string().datetime(),
});
function expandHome(projectPath: string): string {
if (!projectPath.startsWith('~')) {
return projectPath;
}
if (projectPath === '~') {
return os.homedir();
}
if (projectPath.startsWith('~/')) {
return path.join(os.homedir(), projectPath.slice(2));
}
return projectPath;
}
function resolveProjectPath(projectPath: string): string {
return path.resolve(expandHome(projectPath));
}
function toSlug(value: string): string {
return value
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
.replace(/-{2,}/g, '-');
}
function buildTimestamp(date: Date): { datePart: string; timePart: string } {
const iso = date.toISOString();
return {
datePart: iso.slice(0, 10).replace(/-/g, ''),
timePart: iso.slice(11, 19).replace(/:/g, ''),
};
}
function buildPrdId(name: string): string {
const slug = toSlug(name);
const { datePart, timePart } = buildTimestamp(new Date());
return `${slug || 'prd'}-${datePart}-${timePart}`;
}
function prdDirectory(projectPath: string): string {
return path.join(projectPath, PRD_DIRECTORY);
}
function prdFilePath(projectPath: string, id: string): string {
return path.join(prdDirectory(projectPath), `${id}.yaml`);
}
function isNodeErrorWithCode(error: unknown, code: string): boolean {
return (
typeof error === 'object' &&
error !== null &&
'code' in error &&
(error as { code?: string }).code === code
);
}
async function writeFileAtomic(filePath: string, content: string): Promise<void> {
const directory = path.dirname(filePath);
await fs.mkdir(directory, { recursive: true });
const tempPath = path.join(
directory,
`.${path.basename(filePath)}.tmp-${process.pid}-${Date.now()}-${Math.random()
.toString(16)
.slice(2)}`,
);
await fs.writeFile(tempPath, content, 'utf8');
await fs.rename(tempPath, filePath);
}
export async function createPrd(options: CreatePrdOptions): Promise<PrdDocument> {
const resolvedProjectPath = resolveProjectPath(options.projectPath);
const template = resolveTemplate(options.template);
const now = new Date().toISOString();
const document: PrdDocument = {
id: buildPrdId(options.name),
title: options.name.trim(),
status: 'draft',
projectPath: resolvedProjectPath,
template: template.id,
sections: template.sections.map((section) => ({
id: section.id,
title: section.title,
fields: Object.fromEntries(section.fields.map((field) => [field, ''])),
})),
createdAt: now,
updatedAt: now,
};
await savePrd(document);
return document;
}
export async function loadPrd(projectPath: string): Promise<PrdDocument> {
const documents = await listPrds(projectPath);
if (documents.length === 0) {
const resolvedProjectPath = resolveProjectPath(projectPath);
throw new Error(`No PRD documents found in ${prdDirectory(resolvedProjectPath)}`);
}
return documents[0]!;
}
export async function savePrd(doc: PrdDocument): Promise<void> {
const normalized = prdDocumentSchema.parse({
...doc,
projectPath: resolveProjectPath(doc.projectPath),
});
const filePath = prdFilePath(normalized.projectPath, normalized.id);
const serialized = yaml.dump(normalized, {
noRefs: true,
sortKeys: false,
lineWidth: 120,
});
const content = serialized.endsWith('\n') ? serialized : `${serialized}\n`;
await writeFileAtomic(filePath, content);
}
export async function listPrds(projectPath: string): Promise<PrdDocument[]> {
const resolvedProjectPath = resolveProjectPath(projectPath);
const directory = prdDirectory(resolvedProjectPath);
let entries: Dirent[];
try {
entries = await fs.readdir(directory, { withFileTypes: true, encoding: 'utf8' });
} catch (error) {
if (isNodeErrorWithCode(error, 'ENOENT')) {
return [];
}
throw error;
}
const documents: PrdDocument[] = [];
for (const entry of entries) {
if (!entry.isFile()) {
continue;
}
const ext = path.extname(entry.name);
if (!PRD_FILE_EXTENSIONS.has(ext)) {
continue;
}
const filePath = path.join(directory, entry.name);
const raw = await fs.readFile(filePath, 'utf8');
let parsed: unknown;
try {
parsed = yaml.load(raw);
} catch (error) {
throw new Error(`Failed to parse PRD file ${filePath}: ${String(error)}`);
}
const document = prdDocumentSchema.parse(parsed);
documents.push(document);
}
documents.sort((left, right) => right.updatedAt.localeCompare(left.updatedAt));
return documents;
}

View File

@@ -0,0 +1,93 @@
import type { PrdTemplate } from './types.js';
export const BUILTIN_PRD_TEMPLATES: Record<string, PrdTemplate> = {
software: {
id: 'software',
name: 'Software Project',
fields: ['owner', 'status', 'scopeVersion', 'successMetrics'],
sections: [
{ id: 'introduction', title: 'Introduction', fields: ['context', 'objective'] },
{ id: 'problem-statement', title: 'Problem Statement', fields: ['painPoints'] },
{ id: 'scope-non-goals', title: 'Scope / Non-Goals', fields: ['inScope', 'outOfScope'] },
{ id: 'user-stories', title: 'User Stories / Requirements', fields: ['stories'] },
{ id: 'functional-requirements', title: 'Functional Requirements', fields: ['requirements'] },
{
id: 'non-functional-requirements',
title: 'Non-Functional Requirements',
fields: ['performance', 'reliability', 'security'],
},
{ id: 'acceptance-criteria', title: 'Acceptance Criteria', fields: ['criteria'] },
{
id: 'technical-considerations',
title: 'Technical Considerations',
fields: ['constraints', 'dependencies'],
},
{
id: 'risks-open-questions',
title: 'Risks / Open Questions',
fields: ['risks', 'openQuestions'],
},
{
id: 'milestones-delivery',
title: 'Milestones / Delivery',
fields: ['milestones', 'timeline'],
},
],
},
feature: {
id: 'feature',
name: 'Feature PRD',
fields: ['owner', 'status', 'releaseTarget'],
sections: [
{ id: 'problem-statement', title: 'Problem Statement', fields: ['problem'] },
{ id: 'goals', title: 'Goals', fields: ['goals'] },
{ id: 'scope', title: 'Scope', fields: ['inScope', 'outOfScope'] },
{ id: 'user-stories', title: 'User Stories', fields: ['stories'] },
{ id: 'requirements', title: 'Requirements', fields: ['functional', 'nonFunctional'] },
{ id: 'acceptance-criteria', title: 'Acceptance Criteria', fields: ['criteria'] },
{
id: 'technical-considerations',
title: 'Technical Considerations',
fields: ['constraints'],
},
{
id: 'risks-open-questions',
title: 'Risks / Open Questions',
fields: ['risks', 'questions'],
},
{ id: 'milestones', title: 'Milestones', fields: ['milestones'] },
{ id: 'success-metrics', title: 'Success Metrics / Testing', fields: ['metrics', 'testing'] },
],
},
spike: {
id: 'spike',
name: 'Research Spike',
fields: ['owner', 'status', 'decisionDeadline'],
sections: [
{ id: 'background', title: 'Background', fields: ['context'] },
{ id: 'research-questions', title: 'Research Questions', fields: ['questions'] },
{ id: 'constraints', title: 'Constraints', fields: ['constraints'] },
{ id: 'options', title: 'Options Considered', fields: ['options'] },
{ id: 'evaluation', title: 'Evaluation Criteria', fields: ['criteria'] },
{ id: 'findings', title: 'Findings', fields: ['findings'] },
{ id: 'recommendation', title: 'Recommendation', fields: ['recommendation'] },
{ id: 'risks', title: 'Risks / Unknowns', fields: ['risks', 'unknowns'] },
{ id: 'next-steps', title: 'Next Steps', fields: ['nextSteps'] },
{ id: 'milestones', title: 'Milestones / Delivery', fields: ['milestones'] },
],
},
};
export function resolveTemplate(templateName?: string): PrdTemplate {
const name =
templateName === undefined || templateName.trim().length === 0 ? 'software' : templateName;
const template = BUILTIN_PRD_TEMPLATES[name];
if (template === undefined) {
throw new Error(
`Unknown PRD template: ${name}. Expected one of: ${Object.keys(BUILTIN_PRD_TEMPLATES).join(', ')}`,
);
}
return template;
}

View File

@@ -0,0 +1,38 @@
export type PrdStatus = 'draft' | 'review' | 'approved' | 'archived';
export interface PrdTemplateSection {
id: string;
title: string;
fields: string[];
}
export interface PrdTemplate {
id: string;
name: string;
sections: PrdTemplateSection[];
fields: string[];
}
export interface PrdSection {
id: string;
title: string;
fields: Record<string, string>;
}
export interface PrdDocument {
id: string;
title: string;
status: PrdStatus;
projectPath: string;
template: string;
sections: PrdSection[];
createdAt: string;
updatedAt: string;
}
export interface CreatePrdOptions {
name: string;
projectPath: string;
template?: string;
interactive?: boolean;
}

103
packages/prdy/src/wizard.ts Normal file
View File

@@ -0,0 +1,103 @@
import path from 'node:path';
import { cancel, intro, isCancel, outro, select, text } from '@clack/prompts';
import { createPrd, savePrd } from './prd.js';
import type { CreatePrdOptions, PrdDocument } from './types.js';
interface WizardAnswers {
goals: string;
constraints: string;
milestones: string;
}
function updateSectionField(doc: PrdDocument, sectionKeyword: string, value: string): void {
const section = doc.sections.find((candidate) => candidate.id.includes(sectionKeyword));
if (section === undefined) {
return;
}
const fieldName =
Object.keys(section.fields).find((field) => field.toLowerCase().includes(sectionKeyword)) ??
Object.keys(section.fields)[0];
if (fieldName !== undefined) {
section.fields[fieldName] = value;
}
}
async function promptText(message: string, initialValue = ''): Promise<string> {
const response = await text({
message,
initialValue,
});
if (isCancel(response)) {
cancel('PRD wizard cancelled.');
throw new Error('PRD wizard cancelled');
}
return response.trim();
}
async function promptTemplate(template?: string): Promise<string> {
if (template !== undefined && template.trim().length > 0) {
return template;
}
const choice = await select({
message: 'PRD type',
options: [
{ value: 'software', label: 'Software project' },
{ value: 'feature', label: 'Feature' },
{ value: 'spike', label: 'Research spike' },
],
});
if (isCancel(choice)) {
cancel('PRD wizard cancelled.');
throw new Error('PRD wizard cancelled');
}
return choice;
}
function applyWizardAnswers(doc: PrdDocument, answers: WizardAnswers): PrdDocument {
updateSectionField(doc, 'goal', answers.goals);
updateSectionField(doc, 'constraint', answers.constraints);
updateSectionField(doc, 'milestone', answers.milestones);
doc.updatedAt = new Date().toISOString();
return doc;
}
export async function runPrdWizard(options: CreatePrdOptions): Promise<PrdDocument> {
intro('Mosaic PRD wizard');
const name =
options.name.trim().length > 0 ? options.name.trim() : await promptText('Project name');
const template = await promptTemplate(options.template);
const goals = await promptText('Primary goals');
const constraints = await promptText('Key constraints');
const milestones = await promptText('Planned milestones');
const doc = await createPrd({
...options,
name,
template,
interactive: true,
});
const updated = applyWizardAnswers(doc, {
goals,
constraints,
milestones,
});
await savePrd(updated);
outro(`PRD created: ${path.join(updated.projectPath, 'docs', 'prdy', `${updated.id}.yaml`)}`);
return updated;
}

View File

@@ -1,6 +1,7 @@
{
"name": "@mosaic/quality-rails",
"version": "0.0.0",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
@@ -15,7 +16,11 @@
"typecheck": "tsc --noEmit",
"test": "vitest run --passWithNoTests"
},
"dependencies": {
"commander": "^12.0.0"
},
"devDependencies": {
"@types/node": "^22.0.0",
"typescript": "^5.8.0",
"vitest": "^2.0.0"
}

View File

@@ -0,0 +1,202 @@
import { constants } from 'node:fs';
import { access } from 'node:fs/promises';
import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Command } from 'commander';
import { detectProjectKind } from './detect.js';
import { scaffoldQualityRails } from './scaffolder.js';
import type { ProjectKind, QualityProfile, RailsConfig } from './types.js';
const VALID_PROFILES: readonly QualityProfile[] = ['strict', 'standard', 'minimal'];
async function fileExists(filePath: string): Promise<boolean> {
try {
await access(filePath, constants.F_OK);
return true;
} catch {
return false;
}
}
function parseProfile(rawProfile: string): QualityProfile {
if (VALID_PROFILES.includes(rawProfile as QualityProfile)) {
return rawProfile as QualityProfile;
}
throw new Error(`Invalid profile: ${rawProfile}. Use one of ${VALID_PROFILES.join(', ')}.`);
}
function defaultLinters(kind: ProjectKind): string[] {
if (kind === 'node') {
return ['eslint', 'biome'];
}
if (kind === 'python') {
return ['ruff'];
}
if (kind === 'rust') {
return ['clippy'];
}
return [];
}
function defaultFormatters(kind: ProjectKind): string[] {
if (kind === 'node') {
return ['prettier'];
}
if (kind === 'python') {
return ['black'];
}
if (kind === 'rust') {
return ['rustfmt'];
}
return [];
}
function expectedFilesForKind(kind: ProjectKind): string[] {
if (kind === 'node') {
return ['.eslintrc', 'biome.json', '.githooks/pre-commit', 'PR-CHECKLIST.md'];
}
if (kind === 'python') {
return ['pyproject.toml', '.githooks/pre-commit', 'PR-CHECKLIST.md'];
}
if (kind === 'rust') {
return ['rustfmt.toml', '.githooks/pre-commit', 'PR-CHECKLIST.md'];
}
return ['.githooks/pre-commit', 'PR-CHECKLIST.md'];
}
function printScaffoldResult(
config: RailsConfig,
filesWritten: string[],
warnings: string[],
commandsToRun: string[],
): void {
console.log(`[quality-rails] initialized at ${config.projectPath}`);
console.log(`kind=${config.kind} profile=${config.profile}`);
if (filesWritten.length > 0) {
console.log('files written:');
for (const filePath of filesWritten) {
console.log(` - ${filePath}`);
}
}
if (commandsToRun.length > 0) {
console.log('run next:');
for (const command of commandsToRun) {
console.log(` - ${command}`);
}
}
if (warnings.length > 0) {
console.log('warnings:');
for (const warning of warnings) {
console.log(` - ${warning}`);
}
}
}
export function createQualityRailsCli(): Command {
const program = new Command('mosaic');
const qualityRails = program
.command('quality-rails')
.description('Manage quality rails scaffolding');
qualityRails
.command('init')
.requiredOption('--project <path>', 'Project path')
.option('--profile <profile>', 'strict|standard|minimal', 'standard')
.action(async (options: { project: string; profile: string }) => {
const profile = parseProfile(options.profile);
const projectPath = resolve(options.project);
const kind = await detectProjectKind(projectPath);
const config: RailsConfig = {
projectPath,
kind,
profile,
linters: defaultLinters(kind),
formatters: defaultFormatters(kind),
hooks: true,
};
const result = await scaffoldQualityRails(config);
printScaffoldResult(config, result.filesWritten, result.warnings, result.commandsToRun);
});
qualityRails
.command('check')
.requiredOption('--project <path>', 'Project path')
.action(async (options: { project: string }) => {
const projectPath = resolve(options.project);
const kind = await detectProjectKind(projectPath);
const expected = expectedFilesForKind(kind);
const missing: string[] = [];
for (const relativePath of expected) {
const exists = await fileExists(resolve(projectPath, relativePath));
if (!exists) {
missing.push(relativePath);
}
}
if (missing.length > 0) {
console.error('[quality-rails] missing files:');
for (const relativePath of missing) {
console.error(` - ${relativePath}`);
}
process.exitCode = 1;
return;
}
console.log(`[quality-rails] all expected files present for ${kind} project`);
});
qualityRails
.command('doctor')
.requiredOption('--project <path>', 'Project path')
.action(async (options: { project: string }) => {
const projectPath = resolve(options.project);
const kind = await detectProjectKind(projectPath);
const expected = expectedFilesForKind(kind);
console.log(`[quality-rails] doctor for ${projectPath}`);
console.log(`detected project kind: ${kind}`);
for (const relativePath of expected) {
const exists = await fileExists(resolve(projectPath, relativePath));
console.log(` - ${exists ? 'ok' : 'missing'}: ${relativePath}`);
}
if (kind === 'unknown') {
console.log(
'recommendation: add package.json, pyproject.toml, or Cargo.toml for better defaults.',
);
}
});
return program;
}
export async function runQualityRailsCli(argv: string[] = process.argv): Promise<void> {
const program = createQualityRailsCli();
await program.parseAsync(argv);
}
const entryPath = process.argv[1] ? resolve(process.argv[1]) : '';
if (entryPath.length > 0 && entryPath === fileURLToPath(import.meta.url)) {
runQualityRailsCli().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : String(error));
process.exit(1);
});
}

View File

@@ -0,0 +1,30 @@
import { constants } from 'node:fs';
import { access } from 'node:fs/promises';
import { join } from 'node:path';
import type { ProjectKind } from './types.js';
async function fileExists(filePath: string): Promise<boolean> {
try {
await access(filePath, constants.F_OK);
return true;
} catch {
return false;
}
}
export async function detectProjectKind(projectPath: string): Promise<ProjectKind> {
if (await fileExists(join(projectPath, 'package.json'))) {
return 'node';
}
if (await fileExists(join(projectPath, 'pyproject.toml'))) {
return 'python';
}
if (await fileExists(join(projectPath, 'Cargo.toml'))) {
return 'rust';
}
return 'unknown';
}

View File

@@ -1 +1,5 @@
export const VERSION = '0.0.0';
export * from './cli.js';
export * from './detect.js';
export * from './scaffolder.js';
export * from './templates.js';
export * from './types.js';

View File

@@ -0,0 +1,206 @@
import { spawn } from 'node:child_process';
import { chmod, mkdir, readFile, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import {
biomeTemplate,
eslintTemplate,
prChecklistTemplate,
preCommitHookTemplate,
pyprojectSection,
rustfmtTemplate,
} from './templates.js';
import type { RailsConfig, ScaffoldResult } from './types.js';
const PYPROJECT_START_MARKER = '# >>> mosaic-quality-rails >>>';
const PYPROJECT_END_MARKER = '# <<< mosaic-quality-rails <<<';
async function ensureDirectory(filePath: string): Promise<void> {
await mkdir(dirname(filePath), { recursive: true });
}
async function writeRelativeFile(
projectPath: string,
relativePath: string,
contents: string,
result: ScaffoldResult,
): Promise<void> {
const absolutePath = join(projectPath, relativePath);
await ensureDirectory(absolutePath);
await writeFile(absolutePath, contents, { encoding: 'utf8', mode: 0o644 });
result.filesWritten.push(relativePath);
}
async function upsertPyproject(
projectPath: string,
profile: RailsConfig['profile'],
result: ScaffoldResult,
): Promise<void> {
const pyprojectPath = join(projectPath, 'pyproject.toml');
const nextSection = pyprojectSection(profile);
let previous = '';
try {
previous = await readFile(pyprojectPath, 'utf8');
} catch {
previous = '';
}
const existingStart = previous.indexOf(PYPROJECT_START_MARKER);
const existingEnd = previous.indexOf(PYPROJECT_END_MARKER);
if (existingStart >= 0 && existingEnd > existingStart) {
const before = previous.slice(0, existingStart).trimEnd();
const after = previous.slice(existingEnd + PYPROJECT_END_MARKER.length).trimStart();
const rebuilt = [before, nextSection.trim(), after]
.filter((segment) => segment.length > 0)
.join('\n\n');
await writeRelativeFile(projectPath, 'pyproject.toml', `${rebuilt}\n`, result);
return;
}
const separator = previous.trim().length > 0 ? '\n\n' : '';
await writeRelativeFile(
projectPath,
'pyproject.toml',
`${previous.trimEnd()}${separator}${nextSection}`,
result,
);
}
function runCommand(command: string, args: string[], cwd: string): Promise<void> {
return new Promise((resolve, reject) => {
const child = spawn(command, args, {
cwd,
stdio: 'ignore',
env: process.env,
});
child.on('error', reject);
child.on('exit', (code) => {
if (code === 0) {
resolve();
return;
}
reject(new Error(`Command failed (${code ?? 'unknown'}): ${command} ${args.join(' ')}`));
});
});
}
function buildNodeDevDependencies(config: RailsConfig): string[] {
const dependencies = new Set<string>();
if (config.linters.some((linter) => linter.toLowerCase() === 'eslint')) {
dependencies.add('eslint');
dependencies.add('@typescript-eslint/parser');
dependencies.add('@typescript-eslint/eslint-plugin');
}
if (config.linters.some((linter) => linter.toLowerCase() === 'biome')) {
dependencies.add('@biomejs/biome');
}
if (config.formatters.some((formatter) => formatter.toLowerCase() === 'prettier')) {
dependencies.add('prettier');
}
if (config.hooks) {
dependencies.add('husky');
}
return [...dependencies];
}
async function installNodeDependencies(config: RailsConfig, result: ScaffoldResult): Promise<void> {
const dependencies = buildNodeDevDependencies(config);
if (dependencies.length === 0) {
return;
}
const commandLine = `pnpm add -D ${dependencies.join(' ')}`;
if (process.env['MOSAIC_QUALITY_RAILS_SKIP_INSTALL'] === '1') {
result.commandsToRun.push(commandLine);
return;
}
try {
await runCommand('pnpm', ['add', '-D', ...dependencies], config.projectPath);
} catch (error) {
result.warnings.push(
`Failed to auto-install Node dependencies: ${error instanceof Error ? error.message : String(error)}`,
);
result.commandsToRun.push(commandLine);
}
}
export async function scaffoldQualityRails(config: RailsConfig): Promise<ScaffoldResult> {
const result: ScaffoldResult = {
filesWritten: [],
commandsToRun: [],
warnings: [],
};
const normalizedLinters = new Set(config.linters.map((linter) => linter.toLowerCase()));
if (config.kind === 'node') {
if (normalizedLinters.has('eslint')) {
await writeRelativeFile(
config.projectPath,
'.eslintrc',
eslintTemplate(config.profile),
result,
);
}
if (normalizedLinters.has('biome')) {
await writeRelativeFile(
config.projectPath,
'biome.json',
biomeTemplate(config.profile),
result,
);
}
await installNodeDependencies(config, result);
}
if (config.kind === 'python') {
await upsertPyproject(config.projectPath, config.profile, result);
}
if (config.kind === 'rust') {
await writeRelativeFile(
config.projectPath,
'rustfmt.toml',
rustfmtTemplate(config.profile),
result,
);
}
if (config.hooks) {
await writeRelativeFile(
config.projectPath,
'.githooks/pre-commit',
preCommitHookTemplate(config),
result,
);
await chmod(join(config.projectPath, '.githooks/pre-commit'), 0o755);
result.commandsToRun.push('git config core.hooksPath .githooks');
}
await writeRelativeFile(
config.projectPath,
'PR-CHECKLIST.md',
prChecklistTemplate(config.profile),
result,
);
if (config.kind === 'unknown') {
result.warnings.push(
'Unable to detect project kind. Generated generic rails only (hooks + PR checklist).',
);
}
return result;
}

View File

@@ -0,0 +1,181 @@
import type { QualityProfile, RailsConfig } from './types.js';
const PROFILE_TO_MAX_WARNINGS: Record<QualityProfile, number> = {
strict: 0,
standard: 10,
minimal: 50,
};
const PROFILE_TO_LINE_LENGTH: Record<QualityProfile, number> = {
strict: 100,
standard: 110,
minimal: 120,
};
export function eslintTemplate(profile: QualityProfile): string {
return `${JSON.stringify(
{
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
env: {
node: true,
es2022: true,
},
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
rules: {
'no-console': profile === 'strict' ? 'error' : 'warn',
'@typescript-eslint/no-explicit-any':
profile === 'minimal' ? 'off' : profile === 'strict' ? 'error' : 'warn',
'@typescript-eslint/explicit-function-return-type': profile === 'strict' ? 'warn' : 'off',
'max-lines-per-function': [
profile === 'minimal' ? 'off' : 'warn',
{
max: profile === 'strict' ? 60 : 100,
skipBlankLines: true,
skipComments: true,
},
],
},
},
null,
2,
)}\n`;
}
export function biomeTemplate(profile: QualityProfile): string {
return `${JSON.stringify(
{
$schema: 'https://biomejs.dev/schemas/1.8.3/schema.json',
formatter: {
enabled: true,
indentStyle: 'space',
indentWidth: 2,
lineWidth: PROFILE_TO_LINE_LENGTH[profile],
},
linter: {
enabled: true,
rules: {
recommended: true,
suspicious: {
noConsole: profile === 'strict' ? 'error' : 'warn',
},
complexity: {
noExcessiveCognitiveComplexity:
profile === 'strict' ? 'warn' : profile === 'standard' ? 'info' : 'off',
},
},
},
javascript: {
formatter: {
quoteStyle: 'single',
trailingCommas: 'all',
},
},
},
null,
2,
)}\n`;
}
export function pyprojectSection(profile: QualityProfile): string {
const lineLength = PROFILE_TO_LINE_LENGTH[profile];
return [
'# >>> mosaic-quality-rails >>>',
'[tool.ruff]',
`line-length = ${lineLength}`,
'target-version = "py311"',
'',
'[tool.ruff.lint]',
'select = ["E", "F", "I", "UP", "B"]',
`ignore = ${profile === 'minimal' ? '[]' : '["E501"]'}`,
'',
'[tool.black]',
`line-length = ${lineLength}`,
'',
'# <<< mosaic-quality-rails <<<',
'',
].join('\n');
}
export function rustfmtTemplate(profile: QualityProfile): string {
const maxWidth = PROFILE_TO_LINE_LENGTH[profile];
const useSmallHeuristics = profile === 'strict' ? 'Max' : 'Default';
return [
`max_width = ${maxWidth}`,
`use_small_heuristics = "${useSmallHeuristics}"`,
`imports_granularity = "${profile === 'minimal' ? 'Crate' : 'Module'}"`,
`group_imports = "${profile === 'strict' ? 'StdExternalCrate' : 'Preserve'}"`,
'',
].join('\n');
}
function resolveHookCommands(config: RailsConfig): string[] {
const commands: string[] = [];
if (config.kind === 'node') {
if (config.linters.some((linter) => linter.toLowerCase() === 'eslint')) {
commands.push('pnpm lint');
}
if (config.linters.some((linter) => linter.toLowerCase() === 'biome')) {
commands.push('pnpm biome check .');
}
if (config.formatters.some((formatter) => formatter.toLowerCase() === 'prettier')) {
commands.push('pnpm prettier --check .');
}
commands.push('pnpm test --if-present');
}
if (config.kind === 'python') {
commands.push('ruff check .');
commands.push('black --check .');
}
if (config.kind === 'rust') {
commands.push('cargo fmt --check');
commands.push('cargo clippy --all-targets --all-features -- -D warnings');
}
if (commands.length === 0) {
commands.push('echo "No quality commands configured for this project kind"');
}
return commands;
}
export function preCommitHookTemplate(config: RailsConfig): string {
const commands = resolveHookCommands(config)
.map((command) => `${command} || exit 1`)
.join('\n');
return [
'#!/usr/bin/env sh',
'set -eu',
'',
'echo "[quality-rails] Running pre-commit checks..."',
commands,
'echo "[quality-rails] Checks passed."',
'',
].join('\n');
}
export function prChecklistTemplate(profile: QualityProfile): string {
return [
'# Code Review Checklist',
'',
`Profile: **${profile}**`,
'',
'- [ ] Requirements mapped to tests',
'- [ ] Error handling covers unhappy paths',
'- [ ] Lint and typecheck are clean',
'- [ ] Test suite passes',
'- [ ] Security-sensitive paths reviewed',
`- [ ] Warnings count <= ${PROFILE_TO_MAX_WARNINGS[profile]}`,
'',
].join('\n');
}

View File

@@ -0,0 +1,18 @@
export type ProjectKind = 'node' | 'python' | 'rust' | 'unknown';
export type QualityProfile = 'strict' | 'standard' | 'minimal';
export interface RailsConfig {
projectPath: string;
kind: ProjectKind;
profile: QualityProfile;
linters: string[];
formatters: string[];
hooks: boolean;
}
export interface ScaffoldResult {
filesWritten: string[];
commandsToRun: string[];
warnings: string[];
}

72
pnpm-lock.yaml generated
View File

@@ -395,7 +395,26 @@ importers:
version: 2.1.9(@types/node@22.19.15)(lightningcss@1.31.1)
packages/prdy:
dependencies:
'@clack/prompts':
specifier: ^0.9.0
version: 0.9.1
commander:
specifier: ^12.0.0
version: 12.1.0
js-yaml:
specifier: ^4.1.0
version: 4.1.1
zod:
specifier: ^3.22.0
version: 3.25.76
devDependencies:
'@types/js-yaml':
specifier: ^4.0.9
version: 4.0.9
'@types/node':
specifier: ^22.0.0
version: 22.19.15
typescript:
specifier: ^5.8.0
version: 5.9.3
@@ -404,7 +423,14 @@ importers:
version: 2.1.9(@types/node@22.19.15)(lightningcss@1.31.1)
packages/quality-rails:
dependencies:
commander:
specifier: ^12.0.0
version: 12.1.0
devDependencies:
'@types/node':
specifier: ^22.0.0
version: 22.19.15
typescript:
specifier: ^5.8.0
version: 5.9.3
@@ -706,6 +732,12 @@ packages:
'@borewit/text-codec@0.2.2':
resolution: {integrity: sha512-DDaRehssg1aNrH4+2hnj1B7vnUGEjU6OIlyRdkMd0aUdIUvKXrJfXsy8LVtXAy7DRvYVluWbMspsRhz2lcW0mQ==}
'@clack/core@0.4.1':
resolution: {integrity: sha512-Pxhij4UXg8KSr7rPek6Zowm+5M22rbd2g1nfojHJkxp5YkFqiZ2+YLEM/XGVIzvGOcM0nqjIFxrpDwWRZYWYjA==}
'@clack/prompts@0.9.1':
resolution: {integrity: sha512-JIpyaboYZeWYlyP0H+OoPPxd6nqueG/CmN6ixBiNFsIDHREevjIf0n0Ohh5gr5C8pEDknzgvz+pIJ8dMhzWIeg==}
'@discordjs/builders@1.13.1':
resolution: {integrity: sha512-cOU0UDHc3lp/5nKByDxkmRiNZBpdp0kx55aarbiAfakfKJHlxv/yFW1zmIqCAmwH5CRlrH9iMFKJMpvW4DPB+w==}
engines: {node: '>=16.11.0'}
@@ -2779,6 +2811,9 @@ packages:
'@types/estree@1.0.8':
resolution: {integrity: sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w==}
'@types/js-yaml@4.0.9':
resolution: {integrity: sha512-k4MGaQl5TGo/iipqb2UDG2UwjXziSWkh0uysQelTlJpX1qGlpUZYm8PnO4DxG1qBomtJUdYJ6qR6xdIah10JLg==}
'@types/json-schema@7.0.15':
resolution: {integrity: sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==}
@@ -3253,6 +3288,10 @@ packages:
colorette@2.0.20:
resolution: {integrity: sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==}
commander@12.1.0:
resolution: {integrity: sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==}
engines: {node: '>=18'}
commander@13.1.0:
resolution: {integrity: sha512-/rFeCpNJQbhSZjGVwO9RFV3xPqbnERS8MmIQzCtD/zl6gpJuV/bMLuN92oG3F7d8oDEHHRrujSXNUr8fpjntKw==}
engines: {node: '>=18'}
@@ -4658,6 +4697,9 @@ packages:
resolution: {integrity: sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==}
engines: {node: '>=14'}
sisteransi@1.0.5:
resolution: {integrity: sha512-bLGGlR1QxBcynn2d5YmDX4MGjlZvy2MRBDRNHLJ8VI6l6+9FUiyTFNJ0IveOSP0bcXgVDPRcfGqA0pjaqUpfVg==}
slice-ansi@5.0.0:
resolution: {integrity: sha512-FC+lgizVPfie0kkhqUScwRu1O/lF6NOgJmlCgK+/LYxDCTk8sGelYaHDhFcDN+Sn3Cv+3VSa4Byeo+IMCzpMgQ==}
engines: {node: '>=12'}
@@ -5146,6 +5188,9 @@ packages:
peerDependencies:
zod: ^3.25 || ^4
zod@3.25.76:
resolution: {integrity: sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==}
zod@4.3.6:
resolution: {integrity: sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==}
@@ -5594,6 +5639,17 @@ snapshots:
'@borewit/text-codec@0.2.2': {}
'@clack/core@0.4.1':
dependencies:
picocolors: 1.1.1
sisteransi: 1.0.5
'@clack/prompts@0.9.1':
dependencies:
'@clack/core': 0.4.1
picocolors: 1.1.1
sisteransi: 1.0.5
'@discordjs/builders@1.13.1':
dependencies:
'@discordjs/formatters': 0.6.2
@@ -6330,8 +6386,8 @@ snapshots:
'@mistralai/mistralai@1.14.1':
dependencies:
ws: 8.19.0
zod: 4.3.6
zod-to-json-schema: 3.25.1(zod@4.3.6)
zod: 3.25.76
zod-to-json-schema: 3.25.1(zod@3.25.76)
transitivePeerDependencies:
- bufferutil
- utf-8-validate
@@ -7659,6 +7715,8 @@ snapshots:
'@types/estree@1.0.8': {}
'@types/js-yaml@4.0.9': {}
'@types/json-schema@7.0.15': {}
'@types/memcached@2.2.10':
@@ -8116,6 +8174,8 @@ snapshots:
colorette@2.0.20: {}
commander@12.1.0: {}
commander@13.1.0: {}
concat-map@0.0.1: {}
@@ -9568,6 +9628,8 @@ snapshots:
signal-exit@4.1.0: {}
sisteransi@1.0.5: {}
slice-ansi@5.0.0:
dependencies:
ansi-styles: 6.2.3
@@ -10021,8 +10083,14 @@ snapshots:
yoga-layout@3.2.1: {}
zod-to-json-schema@3.25.1(zod@3.25.76):
dependencies:
zod: 3.25.76
zod-to-json-schema@3.25.1(zod@4.3.6):
dependencies:
zod: 4.3.6
zod@3.25.76: {}
zod@4.3.6: {}