refactor(linkedin-studio): M0-2 — storage.ts getDataRoot + external default (ANALYTICS_ROOT alias kept)
This commit is contained in:
parent
dc294495d6
commit
760d4c9101
2 changed files with 91 additions and 42 deletions
|
|
@ -1,19 +1,19 @@
|
|||
import { readFileSync, writeFileSync, readdirSync, existsSync, mkdirSync } from "node:fs";
|
||||
import { join, resolve, dirname } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { homedir } from "node:os";
|
||||
import type { AnalyticsBatch, WeeklyReport, MonthlyReport, PostAnalytics } from "../models/types.js";
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
/**
|
||||
* Walk up from `startDir` until a directory containing `.claude-plugin/plugin.json`
|
||||
* is found. Returns that directory, or null if no marker is found before the
|
||||
* filesystem root.
|
||||
*
|
||||
* Anchoring on the plugin marker keeps the analytics root correct by
|
||||
* Anchoring on the plugin marker locates the plugin tree correctly by
|
||||
* construction regardless of whether this module runs from `src/utils/`
|
||||
* (under tsx) or `build/utils/` (compiled) — and survives a future source
|
||||
* move that a hardcoded "../../../../" count would silently break.
|
||||
* move that a hardcoded "../../../../" count would silently break. Since M0 it
|
||||
* locates bundled read-only assets only; the per-user data root is external
|
||||
* (getDataRoot), no longer derived from the plugin tree.
|
||||
*/
|
||||
export function findPluginRoot(startDir: string): string | null {
|
||||
let dir = resolve(startDir);
|
||||
|
|
@ -33,20 +33,42 @@ export function findPluginRoot(startDir: string): string | null {
|
|||
}
|
||||
|
||||
/**
|
||||
* Get the analytics root directory from environment or default location.
|
||||
* Default is assets/analytics under the plugin root (the dir holding
|
||||
* .claude-plugin/plugin.json). The ANALYTICS_ROOT env override is the test seam.
|
||||
* Resolve HOME with the codebase idiom, but never fall through to "" — that would
|
||||
* turn the absolute data path into a CWD-relative one. os.homedir() is the floor.
|
||||
*/
|
||||
function resolveHome(): string {
|
||||
return process.env.HOME || process.env.USERPROFILE || homedir();
|
||||
}
|
||||
|
||||
/**
|
||||
* CANONICAL — twin of hooks/scripts/data-root.mjs:getDataRoot. MUST STAY IN SYNC:
|
||||
* identical default (~/.claude/linkedin-studio) + identical LINKEDIN_STUDIO_DATA
|
||||
* override semantics. A twin exists (not a shared import) because the runtimes
|
||||
* differ: the hooks are plain .mjs (no tsx) and cannot import this TS module. The
|
||||
* twin-consistency test (hooks/scripts/__tests__/data-root.test.mjs) guards the
|
||||
* contract behaviorally.
|
||||
*
|
||||
* Per-user data root: ~/.claude/linkedin-studio/<subdir>, overridable via
|
||||
* LINKEDIN_STUDIO_DATA (test + power-user seam, mirrors the state-file location).
|
||||
*/
|
||||
export function getDataRoot(subdir: string): string {
|
||||
const dataBase =
|
||||
process.env.LINKEDIN_STUDIO_DATA ||
|
||||
join(resolveHome(), ".claude", "linkedin-studio");
|
||||
return join(dataBase, subdir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the analytics root directory — since M0 the external per-user dir
|
||||
* (getDataRoot("analytics")). The deprecated ANALYTICS_ROOT env override still
|
||||
* wins for back-compat (D4) so cli.ts and existing prose keep working through the
|
||||
* migration. findPluginRoot is preserved (above) for bundled read-only assets.
|
||||
*/
|
||||
export function getAnalyticsRoot(): string {
|
||||
if (process.env.ANALYTICS_ROOT) {
|
||||
return resolve(process.env.ANALYTICS_ROOT);
|
||||
}
|
||||
|
||||
// Anchor on the .claude-plugin/plugin.json marker. Fall back to the legacy
|
||||
// 4-levels-up count (scripts/analytics/{src,build}/utils -> plugin root) only
|
||||
// if no marker is found (e.g. an unusual extraction without the manifest).
|
||||
const pluginRoot = findPluginRoot(__dirname) ?? resolve(__dirname, "../../../../");
|
||||
return join(pluginRoot, "assets", "analytics");
|
||||
return getDataRoot("analytics");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue