@awesomate/hosting-mcp
Version:
Awesomate MCP server — lets Claude manage your Awesomate WordPress hosting, plan, limits, n8n automations, and build Node/static apps + databases
116 lines (107 loc) • 5.32 kB
JavaScript
/**
* The saved Awesomate accounts on this computer, and which one a folder uses.
* Node 18 built-ins only: this file is copied into the user's skills folder
* beside bootstrap.mjs.
*
* activeAccount() follows the MCP server's order exactly (mcp/src/config.ts):
* AWESOMATE_PAT env, AWESOMATE_ACCOUNT env, the nearest .awesomate.json pin
* (AWESOMATE_PIN_FILE, else walked up from the folder, stopping at home), the
* only profile, then defaultProfile. It never prints or returns a token.
*/
import { existsSync, readFileSync } from 'node:fs';
import { dirname, isAbsolute, join, resolve } from 'node:path';
export const PIN_FILENAME = '.awesomate.json';
/**
* Read credentials.json. `unreadable` is true when the file exists but is not
* a JSON object: a caller about to write it must keep a copy first, or every
* profile in it is lost without a word.
*/
export function readCredentials(credPath) {
let raw;
try {
raw = readFileSync(credPath, 'utf8');
} catch (err) {
return { exists: err?.code !== 'ENOENT', unreadable: err?.code !== 'ENOENT', file: {}, profiles: {}, defaultProfile: null };
}
let file = null;
try {
const parsed = JSON.parse(raw);
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) file = parsed;
} catch { /* unreadable below */ }
if (!file) return { exists: true, unreadable: raw.trim() !== '', file: {}, profiles: {}, defaultProfile: null };
const profiles = {};
if (file.profiles && typeof file.profiles === 'object') {
for (const [key, p] of Object.entries(file.profiles)) {
if (p && typeof p === 'object' && typeof p.pat === 'string') profiles[key] = p;
}
} else if (typeof file.pat === 'string' && file.pat) {
profiles[file.slug || 'default'] = file;
}
const defaultProfile = typeof file.defaultProfile === 'string' && profiles[file.defaultProfile] ? file.defaultProfile : null;
return { exists: true, unreadable: false, file, profiles, defaultProfile };
}
export function findPin({ cwd, home, env }) {
const explicit = env.AWESOMATE_PIN_FILE;
if (explicit) {
const path = isAbsolute(explicit) ? explicit : resolve(cwd, explicit);
return existsSync(path) ? path : null;
}
const top = resolve(home);
let dir = resolve(cwd);
for (;;) {
const candidate = join(dir, PIN_FILENAME);
if (existsSync(candidate)) return candidate;
if (dir === top) return null;
const parent = dirname(dir);
if (parent === dir) return null;
dir = parent;
}
}
function pinAccount(pinPath) {
try {
const pin = JSON.parse(readFileSync(pinPath, 'utf8'));
return pin && typeof pin.account === 'string' ? pin.account : null;
} catch { return null; }
}
/**
* Which account a session started in `cwd` uses. slug is null when none can
* be chosen; `problem` then says why in a sentence.
*/
export function activeAccount({ credPath, cwd, home, env }) {
const { profiles, defaultProfile, unreadable } = readCredentials(credPath);
const saved = Object.keys(profiles);
const base = { saved, pinPath: null };
if (unreadable) return { ...base, slug: null, source: 'none', problem: `${credPath} could not be read` };
if (env.AWESOMATE_PAT) {
const match = Object.entries(profiles).find(([, p]) => p.pat === env.AWESOMATE_PAT);
return { ...base, slug: match ? match[1].slug ?? match[0] : null, source: 'env-pat', problem: match ? null : 'AWESOMATE_PAT is set to a key that is not saved here' };
}
if (env.AWESOMATE_ACCOUNT) {
const slug = env.AWESOMATE_ACCOUNT;
return { ...base, slug: profiles[slug] ? slug : null, source: 'env-account', problem: profiles[slug] ? null : `AWESOMATE_ACCOUNT names "${slug}", which has no key saved here` };
}
const pinPath = findPin({ cwd, home, env });
if (pinPath) {
const slug = pinAccount(pinPath);
if (slug && profiles[slug]) return { ...base, slug, source: 'pin', pinPath, problem: null };
return { ...base, slug: null, source: 'pin', pinPath, problem: slug ? `the folder pin names "${slug}", which has no key saved here` : `${pinPath} names no account` };
}
if (saved.length === 1) return { ...base, slug: saved[0], source: 'sole-profile', problem: null };
if (defaultProfile) return { ...base, slug: defaultProfile, source: 'default-profile', problem: null };
if (saved.length === 0) return { ...base, slug: null, source: 'none', problem: 'no account is connected on this computer' };
return { ...base, slug: null, source: 'none', problem: 'several accounts are saved and this folder is not pinned to one' };
}
const WHY = {
'env-pat': 'AWESOMATE_PAT is set',
'env-account': 'AWESOMATE_ACCOUNT is set',
'sole-profile': 'the only account saved here',
'default-profile': 'the default account, so reads only: pin this folder before any change',
};
/** One line for the end of every run. */
export function describeActive(active) {
const others = active.saved.filter((s) => s !== active.slug);
const also = others.length ? ` Also saved on this computer: ${others.join(', ')}.` : '';
if (!active.slug) return `Active Awesomate account here: none (${active.problem}).${also}`;
const why = active.source === 'pin' ? `pinned by ${active.pinPath}` : WHY[active.source] ?? active.source;
return `Active Awesomate account here: ${active.slug} (${why}).${also}`;
}