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",
"page-icon": "^0.3.0",
"progress": "^2.0.0",
"request": "^2.67.0",
"sanitize-filename": "^1.5.3",
"shelljs": "^0.7.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';
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 = {
url: url,
method: 'get',
url,
headers: {
// 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) => {
if (error || response.statusCode !== 200) {
callback(`Request Error: ${error}, Status Code ${response ? response.statusCode : 'No Response'}`);
return;
}
const $ = cheerio.load(body);
const pageTitle = $('title').first().text().replace(/\//g, '');
callback(null, pageTitle);
return axios(options).then(({data}) => {
const $ = cheerio.load(data);
return $('title').first().text().replace(/\//g, '');
});
}

View File

@ -136,18 +136,13 @@ function optionsFactory(inpOptions, callback) {
callback();
return;
}
options.name = DEFAULT_APP_NAME;
inferTitle(options.targetUrl, function(error, pageTitle) {
if (error) {
log.warn(`Unable to automatically determine app name, falling back to '${DEFAULT_APP_NAME}'`);
options.name = DEFAULT_APP_NAME;
} else {
options.name = pageTitle.trim();
}
if (options.platform === 'linux') {
// spaces will cause problems with Ubuntu when pinned to the dock
options.name = _.kebabCase(options.name);
}
inferTitle(options.targetUrl).then(pageTitle => {
options.name = pageTitle;
}).catch(error => {
log.warn(`Unable to automatically determine app name, falling back to '${DEFAULT_APP_NAME}'. Reason: ${error}`);
}).then(() => {
callback();
});
}
@ -156,14 +151,21 @@ function optionsFactory(inpOptions, callback) {
});
}
function sanitizeFilename(str) {
const cleaned = sanitizeFilenameLib(str);
function sanitizeFilename(platform, str) {
let result = sanitizeFilenameLib(str);
// 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) {
options.name = sanitizeFilename(options.name);
options.name = sanitizeFilename(options.platform, options.name);
return options;
}