1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00
metamask-extension/development/build/config.js
Olaf Tomalka 95c37e1ba3
feat: add yaml feature management (#18125)
* feat: add yaml feature management

Add yaml feature file per build type.
Also add method to parse yaml and set
enabled features env to true. The build
process will then replace any process.env[feature]
that exists on the config by its value

* chore: add example for desktop

* Added initial draft of build features

* [TMP] Sync between computers

* Is able to succesfully build stable extension with snaps feature

* Removing var context from builds.yml

* Add asssets to builds.yml

* Minor bug fixes and removing debug logs

* [WIP] Test changes

* Removed TODOs

* Fix regession bug

Also
* remove debug logs
* merge Variables.set and Variables.setMany with an overload

* Fix build, lint and a bunch of issues

* Update LavaMoat policies

* Re-add desktop build type

* Fix some tests

* Fix desktop build

* Define some env variables used by MV3

* Fix lint

* Fix remove-fenced-code tests

* Fix README typo

* Move new code

* Fix missing asset copy

* Move Jest env setup

* Fix path for test after rebase

* Fix code fences

* Fix fencing and LavaMoat policies

* Fix MMI code-fencing after rebase

* Fix MMI code fencing after merge

* Fix more MMI code fencing

---------

Co-authored-by: cryptotavares <joao.tavares@consensys.net>
Co-authored-by: Frederik Bolding <frederik.bolding@gmail.com>
Co-authored-by: Brad Decker <bhdecker84@gmail.com>
2023-04-25 16:32:51 +02:00

162 lines
4.6 KiB
JavaScript

const path = require('path');
const { readFile } = require('fs/promises');
const assert = require('assert');
const { AssertionError } = require('assert');
const ini = require('ini');
const { loadBuildTypesConfig } = require('../lib/build-type');
const { Variables } = require('../lib/variables');
const { ENVIRONMENT } = require('./constants');
const VARIABLES_REQUIRED_IN_PRODUCTION = [
'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',
];
async function fromIniFile(filepath) {
let configContents = '';
try {
configContents = await readFile(filepath, {
encoding: 'utf8',
});
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
return undefined;
}
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);
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 };
}
/**
*
* @param {string?} buildType - The chosen build type to build
* @param environment
* @returns Parsed configuration of the build pipeline
*/
async function getConfig(buildType, environment) {
const config = loadBuildTypesConfig();
const {
declarations: ymlDeclarations,
definitions: ymlDefinitions,
activeBuild,
activeFeatures,
} = await fromBuildsYML(buildType, config);
const variables = new Variables(ymlDeclarations);
// notice that maps have inverted value and key pair in forEach
ymlDefinitions.forEach((value, key) => variables.set(key, value));
(
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));
fromEnv(ymlDeclarations).definitions.forEach((value, key) =>
variables.set(key, value),
);
// TODO(ritave): Move build targets and environments to builds.yml
if (environment === ENVIRONMENT.PRODUCTION) {
const undefinedVariables = VARIABLES_REQUIRED_IN_PRODUCTION.filter(
(variable) => !variables.isDefined(variable),
);
if (undefinedVariables.length !== 0) {
const message = `Some variables required to build production target are not defined.
- ${undefinedVariables.join('\n - ')}
`;
throw new AssertionError({ message });
}
}
return {
variables,
activeBuild,
activeFeatures,
buildsYml: config,
};
}
module.exports = {
getConfig,
};