1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00
metamask-extension/development/lib/variables.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

115 lines
2.6 KiB
JavaScript

/* eslint-disable jsdoc/check-tag-names */
const assert = require('assert');
const DeclaredOnly = Symbol(
'This variable was declared only without being defined',
);
class Variables {
/**
* @type {Map<string, unknown | typeof DeclaredOnly>}
*/
#definitions = new Map();
/**
* @param {Iterable<string>} declarations
*/
constructor(declarations) {
for (const declaration of declarations) {
this.#definitions.set(declaration, DeclaredOnly);
}
}
/**
* @param {string} key - The name of the variable
* @throws {TypeError} If there is no definition of a variable.
*/
get(key) {
const value = this.getMaybe(key);
assert(
value !== DeclaredOnly,
new TypeError(
`Tried to access a declared, but not defined environmental variable "${key}"`,
),
);
return value;
}
/**
* Returns a declared, but maybe not defined variable.
*
* @param {string} key - The name of the variable
* @throws {TypeError} If there was no declaration of the variable.
* @returns The value, or undefined if the variables wasn't defined.
*/
getMaybe(key) {
assert(
this.isDeclared(key),
new TypeError(
`Tried to access an environmental variable "${key}" that wasn't declared in builds.yml`,
),
);
return this.#definitions.get(key);
}
/**
* Sets one key
*
* @overload
* @param {string} key
* @param {unknown} value
* @returns {void}
*/
/**
* @overload
* @param {Record<string, unknown>} records - Key-Value object
* @returns {void}
*/
/**
* @param {string | Record<string, unknown>} keyOrRecord
* @param {unknown} value
* @returns {void}
*/
set(keyOrRecord, value) {
if (typeof keyOrRecord === 'object') {
for (const [key, recordValue] of Object.entries(keyOrRecord)) {
this.set(key, recordValue);
}
return;
}
const key = keyOrRecord;
assert(
this.isDeclared(key),
`Tried to modify a variable "${key}" that wasn't declared in builds.yml`,
);
assert(value !== DeclaredOnly, `Tried to un-define "${key}" variable`);
this.#definitions.set(key, value);
}
isDeclared(key) {
return this.#definitions.has(key);
}
isDefined(key) {
return (
this.#definitions.has(key) && this.#definitions.get(key) !== DeclaredOnly
);
}
[Symbol.iterator] = this.declarations;
*declarations() {
yield* this.#definitions.keys();
}
*definitions() {
for (const [key, value] of this.#definitions.entries()) {
if (value !== DeclaredOnly) {
yield [key, value];
}
}
}
}
module.exports = { Variables };