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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 05:39:23 +00:00
|
|
|
* @param {string} src
|
|
|
|
* @param {string} dest
|
|
|
|
* @param {{}} options
|
|
|
|
* @param callback
|
2016-01-18 14:07:22 +00:00
|
|
|
*/
|
2016-01-29 06:26:35 +00:00
|
|
|
function buildApp(src, dest, options, callback) {
|
2016-01-29 05:39:23 +00:00
|
|
|
const appArgs = selectAppArgs(options);
|
|
|
|
copy(src, dest, error => {
|
2016-01-18 14:07:22 +00:00
|
|
|
if (error) {
|
|
|
|
callback(`Error Copying temporary directory: ${error}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-29 05:39:23 +00:00
|
|
|
fs.writeFileSync(path.join(dest, '/nativefier.json'), JSON.stringify(appArgs));
|
2016-02-25 06:11:48 +00:00
|
|
|
|
2016-02-25 06:56:32 +00:00
|
|
|
maybeCopyScripts(options.inject, dest)
|
2016-05-08 07:23:41 +00:00
|
|
|
.catch(error => {
|
|
|
|
console.warn(error);
|
|
|
|
})
|
2016-02-25 06:56:32 +00:00
|
|
|
.then(() => {
|
|
|
|
changeAppPackageJsonName(dest, appArgs.name, appArgs.targetUrl);
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function maybeCopyScripts(srcs, dest) {
|
2016-02-25 08:51:23 +00:00
|
|
|
if (!srcs) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
2016-02-25 06:56:32 +00:00
|
|
|
const promises = srcs.map(src => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!fs.existsSync(src)) {
|
2016-05-08 07:23:41 +00:00
|
|
|
reject('Error copying injection files: file not found');
|
2016-02-25 06:56:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let destFileName;
|
|
|
|
if (path.extname(src) === '.js') {
|
|
|
|
destFileName = 'inject.js';
|
|
|
|
} else if (path.extname(src) === '.css') {
|
|
|
|
destFileName = 'inject.css';
|
|
|
|
} else {
|
|
|
|
resolve();
|
2016-02-25 06:11:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-25 06:56:32 +00:00
|
|
|
copy(src, path.join(dest, 'inject', destFileName), error => {
|
|
|
|
if (error) {
|
|
|
|
reject(`Error Copying injection files: ${error}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
2016-02-25 06:11:48 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-02-25 06:56:32 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Promise.all(promises)
|
|
|
|
.then(() => {
|
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2016-01-18 14:07:22 +00:00
|
|
|
});
|
2016-01-21 01:06:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-23 18:05:56 +00:00
|
|
|
function changeAppPackageJsonName(appPath, name, url) {
|
2016-01-29 05:39:23 +00:00
|
|
|
const packageJsonPath = path.join(appPath, '/package.json');
|
|
|
|
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
|
2016-02-23 18:05:56 +00:00
|
|
|
packageJson.name = normalizeAppName(name, url);
|
2016-01-29 05:39:23 +00:00
|
|
|
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only picks certain app args to pass to nativefier.json
|
|
|
|
* @param options
|
2016-02-22 20:33:33 +00:00
|
|
|
* @returns {{name: (*|string), targetUrl: (string|*), counter: *, width: *, height: *, showMenuBar: *, userAgent: *, nativefierVersion: *, insecure: *, disableWebSecurity: *}}
|
2016-01-29 05:39:23 +00:00
|
|
|
*/
|
|
|
|
function selectAppArgs(options) {
|
|
|
|
return {
|
|
|
|
name: options.name,
|
|
|
|
targetUrl: options.targetUrl,
|
|
|
|
counter: options.counter,
|
|
|
|
width: options.width,
|
|
|
|
height: options.height,
|
2016-05-26 09:11:51 +00:00
|
|
|
minWidth: options.minWidth,
|
|
|
|
minHeight: options.minHeight,
|
|
|
|
maxWidth: options.maxWidth,
|
|
|
|
maxHeight: options.maxHeight,
|
2016-01-29 05:39:23 +00:00
|
|
|
showMenuBar: options.showMenuBar,
|
2016-04-25 22:09:01 +00:00
|
|
|
fastQuit: options.fastQuit,
|
2016-01-29 05:39:23 +00:00
|
|
|
userAgent: options.userAgent,
|
|
|
|
nativefierVersion: options.nativefierVersion,
|
2016-02-23 13:31:47 +00:00
|
|
|
ignoreCertificate: options.ignoreCertificate,
|
2016-02-23 09:13:39 +00:00
|
|
|
insecure: options.insecure,
|
2016-02-25 10:26:28 +00:00
|
|
|
flashPluginDir: options.flashPluginDir,
|
2016-03-22 17:09:21 +00:00
|
|
|
fullScreen: options.fullScreen,
|
2016-04-16 23:04:10 +00:00
|
|
|
hideWindowFrame: options.hideWindowFrame,
|
2016-04-16 14:06:25 +00:00
|
|
|
maximize: options.maximize,
|
2016-05-26 10:02:43 +00:00
|
|
|
disableContextMenu: options.disableContextMenu,
|
2016-07-12 01:32:40 +00:00
|
|
|
disableDevTools: options.disableDevTools,
|
2016-08-06 18:03:53 +00:00
|
|
|
zoom: options.zoom,
|
2016-10-09 05:52:50 +00:00
|
|
|
internalUrls: options.internalUrls,
|
2017-04-10 02:02:49 +00:00
|
|
|
crashReporter: options.crashReporter,
|
|
|
|
singleInstance: options.singleInstance
|
2016-01-29 05:39:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-02-23 18:05:56 +00:00
|
|
|
function normalizeAppName(appName, url) {
|
2016-01-21 01:06:04 +00:00
|
|
|
// use a simple 3 byte random string to prevent collision
|
2016-02-23 18:05:56 +00:00
|
|
|
let hash = crypto.createHash('md5');
|
|
|
|
hash.update(url);
|
|
|
|
const postFixHash = hash.digest('hex').substring(0, 6);
|
2016-01-21 01:06:04 +00:00
|
|
|
const normalized = _.kebabCase(appName.toLowerCase());
|
|
|
|
return `${normalized}-nativefier-${postFixHash}`;
|
|
|
|
}
|
2016-01-18 14:07:22 +00:00
|
|
|
|
|
|
|
export default buildApp;
|