@kitschpatrol/tldraw-cli 1.2.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.js +18 -15
- package/dist/.DS_Store +0 -0
- package/dist/lib/index.js +4 -1
- package/dist/lib/tldraw-to-image.d.ts +8 -1
- package/dist/tldraw/assets/{index-a71XA0Xq.js → index--JsGxma4.js} +39 -39
- package/dist/tldraw/index.html +1 -1
- package/package.json +5 -6
- package/readme.md +32 -17
package/dist/lib/index.js
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import R from"express";import F from"node:net";import L from"node:os";var y=class extends Error{constructor(t){super(`${t} is locked`)}},f={old:new Set,young:new Set},O=1e3*15;var w,j=()=>{let e=L.networkInterfaces(),t=new Set([void 0,"0.0.0.0"]);for(let r of Object.values(e))for(let o of r)t.add(o.address);return t},I=e=>new Promise((t,r)=>{let o=F.createServer();o.unref(),o.on("error",r),o.listen(e,()=>{let{port:n}=o.address();o.close(()=>{t(n)})})}),D=async(e,t)=>{if(e.host||e.port===0)return I(e);for(let r of t)try{await I({port:e.port,host:r})}catch(o){if(!["EADDRNOTAVAIL","EINVAL"].includes(o.code))throw o}return e.port},q=function*(e){e&&(yield*e),yield 0};async function P(e){let t,r=new Set;if(e&&(e.port&&(t=typeof e.port=="number"?[e.port]:e.port),e.exclude)){let n=e.exclude;if(typeof n[Symbol.iterator]!="function")throw new TypeError("The `exclude` option must be an iterable.");for(let a of n){if(typeof a!="number")throw new TypeError("Each item in the `exclude` option must be a number corresponding to the port you want excluded.");if(!Number.isSafeInteger(a))throw new TypeError(`Number ${a} in the exclude option is not a safe integer and can't be used`)}r=new Set(n)}w===void 0&&(w=setTimeout(()=>{w=void 0,f.old=f.young,f.young=new Set},O),w.unref&&w.unref());let o=j();for(let n of q(t))try{if(r.has(n))continue;let a=await D({...e,port:n},o);for(;f.old.has(a)||f.young.has(a);){if(n!==0)throw new y(n);a=await D({...e,port:n},o)}return f.young.add(a),a}catch(a){if(!["EADDRINUSE","EACCES"].includes(a.code)&&!(a instanceof y))throw a}throw new Error("No available ports found")}import M from"node:fs/promises";import N from"node:os";import l from"node:path";import{dirname as V}from"node:path";import{fileURLToPath as _}from"node:url";import G from"puppeteer";import U from"node:os";var T=U.homedir();function b(e){if(typeof e!="string")throw new TypeError(`Expected a string, got ${typeof e}`);return T?e.replace(/^~(?=$|\/|\\)/,T):e}var H={darkMode:!1,format:"svg",output:"./",transparent:!1,verbose:!0};async function W(e,t={}){let r={...H,...X(t)},{darkMode:o,format:n,output:a,transparent:p,verbose:s}=r,c=!e.startsWith("https://www.tldraw.com/");s&&console.log(c?"Local file detected":"tldraw URL detected");let h=0,m;if(c){s&&console.log("Starting tldraw server...");let d=V(_(import.meta.url)),g=l.join(d,d.endsWith("/src/lib")?"../../dist/tldraw":d.endsWith("/dist/lib")?"../tldraw":"../dist/tldraw");s&&console.log(`tldraw served from "${g}"`),m=await J(g),h=m.address().port,s&&console.log(`tldraw hosted at "http://localhost:${h}"`)}s&&console.log("Starting Puppeteer...");let S=await G.launch({args:c?["--no-sandbox","--disable-web-security"]:[],headless:"new"}),i=await S.newPage();z(i,s);let x=await i.target().createCDPSession();if(await x.send("Browser.setDownloadBehavior",{behavior:"allowAndName",downloadPath:N.tmpdir(),eventsEnabled:!0}),c){await i.setRequestInterception(!0),i.on("request",u=>{u.url().endsWith("favicon.ico")?u.respond({status:200}):u.continue()});let d=l.resolve(b(e));s&&console.log(`Loading tldr file "${d}"`);let g=await M.readFile(d,"utf8");await i.evaluateOnNewDocument(u=>{localStorage.clear(),localStorage.setItem("tldrData",u)},g)}let $=c?`http://localhost:${h}`:e;s&&console.log(`Navigating to: ${$}`),await i.goto($,{waitUntil:"networkidle0"}),s&&console.log(`Setting background transparency: ${p}`),await ee(i,p),s&&console.log(`Setting dark mode: ${o}`);let E=await Z(i);await A(i,o),s&&console.log("Requesting download"),await Y(i),await Q(i,["main.menu","menu-item.edit","menu-item.export-as",`menu-item.export-as-${n}`]);let k=await K(x);s&&console.log("Download complete"),s&&console.log(`Restoring dark mode: ${E}`),await A(i,E),await S.close(),s&&console.log("Stopped Puppeteer"),c&&m&&(m.close(),s&&console.log("Stopped tldraw server"));let B=c?l.basename(e,l.extname(e)):k,C=l.join(N.tmpdir(),k),v=b(l.join(a,`${B}.${n}`));return await M.rename(C,v),s&&console.log(`Saved to "${v}"`),l.resolve(v)}function z(e,t){e.on("console",r=>{let o=r.type(),n=r.text();o==="error"?console.error(`[Browser] ${n}`):o==="warning"?console.warn(`[Browser] ${n}`):t&&console.log(`[Browser] ${n}`)})}async function J(e){let t=R(),r=await P();return t.use(R.static(e)),new Promise((o,n)=>{let a=t.listen(r,()=>{o(a)});a.on("error",p=>{n(p)})})}async function K(e){return new Promise((t,r)=>{e.on("Browser.downloadProgress",o=>{o.state==="completed"?t(o.guid):o.state==="canceled"&&r(new Error("Download was canceled"))})})}async function Q(e,t){for(let r of t)await e.waitForSelector(`[data-testid="${r}"]`),await e.click(`[data-testid="${r}"]`)}function X(e){return Object.fromEntries(Object.entries(e).filter(([,t])=>t!==void 0))}async function Y(e){await e.evaluate("app.clearOpenMenus()")}async function Z(e){return!!await e.evaluate("editor.user.getUserPreferences().isDarkMode")}async function A(e,t){await e.evaluate(`editor.user.updateUserPreferences({ isDarkMode: ${t}})`)}async function ee(e,t){await e.evaluate(`editor.updateInstanceState(
|
|
2
|
+
{ exportBackground: ${!t} },
|
|
3
|
+
{ ephemeral: true },
|
|
4
|
+
)`)}export{W as tldrawToImage};
|
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
import type { ExportFormat } from '../types';
|
|
2
|
-
export
|
|
2
|
+
export type TldrawImageOptions = {
|
|
3
|
+
darkMode?: boolean;
|
|
4
|
+
format?: ExportFormat;
|
|
5
|
+
output?: string;
|
|
6
|
+
transparent?: boolean;
|
|
7
|
+
verbose?: boolean;
|
|
8
|
+
};
|
|
9
|
+
export declare function tldrawToImage(tldrPathOrUrl: string, options?: TldrawImageOptions): Promise<string>;
|