Merge pull request #19 from hensm/installers

Create macOS app installer
This commit is contained in:
Matt Hensman
2018-11-28 03:28:08 +00:00
committed by GitHub
20 changed files with 1873 additions and 1057 deletions

View File

@@ -1,45 +1,148 @@
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");
require('@babel/register');
const { spawnSync } = require("child_process");
const { exec: pkgExec } = require("pkg");
const argv = require('minimist')(process.argv.slice(2));
const rollup = require('rollup');
const { executableName
, executablePath
, manifestName
, manifestPath
, pkgPlatform
, DIST_DIR_PATH } = require("./lib/paths");
const config = require('../rollup/rollup.config').default;
const argv = require("minimist")(process.argv.slice(2));
const MANIFEST_NAME = 'fx_cast_bridge.json';
async function build() {
const {path: executablePath, platform, ...configOptions} = argv;
const BUILD_DIR_PATH = path.join(__dirname, "../build");
const {output: bundleOutputs, ...bundleOptions} = config(configOptions);
const bundle = await rollup.rollup(bundleOptions);
// Clean
fs.removeSync(DIST_DIR_PATH);
for (const output of bundleOutputs) {
await bundle.write(output);
fs.chmodSync(output.file, '755');
// Make directories
fs.ensureDirSync(BUILD_DIR_PATH);
fs.ensureDirSync(DIST_DIR_PATH, { recursive: true });
async function build () {
const platform = argv.platform || os.platform();
// Run Babel
spawnSync(`babel src -d ${BUILD_DIR_PATH} --copy-files `
, { shell: true });
// 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, manifestName)
, JSON.stringify(manifest, null, 4));
// File permissions
for (const file of fs.readdirSync(BUILD_DIR_PATH)) {
fs.chmodSync(path.resolve(BUILD_DIR_PATH, file), 0o755);
}
const targetPlatform = platform || os.type();
const launcherExt = targetPlatform.toLowerCase().startsWith('win')
? 'bat'
: 'sh';
const launcherName = `launcher.${launcherExt}`;
const launcherPath = path.join(__dirname, '../../dist/app', launcherName);
fs.copyFileSync(path.join(`src`, launcherName), launcherPath);
const manifest = {
...(JSON.parse(fs.readFileSync(`src/${MANIFEST_NAME}`, 'utf8')))
, path: (executablePath || path.resolve(launcherPath))
const pkgInfo = {
name: "bridge"
, bin: "main.js"
, pkg: {
// Workaround for pkg asset detection
// https://github.com/thibauts/node-castv2/issues/46
"assets": "../node_modules/castv2/lib/cast_channel.proto"
}
};
fs.writeFileSync(
path.join('../dist/app', MANIFEST_NAME)
, JSON.stringify(manifest, null, 4)
);
fs.writeFileSync(path.join(BUILD_DIR_PATH, "package.json")
, JSON.stringify(pkgInfo))
// Package executable
await pkgExec([
BUILD_DIR_PATH
, "--target", pkgPlatform[platform]
, "--output", path.join(BUILD_DIR_PATH, executableName[platform])
]);
if (argv.package) {
const installerName = await buildInstaller(platform);
// Move installer to dist
fs.moveSync(path.join(BUILD_DIR_PATH, installerName)
, path.join(DIST_DIR_PATH, installerName)
, { overwrite: true });
} else {
// Move binary / app manifest
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])
, { overwrite: true });
}
// Remove build directory
fs.removeSync(BUILD_DIR_PATH);
}
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");
const rootExecutablePath = path.join(rootPath
, executablePath[platform]);
const rootManifestPath = path.join(rootPath
, manifestPath[platform]);
// Create install locations
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, manifestName)
, path.join(rootManifestPath, manifestName));
// Build component package
spawnSync(
`pkgbuild --root ${rootPath} `
+ `--identifier "tf.matt.fx_cast_bridge" `
+ `--version "0.0.1" `
+ `${componentPath}`
, { shell: true });
// Distribution XML file
const distFilePath = path.join(__dirname, "../distribution.xml");
// Build installer package
spawnSync(
`productbuild --distribution ${distFilePath} `
+ `--package-path ${BUILD_DIR_PATH} `
+ `${installerPath}`
, { shell: true });
return installerName;
};
case "win32":
case "linux":
// TODO: installers
default:
console.log("Cannot build installer package for this platform");
}
}
build().catch(e => {

View File

@@ -1,69 +1,65 @@
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) && !argv.remove) {
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]);
if (argv.remove) {
fs.remove(path.join(destination, manifestName));
break;
}
break;
}
fs.ensureDirSync(destination);
fs.copyFileSync(CURRENT_MANIFEST_PATH
, path.join(destination, manifestName));
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 "win32": {
const regedit = require("regedit");
if (argv.remove) {
// TODO: no corresponding method in regedit lib
break;
}
break;
}
case 'Windows_NT': {
const regedit = require('regedit');
const destinationManifestPath = path.join(destination, MANIFEST_NAME)
|| MANIFEST_PATH;
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)
);
}

