mirror of
https://github.com/Llewellynvdm/nativefier.git
synced 2024-06-03 05:10:47 +00:00
6fb3b92eb8
* Update deps except eslint * Update eslint and lint:fix (WIP, needs manual fixing for remaining 44 problems) * Manually fix remaining eslint errors * Document deprecation of `version-string` as of electron-packager 9.0.0 * Upgrade to Electron 1.7.9 (chrome-58, node-7.9.0, v8-5.8) * npm: Disable generation of package-lock.json and gitignore it --Trying this, package-lock is a pain in PRs. May not be a good idea (obviously we lose deps pinning), will revert if necessary.-- * npm tasks: add dev-up-win for Windows developers, and e2e for end-to-end tests. Update docs. * Move normalizeUrl test to a jest unit test, makes no sense to be in the mocha e2e tests * Switch from babel-preset-es2015 to babel-preset-env, with target.node=4.0. Seem like it's today's most convenient way to support the latest ES and let babel transpile to what makes sense for our currently minimal node version
145 lines
4.0 KiB
JavaScript
145 lines
4.0 KiB
JavaScript
import fs from 'fs';
|
|
import crypto from 'crypto';
|
|
import _ from 'lodash';
|
|
import path from 'path';
|
|
import ncp from 'ncp';
|
|
|
|
const copy = ncp.ncp;
|
|
|
|
/**
|
|
* Only picks certain app args to pass to nativefier.json
|
|
* @param options
|
|
*/
|
|
function selectAppArgs(options) {
|
|
return {
|
|
name: options.name,
|
|
targetUrl: options.targetUrl,
|
|
counter: options.counter,
|
|
width: options.width,
|
|
height: options.height,
|
|
minWidth: options.minWidth,
|
|
minHeight: options.minHeight,
|
|
maxWidth: options.maxWidth,
|
|
maxHeight: options.maxHeight,
|
|
showMenuBar: options.showMenuBar,
|
|
fastQuit: options.fastQuit,
|
|
userAgent: options.userAgent,
|
|
nativefierVersion: options.nativefierVersion,
|
|
ignoreCertificate: options.ignoreCertificate,
|
|
ignoreGpuBlacklist: options.ignoreGpuBlacklist,
|
|
enableEs3Apis: options.enableEs3Apis,
|
|
insecure: options.insecure,
|
|
flashPluginDir: options.flashPluginDir,
|
|
diskCacheSize: options.diskCacheSize,
|
|
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,
|
|
appCopyright: options.appCopyright,
|
|
appVersion: options.appVersion,
|
|
buildVersion: options.buildVersion,
|
|
win32metadata: options.win32metadata,
|
|
versionString: options.versionString,
|
|
processEnvs: options.processEnvs,
|
|
tray: options.tray,
|
|
basicAuthUsername: options.basicAuthUsername,
|
|
basicAuthPassword: options.basicAuthPassword,
|
|
};
|
|
}
|
|
|
|
function maybeCopyScripts(srcs, dest) {
|
|
if (!srcs) {
|
|
return new Promise((resolve) => {
|
|
resolve();
|
|
});
|
|
}
|
|
const promises = srcs.map(src => new Promise((resolve, reject) => {
|
|
if (!fs.existsSync(src)) {
|
|
reject(new Error('Error copying injection files: file not found'));
|
|
return;
|
|
}
|
|
|
|
let destFileName;
|
|
if (path.extname(src) === '.js') {
|
|
destFileName = 'inject.js';
|
|
} else if (path.extname(src) === '.css') {
|
|
destFileName = 'inject.css';
|
|
} else {
|
|
resolve();
|
|
return;
|
|
}
|
|
|
|
copy(src, path.join(dest, 'inject', destFileName), (error) => {
|
|
if (error) {
|
|
reject(new Error(`Error Copying injection files: ${error}`));
|
|
return;
|
|
}
|
|
resolve();
|
|
});
|
|
}));
|
|
|
|
return new Promise((resolve, reject) => {
|
|
Promise.all(promises)
|
|
.then(() => {
|
|
resolve();
|
|
})
|
|
.catch((error) => {
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
|
|
function normalizeAppName(appName, url) {
|
|
// use a simple 3 byte random string to prevent collision
|
|
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}`;
|
|
}
|
|
|
|
function changeAppPackageJsonName(appPath, name, url) {
|
|
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));
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
function buildApp(src, dest, options, callback) {
|
|
const appArgs = selectAppArgs(options);
|
|
copy(src, dest, (error) => {
|
|
if (error) {
|
|
callback(`Error Copying temporary directory: ${error}`);
|
|
return;
|
|
}
|
|
|
|
fs.writeFileSync(path.join(dest, '/nativefier.json'), JSON.stringify(appArgs));
|
|
|
|
maybeCopyScripts(options.inject, dest)
|
|
.catch((err) => {
|
|
console.warn(err);
|
|
})
|
|
.then(() => {
|
|
changeAppPackageJsonName(dest, appArgs.name, appArgs.targetUrl);
|
|
callback();
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
export default buildApp;
|