-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added the 'init' command (#77)
- added a native copyFile method... needed as this is not in Node 6 - note that node 6 is needed - doc cleanup - fix globbing pattern for specs - move nyc config into package.json, and exclude test helpers
- Loading branch information
Showing
22 changed files
with
244 additions
and
42 deletions.
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
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
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const imagemagickCli = require('imagemagick-cli'); | ||
const getImageWidth = require('../imagemagick/get-image-width'); | ||
const copyFile = require('../utils/copy-file'); | ||
|
||
/** | ||
* init - creates an icon from a template. | ||
* | ||
* @param template | ||
* @param output | ||
* @param options | ||
* @returns a promise which resolves when the icon has been created. | ||
*/ | ||
function init(template, output, options) { | ||
// If there is no caption, then we can just copy the image. | ||
const caption = (options && options.caption) || ''; | ||
if (!caption) return copyFile(template, output); | ||
|
||
// We have a caption, so we'll need to get the image width to work out how | ||
// to arrange it on the icon. | ||
return getImageWidth(template) | ||
.then((width) => { | ||
// The height will be 80% of the width, i.e. the text almost fills the icon. | ||
// TODO: getImageWidth should be getImageDimensions | ||
const w = Math.floor(width * 0.8); | ||
const h = Math.floor(width * 0.8); | ||
|
||
// Create the command to generate the image. | ||
const command = `convert \ | ||
-background "rgba(0,0,0,0)" -fill white \ | ||
-gravity center -size ${w}x${h} \ | ||
caption:"${caption}" \ | ||
${template} +swap -composite ${output}`; | ||
return imagemagickCli.exec(command); | ||
}); | ||
} | ||
|
||
module.exports = init; |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { expect } = require('chai'); | ||
const init = require('./init'); | ||
const compareImages = require('../testing/compare-images'); | ||
|
||
describe('init', () => { | ||
it('should be able to init an icon', () => { | ||
const template = './src/init/icon.template.png'; | ||
const output = './src/init/test-images/init-output.png'; | ||
const reference = './src/init/test-images/init-reference.png'; | ||
return init(template, output) | ||
.then(() => compareImages(output, reference)) | ||
.then((difference) => { | ||
expect(difference).to.be.below(2.5, 'Generated image is below accepted similarly threshold'); | ||
}); | ||
}); | ||
|
||
it('should be able to init an icon with a caption', () => { | ||
const template = './src/init/icon.template.png'; | ||
const output = './src/init/test-images/init-with-caption-output.png'; | ||
const reference = './src/init/test-images/init-with-caption-reference.png'; | ||
return init(template, output, { caption: 'Test' }) | ||
.then(() => compareImages(output, reference)) | ||
.then((difference) => { | ||
expect(difference).to.be.below(2.5, 'Generated image is below accepted similarly threshold'); | ||
}); | ||
}); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.