diff --git a/app/bin/build.js b/app/bin/build.js index 16c07f9..b9ebb81 100644 --- a/app/bin/build.js +++ b/app/bin/build.js @@ -5,43 +5,21 @@ const path = require("path"); const { spawnSync } = require("child_process"); const { exec: pkgExec } = require("pkg"); +const { executableName + , executablePath + , manifestName + , manifestPath + , pkgPlatform + , DIST_DIR_PATH } = require("./lib/paths"); + const argv = require("minimist")(process.argv.slice(2)); -const MANIFEST_NAME = "fx_cast_bridge.json"; - const BUILD_DIR_PATH = path.join(__dirname, "../build"); -const DIST_DIR_PATH = path.join(__dirname, "../../dist/app"); -try { - // Make directories - fs.mkdirSync(BUILD_DIR_PATH); - fs.mkdirSync(DIST_DIR_PATH, { recursive: true }); -} catch (err) {} - - -const executableName = { - win32: "bridge.exe" - , darwin: "bridge" - , linux: "bridge" -}; - -const executablePath = { - win32: "C:\\Program Files\\fx_cast\\" - , darwin: "/Library/Application Support/fx_cast/" - , linux: "/opt/fx_cast/" -}; - -const manifestPath = { - darwin: "/Library/Application Support/Mozilla/NativeMessagingHosts/" - , linux: "/usr/lib/mozilla/native-messaging-hosts/" -}; - -const pkgPlatform = { - win32: "win" - , darwin: "macos" - , linux: "linux" -}; +// Make directories +fs.ensureDirSync(BUILD_DIR_PATH); +fs.ensureDirSync(DIST_DIR_PATH, { recursive: true }); async function build () { @@ -51,14 +29,14 @@ async function build () { spawnSync(`babel src -d ${BUILD_DIR_PATH} --copy-files ` , { shell: true }); - // Add build platform's executable path to the manifest - const manifest = { - ...(JSON.parse(fs.readFileSync(MANIFEST_NAME, "utf8"))) - , path: path.join(executablePath[platform], executableName[platform]) - }; + // Add either installed path or dist path to app manifest + const manifest = JSON.parse(fs.readFileSync(manifestName, "utf8")); + manifest.path = argv.package + ? path.join(executablePath[platform], executableName[platform]) + : path.join(DIST_DIR_PATH, executableName[platform]); // Write manifest - fs.writeFileSync(path.join(BUILD_DIR_PATH, MANIFEST_NAME) + fs.writeFileSync(path.join(BUILD_DIR_PATH, manifestName) , JSON.stringify(manifest, null, 4)); @@ -97,8 +75,8 @@ async function build () { , { overwrite: true }); } else { // Move binary / app manifest - fs.moveSync(path.join(BUILD_DIR_PATH, MANIFEST_NAME) - , path.join(DIST_DIR_PATH, MANIFEST_NAME) + fs.moveSync(path.join(BUILD_DIR_PATH, manifestName) + , path.join(DIST_DIR_PATH, manifestName) , { overwrite: true }); fs.moveSync(path.join(BUILD_DIR_PATH, executableName[platform]) , path.join(DIST_DIR_PATH, executableName[platform]) @@ -113,6 +91,9 @@ async function buildInstaller (platform) { switch (platform) { case "darwin": { const installerName = "fx_cast_bridge.pkg"; + const componentName = "fx_cast_bridge_default.pkg"; + const installerPath = path.join(BUILD_DIR_PATH, installerName); + const componentPath = path.join(BUILD_DIR_PATH, componentName); // Create pkgbuild root const rootPath = path.join(BUILD_DIR_PATH, "root"); @@ -122,21 +103,31 @@ async function buildInstaller (platform) { , manifestPath[platform]); // Create install locations - fs.mkdirSync(rootExecutablePath, { recursive: true }); - fs.mkdirSync(rootManifestPath, { recursive: true }); + fs.ensureDirSync(rootExecutablePath, { recursive: true }); + fs.ensureDirSync(rootManifestPath, { recursive: true }); // Move files to root fs.moveSync(path.join(BUILD_DIR_PATH, executableName[platform]) , path.join(rootExecutablePath, executableName[platform])); - fs.moveSync(path.join(BUILD_DIR_PATH, MANIFEST_NAME) - , path.join(rootManifestPath, MANIFEST_NAME)); + fs.moveSync(path.join(BUILD_DIR_PATH, manifestName) + , path.join(rootManifestPath, manifestName)); - // Build installer package + // Build component package spawnSync( `pkgbuild --root ${rootPath} ` + `--identifier "tf.matt.fx_cast_bridge" ` + `--version "0.0.1" ` - + `${path.join(BUILD_DIR_PATH, installerName)}` + + `${componentPath}` + , { shell: true }); + + // Distribution XML file + const distFilePath = path.join(__dirname, "../distribution.xml"); + + // Build installer package + spawnSync( + `productbuild --distribution ${distFilePath} ` + + `--package-path ${componentPath} ` + + `${installerPath}` , { shell: true }); return installerName; diff --git a/app/bin/install-manifest.js b/app/bin/install-manifest.js index a14e144..2f3cf52 100644 --- a/app/bin/install-manifest.js +++ b/app/bin/install-manifest.js @@ -1,69 +1,55 @@ -const fs = require('fs'); -const os = require('os'); -const path = require('path'); +const fs = require("fs-extra"); +const os = require("os"); +const path = require("path"); -const argv = require('minimist')(process.argv.slice(2)); -const mkdirpSync = require('mkdirp').sync; +const { executableName + , executablePath + , manifestName + , manifestPath + , DIST_DIR_PATH } = require("./lib/paths"); -const MANIFEST_NAME = 'fx_cast_bridge.json'; -const MANIFEST_PATH = path.resolve(__dirname, '../../dist/app', MANIFEST_NAME); +const argv = require("minimist")(process.argv.slice(2)); -const WIN_REGISTRY_KEY = 'fx_cast_bridge'; -let destination = argv.destination; +const CURRENT_MANIFEST_PATH = path.join(DIST_DIR_PATH, manifestName); +const WIN_REGISTRY_KEY = "fx_cast_bridge"; -switch (os.type()) { - case 'Darwin': { - if (!destination) { - const root = argv.root || process.env.HOME; - destination = path.resolve( - path.join( - root - , 'Library/Application Support/Mozilla/NativeMessagingHosts' - ) - ); - } + +if (!fs.existsSync(CURRENT_MANIFEST_PATH)) { + console.error("No manifest in dist/app/ to install"); + process.exit(1); +} + +const platform = os.platform(); + +switch (platform) { + case "darwin": + case "linux": { + const destination = path.join(os.homedir(), manifestPath[platform]); + + fs.ensureDirSync(destination); + fs.copyFileSync(CURRENT_MANIFEST_PATH + , path.join(destination, manifestName)); break; - } + }; - case 'Linux': { - if (!destination) { - const root = argv.root || `${process.env.HOME}/.`; - destination = root.endsWith('/.') - ? `${root}mozilla/native-messaging-hosts/` - : path.join(root, 'mozilla/native-messaging-hosts/'); - } - - break; - } - - case 'Windows_NT': { - const regedit = require('regedit'); - const destinationManifestPath = path.join(destination, MANIFEST_NAME) - || MANIFEST_PATH; + case "win32": { + const regedit = require("regedit"); regedit.putValue({ - 'HKEY_CURRENT_USER\\SOFTWARE\\Mozilla\\NativeMessagingHosts': { + "HKEY_CURRENT_USER\\SOFTWARE\\Mozilla\\NativeMessagingHosts": { [WIN_REGISTRY_KEY]: { - value: destinationManifestPath - , type: 'REG_DEFAULT' + value: CURRENT_MANIFEST_PATH + , type: "REG_DEFAULT" } } }); break; - } + }; default: - console.error('Sorry, this installer does not yet support your OS'); + console.error("Sorry, this installer does not yet support your OS"); process.exit(1); } - -if (destination) { - mkdirpSync(destination); - fs.copyFileSync( - MANIFEST_PATH - , path.join(destination, MANIFEST_NAME) - ); -} diff --git a/app/bin/lib/paths.js b/app/bin/lib/paths.js new file mode 100644 index 0000000..b45ab38 --- /dev/null +++ b/app/bin/lib/paths.js @@ -0,0 +1,29 @@ +const path = require("path"); + +exports.DIST_DIR_PATH = path.join(__dirname, "../../../dist/app"); + +exports.executableName = { + win32: "bridge.exe" + , darwin: "bridge" + , linux: "bridge" +}; + +exports.executablePath = { + win32: "C:\\Program Files\\fx_cast\\" + , darwin: "/Library/Application Support/fx_cast/" + , linux: "/opt/fx_cast/" +}; + +exports.manifestName = "fx_cast_bridge.json"; + +exports.manifestPath = { + darwin: "/Library/Application Support/Mozilla/NativeMessagingHosts/" + , linux: ".mozilla/native-messaging-hosts/" + , win32: "C:\\Program Files\\fx_cast\\" +}; + +exports.pkgPlatform = { + win32: "win" + , darwin: "macos" + , linux: "linux" +}; diff --git a/app/distribution.xml b/app/distribution.xml new file mode 100644 index 0000000..cf42dff --- /dev/null +++ b/app/distribution.xml @@ -0,0 +1,17 @@ + + + fx_cast Bridge + + + + + + + + + + + + + fx_cast_bridge.pkg +