Skip to content

Commit

Permalink
Avoid temporary file for primitive input
Browse files Browse the repository at this point in the history
This uses primitives new `-o -` option to print the resulting SVG to
stdout. That means we don't need to use a temporary file to capture the
output, which both means one less dependency and running a tad bit
faster. Though to be fair, the run time for primitive dwarfs pretty much
all IO.

fogleman/primitive#71
  • Loading branch information
maxnordlund committed Mar 5, 2019
1 parent ab9bfc1 commit 438845d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 68 deletions.
71 changes: 32 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"fs-extra": "^7.0.1",
"image-size": "^0.7.1",
"svgo": "^1.0.5",
"tempy": "^0.2.1",
"yargs": "^11.0.0"
},
"repository": {
Expand Down
18 changes: 5 additions & 13 deletions src/utils/primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import os from 'os'

import execa from 'execa'
import fs from 'fs-extra'
import tempy from 'tempy'

const VENDOR_DIR = path.resolve(__dirname, '..', '..', 'vendor')
let primitiveExecutable = 'primitive'
Expand Down Expand Up @@ -37,19 +36,18 @@ const checkForPrimitive = async () => {
}
}

// Run Primitive with reasonable defaults (rectangles as shapes, 9 shaper per default) to generate the placeholder SVG
// Run Primitive with reasonable defaults (rectangles as shapes, 9 shaper per
// default) to generate the placeholder SVG
const runPrimitive = async (
filename,
{ numberOfPrimitives = 8, mode = 0 },
dimensions
) => {
const primitiveTempFile = tempy.file({ extension: 'svg' })

await execa(primitiveExecutable, [
const { stdout } = await execa(primitiveExecutable, [
'-i',
filename,
'-o',
primitiveTempFile,
'-',
'-n',
numberOfPrimitives,
'-m',
Expand All @@ -58,13 +56,7 @@ const runPrimitive = async (
findLargerImageDimension(dimensions)
])

const result = await fs.readFile(primitiveTempFile, {
encoding: 'utf-8'
})

await fs.unlink(primitiveTempFile)

return result
return stdout
}

module.exports = {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/utils/__snapshots__/primitive.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Array [
"-i",
"/path/to/input/file.jpg",
"-o",
"/path/to/output/primitive-tempfile.svg",
"-",
"-n",
8,
"-m",
Expand All @@ -27,7 +27,7 @@ Array [
"-i",
"/path/to/input/file.jpg",
"-o",
"/path/to/output/primitive-tempfile.svg",
"-",
"-n",
8,
"-m",
Expand All @@ -45,7 +45,7 @@ Array [
"-i",
"/path/to/input/file.jpg",
"-o",
"/path/to/output/primitive-tempfile.svg",
"-",
"-n",
8,
"-m",
Expand Down
Loading

0 comments on commit 438845d

Please sign in to comment.