1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 23:58:06 +01:00
metamask-extension/development/build/manifest.js
Mark Stacey 75a8aedc32
Derive version suffix from build type and version (#13895)
The version of a build is now derived from both the `version` field in
`package.json` and the requested build type and version. The build type
and version are added onto the manifest version as a suffix, according
to the SemVer prerelease format.

We already have support in the extension for versions of this format,
but to apply a Flask or Beta version required manual updates to
`package.json`. Now it can be done just with build arguments.

A `get-version` module was created to make it easier to generate the
version in the various places we do that during the build. It was
created in the `development/lib` directory because it will be used by
other non-build development scripts in a future PR.

The `BuildType` constant was extracted to its own module as well, and
moved to the `development/lib` directory. This was to make it clear
that it's used by various different development scripts, not just the
build.
2022-03-10 12:31:50 -03:30

173 lines
4.7 KiB
JavaScript

const { promises: fs } = require('fs');
const path = require('path');
const { mergeWith, cloneDeep } = require('lodash');
const baseManifest = require('../../app/manifest/_base.json');
const { BuildType } = require('../lib/build-type');
const { createTask, composeSeries } = require('./task');
module.exports = createManifestTasks;
function createManifestTasks({
browserPlatforms,
browserVersionMap,
buildType,
}) {
// 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',
'manifest',
`${platform}.json`,
),
);
const result = mergeWith(
cloneDeep(baseManifest),
platformModifications,
browserVersionMap[platform],
await getBuildModifications(buildType, platform),
customArrayMerge,
);
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('manifest:dev', composeSeries(prepPlatforms, envDev));
const testDev = createTask(
'manifest:testDev',
composeSeries(prepPlatforms, envTestDev),
);
const test = createTask(
'manifest:test',
composeSeries(prepPlatforms, envTest),
);
const prod = createTask('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);
}),
);
};
}
// 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 {BuildType} buildType - The build type.
* @param {string} platform - The platform (i.e. the browser).
* @returns {Object} The build modificantions for the given build type and platform.
*/
async function getBuildModifications(buildType, platform) {
if (!Object.values(BuildType).includes(buildType)) {
throw new Error(`Invalid build type: ${buildType}`);
} else if (buildType === BuildType.main) {
return {};
}
const builtTypeManifestDirectoryPath = path.resolve(
__dirname,
'..',
'..',
'app',
'build-types',
buildType,
'manifest',
);
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;
}