Promisify inferTitle module

Also remove the request dependency, use Axios instead
This commit is contained in:
Jia Hao Goh 2017-04-21 00:24:18 +08:00
parent eeaa531083
commit b467ac7a51
3 changed files with 27 additions and 29 deletions

View File

@ -49,7 +49,6 @@
"ncp": "^2.0.0", "ncp": "^2.0.0",
"page-icon": "^0.3.0", "page-icon": "^0.3.0",
"progress": "^2.0.0", "progress": "^2.0.0",
"request": "^2.67.0",
"sanitize-filename": "^1.5.3", "sanitize-filename": "^1.5.3",
"shelljs": "^0.7.0", "shelljs": "^0.7.0",
"source-map-support": "^0.4.0", "source-map-support": "^0.4.0",

View File

@ -1,24 +1,21 @@
import request from 'request'; import axios from 'axios';
import cheerio from 'cheerio'; import cheerio from 'cheerio';
function inferTitle(url, callback) { const USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36';
function inferTitle(url) {
const options = { const options = {
url: url, method: 'get',
url,
headers: { headers: {
// fake a user agent because pages like http://messenger.com will throw 404 error // fake a user agent because pages like http://messenger.com will throw 404 error
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36' 'User-Agent': USER_AGENT
} }
}; };
request(options, (error, response, body) => { return axios(options).then(({data}) => {
if (error || response.statusCode !== 200) { const $ = cheerio.load(data);
callback(`Request Error: ${error}, Status Code ${response ? response.statusCode : 'No Response'}`); return $('title').first().text().replace(/\//g, '');
return;
}
const $ = cheerio.load(body);
const pageTitle = $('title').first().text().replace(/\//g, '');
callback(null, pageTitle);
}); });
} }

View File

@ -136,18 +136,13 @@ function optionsFactory(inpOptions, callback) {
callback(); callback();
return; return;
} }
options.name = DEFAULT_APP_NAME;
inferTitle(options.targetUrl, function(error, pageTitle) { inferTitle(options.targetUrl).then(pageTitle => {
if (error) { options.name = pageTitle;
log.warn(`Unable to automatically determine app name, falling back to '${DEFAULT_APP_NAME}'`); }).catch(error => {
options.name = DEFAULT_APP_NAME; log.warn(`Unable to automatically determine app name, falling back to '${DEFAULT_APP_NAME}'. Reason: ${error}`);
} else { }).then(() => {
options.name = pageTitle.trim();
}
if (options.platform === 'linux') {
// spaces will cause problems with Ubuntu when pinned to the dock
options.name = _.kebabCase(options.name);
}
callback(); callback();
}); });
} }
@ -156,14 +151,21 @@ function optionsFactory(inpOptions, callback) {
}); });
} }
function sanitizeFilename(str) { function sanitizeFilename(platform, str) {
const cleaned = sanitizeFilenameLib(str); let result = sanitizeFilenameLib(str);
// remove all non ascii or use default app name // remove all non ascii or use default app name
return cleaned.replace(/[^\x00-\x7F]/g, '') || DEFAULT_APP_NAME; result = result.replace(/[^\x00-\x7F]/g, '') || DEFAULT_APP_NAME;
// spaces will cause problems with Ubuntu when pinned to the dock
if (platform === 'linux') {
return _.kebabCase(result);
}
return result;
} }
function sanitizeOptions(options) { function sanitizeOptions(options) {
options.name = sanitizeFilename(options.name); options.name = sanitizeFilename(options.platform, options.name);
return options; return options;
} }