29
app/bin/lib/paths.js Normal file
View File

@@ -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"
};

View File

@@ -1,21 +0,0 @@
require('@babel/register')({
presets: [
[
"@babel/preset-env"
, {
targets: {
node: "current"
}
}
]
]
, plugins: [
"@babel/plugin-transform-runtime"
, "@babel/plugin-syntax-dynamic-import"
, "@babel/plugin-syntax-import-meta"
, "@babel/plugin-proposal-class-properties"
, "@babel/plugin-proposal-json-strings"
]
});
require('../src/js/main');

17
app/distribution.xml Normal file
View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<installer-gui-script minSpecVersion="1">
<title>fx_cast Bridge</title>
<domains enable_anywhere="false" enable_currentUserHome="true" enable_localSystem="true" />
<pkg-ref id="tf.matt.fx_cast_bridge"/>
<options customize="never" require-scripts="false"/>
<choices-outline>
<line choice="default">
<line choice="tf.matt.fx_cast_bridge"/>
</line>
</choices-outline>
<choice id="default"/>
<choice id="tf.matt.fx_cast_bridge" visible="false">
<pkg-ref id="tf.matt.fx_cast_bridge"/>
</choice>
<pkg-ref id="tf.matt.fx_cast_bridge" version="0.0.1" onConclusion="none">fx_cast_bridge_default.pkg</pkg-ref>
</installer-gui-script>

View File

@@ -1,7 +1,6 @@
{
"name": "fx_cast_bridge"
, "description": ""
, "path": ""
, "type": "stdio"
, "allowed_extensions": [ "fx_cast@matt.tf" ]
}

