diff --git a/package.json b/package.json index b15f7ab..9ef8fa3 100644 --- a/package.json +++ b/package.json @@ -37,9 +37,11 @@ "homepage": "https://github.com/jiahaog/nativefier#readme", "dependencies": { "async": "^1.5.2", + "axios": "^0.9.1", "cheerio": "^0.19.0", "commander": "^2.9.0", "electron-packager": "^5.2.1", + "gitcloud": "0.0.1", "hasbin": "^1.2.0", "lodash": "^4.0.0", "ncp": "^2.0.0", diff --git a/src/helpers/helpers.js b/src/helpers/helpers.js index abe502b..730a1a2 100644 --- a/src/helpers/helpers.js +++ b/src/helpers/helpers.js @@ -1,4 +1,5 @@ import os from 'os'; +import axios from 'axios'; function isOSX() { return os.platform() === 'darwin'; @@ -8,7 +9,18 @@ function isWindows() { return os.platform() === 'win32'; } +function downloadFile(fileUrl) { + return axios.get( + fileUrl, { + responseType: 'arraybuffer' + }) + .then(function(response) { + return response.data; + }); +} + export default { - isOSX: isOSX, - isWindows: isWindows + isOSX, + isWindows, + downloadFile }; diff --git a/src/infer/inferIcon.js b/src/infer/inferIcon.js index 6700ee9..009566d 100644 --- a/src/infer/inferIcon.js +++ b/src/infer/inferIcon.js @@ -2,49 +2,94 @@ import pageIcon from 'page-icon'; import path from 'path'; import fs from 'fs'; import tmp from 'tmp'; +import gitCloud from 'gitcloud'; tmp.setGracefulCleanup(); -/** - * - * @param {string} targetUrl - * @param {string} platform - * @param {string} outDir - * @param {inferIconCallback} callback - */ -function inferIconFromUrlToPath(targetUrl, platform, outDir, callback) { +import helpers from './../helpers/helpers'; +const {downloadFile} = helpers; + +function inferIconFromStore(targetUrl, platform) { + if (platform === 'win32') { + return new Promise((resolve, reject) => reject('Skipping icon retrieval from store on windows')); + } + + return gitCloud('http://jiahaog.com/nativefier-icons/') + .then(fileIndex => { + const matchingUrls = fileIndex + .filter(item => { + return targetUrl + .toLowerCase() + .includes(item.name); + }) + .map(item => item.url); + + const matchingUrl = matchingUrls[0]; + if (!matchingUrl) { + return null; + } + return downloadFile(matchingUrl); + }); +} + +function writeFilePromise(outPath, data) { + return new Promise((resolve, reject) => { + fs.writeFile(outPath, data, error => { + if (error) { + reject(error); + return; + } + resolve(outPath); + }); + }); +} + +function inferFromPage(targetUrl, platform, outDir) { let preferredExt = 'png'; if (platform === 'win32') { preferredExt = 'ico'; } - pageIcon(targetUrl, {ext: preferredExt}) + return pageIcon(targetUrl, {ext: preferredExt}) .then(icon => { if (!icon) { throw 'Icon not found'; } const outfilePath = path.join(outDir, `/icon.${icon.ext}`); - fs.writeFile(outfilePath, icon.data, error => { - callback(error, outfilePath); - }); - }) - .catch(callback); + return writeFilePromise(outfilePath, icon.data); + }); } - /** - * @callback inferIconCallback - * @param error - * @param {string} [iconPath] + * + * @param {string} targetUrl + * @param {string} platform + * @param {string} outDir */ +function inferIconFromUrlToPath(targetUrl, platform, outDir) { + + return inferIconFromStore(targetUrl, platform) + .then(iconData => { + + if (!iconData) { + throw 'Unable to find icon from store'; + } + + const outfilePath = path.join(outDir, `/icon.png`); + return writeFilePromise(outfilePath, iconData); + }).catch(error => { + console.warn('Unable to find icon on store', error); + console.warn('Falling back to inferring from url'); + return inferFromPage(targetUrl, platform, outDir); + }); +} /** * @param {string} targetUrl * @param {string} platform - * @param {inferIconCallback} callback */ -function inferIcon(targetUrl, platform, callback) { +function inferIcon(targetUrl, platform) { const tmpObj = tmp.dirSync({unsafeCleanup: true}); const tmpPath = tmpObj.name; - inferIconFromUrlToPath(targetUrl, platform, tmpPath, callback); + return inferIconFromUrlToPath(targetUrl, platform, tmpPath); } export default inferIcon; diff --git a/src/options/optionsMain.js b/src/options/optionsMain.js index 3392bcc..1d80ed3 100644 --- a/src/options/optionsMain.js +++ b/src/options/optionsMain.js @@ -63,14 +63,15 @@ function optionsFactory(inpOptions, callback) { callback(); return; } - inferIcon(options.targetUrl, options.platform, (error, pngPath) => { - if (error) { - console.warn('Cannot automatically retrieve the app icon:', error); - } else { + inferIcon(options.targetUrl, options.platform) + .then(pngPath => { options.icon = pngPath; - } - callback(); - }); + callback(); + }) + .catch(error => { + console.warn('Cannot automatically retrieve the app icon:', error); + callback(); + }); }, callback => { // length also checks if its the commanderJS function or a string