Allow nativefier to set process.env variables (#419)

This commit is contained in:
Bob Roth 2017-08-16 09:20:43 -05:00 committed by Ronan Jouchet
parent fc7a213a87
commit c9d2040327
7 changed files with 52 additions and 8858 deletions

View File

@ -16,6 +16,13 @@ electronDownload();
const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json'); const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json');
const appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8')); const appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));
if (appArgs.processEnvs) {
Object.keys(appArgs.processEnvs).forEach((key) => {
/* eslint-env node */
process.env[key] = appArgs.processEnvs[key];
});
}
let mainWindow; let mainWindow;
if (typeof appArgs.flashPluginDir === 'string') { if (typeof appArgs.flashPluginDir === 'string') {

View File

@ -408,6 +408,20 @@ Sets a default zoom factor to be used when the app is opened, defaults to `1.0`.
Prevents application from being run multiple times. If such an attempt occurs the already running instance is brought to front. Prevents application from being run multiple times. If such an attempt occurs the already running instance is brought to front.
#### [processEnvs]
```
--processEnvs <json-string>
```
a JSON string of key/value pairs to be set as environment variables before any browser windows are opened.
Example:
```bash
nativefier <your-geolocation-enabled-website> --processEnvs '{"GOOGLE_API_KEY": "<your-google-api-key>"}'
```
## Programmatic API ## Programmatic API
You can use the Nativefier programmatic API as well. You can use the Nativefier programmatic API as well.
@ -445,7 +459,10 @@ var options = {
insecure: false, insecure: false,
honest: false, honest: false,
zoom: 1.0, zoom: 1.0,
singleInstance: false singleInstance: false,
processEnvs: {
"GOOGLE_API_KEY": "<your-google-api-key>"
}
}; };
nativefier(options, function(error, appPath) { nativefier(options, function(error, appPath) {

8840
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -40,6 +40,7 @@ function selectAppArgs(options) {
internalUrls: options.internalUrls, internalUrls: options.internalUrls,
crashReporter: options.crashReporter, crashReporter: options.crashReporter,
singleInstance: options.singleInstance, singleInstance: options.singleInstance,
processEnvs: options.processEnvs,
}; };
} }

View File

@ -94,39 +94,39 @@ function removeInvalidOptions(options, param) {
} }
/** /**
* Removes the `app-copyright` parameter from options if building for Windows while not on Windows * Removes the `appCopyright` parameter from options if building for Windows while not on Windows
* and Wine is not installed * and Wine is not installed
* @param options * @param options
*/ */
function maybeNoAppCopyrightOption(options) { function maybeNoAppCopyrightOption(options) {
return removeInvalidOptions(options, 'app-copyright'); return removeInvalidOptions(options, 'appCopyright');
} }
/** /**
* Removes the `build-version` parameter from options if building for Windows while not on Windows * Removes the `buildVersion` parameter from options if building for Windows while not on Windows
* and Wine is not installed * and Wine is not installed
* @param options * @param options
*/ */
function maybeNoBuildVersionOption(options) { function maybeNoBuildVersionOption(options) {
return removeInvalidOptions(options, 'build-version'); return removeInvalidOptions(options, 'buildVersion');
} }
/** /**
* Removes the `app-version` parameter from options if building for Windows while not on Windows * Removes the `appVersion` parameter from options if building for Windows while not on Windows
* and Wine is not installed * and Wine is not installed
* @param options * @param options
*/ */
function maybeNoAppVersionOption(options) { function maybeNoAppVersionOption(options) {
return removeInvalidOptions(options, 'app-version'); return removeInvalidOptions(options, 'appVersion');
} }
/** /**
* Removes the `version-string` parameter from options if building for Windows while not on Windows * Removes the `versionString` parameter from options if building for Windows while not on Windows
* and Wine is not installed * and Wine is not installed
* @param options * @param options
*/ */
function maybeNoVersionStringOption(options) { function maybeNoVersionStringOption(options) {
return removeInvalidOptions(options, 'version-string'); return removeInvalidOptions(options, 'versionString');
} }
/** /**

View File

@ -16,6 +16,13 @@ function parseJson(val) {
return JSON.parse(val); return JSON.parse(val);
} }
function getProcessEnvs(val) {
if (!val) return {};
const pEnv = {};
pEnv.processEnvs = parseJson(val);
return pEnv;
}
if (require.main === module) { if (require.main === module) {
program program
.version(packageJson.version) .version(packageJson.version)
@ -64,6 +71,7 @@ if (require.main === module) {
.option('--internal-urls <value>', 'regular expression of URLs to consider "internal"; all other URLs will be opened in an external browser. (default: URLs on same second-level domain as app)') .option('--internal-urls <value>', 'regular expression of URLs to consider "internal"; all other URLs will be opened in an external browser. (default: URLs on same second-level domain as app)')
.option('--crash-reporter <value>', 'remote server URL to send crash reports') .option('--crash-reporter <value>', 'remote server URL to send crash reports')
.option('--single-instance', 'allow only a single instance of the application') .option('--single-instance', 'allow only a single instance of the application')
.option('--processEnvs <json-string>', 'a JSON string of key/value pairs to be set as environment variables before any browser windows are opened.', getProcessEnvs)
.parse(process.argv); .parse(process.argv);
if (!process.argv.slice(2).length) { if (!process.argv.slice(2).length) {

View File

@ -17,15 +17,6 @@ export default function (inpOptions) {
const options = { const options = {
dir: PLACEHOLDER_APP_DIR, dir: PLACEHOLDER_APP_DIR,
name: inpOptions.name, name: inpOptions.name,
'app-version': inpOptions.appVersion,
'build-version': inpOptions.buildVersion,
'app-copyright': inpOptions.appCopyright,
'version-string': inpOptions.versionString,
win32metadata: inpOptions.win32metadata || {
ProductName: inpOptions.name,
InternalName: inpOptions.name,
FileDescription: inpOptions.name,
},
targetUrl: normalizeUrl(inpOptions.targetUrl), targetUrl: normalizeUrl(inpOptions.targetUrl),
platform: inpOptions.platform || inferPlatform(), platform: inpOptions.platform || inferPlatform(),
arch: inpOptions.arch || inferArch(), arch: inpOptions.arch || inferArch(),
@ -65,6 +56,16 @@ export default function (inpOptions) {
zoom: inpOptions.zoom || 1.0, zoom: inpOptions.zoom || 1.0,
internalUrls: inpOptions.internalUrls || null, internalUrls: inpOptions.internalUrls || null,
singleInstance: inpOptions.singleInstance || false, singleInstance: inpOptions.singleInstance || false,
appVersion: inpOptions.appVersion,
buildVersion: inpOptions.buildVersion,
appCopyright: inpOptions.appCopyright,
versionString: inpOptions.versionString,
win32metadata: inpOptions.win32metadata || {
ProductName: inpOptions.name,
InternalName: inpOptions.name,
FileDescription: inpOptions.name,
},
processEnvs: inpOptions.processEnvs,
}; };
if (options.verbose) { if (options.verbose) {