UNPKG

piconvert

Version:

Convert pictures to other formats. Supports ai, cdr, vsd, pdf, and more formats. GitHub Actions & CLI Tool.

2 lines (1 loc) 4.16 kB
import{existsSync as u,mkdirSync as L,readFileSync as F,readdirSync as N,statSync as w,writeFileSync as T}from"node:fs";import{basename as $,dirname as q,resolve as p}from"node:path";import A from"events";import E from"js-yaml";import{execSync as S}from"node:child_process";import z from"sharp";function x(l){var t;return l=(((t=l.split("."))==null?void 0:t.pop())||"").toLowerCase().trim(),{jpg:"jpeg"}[l]||l}var j=["jpeg","png","webp","gif","avif","tiff"],P=["svg","png","ps","eps","pdf","emf","wmf","xaml"];async function b(l,a,t,e,o=!1){a=x(a);let i=`svg -> ${a} ${t||""}x${e||""}`;o&&(console.log(`Normalized Type: ${a}`),console.time(i));let s;if(["svg"].includes(a))s=Buffer.from(l);else if(j.includes(a)){o&&console.log("Use: sharp");let r=z(Buffer.from(l));(t===null||t<=0)&&(t=null),(e===null||e<=0)&&(e=null),(t||e)&&r.resize(t,e),s=await r[a]().toBuffer()}else if(P.includes(a)){o&&console.log("Use: inkscape");let r=["--export-area-page",`--export-type ${a}`];t!==null&&t>0&&r.push(`--export-width ${t}`),e!==null&&e>0&&r.push(`--export-height ${e}`),s=S(`inkscape --pipe ${r.join(" ")} --export-filename -`,{input:l,stdio:"pipe"})}else throw new Error(`Unsupported file type: ${a}`);return o&&console.timeEnd(i),s}import{spawn as B}from"node:child_process";import{optimize as U}from"svgo";var D=["ai","cdr","vsd","pdf","jpg","jpeg","png","gif","bmp"];async function C(l,a=!1){let t=x(l),e=`${t} -> svg`;a&&(console.log(`Normalized Type: ${t}`),console.time(e));let o="";if(D.includes(t)){let s=B("inkscape",["--pipe","--export-plain-svg","--pdf-poppler","--export-type","svg","--export-filename","-",l]);s.stdout.on("data",r=>o+=r.toString()),await new Promise(r=>s.on("close",()=>r(o)))}else throw new Error(`Unsupported file type: ${t}`);let i=U(o,{multipass:!0});return a&&console.timeEnd(e),i.data}var _=class extends A{constructor(){super(...arguments);this.imports=[];this.exports={}}emit(t,...e){return super.emit(t,...e)}on(t,e){return super.on(t,e)}async convert(t,e,o,i,s=!1){this.emit("task-start",e,o,i);try{let r=p(o,`${$(e.split(".").slice(0,-1).join("."))}${i.width||i.height?`.${i.width||""}x${i.height||""}`:""}.${i.format}`);if(i.force||!u(r)){u(o)||L(o,{recursive:!0});let m=await b(t,i.format,i.width,i.height,s);T(r,m),this.emit("task-succeeded",e,o,i)}else this.emit("task-skipped",e,o,i)}catch(r){s&&console.error(r),this.emit("task-failed",e,o,i)}}async convert_file(t,e,o,i=!1,s=!1){this.emit("file-start",t,e,o,i,s);let r=p(q(t),t.split(".").slice(0,-1).join(".")),m=u(r+".yml")?r+".yml":u(r+".yaml")?r+".yaml":null,g=o;m&&(g=E.load(F(m,"utf8")));let k=Object.keys(g),h=[];for(let c of k){let f=g[c];if(f)for(let n of f){let O=typeof n=="number"?n:parseInt(n.split("x")[0])||0,I=typeof n=="number"?n:parseInt(n.split("x")[1])||0;h.push({format:c,width:O,height:I,force:i})}else h.push({format:c,width:0,height:0,force:i})}let y=await C(t,s);for(let c of h)await this.convert(y,t,e,c,s);this.emit("file-finish",t,e,o,i,s)}async convert_directory(t,e,o,i,s=!1,r=!1,m=!0){this.emit("directory-start",t,e,o,i,s,r,m);let g=N(t),k=g.filter(f=>w(p(t,f)).isFile()).filter(f=>{var n;return o.includes((n=f.split(".").pop())==null?void 0:n.toLowerCase())}),h=g.filter(f=>w(p(t,f)).isDirectory()),y=u(p(t,"piconvert.yml"))?p(t,"piconvert.yml"):u(p(t,"piconvert.yaml"))?p(t,"piconvert.yaml"):u(p(t,".piconvert.yml"))?p(t,".piconvert.yml"):u(p(t,".piconvert.yaml"))?p(t,".piconvert.yaml"):null,c=i;y&&(c=E.load(F(y,"utf8")));for(let f of k)await this.convert_file(p(t,f),e,c,s,r);if(this.emit("directory-finish",t,e,o,i,s,r,m),m){let f=h.filter(n=>n[0]&&n[0]!==".").map(n=>p(t,n));for(let n of f)await this.convert_directory(n,p(e,$(n)),o,c,s,r,m)}}export(t,e=null){return this.exports[t]=e,this}clear_exports(){return this.exports={},this}import(t){return this.imports.push(t),this}clear_imports(){return this.imports=[],this}async run(t,e,o=!0,i=!1,s=!1){t=p(t),e=p(e),this.emit("conversion-start",t,e),u(t)&&(w(t).isDirectory()?await this.convert_directory(t,e,this.imports,this.exports,i,s,o):await this.convert_file(t,e,this.exports,i,s)),this.emit("conversion-finish",t,e)}};var dt=new _;export{_ as Converter,dt as default};