mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-23 02:10:12 +01:00
95c37e1ba3
* 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>
170 lines
4.2 KiB
JavaScript
170 lines
4.2 KiB
JavaScript
const fs = require('fs');
|
|
const { AssertionError } = require('assert');
|
|
const path = require('path');
|
|
const {
|
|
object,
|
|
string,
|
|
record,
|
|
optional,
|
|
array,
|
|
refine,
|
|
any,
|
|
boolean,
|
|
coerce,
|
|
union,
|
|
unknown,
|
|
validate,
|
|
nullable,
|
|
never,
|
|
} = require('superstruct');
|
|
const yaml = require('js-yaml');
|
|
const { uniqWith } = require('lodash');
|
|
|
|
const BUILDS_YML_PATH = path.resolve('./builds.yml');
|
|
|
|
/**
|
|
* @type {import('superstruct').Infer<typeof BuildTypesStruct> | null}
|
|
*/
|
|
let cachedBuildTypes = null;
|
|
|
|
/**
|
|
* Ensures that the array item contains only elements that are distinct from each other
|
|
*
|
|
* @template {Struct<any>} Element
|
|
* @type {import('./build-type').Unique<Element>}
|
|
*/
|
|
const unique = (struct, eq) =>
|
|
refine(struct, 'unique', (value) => {
|
|
if (uniqWith(value, eq).length === value.length) {
|
|
return true;
|
|
}
|
|
return 'Array contains duplicated values';
|
|
});
|
|
|
|
const EnvDefinitionStruct = coerce(
|
|
object({ key: string(), value: unknown() }),
|
|
refine(record(string(), any()), 'Env variable declaration', (value) => {
|
|
if (Object.keys(value).length !== 1) {
|
|
return 'Declaration should have only one property, the name';
|
|
}
|
|
return true;
|
|
}),
|
|
(value) => ({ key: Object.keys(value)[0], value: Object.values(value)[0] }),
|
|
);
|
|
|
|
const EnvArrayStruct = unique(
|
|
array(union([string(), EnvDefinitionStruct])),
|
|
(a, b) => {
|
|
const keyA = typeof a === 'string' ? a : a.key;
|
|
const keyB = typeof b === 'string' ? b : b.key;
|
|
return keyA === keyB;
|
|
},
|
|
);
|
|
|
|
const BuildTypeStruct = object({
|
|
features: optional(unique(array(string()))),
|
|
env: optional(EnvArrayStruct),
|
|
isPrerelease: optional(boolean()),
|
|
manifestOverrides: optional(string()),
|
|
});
|
|
|
|
const CopyAssetStruct = object({ src: string(), dest: string() });
|
|
const ExclusiveIncludeAssetStruct = coerce(
|
|
object({ exclusiveInclude: string() }),
|
|
string(),
|
|
(exclusiveInclude) => ({ exclusiveInclude }),
|
|
);
|
|
const AssetStruct = union([CopyAssetStruct, ExclusiveIncludeAssetStruct]);
|
|
|
|
const FeatureStruct = object({
|
|
env: optional(EnvArrayStruct),
|
|
// TODO(ritave): Check if the paths exist
|
|
assets: optional(array(AssetStruct)),
|
|
});
|
|
|
|
const FeaturesStruct = refine(
|
|
record(
|
|
string(),
|
|
coerce(FeatureStruct, nullable(never()), () => ({})),
|
|
),
|
|
'feature definitions',
|
|
function* (value) {
|
|
let isValid = true;
|
|
|
|
const definitions = new Set();
|
|
|
|
for (const feature of Object.values(value)) {
|
|
for (const env of feature?.env ?? []) {
|
|
if (typeof env !== 'string') {
|
|
if (definitions.has(env.key)) {
|
|
isValid = false;
|
|
yield `Multiple defined features have a definition of "${env}" env variable, resulting in a conflict`;
|
|
}
|
|
definitions.add(env.key);
|
|
}
|
|
}
|
|
}
|
|
return isValid;
|
|
},
|
|
);
|
|
|
|
const BuildTypesStruct = refine(
|
|
object({
|
|
default: string(),
|
|
buildTypes: record(string(), BuildTypeStruct),
|
|
features: FeaturesStruct,
|
|
env: EnvArrayStruct,
|
|
}),
|
|
'BuildTypes',
|
|
(value) => {
|
|
if (!Object.keys(value.buildTypes).includes(value.default)) {
|
|
return `Default build type "${value.default}" does not exist in builds declarations`;
|
|
}
|
|
return true;
|
|
},
|
|
);
|
|
|
|
/**
|
|
* Loads definitions of build type and what they are composed of.
|
|
*
|
|
* @returns {import('superstruct').Infer<typeof BuildTypesStruct>}
|
|
*/
|
|
function loadBuildTypesConfig() {
|
|
if (cachedBuildTypes !== null) {
|
|
return cachedBuildTypes;
|
|
}
|
|
const buildsData = yaml.load(fs.readFileSync(BUILDS_YML_PATH, 'utf8'), {
|
|
json: true,
|
|
});
|
|
const [err, result] = validate(buildsData, BuildTypesStruct, {
|
|
coerce: true,
|
|
});
|
|
if (err !== undefined) {
|
|
throw new AssertionError({
|
|
message: constructFailureMessage(err),
|
|
});
|
|
}
|
|
cachedBuildTypes = result;
|
|
return buildsData;
|
|
}
|
|
|
|
/**
|
|
* Creates a user readable error message about parse failure.
|
|
*
|
|
* @param {import('superstruct').StructError} structError
|
|
* @returns {string}
|
|
*/
|
|
function constructFailureMessage(structError) {
|
|
return `Failed to parse builds.yml
|
|
-> ${structError
|
|
.failures()
|
|
.map(
|
|
(failure) =>
|
|
`${failure.message} (${BUILDS_YML_PATH}:.${failure.path.join('/')})`,
|
|
)
|
|
.join('\n -> ')}
|
|
`;
|
|
}
|
|
|
|
module.exports = { loadBuildTypesConfig };
|