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

Commit

Permalink
feat: Support exporting to types that aren't png, or what the templ…
Browse files Browse the repository at this point in the history
…ate is.
  • Loading branch information
randytarampi committed Mar 12, 2019
1 parent 67fbab9 commit ddc62b6
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 23 deletions.
63 changes: 40 additions & 23 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,46 @@ const colors = require("colors");

const errorMessage = error => console.error(colors.red("ERROR"), typeof error === "string" ? error : error.message); // eslint-disable-line no-console

const generateAssetDestination = (outputDirectory, template) => path.join(outputDirectory, template.name);

const generateIcon = (image, outputDirectory, iconTemplate) => image
.clone()
.resize(iconTemplate.width, iconTemplate.height || iconTemplate.width)
.toFile(generateAssetDestination(outputDirectory, iconTemplate))
.then(() => {
console.info(colors.green("OK"), "Image resized to", iconTemplate.width, "x", // eslint-disable-line no-console
iconTemplate.height || iconTemplate.width, "and written to", outputDirectory);
})
.catch(errorMessage);

const generateSplashScreen = (image, outputDirectory, splashScreenTemplate) => image
.clone()
.resize(splashScreenTemplate.width, splashScreenTemplate.height || splashScreenTemplate.width)
.toFile(generateAssetDestination(outputDirectory, splashScreenTemplate))
.then(() => {
console.info(colors.green("OK"), "Image scaled and cropped to", splashScreenTemplate.width, "x", // eslint-disable-line no-console
splashScreenTemplate.height || splashScreenTemplate.width, "and written to", outputDirectory);
})
.catch(errorMessage);
const getFileFormatFromFilename = filename => {
const extname = path.extname(filename);
return extname && extname.slice(1);
};

const generateAssetDestination = (outputDirectory, template, format) => {
return path.join(outputDirectory, path.basename(template.name, path.extname(template.name)) + "." + format);
};

const generateIcon = (image, outputDirectory, iconTemplate, format) => {
const outputFormat = format || getFileFormatFromFilename(iconTemplate.name);

return image
.clone()
.resize(iconTemplate.width, iconTemplate.height || iconTemplate.width)
.toFormat(outputFormat)
.toFile(generateAssetDestination(outputDirectory, iconTemplate, outputFormat))
.then(() => {
console.info(colors.green("OK"), "Image resized to", iconTemplate.width, "x", // eslint-disable-line no-console
iconTemplate.height || iconTemplate.width, "and written to", outputDirectory);
})
.catch(errorMessage);
};

const generateSplashScreen = (image, outputDirectory, splashScreenTemplate, format) => {
const outputFormat = format || getFileFormatFromFilename(splashScreenTemplate.name);

return image
.clone()
.resize(splashScreenTemplate.width, splashScreenTemplate.height || splashScreenTemplate.width)
.toFormat(outputFormat)
.toFile(generateAssetDestination(outputDirectory, splashScreenTemplate, outputFormat))
.then(() => {
console.info(colors.green("OK"), "Image scaled and cropped to", splashScreenTemplate.width, "x", // eslint-disable-line no-console
splashScreenTemplate.height || splashScreenTemplate.width, "and written to", outputDirectory);
})
.catch(errorMessage);
};

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

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

module.exports = {
Expand Down
70 changes: 70 additions & 0 deletions test/integration/src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,41 @@ describe("lib", function () {
});
});
});

it("supports a different format", 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");
const format = "webp";

return iconsGenerator(templates, input, output, format)
.then(() => {
templates.forEach(template => {
expect(fs.existsSync(path.join(output, path.basename(template.name, path.extname(template.name)) + "." + format))).to.eql(true);
});
});
});

it("throws on an unsupported format", 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");
const format = "woof";

return iconsGenerator(templates, input, output, format)
.then(() => {
throw new Error("Wtf? This should've thrown");
})
.catch(error => {
expect(error.message).to.eql("Unsupported output format" + " " + format);
});
});
});

describe("iconsGenerator", function () {
Expand All @@ -49,5 +84,40 @@ describe("lib", function () {
});
});
});

it("supports a different format", 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");
const format = "jpeg";

return splashScreensGenerator(templates, input, output, format)
.then(() => {
templates.forEach(template => {
expect(fs.existsSync(path.join(output, path.basename(template.name, path.extname(template.name)) + "." + format))).to.eql(true);
});
});
});

it("throws on an unsupported format", 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");
const format = "woof";

return splashScreensGenerator(templates, input, output, format)
.then(() => {
throw new Error("Wtf? This should've thrown");
})
.catch(error => {
expect(error.message).to.eql("Unsupported output format" + " " + format);
});
});
});
});

0 comments on commit ddc62b6

Please sign in to comment.