Try to use assets store for icons before infer

Also promisified inferIcon
This commit is contained in:
Jia Hao 2016-03-11 12:40:08 +08:00
parent b09021dc3a
commit 4c708fac26
4 changed files with 90 additions and 30 deletions

View File

@ -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",

View File

@ -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
};

View File

@ -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;

View File

@ -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