Centralize package info and do version checking between app/ext

This commit is contained in:
hensm
2018-12-11 19:42:01 +00:00
parent d615caf30d
commit 88a5c68a1b
17 changed files with 222 additions and 76 deletions

View File

@@ -4,6 +4,9 @@ const minimist = require("minimist");
const webpack = require("webpack");
const webExt = require("web-ext").default;
const package = require("./package.json");
const appPackage = require("../app/package.json");
const DIST_PATH = path.join(__dirname, "../dist/ext");
const UNPACKED_PATH = path.join(DIST_PATH, "unpacked");
@@ -13,10 +16,10 @@ const argv = minimist(process.argv.slice(2), {
boolean: [ "package", "watch" ]
, string: [ "mirroringAppId", "mode" ]
, default: {
package: false // Should package with web-ext
, watch: false // Should run webpack in watch mode
, mirroringAppId: "19A6F4AE" // Chromecast mirroring receiver app ID
, mode: "development" // webpack mode
package: false // Should package with web-ext
, watch: false // Should run webpack in watch mode
, mirroringAppId: package.__mirroringAppId // Chromecast receiver app ID
, mode: "development" // webpack mode
}
});
@@ -41,9 +44,11 @@ const webpackConfig = require("./webpack.config.js")({
? UNPACKED_PATH
: DIST_PATH
, extensionName: "fx_cast"
, extensionId: "fx_cast@matt.tf"
, extensionVersion: "0.0.1"
, extensionName: package.__extensionName
, extensionId: package.__extensionId
, extensionVersion: package.__extensionVersion
, applicationName: appPackage.__applicationName
, applicationVersion: appPackage.__applicationVersion
, mirroringAppId: argv.mirroringAppId
});

View File

@@ -1,4 +1,9 @@
{
"__extensionName": "fx_cast",
"__extensionId": "fx_cast@matt.tf",
"__extensionVersion": "0.0.1",
"__mirroringAppId": "19A6F4AE",
"scripts": {
"build": "node build.js",
"package": "node build.js --package",

View File

@@ -3,6 +3,9 @@
import defaultOptions from "./options/defaultOptions";
import messageRouter from "./messageRouter";
import semver from "semver";
const _ = browser.i18n.getMessage;
@@ -277,7 +280,7 @@ browser.menus.onClicked.addListener(async (info, tab) => {
await browser.tabs.executeScript(tab.id, {
code: `let selectedMedia = "${info.pageUrl ? "tab" : "screen"}";
let FX_CAST_RECEIVER_APP_ID = "${options.mirroringEnabled}";`
let FX_CAST_RECEIVER_APP_ID = "${options.mirroringAppId}";`
, frameId
});
@@ -329,19 +332,25 @@ function initBridge (tabId, frameId) {
bridgeMap.delete(tabId);
}
const port = browser.runtime.connectNative("fx_cast_bridge");
const port = browser.runtime.connectNative(APPLICATION_NAME);
if (port.error) {
console.error("Failed connect to fx_cast_bridge:", port.error.message);
console.error(`Failed connect to ${APPLICATION_NAME}:`, port.error.message);
} else {
bridgeMap.set(tabId, port);
}
// Start version handoff
port.postMessage({
subject: "bridge:initialize"
, data: EXTENSION_VERSION
});
port.onDisconnect.addListener(p => {
if (p.error) {
console.error("fx_cast_bridge disconnected:", p.error.message);
console.error(`${APPLICATION_NAME} disconnected:`, p.error.message);
} else {
console.log("fx_cast_bridge disconnected");
console.log(`${APPLICATION_NAME} disconnected`);
}
bridgeMap.delete(tabId);
@@ -414,17 +423,39 @@ async function openPopup (tabId) {
messageRouter.register("main", async (message, sender) => {
const tabId = sender.tab.id;
const tabId = sender && sender.tab.id;
switch (message.subject) {
case "main:initialize":
case "main:initialize": {
initBridge(tabId, sender.tab.frameId);
break;
};
case "main:bridgeInitialized": {
const applicationVersion = message.data;
/**
* Compare installed bridge version to the version the
* extension was built alongside and is known to be
* compatible with.
*
* TODO: Determine compatibility with semver and enforce/notify
* user.
*/
if (applicationVersion !== APPLICATION_VERSION) {
console.error(`Expecting ${APPLICATION_NAME} v${APPLICATION_VERSION}, found v${applicationVersion}.`
, semver.lt(applicationVersion, APPLICATION_VERSION)
? "Try updating the native app to the latest version."
: "Try updating the extension to the latest version");
}
break;
};
case "main:openPopup": {
await openPopup(tabId);
break;
}
};
}
});

View File

@@ -25,10 +25,12 @@ module.exports = (env) => ({
}
, plugins: [
new webpack.DefinePlugin({
"EXTENSION_NAME" : JSON.stringify(env.extensionName)
, "EXTENSION_ID" : JSON.stringify(env.extensionId)
, "EXTENSION_VERSION" : JSON.stringify(env.extensionVersion)
, "MIRRORING_APP_ID" : JSON.stringify(env.mirroringAppId)
"EXTENSION_NAME" : JSON.stringify(env.extensionName)
, "EXTENSION_ID" : JSON.stringify(env.extensionId)
, "EXTENSION_VERSION" : JSON.stringify(env.extensionVersion)
, "MIRRORING_APP_ID" : JSON.stringify(env.mirroringAppId)
, "APPLICATION_NAME" : JSON.stringify(env.applicationName)
, "APPLICATION_VERSION" : JSON.stringify(env.applicationVersion)
})
// Copy static assets
@@ -43,7 +45,9 @@ module.exports = (env) => ({
.replace("EXTENSION_NAME", env.extensionName)
.replace("EXTENSION_ID", env.extensionId)
.replace("EXTENSION_VERSION", env.extensionVersion)
.replace("MIRRORING_APP_ID", env.mirroringAppId));
.replace("MIRRORING_APP_ID", env.mirroringAppId)
.replace("APPLICATION_NAME", env.applicationName)
.replace("APPLICATION_VERSION", env.applicationVersion));
}
return content;