Skip to content

aidenlx/zotlit / .agents/skills/obsidian-asar-extract

obsidian-asar-extract

Verified

Extract and format Obsidian's runtime source (app.js and siblings) from an Obsidian `.asar` archive into `node_modules/.ob-rev-<version>/` so it can be grepped and read for reverse engineering. Use this whenever you need to inspect Obsidian internals, look up a minified export, hook an undocumented API, read Obsidian's own `app.css` stylesheet (a theme variable's value, such as `--text-faint`, for a contrast check), or before running the obsidian-reverse-engineer agent — anytime the analysis needs a readable `app.js` or `app.css` and it isn't extracted yet.

FileKindSize
SKILL.mdskill md6.8 KB
extract.mjsscript4.9 KB
probe-version.mjsscript1.3 KB

extract.mjs

View on GitHub
#!/usr/bin/env node
// Extract Obsidian's runtime source from an `.asar` archive into
// `node_modules/.ob-rev-<version>/`, then format the minified JS with oxfmt so
// it can be grepped and read line-by-line for reverse engineering.
//
// This script does NOT hunt the filesystem for the archive — you pass it
// explicitly with --asar. Run with no --asar to print the common per-OS
// locations to copy a path from.
//
//   node .agents/skills/obsidian-asar-extract/extract.mjs --asar <path> [--version <v>] [--out <dir>] [--force]
//
// Both `@electron/asar` and `oxfmt` resolve from the repo's node_modules
// because this file lives under the repo root.

import { extractAll, extractFile, listPackage } from "@electron/asar";
import { format } from "oxfmt";
import { existsSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { parseArgs } from "node:util";

/** Common places a runtime archive (the one containing app.js) lives, by platform. */
function locationHints() {
  return [
    "Runtime archives contain app.js. The sibling `app.asar` is only the launcher (skip it).",
    "",
    "Auto-updated, newest, per-user:",
    "  macOS   ~/Library/Application Support/obsidian/obsidian-<version>.asar",
    "  Linux   ~/.config/obsidian/obsidian-<version>.asar",
    "  Windows %APPDATA%\\obsidian\\obsidian-<version>.asar",
    "",
    "Bundled with the installed app (installer version, read from its package.json):",
    "  macOS         /Applications/Obsidian.app/Contents/Resources/obsidian.asar",
    "  Windows/Linux <install dir>/resources/obsidian.asar",
    "",
    "GitHub releases (obsidianmd/obsidian-releases) carry `obsidian-<version>.asar.gz`:",
    "  gh release download v<version> --repo obsidianmd/obsidian-releases --pattern '*.asar.gz'",
    "  gunzip obsidian-<version>.asar.gz",
  ].join("\n");
}

function fail(message) {
  console.error(`error: ${message}`);
  process.exit(1);
}

const { values } = parseArgs({
  options: {
    asar: { type: "string" },
    out: { type: "string" },
    force: { type: "boolean", default: false },
  },
});

if (!values.asar) {
  console.error("error: --asar <path> is required.\n");
  console.error(locationHints());
  process.exit(1);
}

const archive = resolve(values.asar);
if (!existsSync(archive)) fail(`asar not found: ${archive}`);

// A runtime archive carries app.js. The launcher app.asar does not — reject it
// early so the agent doesn't analyze the wrong file.
let entries;
try {
  entries = listPackage(archive);
} catch (cause) {
  fail(`not a readable asar archive: ${archive}\n  ${cause.message}`);
}
if (!entries.includes("/app.js")) {
  fail(
    `${archive} has no app.js — this looks like the launcher app.asar, not the runtime.\n\n` +
      locationHints(),
  );
}

// Version is read from the archive's own package.json — the single source of
// truth — so the output directory always matches the runtime it came from.
let version;
try {
  version = JSON.parse(extractFile(archive, "package.json").toString("utf8")).version;
} catch {
  /* fall through to the check below */
}
if (!version) fail("could not read version from the archive's package.json");

const outDir = values.out
  ? resolve(values.out)
  : join(process.cwd(), "node_modules", `.ob-rev-${version}`);

if (existsSync(join(outDir, "app.js")) && !values.force) {
  console.error(`already extracted (use --force to redo): ${outDir}`);
  console.log(outDir);
  process.exit(0);
}

// Guard against a misaimed --out wiping real files: only clear a directory that
// is empty or a prior extraction (carries app.js). Never delete anything else,
// even with --force.
if (existsSync(outDir)) {
  const isPriorExtraction = existsSync(join(outDir, "app.js"));
  if (!isPriorExtraction && readdirSync(outDir).length > 0) {
    fail(`refusing to delete ${outDir}: not empty and not a prior extraction (no app.js).`);
  }
  rmSync(outDir, { recursive: true, force: true });
}
console.error(`extracting ${archive}\n       to ${outDir}`);
extractAll(archive, outDir);

// Format every top-level minified bundle so line-based grep/Read is meaningful.
// lib/ holds vendored deps that the RE workflow rarely needs, so leave it raw.
const topLevelJs = readdirSync(outDir, { withFileTypes: true })
  .filter((e) => e.isFile() && e.name.endsWith(".js"))
  .map((e) => e.name);

for (const name of topLevelJs) {
  const file = join(outDir, name);
  const source = readFileSync(file, "utf8");
  try {
    const { code, errors } = await format(name, source, { semi: true });
    if (errors.length) {
      console.error(`  skip ${name}: ${errors.length} parse error(s), keeping raw`);
      continue;
    }
    writeFileSync(file, code);
    console.error(`  fmt  ${name}`);
  } catch (cause) {
    console.error(`  skip ${name}: ${cause.message}, keeping raw`);
  }
}

console.error(`done. obsidian ${version} source ready.`);
// Machine-readable: the output directory on stdout for the caller to consume.
console.log(outDir);
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall