Skip to content

Commit

Permalink
Merge branch 'release/v2.3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeboer committed Jul 25, 2017
2 parents 47fa94d + 88daa2a commit cea030b
Show file tree
Hide file tree
Showing 13 changed files with 5,075 additions and 120 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
icons/
persist/
npm-debug.log
2 changes: 1 addition & 1 deletion icons.js

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

92 changes: 65 additions & 27 deletions icons.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ const color = require('color');
const fs = require('fs');
const Hugo = require('alfred-hugo');
const path = require('path');
const svgexport = require('svgexport');

const octicons = require('octicons');
const octiconsPath = path.join(path.dirname(require.resolve('octicons')), 'build', 'svg');

// Svgexport is optional
var svgexport = null;
try {
svgexport = require('svgexport') || null;
} catch (err) {}

class Icons {
/**
* SVG icon path color
Expand All @@ -31,35 +36,25 @@ class Icons {
}

/**
* Rebuild icon(s)
* Get list of used icons in projects
* @param {Array.Object} projects
* @param {Object} options
* @return {Promise}
* @async
* @return {Array.string}
*/
async rebuild(projects: Array<Object>, options: Object = {}) {
// Output path
const iconPath = path.join(__dirname, 'icons');
usedIcons(projects: Array<Object>): Array<string> {
let icons = [];

// Octicon names
const octiconNames = Object.keys(octicons);

// Icons to render
let icons = [];

// Icon size
let iconSize = 64;

// Render queue
let renderQueue = [];
// Output path
const iconPath = path.join(__dirname, 'icons');

// Get used icons from projects list
for (let i = 0; i < projects.length; i++) {
if (!projects[i].hasOwnProperty('icon')) {
for (let project of projects) {
if (!project.hasOwnProperty('icon')) {
continue;
}

let icon = path.parse(projects[i].icon.path);
let icon = path.parse(project.icon.path);

if (icon.dir === iconPath && octiconNames.indexOf(icon.name) >= 0) {
if (icons.indexOf(icon.name) === -1) {
Expand All @@ -68,6 +63,47 @@ class Icons {
}
}

return icons;
}

/**
* Check icon building dependencies
* @return {boolean}
*/
checkDependencies(): boolean {
try {
require.resolve('svgexport');
} catch (err) {
return false;
}

return true;
}

/**
* Rebuild icon(s)
* @param {Array.Object} projects
* @param {Object} options
* @return {Promise}
* @async
*/
async rebuild(projects: Array<Object>, options: Object = {}) {
if (!svgexport) {
return false;
}

// Output path
const iconPath = path.join(__dirname, 'icons');

// Icon size
let iconSize = 64;

// Render queue
let renderQueue = [];

// Get used icons from projects list
let icons = this.usedIcons(projects);

// Filter icons
if (options && options.onlyMissing === true) {
icons = icons.filter(icon => {
Expand All @@ -84,19 +120,16 @@ class Icons {
return;
}

if (Hugo.alfredThemeFile) {
try {
iconSize = Hugo.alfredTheme.result.iconSize;
} catch (e) {}
// Icon size if set by theme
if (Hugo.alfredTheme.result && Hugo.alfredTheme.result.iconSize) {
iconSize = Hugo.alfredTheme.result.iconSize;
}

// Render options
let renderOptions = [iconSize + ':' + iconSize, 'pad', 'path{fill:' + this._pathColor() + '}'];

// Build render queue
for (let i = 0; i < icons.length; i++) {
let icon = icons[i];

for (let icon of icons) {
if (icon && icon.length > 0) {
renderQueue.push({
input: path.join(octiconsPath, icon + '.svg'),
Expand All @@ -107,6 +140,11 @@ class Icons {
}
}

// Show notification
Hugo.notify({
message: `Rebuilding ${renderQueue.length} project icons, please wait.`
});

// Render
svgexport.render(renderQueue, err => {
if (err) {
Expand Down
Loading

0 comments on commit cea030b

Please sign in to comment.