2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-06-25 23:03:15 +00:00
nativefier/src/infer/inferIcon.js

149 lines
4.0 KiB
JavaScript
Raw Normal View History

import pageIcon from 'page-icon';
2016-01-28 13:13:57 +00:00
import path from 'path';
import fs from 'fs';
import tmp from 'tmp';
import gitCloud from 'gitcloud';
import helpers from './../helpers/helpers';
2016-03-12 07:17:03 +00:00
const {downloadFile, allowedIconFormats} = helpers;
tmp.setGracefulCleanup();
const GITCLOUD_SPACE_DELIMITER = '-';
function inferIconFromStore(targetUrl, platform) {
2016-03-12 07:17:03 +00:00
const allowedFormats = allowedIconFormats(platform);
return gitCloud('http://jiahaog.com/nativefier-icons/')
.then(fileIndex => {
const iconWithScores = mapIconWithMatchScore(fileIndex, targetUrl);
const maxScore = getMaxMatchScore(iconWithScores);
if (maxScore === 0) {
return null;
}
const matchingIcons = getMatchingIcons(iconWithScores, maxScore);
2016-03-12 07:17:03 +00:00
let matchingUrl;
for (let format of allowedFormats) {
for (let icon of matchingIcons) {
if (icon.ext !== format) {
continue;
}
matchingUrl = icon.url;
}
}
if (!matchingUrl) {
return null;
}
return downloadFile(matchingUrl);
});
}
function mapIconWithMatchScore(fileIndex, targetUrl) {
const normalisedTargetUrl = targetUrl.toLowerCase();
return fileIndex
.map(item => {
const itemWords = item.name.split(GITCLOUD_SPACE_DELIMITER);
const score = itemWords.reduce((currentScore, word) => {
if (normalisedTargetUrl.includes(word)) {
return currentScore + 1;
}
return currentScore;
}, 0);
return Object.assign({},
item,
{score}
);
});
}
function getMaxMatchScore(iconWithScores) {
return iconWithScores.reduce((maxScore, currentIcon) => {
const currentScore = currentIcon.score;
if (currentScore > maxScore) {
return currentScore;
}
return maxScore;
}, 0);
}
/**
* also maps ext to icon object
*/
function getMatchingIcons(iconWithScores, maxScore) {
return iconWithScores
.filter(item => {
return item.score === maxScore;
})
.map(item => {
return Object.assign(
{},
item,
{ext: path.extname(item.url)}
);
});
}
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';
2016-03-09 06:50:25 +00:00
if (platform === 'win32') {
preferredExt = '.ico';
2016-03-09 06:50:25 +00:00
}
2016-03-12 07:17:03 +00:00
// todo might want to pass list of preferences instead
return pageIcon(targetUrl, {ext: preferredExt})
.then(icon => {
if (!icon) {
2016-03-12 07:17:03 +00:00
return null;
}
2016-03-12 07:17:03 +00:00
const outfilePath = path.join(outDir, `/icon${icon.ext}`);
return writeFilePromise(outfilePath, icon.data);
});
2016-01-28 13:13:57 +00:00
}
/**
*
* @param {string} targetUrl
* @param {string} platform
* @param {string} outDir
2016-01-28 13:13:57 +00:00
*/
function inferIconFromUrlToPath(targetUrl, platform, outDir) {
return inferIconFromStore(targetUrl, platform)
2016-03-12 07:17:03 +00:00
.then(icon => {
if (!icon) {
return inferFromPage(targetUrl, platform, outDir);
}
2016-03-12 07:17:03 +00:00
const outfilePath = path.join(outDir, `/icon${icon.ext}`);
return writeFilePromise(outfilePath, icon.data);
});
}
2016-01-28 13:13:57 +00:00
/**
* @param {string} targetUrl
2016-03-09 06:50:25 +00:00
* @param {string} platform
2016-01-28 13:13:57 +00:00
*/
function inferIcon(targetUrl, platform) {
2016-01-28 13:13:57 +00:00
const tmpObj = tmp.dirSync({unsafeCleanup: true});
const tmpPath = tmpObj.name;
return inferIconFromUrlToPath(targetUrl, platform, tmpPath);
2016-01-28 13:13:57 +00:00
}
export default inferIcon;