-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new puppeteer implementation of fit code
- Loading branch information
Mike Mellor
committed
Jan 20, 2023
1 parent
91768f1
commit 4b85b26
Showing
1 changed file
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,46 @@ | ||
console.log("ASDF"); | ||
import fs from "fs-extra"; | ||
import puppeteer from "puppeteer-core"; | ||
import path from "path"; | ||
|
||
export async function fit(inputs, outputs){ | ||
let dest = outputs; | ||
let input = inputs; | ||
|
||
const browser = await puppeteer.launch({ | ||
executablePath: "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome", | ||
args: ['--headless', '--no-sandbox', '--disable-gpu', '--disable-dev-shm-usage'], | ||
}); | ||
|
||
try{ | ||
const page = await browser.newPage(); | ||
|
||
let svg = fs.readFileSync(input, {encoding: 'utf8'}); | ||
|
||
await page.setContent(svg); | ||
|
||
const result = await (await page.waitForSelector("svg")).evaluate((el) => { | ||
let { x, y, width, height } = el.getBBox(); | ||
|
||
el.setAttribute('viewBox', `${x} ${y} ${width} ${height}`); | ||
|
||
if(el.hasAttribute('width')){ | ||
el.setAttribute('width', width); | ||
} | ||
|
||
if(el.hasAttribute('height')){ | ||
el.setAttribute('height', height); | ||
} | ||
|
||
return el.outerHTML; | ||
}); | ||
|
||
fs.mkdirpSync(dest); | ||
fs.writeFileSync(`${dest}/${path.basename(input)}`, result); | ||
} catch(e){console.log(e);} | ||
|
||
await browser.close(); | ||
} | ||
|
||
export default { | ||
fit | ||
}; |