2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-09-28 20:29:04 +00:00
nativefier/src/infer/inferIcon.js

48 lines
1.1 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';
tmp.setGracefulCleanup();
/**
*
* @param {string} targetUrl
2016-03-09 06:50:25 +00:00
* @param {string} platform
2016-01-28 13:13:57 +00:00
* @param {string} outDir
* @param {inferIconCallback} callback
*/
2016-03-09 06:50:25 +00:00
function inferIconFromUrlToPath(targetUrl, platform, outDir, callback) {
let preferredExt = 'png';
if (platform === 'win32') {
preferredExt = 'ico';
}
pageIcon(targetUrl, {ext: preferredExt})
.then(icon => {
2016-03-08 15:57:19 +00:00
const outfilePath = path.join(outDir, `/icon.${icon.ext}`);
fs.writeFile(outfilePath, icon.data, error => {
2016-01-28 13:13:57 +00:00
callback(error, outfilePath);
});
})
.catch(callback);
2016-01-28 13:13:57 +00:00
}
/**
* @callback inferIconCallback
* @param error
* @param {string} [iconPath]
*/
/**
* @param {string} targetUrl
2016-03-09 06:50:25 +00:00
* @param {string} platform
2016-01-28 13:13:57 +00:00
* @param {inferIconCallback} callback
*/
2016-03-09 06:50:25 +00:00
function inferIcon(targetUrl, platform, callback) {
2016-01-28 13:13:57 +00:00
const tmpObj = tmp.dirSync({unsafeCleanup: true});
const tmpPath = tmpObj.name;
2016-03-09 06:50:25 +00:00
inferIconFromUrlToPath(targetUrl, platform, tmpPath, callback);
2016-01-28 13:13:57 +00:00
}
export default inferIcon;