diff --git a/package.json b/package.json index db46b9e..214290c 100644 --- a/package.json +++ b/package.json @@ -24,9 +24,11 @@ "homepage": "https://github.com/jiahaog/nativefier#readme", "dependencies": { "async": "^1.5.2", + "cheerio": "^0.19.0", "commander": "^2.9.0", "electron-packager": "^5.2.1", "ncp": "^2.0.0", + "request": "^2.67.0", "tmp": "0.0.28", "validator": "^4.5.0" }, diff --git a/src/cli.js b/src/cli.js index 2bfb826..90e7489 100755 --- a/src/cli.js +++ b/src/cli.js @@ -2,6 +2,7 @@ import path from 'path'; import program from 'commander'; +import async from 'async'; import optionsFactory from './options'; import buildApp from './buildApp'; @@ -9,21 +10,28 @@ import buildApp from './buildApp'; const packageJson = require(path.join('..', 'package')); function main(program) { - const options = optionsFactory( - program.appName, - program.targetUrl, - program.platform, - program.arch, - program.electronVersion, - program.outDir, - program.overwrite, - program.conceal, - program.icon, - program.badge, - program.width, - program.height); - buildApp(options, (error, appPath) => { + async.waterfall([ + callback => { + optionsFactory( + program.appName, + program.targetUrl, + program.platform, + program.arch, + program.electronVersion, + program.outDir, + program.overwrite, + program.conceal, + program.icon, + program.badge, + program.width, + program.height, callback); + }, + + (options, callback) => { + buildApp(options, callback); + } + ], (error, appPath) => { if (error) { console.error(error); return; diff --git a/src/options.js b/src/options.js index 50ceaf5..b00d968 100644 --- a/src/options.js +++ b/src/options.js @@ -1,12 +1,15 @@ import os from 'os'; import path from 'path'; +import request from 'request'; +import cheerio from 'cheerio'; import validator from 'validator'; const TEMPLATE_APP_DIR = path.join(__dirname, '../', 'app'); const ELECTRON_VERSION = '0.36.4'; +const DEFAULT_APP_NAME = 'My App'; -function optionsFactory(name = 'MyApp', +function optionsFactory(name, targetUrl = 'http://google.com', platform = detectPlatform(), arch = detectArch(), @@ -17,13 +20,13 @@ function optionsFactory(name = 'MyApp', icon, badge = false, width = 1280, - height = 800) { + height = 800, callback) { if (!validator.isURL(targetUrl, {require_protocol: true})) { throw 'Your Url is invalid!, did you remember to include \'http://\'?'; } - return { + const options = { dir: TEMPLATE_APP_DIR, name: name, @@ -45,6 +48,23 @@ function optionsFactory(name = 'MyApp', width: width, height: height } + + if (name && name.length > 0) { + options.name = name; + callback(null, options); + return; + } + + getTitle(options.targetUrl, function (error, pageTitle) { + if (error) { + console.warn(`Unable to automatically determine app name, falling back to '${DEFAULT_APP_NAME}'`); + options.name = DEFAULT_APP_NAME; + } else { + options.name = pageTitle; + } + + callback(null, options); + }); } function detectPlatform() { @@ -65,4 +85,17 @@ function detectArch() { return os.arch(); } +function getTitle(url, callback) { + request(url, (error, response, body) => { + if (error || response.statusCode !== 200) { + callback(`Request Error: ${error}, Status Code ${response.statusCode}`); + return; + } + + const $ = cheerio.load(body); + const pageTitle = $("title").text(); + callback(null, pageTitle); + }); +} + export default optionsFactory;