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));
|
|
|
|
changeAppPackageJsonName(dest, appArgs.name);
|
|
|
|
callback();
|
2016-01-18 14:07:22 +00:00
|
|
|
});
|
2016-01-21 01:06:04 +00:00
|
|
|
}
|
|
|
|
|
2016-01-29 05:39:23 +00: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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-01-21 01:06:04 +00: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 14:07:22 +00:00
|
|
|
|
|
|
|
export default buildApp;
|