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');
|
2023-04-25 16:32:51 +02:00
|
|
|
const assert = require('assert');
|
|
|
|
const { AssertionError } = require('assert');
|
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 ini = require('ini');
|
2023-04-25 16:32:51 +02:00
|
|
|
const { loadBuildTypesConfig } = require('../lib/build-type');
|
|
|
|
const { Variables } = require('../lib/variables');
|
|
|
|
const { ENVIRONMENT } = require('./constants');
|
2022-09-16 18:58:43 +02:00
|
|
|
|
2023-05-03 20:29:11 +02:00
|
|
|
const VARIABLES_REQUIRED_IN_PRODUCTION = {
|
|
|
|
main: ['INFURA_PROD_PROJECT_ID', 'SEGMENT_PROD_WRITE_KEY', 'SENTRY_DSN'],
|
|
|
|
beta: ['INFURA_BETA_PROJECT_ID', 'SEGMENT_BETA_WRITE_KEY', 'SENTRY_DSN'],
|
|
|
|
flask: ['INFURA_FLASK_PROJECT_ID', 'SEGMENT_FLASK_WRITE_KEY', 'SENTRY_DSN'],
|
2023-07-12 14:01:49 +02:00
|
|
|
mmi: [
|
|
|
|
'INFURA_MMI_PROJECT_ID',
|
|
|
|
'MMI_CONFIGURATION_SERVICE_URL',
|
|
|
|
'SEGMENT_MMI_WRITE_KEY',
|
|
|
|
'SENTRY_DSN',
|
|
|
|
],
|
2023-05-03 20:29:11 +02:00
|
|
|
};
|
2022-09-16 18:58:43 +02:00
|
|
|
|
2023-04-25 16:32:51 +02:00
|
|
|
async function fromIniFile(filepath) {
|
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
|
|
|
let configContents = '';
|
|
|
|
try {
|
2023-04-25 16:32:51 +02:00
|
|
|
configContents = await readFile(filepath, {
|
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
|
|
|
encoding: 'utf8',
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code !== 'ENOENT') {
|
|
|
|
throw error;
|
|
|
|
}
|
2023-04-25 16:32:51 +02:00
|
|
|
return undefined;
|
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
|
|
|
}
|
2022-09-16 18:58:43 +02:00
|
|
|
|
2023-04-25 16:32:51 +02:00
|
|
|
const variables = ini.parse(configContents);
|
|
|
|
assert(
|
|
|
|
!Object.values(variables).some((variable) => typeof variable === 'object'),
|
|
|
|
`When loading ${filepath} - INI categories are not supported`,
|
|
|
|
);
|
|
|
|
const entries = Object.entries(variables);
|
2022-09-16 18:58:43 +02:00
|
|
|
|
2023-04-25 16:32:51 +02:00
|
|
|
const declarations = new Set(
|
|
|
|
entries.filter(([, value]) => value === '').map(([key]) => key),
|
|
|
|
);
|
|
|
|
const definitions = new Map(
|
|
|
|
entries
|
|
|
|
.filter(([, value]) => value !== '')
|
|
|
|
.map(([key, value]) => [key, value]),
|
|
|
|
);
|
|
|
|
|
|
|
|
return { declarations, definitions };
|
|
|
|
}
|
|
|
|
|
|
|
|
function fromEnv(declarations) {
|
|
|
|
const definitions = new Map(
|
|
|
|
[...declarations]
|
|
|
|
.filter((declaration) => declaration in process.env)
|
|
|
|
.map((declaration) => [declaration, process.env[declaration]]),
|
|
|
|
);
|
|
|
|
return { definitions, declarations: new Set() };
|
|
|
|
}
|
|
|
|
|
|
|
|
function fromBuildsYML(buildType, config) {
|
|
|
|
const extractDeclarations = (envArray) =>
|
|
|
|
envArray === undefined
|
|
|
|
? []
|
|
|
|
: envArray.map((env) => (typeof env === 'string' ? env : env.key));
|
|
|
|
const extractDefinitions = (envArray) =>
|
|
|
|
envArray === undefined
|
|
|
|
? []
|
|
|
|
: envArray.filter((env) => typeof env !== 'string');
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
buildType = buildType ?? config.default;
|
|
|
|
const activeBuild = config.buildTypes[buildType];
|
|
|
|
const activeFeatures = activeBuild.features ?? [];
|
|
|
|
|
|
|
|
let declarations = [...extractDeclarations(config.env)];
|
|
|
|
|
|
|
|
activeFeatures
|
|
|
|
.map((feature) => config.features[feature])
|
|
|
|
.filter((feature) => feature !== null)
|
|
|
|
.forEach(({ env }) => declarations.push(...extractDeclarations(env)));
|
|
|
|
|
|
|
|
declarations.push(...extractDeclarations(activeBuild.env));
|
|
|
|
declarations = new Set(declarations);
|
|
|
|
|
|
|
|
const definitions = new Map();
|
|
|
|
|
|
|
|
// 1. root env
|
|
|
|
extractDefinitions(config.env).forEach(({ key, value }) =>
|
|
|
|
definitions.set(key, value),
|
|
|
|
);
|
|
|
|
// 2. features env
|
|
|
|
activeFeatures
|
|
|
|
.filter((key) => config.features[key] !== null)
|
|
|
|
.map((key) => config.features[key].env)
|
|
|
|
.map(extractDefinitions)
|
|
|
|
.flat()
|
|
|
|
.forEach(({ key, value }) => definitions.set(key, value));
|
|
|
|
// 3. build type env
|
|
|
|
extractDefinitions(activeBuild.env).forEach(({ key, value }) =>
|
|
|
|
definitions.set(key, value),
|
|
|
|
);
|
|
|
|
|
|
|
|
return { declarations, definitions, activeFeatures, activeBuild };
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2023-04-25 16:32:51 +02:00
|
|
|
* @param {string?} buildType - The chosen build type to build
|
|
|
|
* @param environment
|
|
|
|
* @returns Parsed configuration of the build pipeline
|
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
|
|
|
*/
|
2023-04-25 16:32:51 +02:00
|
|
|
async function getConfig(buildType, environment) {
|
|
|
|
const config = loadBuildTypesConfig();
|
|
|
|
const {
|
|
|
|
declarations: ymlDeclarations,
|
|
|
|
definitions: ymlDefinitions,
|
|
|
|
activeBuild,
|
|
|
|
activeFeatures,
|
|
|
|
} = await fromBuildsYML(buildType, config);
|
2022-09-16 18:58:43 +02:00
|
|
|
|
2023-04-25 16:32:51 +02:00
|
|
|
const variables = new Variables(ymlDeclarations);
|
2022-09-16 18:58:43 +02:00
|
|
|
|
2023-04-25 16:32:51 +02:00
|
|
|
// notice that maps have inverted value and key pair in forEach
|
|
|
|
ymlDefinitions.forEach((value, key) => variables.set(key, value));
|
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
|
|
|
|
2023-04-25 16:32:51 +02:00
|
|
|
(
|
|
|
|
await fromIniFile(path.resolve(__dirname, '..', '..', '.metamaskrc'))
|
|
|
|
)?.definitions.forEach((value, key) => variables.set(key, value));
|
|
|
|
(
|
|
|
|
await fromIniFile(path.resolve(__dirname, '..', '..', '.metamaskprodrc'))
|
|
|
|
)?.definitions.forEach((value, key) => variables.set(key, value));
|
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
|
|
|
|
2023-04-25 16:32:51 +02:00
|
|
|
fromEnv(ymlDeclarations).definitions.forEach((value, key) =>
|
|
|
|
variables.set(key, value),
|
|
|
|
);
|
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
|
|
|
|
2023-04-25 16:32:51 +02:00
|
|
|
// TODO(ritave): Move build targets and environments to builds.yml
|
|
|
|
if (environment === ENVIRONMENT.PRODUCTION) {
|
2023-05-03 20:29:11 +02:00
|
|
|
const undefinedVariables = VARIABLES_REQUIRED_IN_PRODUCTION[
|
|
|
|
buildType
|
|
|
|
].filter((variable) => !variables.isDefined(variable));
|
2023-04-25 16:32:51 +02:00
|
|
|
if (undefinedVariables.length !== 0) {
|
|
|
|
const message = `Some variables required to build production target are not defined.
|
|
|
|
- ${undefinedVariables.join('\n - ')}
|
|
|
|
`;
|
|
|
|
throw new AssertionError({ message });
|
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
|
|
|
}
|
|
|
|
}
|
2023-04-25 16:32:51 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
variables,
|
|
|
|
activeBuild,
|
|
|
|
activeFeatures,
|
|
|
|
buildsYml: config,
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
|
2023-04-25 16:32:51 +02:00
|
|
|
module.exports = {
|
|
|
|
getConfig,
|
|
|
|
};
|