Add validation to production build script (#15468)
Validation has been added to the build script when the "prod" target is
selected. We now ensure that all expected environment variables are
set, and that no extra environment variables are present (which might
indicate that the wrong configuration file is being used).
The `prod` target uses a new `.metamaskprodrc` configuration file. Each
required variable can be specified either via environment variable or
via this config file. CI will continue set these via environment
variable, but for local manual builds we can use the config file to
simplify the build process and ensure consistency.
A new "dist" target has been added to preserve the ability to build a
"production-like" build without this validation.
The config validation is invoked early in the script, in the CLI
argument parsing step, so that it would fail more quickly. Otherwise
we'd have to wait a few minutes longer for the validation to run.
This required some refactoring, moving functions to the utility module
and moving the config to a dedicated module.
Additionally, support has been added for all environment variables to
be set via the config file. Previously the values `PUBNUB_PUB_KEY`,
`PUBNUB_SUB_KEY`, `SENTRY_DSN`, and `SWAPS_USE_DEV_APIS` could only be
set via environment variable. Now, all of these variables can be set
either way.
Closes #15003
2022-08-19 20:16:18 +02:00
|
|
|
const path = require('path');
|
|
|
|
const { readFile } = require('fs/promises');
|
|
|
|
const ini = require('ini');
|
|
|
|
const { BuildType } = require('../lib/build-type');
|
|
|
|
|
2022-09-16 18:58:43 +02:00
|
|
|
const commonConfigurationPropertyNames = ['PUBNUB_PUB_KEY', 'PUBNUB_SUB_KEY'];
|
|
|
|
|
|
|
|
const configurationPropertyNames = [
|
|
|
|
...commonConfigurationPropertyNames,
|
|
|
|
'INFURA_PROJECT_ID',
|
|
|
|
'PHISHING_WARNING_PAGE_URL',
|
|
|
|
'PORTFOLIO_URL',
|
|
|
|
'SEGMENT_HOST',
|
|
|
|
'SEGMENT_WRITE_KEY',
|
|
|
|
'SENTRY_DSN_DEV',
|
|
|
|
'SWAPS_USE_DEV_APIS',
|
2023-02-20 18:13:12 +01:00
|
|
|
// Desktop
|
|
|
|
'COMPATIBILITY_VERSION_EXTENSION',
|
|
|
|
'DISABLE_WEB_SOCKET_ENCRYPTION',
|
|
|
|
'METAMASK_DEBUG',
|
|
|
|
'SKIP_OTP_PAIRING_FLOW',
|
2022-09-16 18:58:43 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
const productionConfigurationPropertyNames = [
|
|
|
|
...commonConfigurationPropertyNames,
|
|
|
|
'INFURA_BETA_PROJECT_ID',
|
|
|
|
'INFURA_FLASK_PROJECT_ID',
|
|
|
|
'INFURA_PROD_PROJECT_ID',
|
|
|
|
'SEGMENT_BETA_WRITE_KEY',
|
|
|
|
'SEGMENT_FLASK_WRITE_KEY',
|
|
|
|
'SEGMENT_PROD_WRITE_KEY',
|
|
|
|
'SENTRY_DSN',
|
|
|
|
];
|
|
|
|
|
Add validation to production build script (#15468)
Validation has been added to the build script when the "prod" target is
selected. We now ensure that all expected environment variables are
set, and that no extra environment variables are present (which might
indicate that the wrong configuration file is being used).
The `prod` target uses a new `.metamaskprodrc` configuration file. Each
required variable can be specified either via environment variable or
via this config file. CI will continue set these via environment
variable, but for local manual builds we can use the config file to
simplify the build process and ensure consistency.
A new "dist" target has been added to preserve the ability to build a
"production-like" build without this validation.
The config validation is invoked early in the script, in the CLI
argument parsing step, so that it would fail more quickly. Otherwise
we'd have to wait a few minutes longer for the validation to run.
This required some refactoring, moving functions to the utility module
and moving the config to a dedicated module.
Additionally, support has been added for all environment variables to
be set via the config file. Previously the values `PUBNUB_PUB_KEY`,
`PUBNUB_SUB_KEY`, `SENTRY_DSN`, and `SWAPS_USE_DEV_APIS` could only be
set via environment variable. Now, all of these variables can be set
either way.
Closes #15003
2022-08-19 20:16:18 +02:00
|
|
|
/**
|
|
|
|
* Get configuration for non-production builds.
|
|
|
|
*
|
|
|
|
* @returns {object} The production configuration.
|
|
|
|
*/
|
|
|
|
async function getConfig() {
|
|
|
|
const configPath = path.resolve(__dirname, '..', '..', '.metamaskrc');
|
|
|
|
let configContents = '';
|
|
|
|
try {
|
|
|
|
configContents = await readFile(configPath, {
|
|
|
|
encoding: 'utf8',
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code !== 'ENOENT') {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2022-09-16 18:58:43 +02:00
|
|
|
|
|
|
|
const environmentVariables = {};
|
|
|
|
for (const propertyName of configurationPropertyNames) {
|
|
|
|
if (process.env[propertyName]) {
|
|
|
|
environmentVariables[propertyName] = process.env[propertyName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add validation to production build script (#15468)
Validation has been added to the build script when the "prod" target is
selected. We now ensure that all expected environment variables are
set, and that no extra environment variables are present (which might
indicate that the wrong configuration file is being used).
The `prod` target uses a new `.metamaskprodrc` configuration file. Each
required variable can be specified either via environment variable or
via this config file. CI will continue set these via environment
variable, but for local manual builds we can use the config file to
simplify the build process and ensure consistency.
A new "dist" target has been added to preserve the ability to build a
"production-like" build without this validation.
The config validation is invoked early in the script, in the CLI
argument parsing step, so that it would fail more quickly. Otherwise
we'd have to wait a few minutes longer for the validation to run.
This required some refactoring, moving functions to the utility module
and moving the config to a dedicated module.
Additionally, support has been added for all environment variables to
be set via the config file. Previously the values `PUBNUB_PUB_KEY`,
`PUBNUB_SUB_KEY`, `SENTRY_DSN`, and `SWAPS_USE_DEV_APIS` could only be
set via environment variable. Now, all of these variables can be set
either way.
Closes #15003
2022-08-19 20:16:18 +02:00
|
|
|
return {
|
|
|
|
...ini.parse(configContents),
|
2022-09-16 18:58:43 +02:00
|
|
|
...environmentVariables,
|
Add validation to production build script (#15468)
Validation has been added to the build script when the "prod" target is
selected. We now ensure that all expected environment variables are
set, and that no extra environment variables are present (which might
indicate that the wrong configuration file is being used).
The `prod` target uses a new `.metamaskprodrc` configuration file. Each
required variable can be specified either via environment variable or
via this config file. CI will continue set these via environment
variable, but for local manual builds we can use the config file to
simplify the build process and ensure consistency.
A new "dist" target has been added to preserve the ability to build a
"production-like" build without this validation.
The config validation is invoked early in the script, in the CLI
argument parsing step, so that it would fail more quickly. Otherwise
we'd have to wait a few minutes longer for the validation to run.
This required some refactoring, moving functions to the utility module
and moving the config to a dedicated module.
Additionally, support has been added for all environment variables to
be set via the config file. Previously the values `PUBNUB_PUB_KEY`,
`PUBNUB_SUB_KEY`, `SENTRY_DSN`, and `SWAPS_USE_DEV_APIS` could only be
set via environment variable. Now, all of these variables can be set
either way.
Closes #15003
2022-08-19 20:16:18 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get configuration for production builds and perform validation.
|
|
|
|
*
|
|
|
|
* This function validates that all required variables are present, and that
|
|
|
|
* the production configuration file doesn't include any extraneous entries.
|
|
|
|
*
|
|
|
|
* @param {BuildType} buildType - The current build type (e.g. "main", "flask",
|
|
|
|
* etc.).
|
|
|
|
* @returns {object} The production configuration.
|
|
|
|
*/
|
|
|
|
async function getProductionConfig(buildType) {
|
|
|
|
const prodConfigPath = path.resolve(__dirname, '..', '..', '.metamaskprodrc');
|
|
|
|
let prodConfigContents = '';
|
|
|
|
try {
|
|
|
|
prodConfigContents = await readFile(prodConfigPath, {
|
|
|
|
encoding: 'utf8',
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code !== 'ENOENT') {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2022-09-16 18:58:43 +02:00
|
|
|
|
|
|
|
const environmentVariables = {};
|
|
|
|
for (const propertyName of productionConfigurationPropertyNames) {
|
|
|
|
if (process.env[propertyName]) {
|
|
|
|
environmentVariables[propertyName] = process.env[propertyName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add validation to production build script (#15468)
Validation has been added to the build script when the "prod" target is
selected. We now ensure that all expected environment variables are
set, and that no extra environment variables are present (which might
indicate that the wrong configuration file is being used).
The `prod` target uses a new `.metamaskprodrc` configuration file. Each
required variable can be specified either via environment variable or
via this config file. CI will continue set these via environment
variable, but for local manual builds we can use the config file to
simplify the build process and ensure consistency.
A new "dist" target has been added to preserve the ability to build a
"production-like" build without this validation.
The config validation is invoked early in the script, in the CLI
argument parsing step, so that it would fail more quickly. Otherwise
we'd have to wait a few minutes longer for the validation to run.
This required some refactoring, moving functions to the utility module
and moving the config to a dedicated module.
Additionally, support has been added for all environment variables to
be set via the config file. Previously the values `PUBNUB_PUB_KEY`,
`PUBNUB_SUB_KEY`, `SENTRY_DSN`, and `SWAPS_USE_DEV_APIS` could only be
set via environment variable. Now, all of these variables can be set
either way.
Closes #15003
2022-08-19 20:16:18 +02:00
|
|
|
const prodConfig = {
|
|
|
|
...ini.parse(prodConfigContents),
|
2022-09-16 18:58:43 +02:00
|
|
|
...environmentVariables,
|
Add validation to production build script (#15468)
Validation has been added to the build script when the "prod" target is
selected. We now ensure that all expected environment variables are
set, and that no extra environment variables are present (which might
indicate that the wrong configuration file is being used).
The `prod` target uses a new `.metamaskprodrc` configuration file. Each
required variable can be specified either via environment variable or
via this config file. CI will continue set these via environment
variable, but for local manual builds we can use the config file to
simplify the build process and ensure consistency.
A new "dist" target has been added to preserve the ability to build a
"production-like" build without this validation.
The config validation is invoked early in the script, in the CLI
argument parsing step, so that it would fail more quickly. Otherwise
we'd have to wait a few minutes longer for the validation to run.
This required some refactoring, moving functions to the utility module
and moving the config to a dedicated module.
Additionally, support has been added for all environment variables to
be set via the config file. Previously the values `PUBNUB_PUB_KEY`,
`PUBNUB_SUB_KEY`, `SENTRY_DSN`, and `SWAPS_USE_DEV_APIS` could only be
set via environment variable. Now, all of these variables can be set
either way.
Closes #15003
2022-08-19 20:16:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const requiredEnvironmentVariables = {
|
|
|
|
all: ['PUBNUB_PUB_KEY', 'PUBNUB_SUB_KEY', 'SENTRY_DSN'],
|
|
|
|
[BuildType.beta]: ['INFURA_BETA_PROJECT_ID', 'SEGMENT_BETA_WRITE_KEY'],
|
|
|
|
[BuildType.flask]: ['INFURA_FLASK_PROJECT_ID', 'SEGMENT_FLASK_WRITE_KEY'],
|
|
|
|
[BuildType.main]: ['INFURA_PROD_PROJECT_ID', 'SEGMENT_PROD_WRITE_KEY'],
|
2023-03-01 10:38:15 +01:00
|
|
|
[BuildType.mmi]: ['INFURA_MMI_PROJECT_ID', 'SEGMENT_MMI_WRITE_KEY'],
|
Add validation to production build script (#15468)
Validation has been added to the build script when the "prod" target is
selected. We now ensure that all expected environment variables are
set, and that no extra environment variables are present (which might
indicate that the wrong configuration file is being used).
The `prod` target uses a new `.metamaskprodrc` configuration file. Each
required variable can be specified either via environment variable or
via this config file. CI will continue set these via environment
variable, but for local manual builds we can use the config file to
simplify the build process and ensure consistency.
A new "dist" target has been added to preserve the ability to build a
"production-like" build without this validation.
The config validation is invoked early in the script, in the CLI
argument parsing step, so that it would fail more quickly. Otherwise
we'd have to wait a few minutes longer for the validation to run.
This required some refactoring, moving functions to the utility module
and moving the config to a dedicated module.
Additionally, support has been added for all environment variables to
be set via the config file. Previously the values `PUBNUB_PUB_KEY`,
`PUBNUB_SUB_KEY`, `SENTRY_DSN`, and `SWAPS_USE_DEV_APIS` could only be
set via environment variable. Now, all of these variables can be set
either way.
Closes #15003
2022-08-19 20:16:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
for (const required of [
|
|
|
|
...requiredEnvironmentVariables.all,
|
|
|
|
...requiredEnvironmentVariables[buildType],
|
|
|
|
]) {
|
|
|
|
if (!prodConfig[required]) {
|
|
|
|
throw new Error(`Missing '${required}' environment variable`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const allValid = Object.values(requiredEnvironmentVariables).flat();
|
|
|
|
for (const environmentVariable of Object.keys(prodConfig)) {
|
|
|
|
if (!allValid.includes(environmentVariable)) {
|
|
|
|
throw new Error(`Invalid environment variable: '${environmentVariable}'`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prodConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { getConfig, getProductionConfig };
|