2386
app/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -4,17 +4,17 @@
"version": "0.1.0-alpha1",
"scripts": {
"build": "node bin/build.js",
"package": "node bin/build.js --package",
"install-manifest": "node bin/install-manifest.js",
"start-debug": "node bin/start-debug.js",
"start": "node dist/app.js"
"remove-manifest": "node bin/install-manifest.js --remove"
},
"dependencies": {
"@babel/runtime": "^7.0.0",
"castv2": "^0.1.9",
"mdns-js": "^1.0.1",
"mkdirp": "^0.5.1"
"mdns-js": "^1.0.1"
},
"devDependencies": {
"@babel/cli": "^7.1.5",
"@babel/core": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-json-strings": "^7.0.0",
@@ -23,14 +23,8 @@
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/register": "^7.0.0",
"magic-string": "^0.25.0",
"minimist": "^1.2.0",
"rollup": "^0.66.0",
"rollup-plugin-babel": "^4.0.3",
"rollup-plugin-commonjs": "^9.1.3",
"rollup-plugin-json": "^3.1.0",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-resolve": "^3.4.0"
"pkg": "^4.3.4"
},
"optionalDependencies": {
"regedit": "^3.0.0"

View File

@@ -1,34 +0,0 @@
import MagicString from 'magic-string';
const MEMCPY_REQUIRE_STRING = 'require("memcpy")';
const REPLACEMENT_STRING = 'null';
// We're doing this because memcpy is an optional dependency of ByteBufferNB,
// but Rollup doesn't understand optional dependencies and tries to use it.
// We can't simply use memcpy, because it has native code for older versions of
// Node and no longer builds!
export default (variant) => {
let mainId = null;
return {
name: 'no-memcpy-plugin'
, transform: (code, id) => {
if (id.endsWith('ByteBufferNB.js')) {
const start = code.indexOf(MEMCPY_REQUIRE_STRING);
if (start >= 0) {
const magicString = new MagicString(code);
const end = start + MEMCPY_REQUIRE_STRING.length;
magicString.overwrite(start, end, REPLACEMENT_STRING);
return {
code: magicString.toString()
, map: magicString.generateMap()
};
}
}
return null;
}
}
}

View File

@@ -1,56 +0,0 @@
import { resolve } from 'path'
import babel from 'rollup-plugin-babel'
import builtins from 'rollup-plugin-node-builtins'
import commonjs from 'rollup-plugin-commonjs'
import json from 'rollup-plugin-json'
import nodeResolve from 'rollup-plugin-node-resolve'
import noMemcpy from './no-memcpy-plugin'
export default options => {
// TODO: Enabling this option presently doesn't work because castv2 proto
// files aren't bundled, can be fixed with a couple of plugins
const external = options.dependencies ? () => false : (id, parentId) => {
if (!parentId) {
return true;
}
return !!id.match(/^[^./].+$/)
|| resolve(parentId, '..', id).includes('/node_modules/');
};
return {
external
, input: './src/js/main.js'
, plugins: [
noMemcpy()
, babel({
exclude: [
'node_modules/**'
]
, runtimeHelpers: true
})
, json()
, builtins()
, nodeResolve({
module: true
, jsnext: true
, main: true
, browser: false
})
, commonjs({
// Protobuf detects itself running in Node by attempting to
// require fs, so we better allow it...
ignore: ['fs']
})
]
, output: [
{
file: resolve(__dirname, '../../dist/app/app.js')
, format: 'cjs'
, sourcemap: options.sourcemap || false
}
]
};
}

View File

@@ -1,2 +0,0 @@
@echo off
node main.js

View File

@@ -1,4 +0,0 @@
#!/usr/bin/env bash
BASEDIR=$(dirname $0)
PATH=/usr/local/bin:$PATH
AVAHI_COMPAT_NOWARN=1 node $BASEDIR/app.js

View File

@@ -1,11 +1,11 @@
import { Client } from 'castv2'
import { createBrowser, tcp } from 'mdns-js'
import { Client } from "castv2";
import { createBrowser, tcp } from "mdns-js";
import http from 'http'
import fs from 'fs'
import * as transforms from './transforms'
import http from "http";
import fs from "fs";
import path from "path";
import * as transforms from "./transforms";
const browser = createBrowser(tcp("googlecast"));

View File

@@ -1,6 +1,6 @@
"use strict";
const { Transform } = require("stream");
import { Transform } from "stream";
/**

View File

@@ -1,15 +1,26 @@
const { spawn } = require('child_process');
const argv = require('minimist')(process.argv.slice(2));
const fs = require("fs-extra");
const path = require("path");
const { spawn } = require("child_process");
const argv = require("minimist")(process.argv.slice(2));
const extensionName = "fx_cast";
const extensionId = "fx_cast@matt.tf";
const extensionVersion = "0.0.1";
if (argv.package) {
argv.mode = "production";
argv.watch = false;
}
// Default argument values
const { mirroringAppId = "19A6F4AE"
, mode = "development" } = argv;
// Clean
fs.removeSync(path.join(__dirname, "../dist/ext/"));
const child = spawn(
`webpack --env.extensionName=${extensionName} `
+ `--env.extensionId=${extensionId} `
@@ -18,12 +29,16 @@ const child = spawn(
+ `--mode=${mode} `
+ `${argv.watch ? "--watch" : ""} `
+ `&& web-ext build --overwrite-dest `
+ `--source-dir ../dist/ext `
+ `--source-dir ../dist/ext/unpacked `
+ `--artifacts-dir ../dist/ext `
, {
shell: true
}
, { shell: true }
);
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
child.on("exit", () => {
if (argv.package) {
fs.remove(path.join(__dirname, "../dist/ext/unpacked"));
}
});

22
ext/package-lock.json generated
View File

@@ -4373,6 +4373,17 @@
"requires": {
"lodash": "^4.14.0"
}
},
"fs-extra": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-4.0.3.tgz",
"integrity": "sha512-q6rbdDd1o2mAnQreO7YADIxf/Whx4AHBiRf6d+/cVT8h44ss+lHgxf1FemcqDnQt9X3ct4McHr+JMGlYSsK7Cg==",
"dev": true,
"requires": {
"graceful-fs": "^4.1.2",
"jsonfile": "^4.0.0",
"universalify": "^0.1.0"
}
}
}
},
@@ -4476,17 +4487,6 @@
"integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==",
"dev": true
},
"fs-extra": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-4.0.3.tgz",
"integrity": "sha512-q6rbdDd1o2mAnQreO7YADIxf/Whx4AHBiRf6d+/cVT8h44ss+lHgxf1FemcqDnQt9X3ct4McHr+JMGlYSsK7Cg==",
"dev": true,
"requires": {
"graceful-fs": "^4.1.2",
"jsonfile": "^4.0.0",
"universalify": "^0.1.0"
}
},
"fs-write-stream-atomic": {
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/fs-write-stream-atomic/-/fs-write-stream-atomic-1.0.10.tgz",

View File

@@ -1,6 +1,7 @@
{
"scripts": {
"build": "node build.js",
"package": "node build.js --package",
"watch": "node build.js --watch"
},
"devDependencies": {

View File

@@ -6,7 +6,7 @@ const CopyWebpackPlugin = require("copy-webpack-plugin");
const includePath = path.resolve(__dirname, "src");
const outputPath = path.resolve(__dirname, "../dist/ext/");
const outputPath = path.resolve(__dirname, "../dist/ext/unpacked");
module.exports = (env) => ({
entry: {

32
package-lock.json generated
View File

@@ -42,6 +42,17 @@
"integrity": "sha1-AQ1YWEI6XxGJeWZfRkhqlcbuK7Y=",
"dev": true
},
"fs-extra": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-7.0.1.tgz",
"integrity": "sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw==",
"dev": true,
"requires": {
"graceful-fs": "^4.1.2",
"jsonfile": "^4.0.0",
"universalify": "^0.1.0"
}
},
"fs.realpath": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
@@ -62,6 +73,12 @@
"path-is-absolute": "^1.0.0"
}
},
"graceful-fs": {
"version": "4.1.15",
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.15.tgz",
"integrity": "sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==",
"dev": true
},
"immediate": {
"version": "3.0.6",
"resolved": "https://registry.npmjs.org/immediate/-/immediate-3.0.6.tgz",
@@ -106,6 +123,15 @@
"integrity": "sha512-pa9tbBWgU0EE4SWgc85T4sa886ufuQdsgruQANhECYjwqgV4z7Vw/499aCaP8ZH79JDS4vhm8doDG9HO4+e4sA==",
"dev": true
},
"jsonfile": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz",
"integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=",
"dev": true,
"requires": {
"graceful-fs": "^4.1.6"
}
},
"jszip": {
"version": "3.1.5",
"resolved": "https://registry.npmjs.org/jszip/-/jszip-3.1.5.tgz",
@@ -226,6 +252,12 @@
"os-tmpdir": "~1.0.1"
}
},
"universalify": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz",
"integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==",
"dev": true
},
"util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",

View File

@@ -3,13 +3,22 @@
"postinstall": "npm run postinstall:app && npm run postinstall:ext",
"postinstall:app": "npm install ./app --prefix ./app",
"postinstall:ext": "npm install ./ext --prefix ./ext",
"build": "npm run build:app && npm run build:ext",
"build:app": "npm run build --prefix ./app",
"build:ext": "npm run build --prefix ./ext",
"package": "npm run package:app && npm run package:ext",
"package:app": "npm run package --prefix ./app",
"package:ext": "npm run package --prefix ./ext",
"test": "jasmine --config=jasmine.json",
"install-manifest": "npm run install-manifest --prefix ./app"
"install-manifest": "npm run install-manifest --prefix ./app",
"remove-manifest": "npm run remove-manifest --prefix ./app"
},
"devDependencies": {
"fs-extra": "^7.0.1",
"jasmine": "^3.1.0",
"selenium-webdriver": "^4.0.0-alpha.1"
}