2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-11-10 15:21:03 +00:00
nativefier/src/build/buildApp.js

66 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-01-18 14:07:22 +00:00
import fs from 'fs';
import crypto from 'crypto';
import _ from 'lodash';
2016-01-29 06:26:35 +00:00
import path from 'path';
import ncp from 'ncp';
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-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
};
}
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;