Skip to content
This repository has been archived by the owner on May 3, 2020. It is now read-only.

Commit

Permalink
feat: Support generation of assets to plain Buffers.
Browse files Browse the repository at this point in the history
  • Loading branch information
randytarampi committed Mar 17, 2019
1 parent 990511e commit d954ebc
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
42 changes: 35 additions & 7 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,50 @@ const generateAssetDestination = (outputDirectory, template, format) => {
return path.join(outputDirectory, path.basename(template.name, path.extname(template.name)) + "." + format);
};

const generateImageForTemplate = (image, outputDirectory, template, format) => {
const generateImageForTemplateInternal = (image, outputDirectory, template, format) => {
const outputFormat = format || getFileFormatFromFilename(template.name);

return image
.clone()
.resize(template.width, template.height || template.width)
.toFormat(outputFormat)
.toFile(generateAssetDestination(outputDirectory, template, outputFormat))
.then(() => {
logMessage("Image resized to", template.width, "x", template.height || template.width, "and written to",
.toFormat(outputFormat);
};

const generateImageForTemplateToFile = (image, outputDirectory, template, format) => {
const outputFormat = format || getFileFormatFromFilename(template.name);
const assetDestination = generateAssetDestination(outputDirectory, template, outputFormat);

return generateImageForTemplateInternal(image, outputDirectory, template, format)
.toFile(assetDestination)
.then(info => {
logMessage("Image resized to", info.width, "x", info.height, "and written to",
outputDirectory);
})
.catch(errorMessage);
};

const generateAssetsGenerator = generator => (templates, input, outputDirectory, format) => {
const generateImageForTemplateToBuffer = (image, outputDirectory, template, format) => {
return generateImageForTemplateInternal(image, outputDirectory, template, format)
.toBuffer({resolveWithObject: true})
.then(({data, info}) => {
logMessage("Image resized to", info.width, "x", info.height, "and returned as buffer");
return data;
})
.catch(errorMessage);
};

const generateImageForTemplate = (image, outputDirectory, template, format, type) => {
switch (type) {
case "buffer":
return generateImageForTemplateToBuffer(image, outputDirectory, template, format);

case "file":
default:
return generateImageForTemplateToFile(image, outputDirectory, template, format);
}
};

const generateAssetsGenerator = generator => (templates, input, outputDirectory, format, type = "file") => {
const image = sharp(input);

return new Promise((resolve, reject) => {
Expand All @@ -42,7 +70,7 @@ const generateAssetsGenerator = generator => (templates, input, outputDirectory,
resolve();
});
})
.then(() => Promise.all(templates.map(template => generator(image, outputDirectory, template, format))));
.then(() => Promise.all(templates.map(template => generator(image, outputDirectory, template, format, type))));
};

module.exports = {
Expand Down
41 changes: 39 additions & 2 deletions test/integration/src/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const {Buffer} = require("buffer");
const fs = require("fs");
const path = require("path");
const mkdirp = require("mkdirp");
Expand All @@ -19,7 +20,7 @@ describe("lib", function () {
});

describe("iconsGenerator", function () {
it("generates icons for templates", function () {
it("generates icons for templates (to file)", function () {
const templates = [
{ name: "woof.icon.png", width: 100, height: 100 },
{ name: "meow.icon.png", width: 1000, height: 1000 },
Expand All @@ -35,6 +36,24 @@ describe("lib", function () {
});
});

it("generates icons for templates (to buffer)", function () {
const templates = [
{ name: "woof.icon.png", width: 100, height: 100 },
{ name: "meow.icon.png", width: 1000, height: 1000 },
{ name: "grr.splash.png", width: 1000 },
];
const input = path.join(__dirname, "../../../resources/com.appbusinesspodcast.www.icon.png");

return iconsGenerator(templates, input, output, undefined, "buffer")
.then(buffers => {
expect(buffers).to.have.length(templates.length);

buffers.forEach(buffer => {
expect(buffer).to.be.instanceOf(Buffer);
});
});
});

it("supports a different format", function () {
const templates = [
{ name: "woof.icon.png", width: 100, height: 100 },
Expand Down Expand Up @@ -72,7 +91,7 @@ describe("lib", function () {
});

describe("splashScreensGenerator", function () {
it("generates splash screens for templates", function () {
it("generates splash screens for templates (to file)", function () {
const templates = [
{ name: "woof.splash.png", width: 900, height: 1600 },
{ name: "meow.splash.png", width: 1600, height: 1000 },
Expand All @@ -88,6 +107,24 @@ describe("lib", function () {
});
});

it("generates splash screens for templates (to buffer)", function () {
const templates = [
{ name: "woof.splash.png", width: 900, height: 1600 },
{ name: "meow.splash.png", width: 1600, height: 1000 },
{ name: "grr.splash.png", width: 1600 },
];
const input = path.join(__dirname, "../../../resources/com.appbusinesspodcast.www.splash.png");

return splashScreensGenerator(templates, input, output, undefined, "buffer")
.then(buffers => {
expect(buffers).to.have.length(templates.length);

buffers.forEach(buffer => {
expect(buffer).to.be.instanceOf(Buffer);
});
});
});

it("supports a different format", function () {
const templates = [
{ name: "woof.splash.png", width: 900, height: 1600 },
Expand Down

0 comments on commit d954ebc

Please sign in to comment.