1
0
mirror of https://github.com/jiahaog/Nativefier synced 2024-06-26 07:38:59 +02:00
Nativefier/src/build/buildApp.js

66 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-01-18 15:07:22 +01:00
import fs from 'fs';
import crypto from 'crypto';
import _ from 'lodash';
2016-01-29 07:26:35 +01:00
import path from 'path';
import ncp from 'ncp';
2016-01-18 15:07:22 +01: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 06:39:23 +01:00
* @param {string} src
* @param {string} dest
* @param {{}} options
* @param callback
2016-01-18 15:07:22 +01:00
*/
2016-01-29 07:26:35 +01:00
function buildApp(src, dest, options, callback) {
2016-01-29 06:39:23 +01:00
const appArgs = selectAppArgs(options);
copy(src, dest, error => {
2016-01-18 15:07:22 +01:00
if (error) {
callback(`Error Copying temporary directory: ${error}`);
return;
}
2016-01-29 06:39:23 +01:00
fs.writeFileSync(path.join(dest, '/nativefier.json'), JSON.stringify(appArgs));
changeAppPackageJsonName(dest, appArgs.name);
callback();
2016-01-18 15:07:22 +01:00
});
}
2016-01-29 06:39:23 +01: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 15:07:22 +01:00
export default buildApp;