Skip to content

Commit

Permalink
Open the player instead of the browser (#3)
Browse files Browse the repository at this point in the history
* Open the player instead of the browser

* Remove async, change order of checks and return the rejections
  • Loading branch information
MihailTodorov authored Jan 22, 2024
1 parent 0830331 commit 85de8bc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/cra-template-cohtml/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cra-template-cohtml",
"version": "1.0.1",
"version": "1.1.0",
"keywords": [
"react",
"create-react-app",
Expand Down
1 change: 1 addition & 0 deletions packages/cra-template-cohtml/template/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PLAYER_PATH = "./"
2 changes: 1 addition & 1 deletion packages/react-scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-scripts-cohtml",
"version": "1.0.1",
"version": "1.1.0",
"description": "Configuration and scripts for Create React App with COHTML.",
"repository": {
"type": "git",
Expand Down
29 changes: 29 additions & 0 deletions packages/react-scripts/scripts/openPlayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const path = require('path');
const chalk = require('react-dev-utils/chalk');
const fs = require('fs');
const { execFile } = require('child_process');

function openPlayer(url) {
return new Promise((_, reject) => {
if (!process.env.PLAYER_PATH) {
console.error(chalk.red('PLAYER_PATH environment variable not set. Set it in the ".env" file to run the player.'));
return reject();
}

const playerPath = path.resolve(__dirname, process.env.PLAYER_PATH);
if (!fs.existsSync(playerPath)) {
console.error(chalk.red(`Player not found at ${playerPath}. Set the PLAYER_PATH environment variable in the ".env" file to run the player.`));
return reject();
}

if (path.extname(process.env.PLAYER_PATH) !== '.exe') {
console.error(chalk.red('PLAYER_PATH environment variable should point to the player executable. Set it in the ".env" file to run the player.'));
return reject();
}


execFile(playerPath, ['--player', `--url=${url}`, '--root'], { stdio: 'inherit' });
});
}

module.exports = openPlayer;
9 changes: 7 additions & 2 deletions packages/react-scripts/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const {
prepareProxy,
prepareUrls,
} = require('react-dev-utils/WebpackDevServerUtils');
const openBrowser = require('react-dev-utils/openBrowser');
const openPlayer = require('./openPlayer');
const semver = require('semver');
const paths = require('../config/paths');
const configFactory = require('../config/webpack.config');
Expand Down Expand Up @@ -136,7 +136,12 @@ checkBrowsers(paths.appPath, isInteractive)
}

console.log(chalk.cyan('Starting the development server...\n'));
openBrowser(urls.localUrlForBrowser);
if (process.env.NO_PLAYER) return;

openPlayer(urls.localUrlForBrowser).catch(() => {
devServer.close();
process.exit();
});
});

['SIGINT', 'SIGTERM'].forEach(function (sig) {
Expand Down

0 comments on commit 85de8bc

Please sign in to comment.