@@ -4,11 +4,16 @@ const color = require('color');
44const fs = require('fs');
55const Hugo = require('alfred-hugo');
66const path = require('path');
7- const svgexport = require('svgexport');
87
98const octicons = require('octicons');
109const octiconsPath = path.join(path.dirname(require.resolve('octicons')), 'build', 'svg');
1110
11+ // Svgexport is optional
12+ var svgexport = null;
13+ try {
14+ svgexport = require('svgexport') || null;
15+ } catch (err) {}
16+
1217class Icons {
1318 /**
1419 * SVG icon path color
@@ -31,35 +36,25 @@ class Icons {
3136 }
3237
3338 /**
34- * Rebuild icon(s)
39+ * Get list of used icons in projects
3540 * @param {Array.Object} projects
36- * @param {Object} options
37- * @return {Promise}
38- * @async
41+ * @return {Array.string}
3942 */
40- async rebuild(projects: Array<Object>, options: Object = {}) {
41- // Output path
42- const iconPath = path.join(__dirname, 'icons');
43+ usedIcons(projects: Array<Object>): Array<string> {
44+ let icons = [];
4345
4446 // Octicon names
4547 const octiconNames = Object.keys(octicons);
4648
47- // Icons to render
48- let icons = [];
49-
50- // Icon size
51- let iconSize = 64;
52-
53- // Render queue
54- let renderQueue = [];
49+ // Output path
50+ const iconPath = path.join(__dirname, 'icons');
5551
56- // Get used icons from projects list
57- for (let i = 0; i < projects.length; i++) {
58- if (!projects[i].hasOwnProperty('icon')) {
52+ for (let project of projects) {
53+ if (!project.hasOwnProperty('icon')) {
5954 continue;
6055 }
6156
62- let icon = path.parse(projects[i] .icon.path);
57+ let icon = path.parse(project .icon.path);
6358
6459 if (icon.dir === iconPath && octiconNames.indexOf(icon.name) >= 0) {
6560 if (icons.indexOf(icon.name) === -1) {
@@ -68,6 +63,47 @@ class Icons {
6863 }
6964 }
7065
66+ return icons;
67+ }
68+
69+ /**
70+ * Check icon building dependencies
71+ * @return {boolean}
72+ */
73+ checkDependencies(): boolean {
74+ try {
75+ require.resolve('svgexport');
76+ } catch (err) {
77+ return false;
78+ }
79+
80+ return true;
81+ }
82+
83+ /**
84+ * Rebuild icon(s)
85+ * @param {Array.Object} projects
86+ * @param {Object} options
87+ * @return {Promise}
88+ * @async
89+ */
90+ async rebuild(projects: Array<Object>, options: Object = {}) {
91+ if (!svgexport) {
92+ return false;
93+ }
94+
95+ // Output path
96+ const iconPath = path.join(__dirname, 'icons');
97+
98+ // Icon size
99+ let iconSize = 64;
100+
101+ // Render queue
102+ let renderQueue = [];
103+
104+ // Get used icons from projects list
105+ let icons = this.usedIcons(projects);
106+
71107 // Filter icons
72108 if (options && options.onlyMissing === true) {
73109 icons = icons.filter(icon => {
@@ -84,19 +120,16 @@ class Icons {
84120 return;
85121 }
86122
87- if (Hugo.alfredThemeFile) {
88- try {
89- iconSize = Hugo.alfredTheme.result.iconSize;
90- } catch (e) {}
123+ // Icon size if set by theme
124+ if (Hugo.alfredTheme.result && Hugo.alfredTheme.result.iconSize) {
125+ iconSize = Hugo.alfredTheme.result.iconSize;
91126 }
92127
93128 // Render options
94129 let renderOptions = [iconSize + ':' + iconSize, 'pad', 'path{fill:' + this._pathColor() + '}'];
95130
96131 // Build render queue
97- for (let i = 0; i < icons.length; i++) {
98- let icon = icons[i];
99-
132+ for (let icon of icons) {
100133 if (icon && icon.length > 0) {
101134 renderQueue.push({
102135 input: path.join(octiconsPath, icon + '.svg'),
@@ -107,6 +140,11 @@ class Icons {
107140 }
108141 }
109142
143+ // Show notification
144+ Hugo.notify({
145+ message: `Rebuilding ${renderQueue.length} project icons, please wait.`
146+ });
147+
110148 // Render
111149 svgexport.render(renderQueue, err => {
112150 if (err) {
0 commit comments