2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-12-24 11:05:26 +00:00

Cleanup build app code

This commit is contained in:
Jia Hao 2016-01-29 13:39:23 +08:00
parent a9c294b4b6
commit 4bdc0d27d2
2 changed files with 81 additions and 71 deletions

View File

@ -1,10 +1,6 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import crypto from 'crypto'; import crypto from 'crypto';
import optionsFactory from './options';
import iconBuild from './iconBuild';
import helpers from './helpers';
import packager from 'electron-packager'; import packager from 'electron-packager';
import tmp from 'tmp'; import tmp from 'tmp';
import ncp from 'ncp'; import ncp from 'ncp';
@ -12,7 +8,9 @@ import async from 'async';
import _ from 'lodash'; import _ from 'lodash';
import hasBinary from 'hasbin'; import hasBinary from 'hasbin';
import packageJson from './../package.json'; import optionsFactory from './options';
import iconBuild from './iconBuild';
import helpers from './helpers';
const copy = ncp.ncp; const copy = ncp.ncp;
const isWindows = helpers.isWindows; const isWindows = helpers.isWindows;
@ -20,7 +18,7 @@ const isWindows = helpers.isWindows;
/** /**
* @callback buildAppCallback * @callback buildAppCallback
* @param error * @param error
* @param appPath * @param {string} appPath
*/ */
/** /**
@ -39,18 +37,22 @@ function buildApp(options, callback) {
optionsFactory(options, callback); optionsFactory(options, callback);
}, },
(options, callback) => { (options, callback) => {
copyPlaceholderApp(options.dir, tmpPath, options.name, options.targetUrl, options.counter, options.width, options.height, options.showMenuBar, options.userAgent, options.insecure, (error, tempDirPath) => { copyPlaceholderApp(options.dir, tmpPath, options, error => {
callback(error, tempDirPath, options); if (error) {
callback(error);
return;
}
// dir now correctly references the app folder to package
options.dir = tmpPath;
callback(null, options);
}); });
}, },
(tempDir, options, callback) => { (options, callback) => {
iconBuild(options, (error, optionsWithIcon) => { iconBuild(options, (error, optionsWithIcon) => {
callback(null, tempDir, optionsWithIcon); callback(null, optionsWithIcon);
}); });
}, },
(tempDir, options, callback) => { (options, callback) => {
options.dir = tempDir;
// maybe skip passing icon parameter to electron packager // maybe skip passing icon parameter to electron packager
const packageOptions = maybeNoIconOption(options); const packageOptions = maybeNoIconOption(options);
packager(packageOptions, (error, appPathArray) => { packager(packageOptions, (error, appPathArray) => {
@ -58,7 +60,6 @@ function buildApp(options, callback) {
callback(error, options, appPathArray); callback(error, options, appPathArray);
}); });
}, },
(options, appPathArray, callback) => { (options, appPathArray, callback) => {
// somehow appPathArray is a 1 element array // somehow appPathArray is a 1 element array
if (appPathArray.length === 0) { if (appPathArray.length === 0) {
@ -71,82 +72,64 @@ function buildApp(options, callback) {
if (appPathArray.length > 1) { if (appPathArray.length > 1) {
console.warn('Warning: Packaged app path contains more than one element:', appPathArray); console.warn('Warning: Packaged app path contains more than one element:', appPathArray);
} }
const appPath = appPathArray[0]; const appPath = appPathArray[0];
maybeCopyIcons(options, appPath, error => {
if (!options.icon) {
callback(null, appPath);
return;
}
if (options.platform === 'darwin') {
callback(null, appPath);
return;
}
// windows & linux
const destIconPath = path.join(appPath, 'resources/app');
copy(options.icon, path.join(destIconPath, 'icon.png'), error => {
callback(error, appPath); callback(error, appPath);
}); });
} }
], callback); ], callback);
} }
/**
* @callback tempDirCallback
* @param error
* @param {string} [tempDirPath]
*/
/** /**
* Creates a temporary directory and copies the './app folder' inside, and adds a text file with the configuration * Creates a temporary directory and copies the './app folder' inside, and adds a text file with the configuration
* for the single page app. * for the single page app.
* *
* @param {string} srcAppDir * @param {string} src
* @param {string} tempDir * @param {string} dest
* @param {string} name * @param {{}} options
* @param {string} targetURL * @param callback
* @param {boolean} counter
* @param {number} width
* @param {number} height
* @param {boolean} showMenuBar
* @param {string} userAgent
* @param {tempDirCallback} callback
*/ */
function copyPlaceholderApp(srcAppDir, tempDir, name, targetURL, counter, width, height, showMenuBar, userAgent, insecure, callback) { function copyPlaceholderApp(src, dest, options, callback) {
const loadedPackageJson = packageJson; const appArgs = selectAppArgs(options);
copy(srcAppDir, tempDir, function(error) { copy(src, dest, error => {
if (error) { if (error) {
console.error(error);
callback(`Error Copying temporary directory: ${error}`); callback(`Error Copying temporary directory: ${error}`);
return; return;
} }
const appArgs = { fs.writeFileSync(path.join(dest, '/nativefier.json'), JSON.stringify(appArgs));
name: name, changeAppPackageJsonName(dest, appArgs.name);
targetUrl: targetURL, callback();
counter: counter,
width: width,
height: height,
showMenuBar: showMenuBar,
userAgent: userAgent,
nativefierVersion: loadedPackageJson.version,
insecure: insecure
};
fs.writeFileSync(path.join(tempDir, '/nativefier.json'), JSON.stringify(appArgs));
// 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));
packageJson.name = normalizeAppName(appArgs.name);
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
callback(null, tempDir);
}); });
} }
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) { function normalizeAppName(appName) {
// use a simple 3 byte random string to prevent collision // use a simple 3 byte random string to prevent collision
const postFixHash = crypto.randomBytes(3).toString('hex'); const postFixHash = crypto.randomBytes(3).toString('hex');
@ -163,4 +146,30 @@ function maybeNoIconOption(options) {
} }
return packageOptions; return packageOptions;
} }
/**
* 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);
});
}
export default buildApp; export default buildApp;

View File

@ -9,6 +9,7 @@ import async from 'async';
import inferIcon from './infer/inferIcon'; import inferIcon from './infer/inferIcon';
import inferTitle from './infer/inferTitle'; import inferTitle from './infer/inferTitle';
import inferOs from './infer/inferOs'; import inferOs from './infer/inferOs';
import packageJson from './../package.json';
const {inferPlatform, inferArch} = inferOs; const {inferPlatform, inferArch} = inferOs;
@ -36,6 +37,7 @@ function optionsFactory(inpOptions, callback) {
platform: inpOptions.platform || inferPlatform(), platform: inpOptions.platform || inferPlatform(),
arch: inpOptions.arch || inferArch(), arch: inpOptions.arch || inferArch(),
version: ELECTRON_VERSION, version: ELECTRON_VERSION,
nativefierVersion: packageJson.version,
out: inpOptions.out || process.cwd(), out: inpOptions.out || process.cwd(),
overwrite: inpOptions.overwrite || false, overwrite: inpOptions.overwrite || false,
asar: inpOptions.conceal || false, asar: inpOptions.conceal || false,
@ -85,7 +87,6 @@ function optionsFactory(inpOptions, callback) {
} }
], error => { ], error => {
callback(error, sanitizeOptions(options)); callback(error, sanitizeOptions(options));
console.log(options);
}); });
} }