2016-01-18 15:07:22 +01:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2016-01-21 02:06:04 +01:00
|
|
|
import crypto from 'crypto';
|
2016-01-18 15:07:22 +01:00
|
|
|
|
2016-01-24 13:40:32 +01:00
|
|
|
import optionsFactory from './options';
|
2016-01-18 15:07:22 +01:00
|
|
|
import packager from 'electron-packager';
|
|
|
|
import tmp from 'tmp';
|
|
|
|
import ncp from 'ncp';
|
|
|
|
import async from 'async';
|
2016-01-19 14:19:09 +01:00
|
|
|
import _ from 'lodash';
|
2016-01-18 15:07:22 +01:00
|
|
|
|
2016-01-21 06:42:27 +01:00
|
|
|
import packageJson from './../package.json';
|
|
|
|
|
2016-01-18 15:07:22 +01:00
|
|
|
const copy = ncp.ncp;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback buildAppCallback
|
|
|
|
* @param error
|
|
|
|
* @param appPath
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2016-01-23 19:02:23 +01:00
|
|
|
* @param {{}} options
|
2016-01-18 15:07:22 +01:00
|
|
|
* @param {buildAppCallback} callback
|
|
|
|
*/
|
|
|
|
function buildApp(options, callback) {
|
|
|
|
// pre process app
|
|
|
|
|
2016-01-19 04:58:26 +01:00
|
|
|
var tmpObj = tmp.dirSync({unsafeCleanup: true});
|
|
|
|
const tmpPath = tmpObj.name;
|
2016-01-18 15:07:22 +01:00
|
|
|
|
|
|
|
async.waterfall([
|
2016-01-18 16:56:17 +01:00
|
|
|
callback => {
|
2016-01-24 13:40:32 +01:00
|
|
|
optionsFactory(
|
|
|
|
options.appName,
|
|
|
|
options.targetUrl,
|
|
|
|
options.platform,
|
|
|
|
options.arch,
|
|
|
|
options.electronVersion,
|
|
|
|
options.outDir,
|
|
|
|
options.overwrite,
|
|
|
|
options.conceal,
|
|
|
|
options.icon,
|
|
|
|
options.counter,
|
|
|
|
options.width,
|
|
|
|
options.height,
|
|
|
|
options.showMenuBar,
|
|
|
|
options.userAgent,
|
|
|
|
options.honest,
|
2016-01-25 16:42:28 +01:00
|
|
|
options.insecure,
|
2016-01-24 13:40:32 +01:00
|
|
|
callback);
|
2016-01-18 15:07:22 +01:00
|
|
|
},
|
2016-01-24 13:40:32 +01:00
|
|
|
(options, callback) => {
|
2016-01-25 16:42:28 +01:00
|
|
|
copyPlaceholderApp(options.dir, tmpPath, options.name, options.targetUrl, options.counter, options.width, options.height, options.showMenuBar, options.userAgent, options.insecure, (error, tempDirPath) => {
|
2016-01-24 13:40:32 +01:00
|
|
|
callback(error, tempDirPath, options);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(tempDir, options, callback) => {
|
2016-01-18 15:07:22 +01:00
|
|
|
options.dir = tempDir;
|
|
|
|
packager(options, callback);
|
|
|
|
},
|
2016-01-18 16:56:17 +01:00
|
|
|
|
2016-01-24 18:10:03 +01:00
|
|
|
(appPathArray, callback) => {
|
|
|
|
// somehow appPathArray is a 1 element array
|
2016-01-25 17:37:00 +01:00
|
|
|
if (appPathArray.length > 1) {
|
|
|
|
console.warn('Warning: Packaged app path contains more than one element:', appPathArray);
|
2016-01-24 18:10:03 +01:00
|
|
|
}
|
|
|
|
const appPath = appPathArray[0];
|
2016-01-18 15:07:22 +01:00
|
|
|
callback(null, appPath);
|
|
|
|
}
|
|
|
|
], callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback tempDirCallback
|
|
|
|
* @param error
|
2016-01-18 16:56:17 +01:00
|
|
|
* @param {string} [tempDirPath]
|
2016-01-18 15:07:22 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a temporary directory and copies the './app folder' inside, and adds a text file with the configuration
|
|
|
|
* for the single page app.
|
|
|
|
*
|
|
|
|
* @param {string} srcAppDir
|
|
|
|
* @param {string} tempDir
|
|
|
|
* @param {string} name
|
|
|
|
* @param {string} targetURL
|
2016-01-22 04:35:05 +01:00
|
|
|
* @param {boolean} counter
|
2016-01-19 04:30:42 +01:00
|
|
|
* @param {number} width
|
|
|
|
* @param {number} height
|
2016-01-22 15:42:56 +01:00
|
|
|
* @param {boolean} showMenuBar
|
2016-01-19 04:30:42 +01:00
|
|
|
* @param {string} userAgent
|
2016-01-18 15:07:22 +01:00
|
|
|
* @param {tempDirCallback} callback
|
|
|
|
*/
|
2016-01-25 16:42:28 +01:00
|
|
|
function copyPlaceholderApp(srcAppDir, tempDir, name, targetURL, counter, width, height, showMenuBar, userAgent, insecure, callback) {
|
2016-01-21 06:42:27 +01:00
|
|
|
const loadedPackageJson = packageJson;
|
|
|
|
copy(srcAppDir, tempDir, function(error) {
|
2016-01-18 15:07:22 +01:00
|
|
|
if (error) {
|
|
|
|
console.error(error);
|
|
|
|
callback(`Error Copying temporary directory: ${error}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const appArgs = {
|
|
|
|
name: name,
|
|
|
|
targetUrl: targetURL,
|
2016-01-22 04:35:05 +01:00
|
|
|
counter: counter,
|
2016-01-18 15:07:22 +01:00
|
|
|
width: width,
|
2016-01-19 04:30:42 +01:00
|
|
|
height: height,
|
2016-01-22 15:42:56 +01:00
|
|
|
showMenuBar: showMenuBar,
|
2016-01-21 06:42:27 +01:00
|
|
|
userAgent: userAgent,
|
2016-01-25 16:42:28 +01:00
|
|
|
nativefierVersion: loadedPackageJson.version,
|
|
|
|
insecure: insecure
|
2016-01-18 15:07:22 +01:00
|
|
|
};
|
|
|
|
|
2016-01-19 04:45:17 +01:00
|
|
|
fs.writeFileSync(path.join(tempDir, '/nativefier.json'), JSON.stringify(appArgs));
|
2016-01-19 14:06:42 +01:00
|
|
|
|
|
|
|
// change name of packageJson so that temporary files will not be shared across different app instances
|
|
|
|
const packageJsonPath = path.join(tempDir, '/package.json');
|
|
|
|
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
|
2016-01-21 02:06:04 +01:00
|
|
|
packageJson.name = normalizeAppName(appArgs.name);
|
2016-01-19 14:06:42 +01:00
|
|
|
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
|
|
|
|
|
2016-01-18 15:07:22 +01:00
|
|
|
callback(null, tempDir);
|
|
|
|
});
|
2016-01-21 02:06:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeAppName(appName) {
|
|
|
|
// use a simple 3 byte random string to prevent collision
|
|
|
|
const postFixHash = crypto.randomBytes(3).toString('hex');
|
|
|
|
const normalized = _.kebabCase(appName.toLowerCase());
|
|
|
|
return `${normalized}-nativefier-${postFixHash}`;
|
|
|
|
}
|
2016-01-18 15:07:22 +01:00
|
|
|
|
|
|
|
export default buildApp;
|