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

213 lines
6.0 KiB
JavaScript

const { promises: fs } = require('fs');
const path = require('path');
const childProcess = require('child_process');
const { mergeWith, cloneDeep, capitalize } = require('lodash');
const baseManifest = process.env.ENABLE_MV3
? require('../../app/manifest/v3/_base.json')
: require('../../app/manifest/v2/_base.json');
const { loadBuildTypesConfig } = require('../lib/build-type');
const { TASKS, ENVIRONMENT } = require('./constants');
const { createTask, composeSeries } = require('./task');
const { getEnvironment } = require('./utils');
module.exports = createManifestTasks;
function createManifestTasks({
browserPlatforms,
browserVersionMap,
buildType,
applyLavaMoat,
shouldIncludeSnow,
entryTask,
}) {
// merge base manifest with per-platform manifests
const prepPlatforms = async () => {
return Promise.all(
browserPlatforms.map(async (platform) => {
const platformModifications = await readJson(
path.join(
__dirname,
'..',
'..',
'app',
process.env.ENABLE_MV3 ? 'manifest/v3' : 'manifest/v2',
`${platform}.json`,
),
);
const result = mergeWith(
cloneDeep(baseManifest),
platformModifications,
browserVersionMap[platform],
await getBuildModifications(buildType, platform),
customArrayMerge,
);
modifyNameAndDescForNonProd(result);
const dir = path.join('.', 'dist', platform);
await fs.mkdir(dir, { recursive: true });
await writeJson(result, path.join(dir, 'manifest.json'));
}),
);
};
// dev: add perms
const envDev = createTaskForModifyManifestForEnvironment((manifest) => {
manifest.permissions = [...manifest.permissions, 'webRequestBlocking'];
});
// testDev: add perms
const envTestDev = createTaskForModifyManifestForEnvironment((manifest) => {
manifest.permissions = [
...manifest.permissions,
'webRequestBlocking',
'http://localhost/*',
];
});
// test: add permissions
const envTest = createTaskForModifyManifestForEnvironment((manifest) => {
manifest.permissions = [
...manifest.permissions,
'webRequestBlocking',
'http://localhost/*',
];
});
// high level manifest tasks
const dev = createTask(
TASKS.MANIFEST_DEV,
composeSeries(prepPlatforms, envDev),
);
const testDev = createTask(
TASKS.MANIFEST_TEST_DEV,
composeSeries(prepPlatforms, envTestDev),
);
const test = createTask(
TASKS.MANIFEST_TEST,
composeSeries(prepPlatforms, envTest),
);
const prod = createTask(TASKS.MANIFEST_PROD, prepPlatforms);
return { prod, dev, testDev, test };
// helper for modifying each platform's manifest.json in place
function createTaskForModifyManifestForEnvironment(transformFn) {
return () => {
return Promise.all(
browserPlatforms.map(async (platform) => {
const manifestPath = path.join(
'.',
'dist',
platform,
'manifest.json',
);
const manifest = await readJson(manifestPath);
transformFn(manifest);
await writeJson(manifest, manifestPath);
}),
);
};
}
// For non-production builds only, modify the extension's name and description
function modifyNameAndDescForNonProd(manifest) {
const environment = getEnvironment({ buildTarget: entryTask });
if (environment === ENVIRONMENT.PRODUCTION) {
return;
}
const mv3Str = process.env.ENABLE_MV3 ? ' MV3' : '';
const lavamoatStr = applyLavaMoat ? ' lavamoat' : '';
const snowStr = shouldIncludeSnow ? ' snow' : '';
// Get the first 8 characters of the git revision id
const gitRevisionStr = childProcess
.execSync('git rev-parse HEAD')
.toString()
.trim()
.substring(0, 8);
manifest.name = `MetaMask ${capitalize(
buildType,
)}${mv3Str}${lavamoatStr}${snowStr}`;
manifest.description = `${environment} build from git id: ${gitRevisionStr}`;
}
// helper for merging obj value
function customArrayMerge(objValue, srcValue) {
if (Array.isArray(objValue)) {
return [...new Set([...objValue, ...srcValue])];
}
return undefined;
}
}
// helper for reading and deserializing json from fs
async function readJson(file) {
return JSON.parse(await fs.readFile(file, 'utf8'));
}
// helper for serializing and writing json to fs
async function writeJson(obj, file) {
return fs.writeFile(file, JSON.stringify(obj, null, 2));
}
/**
* Get manifest modifications for the given build type, including modifications specific to the
* given platform.
*
* @param {string} buildType - The build type.
* @param {string} platform - The platform (i.e. the browser).
* @returns {object} The build modifications for the given build type and platform.
*/
async function getBuildModifications(buildType, platform) {
const buildConfig = loadBuildTypesConfig();
if (!(buildType in buildConfig.buildTypes)) {
throw new Error(`Invalid build type: ${buildType}`);
}
const overridesPath = buildConfig.buildTypes[buildType].manifestOverrides;
if (overridesPath === undefined) {
return {};
}
const builtTypeManifestDirectoryPath = path.resolve(
process.cwd(),
overridesPath,
);
const baseBuildTypeModificationsPath = path.join(
builtTypeManifestDirectoryPath,
'_base.json',
);
const buildModifications = await readJson(baseBuildTypeModificationsPath);
const platformBuildTypeModificationsPath = path.join(
builtTypeManifestDirectoryPath,
`${platform}.json`,
);
try {
const platformBuildTypeModifications = await readJson(
platformBuildTypeModificationsPath,
);
Object.assign(buildModifications, platformBuildTypeModifications);
} catch (error) {
// Suppress 'ENOENT' error because it indicates there are no platform-specific manifest
// modifications for this build type.
if (error.code !== 'ENOENT') {
throw error;
}
}
return buildModifications;
}