2021-02-04 19:15:23 +01:00
|
|
|
const { promises: fs } = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const { merge, cloneDeep } = require('lodash');
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const baseManifest = require('../../app/manifest/_base.json');
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { createTask, composeSeries } = require('./task');
|
2021-10-25 18:57:30 +02:00
|
|
|
const { BuildType } = require('./utils');
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
module.exports = createManifestTasks;
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-10-06 19:44:48 +02:00
|
|
|
function createManifestTasks({
|
|
|
|
browserPlatforms,
|
|
|
|
browserVersionMap,
|
|
|
|
buildType,
|
|
|
|
}) {
|
2020-03-09 01:55:02 +01:00
|
|
|
// merge base manifest with per-platform manifests
|
|
|
|
const prepPlatforms = async () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
return Promise.all(
|
|
|
|
browserPlatforms.map(async (platform) => {
|
|
|
|
const platformModifications = await readJson(
|
|
|
|
path.join(
|
|
|
|
__dirname,
|
|
|
|
'..',
|
|
|
|
'..',
|
|
|
|
'app',
|
|
|
|
'manifest',
|
|
|
|
`${platform}.json`,
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-05-10 23:16:03 +02:00
|
|
|
const result = merge(
|
|
|
|
cloneDeep(baseManifest),
|
|
|
|
platformModifications,
|
2021-10-06 19:44:48 +02:00
|
|
|
browserVersionMap[platform],
|
2021-11-11 00:03:59 +01:00
|
|
|
await getBuildModifications(buildType, platform),
|
2021-05-10 23:16:03 +02:00
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
const dir = path.join('.', 'dist', platform);
|
|
|
|
await fs.mkdir(dir, { recursive: true });
|
|
|
|
await writeJson(result, path.join(dir, 'manifest.json'));
|
2020-11-03 00:41:28 +01:00
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
};
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-03-31 05:12:28 +02:00
|
|
|
// dev: add perms
|
2020-03-09 01:55:02 +01:00
|
|
|
const envDev = createTaskForModifyManifestForEnvironment((manifest) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
manifest.permissions = [...manifest.permissions, 'webRequestBlocking'];
|
|
|
|
});
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-03-31 05:12:28 +02:00
|
|
|
// testDev: add perms
|
2020-03-09 01:55:02 +01:00
|
|
|
const envTestDev = createTaskForModifyManifestForEnvironment((manifest) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
manifest.permissions = [
|
|
|
|
...manifest.permissions,
|
|
|
|
'webRequestBlocking',
|
|
|
|
'http://localhost/*',
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
|
|
|
});
|
2020-03-09 01:55:02 +01:00
|
|
|
|
|
|
|
// test: add permissions
|
|
|
|
const envTest = createTaskForModifyManifestForEnvironment((manifest) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
manifest.permissions = [
|
|
|
|
...manifest.permissions,
|
|
|
|
'webRequestBlocking',
|
|
|
|
'http://localhost/*',
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
|
|
|
});
|
2020-03-09 01:55:02 +01:00
|
|
|
|
|
|
|
// high level manifest tasks
|
2021-02-04 19:15:23 +01:00
|
|
|
const dev = createTask('manifest:dev', composeSeries(prepPlatforms, envDev));
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const testDev = createTask(
|
|
|
|
'manifest:testDev',
|
|
|
|
composeSeries(prepPlatforms, envTestDev),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const test = createTask(
|
|
|
|
'manifest:test',
|
|
|
|
composeSeries(prepPlatforms, envTest),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const prod = createTask('manifest:prod', prepPlatforms);
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return { prod, dev, testDev, test };
|
2020-03-09 01:55:02 +01:00
|
|
|
|
|
|
|
// helper for modifying each platform's manifest.json in place
|
2020-11-03 00:41:28 +01:00
|
|
|
function createTaskForModifyManifestForEnvironment(transformFn) {
|
2020-03-09 01:55:02 +01:00
|
|
|
return () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
return Promise.all(
|
|
|
|
browserPlatforms.map(async (platform) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const manifestPath = path.join(
|
|
|
|
'.',
|
|
|
|
'dist',
|
|
|
|
platform,
|
|
|
|
'manifest.json',
|
|
|
|
);
|
|
|
|
const manifest = await readJson(manifestPath);
|
|
|
|
transformFn(manifest);
|
|
|
|
await writeJson(manifest, manifestPath);
|
2020-11-03 00:41:28 +01:00
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
};
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper for reading and deserializing json from fs
|
2020-11-03 00:41:28 +01:00
|
|
|
async function readJson(file) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return JSON.parse(await fs.readFile(file, 'utf8'));
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper for serializing and writing json to fs
|
2020-11-03 00:41:28 +01:00
|
|
|
async function writeJson(obj, file) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return fs.writeFile(file, JSON.stringify(obj, null, 2));
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
2021-09-08 22:08:23 +02:00
|
|
|
|
2021-11-11 00:03:59 +01:00
|
|
|
/**
|
|
|
|
* 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 {};
|
Rationalize build system arguments (#12047)
This rationalizes how arguments are passed to and parsed by the build system. To accomplish this, everything that isn't an environment variable from `.metamaskrc` or our CI environment is now passed as an argument on the command line.
Of such arguments, the `entryTask` is still expected as a positional argument in the first position (i.e. `process.argv[2]`), but everything else must be passed as a named argument. We use `minimist` to parse the arguments, and set defaults to preserve existing behavior.
Arguments are parsed in a new function, `parseArgv`, in `development/build/index.js`. They are assigned to environment variables where convenient, and otherwise returned from `parseArgv` to be passed to other functions invoked in the same file.
This change is motivated by our previous inconsistent handling of arguments to the build system, which will grow increasingly problematic as the build system grows in complexity. (Which it will very shortly, as we introduce Flask builds.)
Miscellaneous changes:
- Adds a build system readme at `development/build/README.md`
- Removes the `beta` package script. Now, we can instead call: `yarn dist --build-type beta`
- Fixes the casing of some log messages and reorders some parameters in the build system
2021-09-09 21:44:57 +02:00
|
|
|
}
|
2021-11-11 00:03:59 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 19:44:48 +02:00
|
|
|
return buildModifications;
|
2021-09-08 22:08:23 +02:00
|
|
|
}
|