Nativefier/src/build/buildApp.js

176 lines
5.1 KiB
JavaScript
Raw Normal View History

2016-01-18 15:07:22 +01:00
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
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';
import _ from 'lodash';
import hasBinary from 'hasbin';
2016-01-18 15:07:22 +01:00
2016-01-29 07:09:36 +01:00
import optionsFactory from './../options';
2016-01-29 06:39:23 +01:00
import iconBuild from './iconBuild';
2016-01-29 07:09:36 +01:00
import helpers from './../helpers/helpers';
2016-01-18 15:07:22 +01:00
const copy = ncp.ncp;
const isWindows = helpers.isWindows;
2016-01-18 15:07:22 +01:00
/**
* @callback buildAppCallback
* @param error
2016-01-29 06:39:23 +01:00
* @param {string} appPath
2016-01-18 15:07:22 +01:00
*/
/**
*
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
var tmpObj = tmp.dirSync({unsafeCleanup: true});
const tmpPath = tmpObj.name;
2016-01-18 15:07:22 +01:00
async.waterfall([
callback => {
optionsFactory(options, callback);
2016-01-18 15:07:22 +01:00
},
(options, callback) => {
2016-01-29 06:39:23 +01:00
copyPlaceholderApp(options.dir, tmpPath, options, error => {
if (error) {
callback(error);
return;
}
// dir now correctly references the app folder to package
options.dir = tmpPath;
callback(null, options);
});
},
2016-01-29 06:39:23 +01:00
(options, callback) => {
iconBuild(options, (error, optionsWithIcon) => {
2016-01-29 06:39:23 +01:00
callback(null, optionsWithIcon);
});
},
2016-01-29 06:39:23 +01:00
(options, callback) => {
// maybe skip passing icon parameter to electron packager
const packageOptions = maybeNoIconOption(options);
packager(packageOptions, (error, appPathArray) => {
// pass options which still contains the icon to waterfall
callback(error, options, appPathArray);
});
2016-01-18 15:07:22 +01:00
},
(options, appPathArray, callback) => {
// somehow appPathArray is a 1 element array
if (appPathArray.length === 0) {
// directory already exists, --overwrite is not set
// exit here
callback();
return;
}
if (appPathArray.length > 1) {
console.warn('Warning: Packaged app path contains more than one element:', appPathArray);
}
2016-01-29 06:39:23 +01:00
const appPath = appPathArray[0];
maybeCopyIcons(options, appPath, error => {
callback(error, appPath);
});
2016-01-18 15:07:22 +01:00
}
], callback);
}
/**
* Creates a temporary directory and copies the './app folder' inside, and adds a text file with the configuration
* for the single page app.
*
2016-01-29 06:39:23 +01:00
* @param {string} src
* @param {string} dest
* @param {{}} options
* @param callback
2016-01-18 15:07:22 +01:00
*/
2016-01-29 06:39:23 +01:00
function copyPlaceholderApp(src, dest, options, callback) {
const appArgs = selectAppArgs(options);
copy(src, dest, error => {
2016-01-18 15:07:22 +01:00
if (error) {
callback(`Error Copying temporary directory: ${error}`);
return;
}
2016-01-29 06:39:23 +01:00
fs.writeFileSync(path.join(dest, '/nativefier.json'), JSON.stringify(appArgs));
changeAppPackageJsonName(dest, appArgs.name);
callback();
2016-01-18 15:07:22 +01:00
});
}
2016-01-29 06:39:23 +01:00
function changeAppPackageJsonName(appPath, name) {
const packageJsonPath = path.join(appPath, '/package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
packageJson.name = normalizeAppName(name);
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
}
/**
* Only picks certain app args to pass to nativefier.json
* @param options
* @returns {{name: (*|string), targetUrl: (string|*), counter: *, width: *, height: *, showMenuBar: *, userAgent: *, nativefierVersion: *, insecure: *}}
*/
function selectAppArgs(options) {
return {
name: options.name,
targetUrl: options.targetUrl,
counter: options.counter,
width: options.width,
height: options.height,
showMenuBar: options.showMenuBar,
userAgent: options.userAgent,
nativefierVersion: options.nativefierVersion,
insecure: options.insecure
};
}
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
function maybeNoIconOption(options) {
const packageOptions = JSON.parse(JSON.stringify(options));
if (options.platform === 'win32' && !isWindows()) {
if (!hasBinary.sync('wine')) {
packageOptions.icon = null;
}
}
return packageOptions;
}
2016-01-29 06:39:23 +01:00
/**
* For windows and linux, we have to copy over the icon to the resources/app folder, which the
* BrowserWindow is hard coded to read the icon from
* @param {{}} options
* @param {string} appPath
* @param callback
*/
function maybeCopyIcons(options, appPath, callback) {
if (!options.icon) {
callback();
return;
}
if (options.platform === 'darwin') {
callback();
return;
}
// windows & linux
const destIconPath = path.join(appPath, 'resources/app');
copy(options.icon, path.join(destIconPath, 'icon.png'), error => {
callback(error);
});
}
2016-01-18 15:07:22 +01:00
export default buildApp;