1
0
mirror of https://github.com/lexogrine/cs2-react-hud.git synced 2025-12-10 02:42:49 +01:00
cs2-react-hud/OpenBrowserPlugin.js
2023-09-11 12:37:32 +02:00

50 lines
1.1 KiB
JavaScript

const open = require('open');
module.exports = class OpenBrowser {
constructor(options) {
if (typeof options === 'string') {
this.options = Object.assign(
{
hasOpen: false
},
{
url: options
}
);
} else {
this.options = Object.assign(
{
port: 8080,
host: 'localhost',
protocol: 'http:',
hasOpen: false
},
options
);
}
}
apply(compiler) {
const options = this.options;
let url;
let hasOpen = options.hasOpen;
if (options.protocol && !options.protocol.endsWith(':')) options.protocol += ':';
if (options.url) url = options.url;
else url = `${options.protocol}//${options.host}:${options.port}`;
if (compiler.hooks) {
compiler.hooks.afterEmit.tap('openBrowser', () => {
if (!hasOpen) open(url);
hasOpen = true;
this.options.hasOpen = true;
});
} else {
compiler.plugin('after-emit', (c, cb) => {
if (!hasOpen) open(url);
hasOpen = true;
this.options.hasOpen = true;
return cb();
});
}
}
};