/** * Pure helpers for icloud-note (unit-tested; no Playwright). */ /** Join title + body the way Apple Notes expects (first line = title). */ export function formatNoteText(title, body) { const t = title == null ? "" : String(title); const b = body == null ? "" : String(body); return [t, b].filter((s) => s.length > 0).join("\n\n"); } /** True when a URL is clearly an Apple auth/sign-in page. */ export function isAppleAuthUrl(url) { return /idmsa\.apple\.com|appleid\.apple\.com/i.test(String(url || "")); } /** * Heuristic "logged out" detection from page signals (mirrors pageLooksLoggedOut). * @param {{ url?: string, bodyText?: string, hasPasswordField?: boolean, hasSignInControl?: boolean, hasNotesApp?: boolean }} s */ export function looksLoggedOut(s = {}) { if (isAppleAuthUrl(s.url)) return true; const t = String(s.bodyText || "").slice(0, 4000); if (/Sign in to iCloud/i.test(t)) return true; if (s.hasPasswordField) return true; if (s.hasSignInControl && !s.hasNotesApp) return true; return false; } /** Resolve XDG-style config paths (overridable via env for tests). */ export function resolveConfigPaths(env = process.env, homedir = "") { const home = homedir || env.HOME || env.USERPROFILE || "/tmp"; const configDir = env.ICLOUD_NOTES_CONFIG || `${home}/.config/icloud-notes`; return { configDir, profileDir: `${configDir}/browser-profile`, statePath: `${configDir}/storage-state.json`, }; } /** * Parse create CLI flags (subset of node:util parseArgs shape). * @returns {{ title: string, bodyFile?: string, body?: string, positionals: string[] }} */ export function parseCreateArgv(argv) { const out = { title: "", body: undefined, bodyFile: undefined, positionals: [] }; for (let i = 0; i < argv.length; i++) { const a = argv[i]; if (a === "--title") { out.title = String(argv[++i] ?? ""); } else if (a === "--body") { out.body = String(argv[++i] ?? ""); } else if (a === "--body-file") { out.bodyFile = String(argv[++i] ?? ""); } else if (a.startsWith("-")) { throw new Error(`Unknown option: ${a}`); } else { out.positionals.push(a); } } if (!out.title && out.positionals[0]) { out.title = out.positionals[0]; } return out; } /** Validate create has something to send. */ export function requireCreateContent(title, body) { const t = title == null ? "" : String(title); const b = body == null ? "" : String(body).replace(/\s+$/, ""); if (!t && !b) { throw new Error("Provide --title and/or --body / --body-file / stdin"); } return { title: t, body: b }; } /** Conventional Commits 1.0.0 subject line (first line of message). */ export function isConventionalCommitSubject(subject) { const line = String(subject || "").trim(); return /^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\([a-zA-Z0-9._/-]+\))?!?: .+/.test( line, ); }