#!/usr/bin/env node import { spawn } from "node:child_process"; import { existsSync as exists_sync, statSync as stat_sync } from "node:fs"; import path from "node:path"; import { fileURLToPath as file_url_to_path } from "node:url"; import electron from "electron"; const args = process.argv.slice(2); const root = file_url_to_path(new URL("../", import.meta.url)); if (args.length === 1 && ["--help", "-h"].includes(args[0])) { console.log("Usage: paper "); } else { try { if (args.length !== 1) { throw new Error("Usage: paper "); } const target = path.resolve(args[0]); if (![".md", ".markdown", ".mdown", ".txt"].includes(path.extname(target).toLowerCase())) { throw new Error("Supported files: .md, .markdown, .mdown, .txt"); } if (!stat_sync(target).isFile()) { throw new Error("The path must point to a file."); } if (!exists_sync(path.join(root, "dist/electron/main.js"))) { throw new Error("Build Paper first with npm run build."); } const child = spawn(electron, [root, "--paper-file", target], { detached: true, stdio: "ignore", env: { ...process.env, ELECTRON_RUN_AS_NODE: undefined }, }); child.on("error", (error) => { console.error(`Paper: ${error.message}`); process.exitCode = 1; }); child.unref(); } catch (error) { console.error(`Paper: ${error.message}`); process.exitCode = 1; } }