refactor: adopt planningPaths() across 4 modules — eliminate 34 inline path constructions

Consolidates repeated path.join(cwd, '.planning', ...) patterns into
calls to the shared planningPaths() helper from core.cjs.

Modules updated:
- state.cjs: 16 → planningPaths() (2 remain for non-standard paths)
- commands.cjs: 8 → planningPaths() (4 remain for todos dir)
- milestone.cjs: 5 → planningPaths() (3 remain for archive/milestones)
- roadmap.cjs: 4 → planningPaths()

Benefits:
- Single source of truth for .planning/ directory structure
- Easier to change planning dir location in the future
- Consistent path construction across the codebase
- No behavioral changes — pure refactor

797/797 tests pass.
This commit is contained in:
Tom Boucher
2026-03-18 23:38:36 -04:00
parent 5fd384f336
commit 1d4deb0f8b
5 changed files with 41 additions and 41 deletions

View File

@@ -26,15 +26,15 @@ function stateExtractField(content, fieldName) {
function cmdStateLoad(cwd, raw) {
const config = loadConfig(cwd);
const planningDir = path.join(cwd, '.planning');
const planDir = planningPaths(cwd).planning;
let stateRaw = '';
try {
stateRaw = fs.readFileSync(path.join(planningDir, 'STATE.md'), 'utf-8');
stateRaw = fs.readFileSync(path.join(planDir, 'STATE.md'), 'utf-8');
} catch { /* intentionally empty */ }
const configExists = fs.existsSync(path.join(planningDir, 'config.json'));
const roadmapExists = fs.existsSync(path.join(planningDir, 'ROADMAP.md'));
const configExists = fs.existsSync(path.join(planDir, 'config.json'));
const roadmapExists = fs.existsSync(path.join(planDir, 'ROADMAP.md'));
const stateExists = stateRaw.length > 0;
const result = {
@@ -70,7 +70,7 @@ function cmdStateLoad(cwd, raw) {
}
function cmdStateGet(cwd, section, raw) {
const statePath = path.join(cwd, '.planning', 'STATE.md');
const statePath = planningPaths(cwd).state;
try {
const content = fs.readFileSync(statePath, 'utf-8');
@@ -124,7 +124,7 @@ function readTextArgOrFile(cwd, value, filePath, label) {
}
function cmdStatePatch(cwd, patches, raw) {
const statePath = path.join(cwd, '.planning', 'STATE.md');
const statePath = planningPaths(cwd).state;
try {
let content = fs.readFileSync(statePath, 'utf-8');
const results = { updated: [], failed: [] };
@@ -161,7 +161,7 @@ function cmdStateUpdate(cwd, field, value) {
error('field and value required for state update');
}
const statePath = path.join(cwd, '.planning', 'STATE.md');
const statePath = planningPaths(cwd).state;
try {
let content = fs.readFileSync(statePath, 'utf-8');
const fieldEscaped = escapeRegex(field);
@@ -202,7 +202,7 @@ function stateReplaceField(content, fieldName, newValue) {
}
function cmdStateAdvancePlan(cwd, raw) {
const statePath = path.join(cwd, '.planning', 'STATE.md');
const statePath = planningPaths(cwd).state;
if (!fs.existsSync(statePath)) { output({ error: 'STATE.md not found' }, raw); return; }
let content = fs.readFileSync(statePath, 'utf-8');
@@ -231,7 +231,7 @@ function cmdStateAdvancePlan(cwd, raw) {
}
function cmdStateRecordMetric(cwd, options, raw) {
const statePath = path.join(cwd, '.planning', 'STATE.md');
const statePath = planningPaths(cwd).state;
if (!fs.existsSync(statePath)) { output({ error: 'STATE.md not found' }, raw); return; }
let content = fs.readFileSync(statePath, 'utf-8');
@@ -265,13 +265,13 @@ function cmdStateRecordMetric(cwd, options, raw) {
}
function cmdStateUpdateProgress(cwd, raw) {
const statePath = path.join(cwd, '.planning', 'STATE.md');
const statePath = planningPaths(cwd).state;
if (!fs.existsSync(statePath)) { output({ error: 'STATE.md not found' }, raw); return; }
let content = fs.readFileSync(statePath, 'utf-8');
// Count summaries across current milestone phases only
const phasesDir = path.join(cwd, '.planning', 'phases');
const phasesDir = planningPaths(cwd).phases;
let totalPlans = 0;
let totalSummaries = 0;
@@ -310,7 +310,7 @@ function cmdStateUpdateProgress(cwd, raw) {
}
function cmdStateAddDecision(cwd, options, raw) {
const statePath = path.join(cwd, '.planning', 'STATE.md');
const statePath = planningPaths(cwd).state;
if (!fs.existsSync(statePath)) { output({ error: 'STATE.md not found' }, raw); return; }
const { phase, summary, summary_file, rationale, rationale_file } = options;
@@ -348,7 +348,7 @@ function cmdStateAddDecision(cwd, options, raw) {
}
function cmdStateAddBlocker(cwd, text, raw) {
const statePath = path.join(cwd, '.planning', 'STATE.md');
const statePath = planningPaths(cwd).state;
if (!fs.existsSync(statePath)) { output({ error: 'STATE.md not found' }, raw); return; }
const blockerOptions = typeof text === 'object' && text !== null ? text : { text };
let blockerText = null;
@@ -381,7 +381,7 @@ function cmdStateAddBlocker(cwd, text, raw) {
}
function cmdStateResolveBlocker(cwd, text, raw) {
const statePath = path.join(cwd, '.planning', 'STATE.md');
const statePath = planningPaths(cwd).state;
if (!fs.existsSync(statePath)) { output({ error: 'STATE.md not found' }, raw); return; }
if (!text) { output({ error: 'text required' }, raw); return; }
@@ -413,7 +413,7 @@ function cmdStateResolveBlocker(cwd, text, raw) {
}
function cmdStateRecordSession(cwd, options, raw) {
const statePath = path.join(cwd, '.planning', 'STATE.md');
const statePath = planningPaths(cwd).state;
if (!fs.existsSync(statePath)) { output({ error: 'STATE.md not found' }, raw); return; }
let content = fs.readFileSync(statePath, 'utf-8');
@@ -448,7 +448,7 @@ function cmdStateRecordSession(cwd, options, raw) {
}
function cmdStateSnapshot(cwd, raw) {
const statePath = path.join(cwd, '.planning', 'STATE.md');
const statePath = planningPaths(cwd).state;
if (!fs.existsSync(statePath)) {
output({ error: 'STATE.md not found' }, raw);
@@ -580,7 +580,7 @@ function buildStateFrontmatter(bodyContent, cwd) {
if (cwd) {
try {
const phasesDir = path.join(cwd, '.planning', 'phases');
const phasesDir = planningPaths(cwd).phases;
if (fs.existsSync(phasesDir)) {
const isDirInMilestone = getMilestonePhaseFilter(cwd);
const phaseDirs = fs.readdirSync(phasesDir, { withFileTypes: true })
@@ -722,7 +722,7 @@ function writeStateMd(statePath, content, cwd) {
}
function cmdStateJson(cwd, raw) {
const statePath = path.join(cwd, '.planning', 'STATE.md');
const statePath = planningPaths(cwd).state;
if (!fs.existsSync(statePath)) {
output({ error: 'STATE.md not found' }, raw, 'STATE.md not found');
return;
@@ -748,7 +748,7 @@ function cmdStateJson(cwd, raw) {
* Fixes: #1102 (plan counts), #1103 (status/last_activity), #1104 (body text).
*/
function cmdStateBeginPhase(cwd, phaseNumber, phaseName, planCount, raw) {
const statePath = path.join(cwd, '.planning', 'STATE.md');
const statePath = planningPaths(cwd).state;
if (!fs.existsSync(statePath)) {
output({ error: 'STATE.md not found' }, raw);
return;