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');
|
2021-05-10 23:16:03 +02:00
|
|
|
const { version } = require('../../package.json');
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { createTask, composeSeries } = require('./task');
|
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
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function createManifestTasks({ browserPlatforms }) {
|
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),
|
|
|
|
{ version },
|
|
|
|
platformModifications,
|
|
|
|
);
|
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
|
|
|
}
|