2016-03-08 13:29:04 +01:00
|
|
|
import pageIcon from 'page-icon';
|
2016-01-28 14:13:57 +01:00
|
|
|
import path from 'path';
|
|
|
|
import fs from 'fs';
|
|
|
|
import tmp from 'tmp';
|
2016-03-11 05:40:08 +01:00
|
|
|
import gitCloud from 'gitcloud';
|
|
|
|
import helpers from './../helpers/helpers';
|
2016-03-12 08:17:03 +01:00
|
|
|
|
|
|
|
const {downloadFile, allowedIconFormats} = helpers;
|
|
|
|
tmp.setGracefulCleanup();
|
2016-03-11 05:40:08 +01:00
|
|
|
|
|
|
|
function inferIconFromStore(targetUrl, platform) {
|
2016-03-12 08:17:03 +01:00
|
|
|
const allowedFormats = allowedIconFormats(platform);
|
2016-03-11 05:40:08 +01:00
|
|
|
|
|
|
|
return gitCloud('http://jiahaog.com/nativefier-icons/')
|
|
|
|
.then(fileIndex => {
|
2016-03-12 08:17:03 +01:00
|
|
|
const matchingIcons = fileIndex
|
2016-03-11 05:40:08 +01:00
|
|
|
.filter(item => {
|
2016-03-12 08:17:03 +01:00
|
|
|
// todo might have problems with matching length, e.g. `book` vs `facebook`
|
2016-03-11 05:40:08 +01:00
|
|
|
return targetUrl
|
|
|
|
.toLowerCase()
|
|
|
|
.includes(item.name);
|
|
|
|
})
|
2016-03-12 08:17:03 +01:00
|
|
|
.map(item => {
|
|
|
|
item.ext = path.extname(item.url);
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
|
|
|
|
let matchingUrl;
|
|
|
|
for (let format of allowedFormats) {
|
|
|
|
for (let icon of matchingIcons) {
|
|
|
|
if (icon.ext !== format) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
matchingUrl = icon.url;
|
|
|
|
}
|
|
|
|
}
|
2016-03-11 05:40:08 +01:00
|
|
|
|
|
|
|
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) {
|
2016-03-14 05:28:14 +01:00
|
|
|
let preferredExt = '.png';
|
2016-03-09 07:50:25 +01:00
|
|
|
if (platform === 'win32') {
|
2016-03-14 05:28:14 +01:00
|
|
|
preferredExt = '.ico';
|
2016-03-09 07:50:25 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 08:17:03 +01:00
|
|
|
// todo might want to pass list of preferences instead
|
2016-03-11 05:40:08 +01:00
|
|
|
return pageIcon(targetUrl, {ext: preferredExt})
|
2016-03-08 13:29:04 +01:00
|
|
|
.then(icon => {
|
2016-03-10 08:52:02 +01:00
|
|
|
if (!icon) {
|
2016-03-12 08:17:03 +01:00
|
|
|
return null;
|
2016-03-10 08:52:02 +01:00
|
|
|
}
|
2016-03-12 08:17:03 +01:00
|
|
|
|
2016-03-14 05:28:14 +01:00
|
|
|
const outfilePath = path.join(outDir, `/icon${icon.ext}`);
|
2016-03-11 05:40:08 +01:00
|
|
|
return writeFilePromise(outfilePath, icon.data);
|
|
|
|
});
|
2016-01-28 14:13:57 +01:00
|
|
|
}
|
|
|
|
/**
|
2016-03-11 05:40:08 +01:00
|
|
|
*
|
|
|
|
* @param {string} targetUrl
|
|
|
|
* @param {string} platform
|
|
|
|
* @param {string} outDir
|
2016-01-28 14:13:57 +01:00
|
|
|
*/
|
2016-03-11 05:40:08 +01:00
|
|
|
function inferIconFromUrlToPath(targetUrl, platform, outDir) {
|
|
|
|
|
|
|
|
return inferIconFromStore(targetUrl, platform)
|
2016-03-12 08:17:03 +01:00
|
|
|
.then(icon => {
|
|
|
|
if (!icon) {
|
|
|
|
return inferFromPage(targetUrl, platform, outDir);
|
2016-03-11 05:40:08 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 08:17:03 +01:00
|
|
|
const outfilePath = path.join(outDir, `/icon${icon.ext}`);
|
|
|
|
return writeFilePromise(outfilePath, icon.data);
|
2016-03-11 05:40:08 +01:00
|
|
|
});
|
|
|
|
}
|
2016-01-28 14:13:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} targetUrl
|
2016-03-09 07:50:25 +01:00
|
|
|
* @param {string} platform
|
2016-01-28 14:13:57 +01:00
|
|
|
*/
|
2016-03-11 05:40:08 +01:00
|
|
|
function inferIcon(targetUrl, platform) {
|
2016-01-28 14:13:57 +01:00
|
|
|
const tmpObj = tmp.dirSync({unsafeCleanup: true});
|
|
|
|
const tmpPath = tmpObj.name;
|
2016-03-11 05:40:08 +01:00
|
|
|
return inferIconFromUrlToPath(targetUrl, platform, tmpPath);
|
2016-01-28 14:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default inferIcon;
|