2016-01-18 14:07:22 +00:00
|
|
|
import fs from 'fs';
|
2016-01-21 01:06:04 +00:00
|
|
|
import crypto from 'crypto';
|
2016-01-19 13:19:09 +00:00
|
|
|
import _ from 'lodash';
|
2016-01-29 06:26:35 +00:00
|
|
|
import path from 'path';
|
|
|
|
import ncp from 'ncp';
|
2016-01-21 05:42:27 +00:00
|
|
|
|
2016-01-18 14:07:22 +00:00
|
|
|
const copy = ncp.ncp;
|
2017-12-19 13:42:06 +00:00
|
|
|
const log = require('loglevel');
|
2016-01-18 14:07:22 +00:00
|
|
|
/**
|
2017-04-29 14:52:12 +00:00
|
|
|
* Only picks certain app args to pass to nativefier.json
|
|
|
|
* @param options
|
2016-01-18 14:07:22 +00:00
|
|
|
*/
|
2017-04-29 14:52:12 +00:00
|
|
|
function selectAppArgs(options) {
|
|
|
|
return {
|
|
|
|
name: options.name,
|
|
|
|
targetUrl: options.targetUrl,
|
|
|
|
counter: options.counter,
|
2018-04-14 21:17:25 +00:00
|
|
|
bounce: options.bounce,
|
2017-04-29 14:52:12 +00:00
|
|
|
width: options.width,
|
|
|
|
height: options.height,
|
|
|
|
minWidth: options.minWidth,
|
|
|
|
minHeight: options.minHeight,
|
|
|
|
maxWidth: options.maxWidth,
|
|
|
|
maxHeight: options.maxHeight,
|
2017-12-26 18:00:39 +00:00
|
|
|
x: options.x,
|
|
|
|
y: options.y,
|
2017-04-29 14:52:12 +00:00
|
|
|
showMenuBar: options.showMenuBar,
|
|
|
|
fastQuit: options.fastQuit,
|
|
|
|
userAgent: options.userAgent,
|
|
|
|
nativefierVersion: options.nativefierVersion,
|
|
|
|
ignoreCertificate: options.ignoreCertificate,
|
2018-04-22 19:56:10 +00:00
|
|
|
disableGpu: options.disableGpu,
|
2017-07-13 16:23:07 +00:00
|
|
|
ignoreGpuBlacklist: options.ignoreGpuBlacklist,
|
|
|
|
enableEs3Apis: options.enableEs3Apis,
|
2017-04-29 14:52:12 +00:00
|
|
|
insecure: options.insecure,
|
|
|
|
flashPluginDir: options.flashPluginDir,
|
2017-07-05 13:07:31 +00:00
|
|
|
diskCacheSize: options.diskCacheSize,
|
2017-04-29 14:52:12 +00:00
|
|
|
fullScreen: options.fullScreen,
|
|
|
|
hideWindowFrame: options.hideWindowFrame,
|
|
|
|
maximize: options.maximize,
|
|
|
|
disableContextMenu: options.disableContextMenu,
|
|
|
|
disableDevTools: options.disableDevTools,
|
|
|
|
zoom: options.zoom,
|
|
|
|
internalUrls: options.internalUrls,
|
|
|
|
crashReporter: options.crashReporter,
|
|
|
|
singleInstance: options.singleInstance,
|
2019-02-08 15:03:29 +00:00
|
|
|
clearCache: options.clearCache,
|
2017-10-03 16:02:41 +00:00
|
|
|
appCopyright: options.appCopyright,
|
|
|
|
appVersion: options.appVersion,
|
|
|
|
buildVersion: options.buildVersion,
|
|
|
|
win32metadata: options.win32metadata,
|
|
|
|
versionString: options.versionString,
|
2017-08-16 14:20:43 +00:00
|
|
|
processEnvs: options.processEnvs,
|
2018-01-26 14:59:58 +00:00
|
|
|
fileDownloadOptions: options.fileDownloadOptions,
|
2017-10-05 23:32:48 +00:00
|
|
|
tray: options.tray,
|
2017-10-05 22:44:03 +00:00
|
|
|
basicAuthUsername: options.basicAuthUsername,
|
|
|
|
basicAuthPassword: options.basicAuthPassword,
|
2018-03-16 22:15:44 +00:00
|
|
|
alwaysOnTop: options.alwaysOnTop,
|
2018-05-31 12:05:12 +00:00
|
|
|
titleBarStyle: options.titleBarStyle,
|
2018-11-05 02:03:52 +00:00
|
|
|
globalShortcuts: options.globalShortcuts,
|
2019-08-22 19:37:49 +00:00
|
|
|
browserwindowOptions: options.browserwindowOptions,
|
2019-08-22 19:25:58 +00:00
|
|
|
backgroundColor: options.backgroundColor,
|
2019-08-22 19:05:39 +00:00
|
|
|
darwinDarkModeSupport: options.darwinDarkModeSupport,
|
2017-04-29 14:52:12 +00:00
|
|
|
};
|
2016-02-25 06:56:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function maybeCopyScripts(srcs, dest) {
|
2017-04-29 14:52:12 +00:00
|
|
|
if (!srcs) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
2018-05-24 07:02:44 +00:00
|
|
|
const promises = srcs.map(
|
|
|
|
(src) =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
if (!fs.existsSync(src)) {
|
|
|
|
reject(new Error('Error copying injection files: file not found'));
|
|
|
|
return;
|
|
|
|
}
|
2016-02-25 06:56:32 +00:00
|
|
|
|
2018-05-24 07:02:44 +00:00
|
|
|
let destFileName;
|
|
|
|
if (path.extname(src) === '.js') {
|
|
|
|
destFileName = 'inject.js';
|
|
|
|
} else if (path.extname(src) === '.css') {
|
|
|
|
destFileName = 'inject.css';
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
2016-02-25 06:11:48 +00:00
|
|
|
|
2018-05-24 07:02:44 +00:00
|
|
|
copy(src, path.join(dest, 'inject', destFileName), (error) => {
|
|
|
|
if (error) {
|
|
|
|
reject(new Error(`Error Copying injection files: ${error}`));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
);
|
2016-02-25 06:11:48 +00:00
|
|
|
|
2017-04-29 14:52:12 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Promise.all(promises)
|
|
|
|
.then(() => {
|
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeAppName(appName, url) {
|
2017-11-14 13:05:01 +00:00
|
|
|
// use a simple 3 byte random string to prevent collision
|
2017-04-29 14:52:12 +00:00
|
|
|
const hash = crypto.createHash('md5');
|
|
|
|
hash.update(url);
|
|
|
|
const postFixHash = hash.digest('hex').substring(0, 6);
|
|
|
|
const normalized = _.kebabCase(appName.toLowerCase());
|
|
|
|
return `${normalized}-nativefier-${postFixHash}`;
|
2016-01-21 01:06:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-23 18:05:56 +00:00
|
|
|
function changeAppPackageJsonName(appPath, name, url) {
|
2017-04-29 14:52:12 +00:00
|
|
|
const packageJsonPath = path.join(appPath, '/package.json');
|
|
|
|
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
|
|
|
|
packageJson.name = normalizeAppName(name, url);
|
|
|
|
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
|
2016-01-29 05:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-29 14:52:12 +00: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} src
|
|
|
|
* @param {string} dest
|
|
|
|
* @param {{}} options
|
|
|
|
* @param callback
|
2016-01-29 05:39:23 +00:00
|
|
|
*/
|
2017-04-29 14:52:12 +00:00
|
|
|
function buildApp(src, dest, options, callback) {
|
|
|
|
const appArgs = selectAppArgs(options);
|
2019-08-22 19:25:58 +00:00
|
|
|
|
2017-04-29 14:52:12 +00:00
|
|
|
copy(src, dest, (error) => {
|
|
|
|
if (error) {
|
|
|
|
callback(`Error Copying temporary directory: ${error}`);
|
|
|
|
return;
|
|
|
|
}
|
2016-01-29 05:39:23 +00:00
|
|
|
|
2018-05-24 07:02:44 +00:00
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(dest, '/nativefier.json'),
|
|
|
|
JSON.stringify(appArgs),
|
|
|
|
);
|
2017-04-29 14:52:12 +00:00
|
|
|
|
|
|
|
maybeCopyScripts(options.inject, dest)
|
2017-11-14 13:05:01 +00:00
|
|
|
.catch((err) => {
|
2017-12-19 13:42:06 +00:00
|
|
|
log.warn(err);
|
2017-04-29 14:52:12 +00:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
changeAppPackageJsonName(dest, appArgs.name, appArgs.targetUrl);
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
2016-01-21 01:06:04 +00:00
|
|
|
}
|
2016-01-18 14:07:22 +00:00
|
|
|
|
|
|
|
export default buildApp;
|