Re-write app build, created macOS installer package

This commit is contained in:
hensm
2018-11-27 20:10:30 +00:00
parent c63531cdce
commit 267c92825c
15 changed files with 1761 additions and 976 deletions

View File

@@ -1,45 +1,154 @@
const fs = require('fs'); const fs = require("fs-extra");
const os = require('os'); const os = require("os");
const path = require('path'); 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 argv = require("minimist")(process.argv.slice(2));
const rollup = require('rollup');
const config = require('../rollup/rollup.config').default;
const MANIFEST_NAME = 'fx_cast_bridge.json'; const MANIFEST_NAME = "fx_cast_bridge.json";
async function build() { const BUILD_DIR_PATH = path.join(__dirname, "../build");
const {path: executablePath, platform, ...configOptions} = argv; const DIST_DIR_PATH = path.join(__dirname, "../../dist/app");
const {output: bundleOutputs, ...bundleOptions} = config(configOptions); try {
const bundle = await rollup.rollup(bundleOptions); // Make directories
fs.mkdirSync(BUILD_DIR_PATH);
fs.mkdirSync(DIST_DIR_PATH, { recursive: true });
} catch (err) {}
for (const output of bundleOutputs) {
await bundle.write(output);
fs.chmodSync(output.file, '755');
}
const targetPlatform = platform || os.type(); const executableName = {
const launcherExt = targetPlatform.toLowerCase().startsWith('win') win32: "bridge.exe"
? 'bat' , darwin: "bridge"
: 'sh'; , linux: "bridge"
const launcherName = `launcher.${launcherExt}`; };
const launcherPath = path.join(__dirname, '../../dist/app', launcherName);
fs.copyFileSync(path.join(`src`, launcherName), launcherPath); 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"
};
async function build () {
const platform = argv.platform || os.platform();
// Run Babel
spawnSync(`babel src -d ${BUILD_DIR_PATH} --copy-files `
, { shell: true });
// Add build platform's executable path to the manifest
const manifest = { const manifest = {
...(JSON.parse(fs.readFileSync(`src/${MANIFEST_NAME}`, 'utf8'))) ...(JSON.parse(fs.readFileSync(MANIFEST_NAME, "utf8")))
, path: (executablePath || path.resolve(launcherPath)) , path: path.join(executablePath[platform], executableName[platform])
}; };
fs.writeFileSync( // Write manifest
path.join('../dist/app', MANIFEST_NAME) fs.writeFileSync(path.join(BUILD_DIR_PATH, MANIFEST_NAME)
, JSON.stringify(manifest, null, 4) , 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 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(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, MANIFEST_NAME)
, path.join(DIST_DIR_PATH, MANIFEST_NAME)
, { 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";
// 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.mkdirSync(rootExecutablePath, { recursive: true });
fs.mkdirSync(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));
// Build installer package
spawnSync(
`pkgbuild --root ${rootPath} `
+ `--identifier "tf.matt.fx_cast_bridge" `
+ `--version "0.0.1" `
+ `${path.join(BUILD_DIR_PATH, installerName)}`
, { shell: true });
return installerName;
};
case "win32":
case "linux":
// TODO: installers
default:
console.log("Cannot build installer package for this platform");
}
} }
build().catch(e => { build().catch(e => {

View File

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

2397
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", "version": "0.1.0-alpha1",
"scripts": { "scripts": {
"build": "node bin/build.js", "build": "node bin/build.js",
"package": "node bin/build.js --package",
"install-manifest": "node bin/install-manifest.js", "install-manifest": "node bin/install-manifest.js",
"start-debug": "node bin/start-debug.js", "start-debug": "node bin/start-debug.js"
"start": "node dist/app.js"
}, },
"dependencies": { "dependencies": {
"@babel/runtime": "^7.0.0", "@babel/runtime": "^7.0.0",
"castv2": "^0.1.9", "castv2": "^0.1.9",
"mdns-js": "^1.0.1", "mdns-js": "^1.0.1"
"mkdirp": "^0.5.1"
}, },
"devDependencies": { "devDependencies": {
"@babel/cli": "^7.1.5",
"@babel/core": "^7.0.0", "@babel/core": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0", "@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-json-strings": "^7.0.0", "@babel/plugin-proposal-json-strings": "^7.0.0",
@@ -23,14 +23,9 @@
"@babel/plugin-transform-runtime": "^7.0.0", "@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.0.0", "@babel/preset-env": "^7.0.0",
"@babel/register": "^7.0.0", "@babel/register": "^7.0.0",
"magic-string": "^0.25.0", "fs-extra": "^7.0.1",
"minimist": "^1.2.0", "minimist": "^1.2.0",
"rollup": "^0.66.0", "pkg": "^4.3.4"
"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"
}, },
"optionalDependencies": { "optionalDependencies": {
"regedit": "^3.0.0" "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,10 +1,11 @@
import { Client } from 'castv2' import { Client } from "castv2"
import { createBrowser, tcp } from 'mdns-js' import { createBrowser, tcp } from "mdns-js"
import http from 'http' import http from "http"
import fs from 'fs' import fs from "fs"
import path from "path"
import * as transforms from './transforms' import * as transforms from "./transforms"
const browser = createBrowser(tcp("googlecast")); const browser = createBrowser(tcp("googlecast"));

View File

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

17
ext/package-lock.json generated
View File

@@ -4373,6 +4373,17 @@
"requires": { "requires": {
"lodash": "^4.14.0" "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"
}
} }
} }
}, },
@@ -4477,9 +4488,9 @@
"dev": true "dev": true
}, },
"fs-extra": { "fs-extra": {
"version": "4.0.3", "version": "7.0.1",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-4.0.3.tgz", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-7.0.1.tgz",
"integrity": "sha512-q6rbdDd1o2mAnQreO7YADIxf/Whx4AHBiRf6d+/cVT8h44ss+lHgxf1FemcqDnQt9X3ct4McHr+JMGlYSsK7Cg==", "integrity": "sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw==",
"dev": true, "dev": true,
"requires": { "requires": {
"graceful-fs": "^4.1.2", "graceful-fs": "^4.1.2",

View File

@@ -1,6 +1,7 @@
{ {
"scripts": { "scripts": {
"build": "node build.js", "build": "node build.js",
"package": "node build.js --package",
"watch": "node build.js --watch" "watch": "node build.js --watch"
}, },
"devDependencies": { "devDependencies": {
@@ -12,6 +13,7 @@
"@babel/preset-react": "^7.0.0", "@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.4", "babel-loader": "^8.0.4",
"copy-webpack-plugin": "^4.3.1", "copy-webpack-plugin": "^4.3.1",
"fs-extra": "^7.0.1",
"minimist": "^1.2.0", "minimist": "^1.2.0",
"react": "^16.6.1", "react": "^16.6.1",
"react-dom": "^16.6.1", "react-dom": "^16.6.1",

View File

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

View File

@@ -6,6 +6,9 @@
"build": "npm run build:app && npm run build:ext", "build": "npm run build:app && npm run build:ext",
"build:app": "npm run build --prefix ./app", "build:app": "npm run build --prefix ./app",
"build:ext": "npm run build --prefix ./ext", "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", "test": "jasmine --config=jasmine.json",
"install-manifest": "npm run install-manifest --prefix ./app" "install-manifest": "npm run install-manifest --prefix ./app"
}, },