mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-11 10:09:59 +00:00
Use Node.js APIs for ext build script instead of shell commands
This commit is contained in:
121
ext/build.js
121
ext/build.js
@@ -1,65 +1,112 @@
|
|||||||
const fs = require("fs-extra");
|
const fs = require("fs-extra");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const { spawn } = require("child_process");
|
|
||||||
const minimist = require("minimist");
|
const minimist = require("minimist");
|
||||||
|
const webpack = require("webpack");
|
||||||
|
const webExt = require("web-ext").default;
|
||||||
|
|
||||||
|
|
||||||
|
const DIST_PATH = path.join(__dirname, "../dist/ext");
|
||||||
|
const UNPACKED_PATH = path.join(DIST_PATH, "unpacked");
|
||||||
|
|
||||||
|
|
||||||
const argv = minimist(process.argv.slice(2), {
|
const argv = minimist(process.argv.slice(2), {
|
||||||
boolean: [ "package", "watch" ]
|
boolean: [ "package", "watch" ]
|
||||||
, string: [ "mirroringAppId", "mode" ]
|
, string: [ "mirroringAppId", "mode" ]
|
||||||
, default: {
|
, default: {
|
||||||
package: false
|
package: false // Should package with web-ext
|
||||||
, watch: false
|
, watch: false // Should run webpack in watch mode
|
||||||
, mirroringAppId: "19A6F4AE"
|
, mirroringAppId: "19A6F4AE" // Chromecast mirroring receiver app ID
|
||||||
, mode: "development"
|
, mode: "development" // webpack mode
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (argv.package && argv.watch) {
|
||||||
|
console.error("Cannot package whilst watching files.");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If packaging, use production mode
|
||||||
if (argv.package) {
|
if (argv.package) {
|
||||||
argv.mode = "production";
|
argv.mode = "production";
|
||||||
argv.watch = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const extensionName = "fx_cast";
|
// Import webpack config and specify env values
|
||||||
const extensionId = "fx_cast@matt.tf";
|
const webpackConfig = require("./webpack.config.js")({
|
||||||
const extensionVersion = "0.0.1";
|
extensionName: "fx_cast"
|
||||||
|
, extensionId: "fx_cast@matt.tf"
|
||||||
|
, extensionVersion: "0.0.1"
|
||||||
|
, mirroringAppId: argv.mirroringAppId
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add mode to config
|
||||||
|
webpackConfig.mode = argv.mode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If watching files, output directly to dist. Unpacked
|
||||||
|
* directory is used as a staging area for web-ext builds.
|
||||||
|
*/
|
||||||
|
webpackConfig.output.path = argv.package
|
||||||
|
? UNPACKED_PATH
|
||||||
|
: DIST_PATH;
|
||||||
|
|
||||||
const DIST_PATH = path.join(__dirname, "../dist/ext");
|
|
||||||
const UNPACKED_PATH = path.join(DIST_PATH, "unpacked");
|
|
||||||
|
|
||||||
// Clean
|
// Clean
|
||||||
fs.removeSync(DIST_PATH);
|
fs.removeSync(DIST_PATH);
|
||||||
|
|
||||||
const buildCmd = `web-ext build --overwrite-dest `
|
|
||||||
+ `--source-dir ${UNPACKED_PATH} `
|
|
||||||
+ `--artifacts-dir ${DIST_PATH} `;
|
|
||||||
|
|
||||||
const child = spawn(
|
// Create webpack compiler instance
|
||||||
`webpack --env.extensionName=${extensionName} `
|
const compiler = webpack(webpackConfig);
|
||||||
+ `--env.extensionId=${extensionId} `
|
|
||||||
+ `--env.extensionVersion=${extensionVersion} `
|
|
||||||
+ `--env.mirroringAppId=${argv.mirroringAppId} `
|
|
||||||
+ `--mode=${argv.mode} `
|
|
||||||
+ `${argv.watch ? "--watch" : ""} `
|
|
||||||
+ `${argv.package ? "&&" + buildCmd : ""} `
|
|
||||||
, { shell: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
child.stdout.pipe(process.stdout);
|
if (argv.watch) {
|
||||||
child.stderr.pipe(process.stderr);
|
// Start webpack watch
|
||||||
|
compiler.watch({}, handleCompilerOutput);
|
||||||
|
} else {
|
||||||
|
compiler.run((...args) => {
|
||||||
|
handleCompilerOutput(...args);
|
||||||
|
|
||||||
child.on("exit", () => {
|
if (argv.package) {
|
||||||
if (argv.package) {
|
webExt.cmd.build({
|
||||||
fs.remove(UNPACKED_PATH);
|
/**
|
||||||
} else {
|
* Webpack output at sourceDir is built into an extension
|
||||||
for (const file of fs.readdirSync(UNPACKED_PATH)) {
|
* archive at artifactsDir.
|
||||||
fs.moveSync(path.join(UNPACKED_PATH, file)
|
*/
|
||||||
, path.join(DIST_PATH, file)
|
sourceDir: UNPACKED_PATH
|
||||||
, { overwrite: true });
|
, artifactsDir: DIST_PATH
|
||||||
|
, overwriteDest: true
|
||||||
|
}, {
|
||||||
|
// Prevent auto-exit
|
||||||
|
shouldExitProgram: false
|
||||||
|
|
||||||
|
}).then(() => {
|
||||||
|
// Only need the built extension archive
|
||||||
|
fs.remove(UNPACKED_PATH);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log errors and output formatted compilation info.
|
||||||
|
*/
|
||||||
|
function handleCompilerOutput (err, stats) {
|
||||||
|
// If there are fatal errors, log and exit
|
||||||
|
if (err) {
|
||||||
|
console.error(err.stack || err);
|
||||||
|
if (err.details) {
|
||||||
|
console.error(err.details);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove empty unpacked directory
|
return;
|
||||||
fs.remove(UNPACKED_PATH);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
// Get compilation info
|
||||||
|
const info = stats.toJson();
|
||||||
|
|
||||||
|
// Log errors/warnings
|
||||||
|
if (stats.hasErrors()) console.error(info.errors);
|
||||||
|
if (stats.hasWarnings()) console.warn(info.warnings);
|
||||||
|
|
||||||
|
// Log formatted output
|
||||||
|
console.log(stats.toString());
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user