Skip to content

Commit

Permalink
refactor(vidavidorra-logo): use Options interface for consturctor
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbruijn committed Aug 2, 2020
1 parent 55a5f89 commit 66f961a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
34 changes: 11 additions & 23 deletions src/vidavidorra-logo/cli.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import * as validator from '../helpers/validator';
import { VidavidorraLogo } from './vidavidorra-logo';
import VidavidorraLogo, { Options } from './vidavidorra-logo';
import chalk from 'chalk';
import inquirer from 'inquirer';

interface Arguments {
outputDirectory: string;
height: number;
lineThickness: number;
colour: string;
pngHeight: number;
pngSquare: boolean;
}

const questions = [
{
type: 'input',
Expand Down Expand Up @@ -48,10 +39,14 @@ const questions = [
default: 2160,
},
{
type: 'boolean',
name: 'pngSquare',
message: 'Do you want the PNG to be square?',
default: false,
type: 'list',
name: 'pngFormat',
message: 'What should the format of the PNG output be?',
choices: [
{ name: 'Rectangle', value: 'rectangle' },
{ name: 'Square', value: 'square' },
],
default: 'rectangle',
},
];

Expand All @@ -61,15 +56,8 @@ ui.log.write(
chalk.yellow('⚠ Note that these default settings are for the standard logo!'),
);

inquirer.prompt<Arguments>(questions).then((answers) => {
const vidavidorraLogo = new VidavidorraLogo(
answers.outputDirectory,
answers.height,
answers.lineThickness,
answers.colour,
answers.pngHeight,
answers.pngSquare,
);
inquirer.prompt<Options>(questions).then((answers) => {
const vidavidorraLogo = new VidavidorraLogo(answers);

return vidavidorraLogo.create();
});
47 changes: 29 additions & 18 deletions src/vidavidorra-logo/vidavidorra-logo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ import mustache from 'mustache';
import path from 'path';
import sharp from 'sharp';

export const tool = new Tool(
const tool = new Tool(
'vidavidorra-logo',
'Create the vidavidorra logo as SVG.',
);

interface Options {
outputDirectory: string;
height: number;
lineThickness: number;
colour: string;
pngHeight: number;
pngFormat: 'rectangle' | 'square';
}

/**
* See the logo.md document in this directory for a simplified ASCII
* representation and description of the logo. That document also contains the
Expand All @@ -33,16 +42,9 @@ export class VidavidorraLogo {
private doubleVPoints: Points;
private singleVPoints: Points;

constructor(
private outputDirectory: string,
height: number, // H in this class.
lineThickness: number, // T in this class.
private colour: string,
private pngHeight: number,
private pngSquare: boolean,
) {
this.H = height;
this.T = lineThickness;
constructor(private options: Options) {
this.H = this.options.height;
this.T = this.options.lineThickness;
this.PT = this.T / 2;
this.HS = Math.sqrt(this.T ** 2 + (0.5 * this.T) ** 2);
this.VS = this.HS * 2;
Expand Down Expand Up @@ -98,7 +100,7 @@ export class VidavidorraLogo {
const view = {
width: this.maximumWidth(),
height: this.maximumHeight(),
colour: this.colour,
colour: this.options.colour,
doubleVData: this.singleVPoints.toSvgPathData(
this.svgPathDataIndentation,
),
Expand All @@ -111,7 +113,7 @@ export class VidavidorraLogo {
this.svg = mustache.render(template, view);

fs.writeFileSync(
path.join(this.outputDirectory, `${this.name}.svg`),
path.join(this.options.outputDirectory, `${this.name}.svg`),
this.svg,
);
}
Expand All @@ -120,10 +122,11 @@ export class VidavidorraLogo {
const svgHeight = Math.ceil(this.maximumHeight());

const resizeOptions: sharp.ResizeOptions = {
height: this.pngHeight,
height: this.options.pngHeight,
};
if (this.pngSquare) {
resizeOptions.width = this.pngHeight;

if (this.options.pngFormat === 'square') {
resizeOptions.width = this.options.pngHeight;
resizeOptions.fit = 'contain';
resizeOptions.background = { r: 0, g: 0, b: 0, alpha: 0 };
}
Expand All @@ -138,15 +141,20 @@ export class VidavidorraLogo {
return new Promise((resolve) => {
try {
sharp(Buffer.from(this.svg), {
density: Math.ceil((defaultDensity * this.pngHeight) / svgHeight),
density: Math.ceil(
(defaultDensity * this.options.pngHeight) / svgHeight,
),
})
.resize(resizeOptions)
.png()
.toBuffer({ resolveWithObject: true })
.then(({ data, info }) => {
console.log(
`Output: ${this.name}-${info.width}x${info.height}.png`,
);
fs.writeFileSync(
path.join(
this.outputDirectory,
this.options.outputDirectory,
`${this.name}-${info.width}x${info.height}.png`,
),
data,
Expand Down Expand Up @@ -175,3 +183,6 @@ export class VidavidorraLogo {
);
}
}

export default VidavidorraLogo;
export { tool, Options };

0 comments on commit 66f961a

Please sign in to comment.