summaryrefslogtreecommitdiff
path: root/legacy/nodejs/scripts/lib/note-helpers.mjs
diff options
context:
space:
mode:
authorChristopher Warner <cwarner@kernelcode.com>2026-10-10 18:47:24 -0400
committerChristopher Warner <cwarner@kernelcode.com>2026-10-10 18:47:24 -0400
commit5c61196f27b9342e5f25414c11df5710f956f406 (patch)
treec81f15f54c9baa35f61ce3c71b81f1c1e7b5f590 /legacy/nodejs/scripts/lib/note-helpers.mjs
parent78862ba855c6e66cac64a86c25dca6149d8fb2fd (diff)
downloadiphone-notes-5c61196f27b9342e5f25414c11df5710f956f406.tar.gz
iphone-notes-5c61196f27b9342e5f25414c11df5710f956f406.tar.bz2
iphone-notes-5c61196f27b9342e5f25414c11df5710f956f406.tar.xz
iphone-notes-5c61196f27b9342e5f25414c11df5710f956f406.zip
feat!: rewrite icloud-note CLI in Rust with chromiumoxide
Replace the Node/Playwright driver with a single Rust binary so create/status stay headless without a visible browser. Move the previous implementation to legacy/nodejs for reference. BREAKING CHANGE: the supported CLI is the Rust `icloud-note` binary; the Node entrypoint is no longer installed by setup.sh. Co-authored-by: Cursor <cursoragent@cursor.com>
Diffstat (limited to 'legacy/nodejs/scripts/lib/note-helpers.mjs')
-rw-r--r--legacy/nodejs/scripts/lib/note-helpers.mjs88
1 files changed, 88 insertions, 0 deletions
diff --git a/legacy/nodejs/scripts/lib/note-helpers.mjs b/legacy/nodejs/scripts/lib/note-helpers.mjs
new file mode 100644
index 0000000..5dd43a0
--- /dev/null
+++ b/legacy/nodejs/scripts/lib/note-helpers.mjs
@@ -0,0 +1,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,
+ );
+}