1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
/**
* 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,
);
}
|