diff --git a/packages/cra-template-cohtml/package.json b/packages/cra-template-cohtml/package.json index 40bbe91d1ed..23b7f51f23f 100644 --- a/packages/cra-template-cohtml/package.json +++ b/packages/cra-template-cohtml/package.json @@ -1,6 +1,6 @@ { "name": "cra-template-cohtml", - "version": "1.0.1", + "version": "1.1.0", "keywords": [ "react", "create-react-app", diff --git a/packages/cra-template-cohtml/template/.env b/packages/cra-template-cohtml/template/.env new file mode 100644 index 00000000000..1182b841977 --- /dev/null +++ b/packages/cra-template-cohtml/template/.env @@ -0,0 +1 @@ +PLAYER_PATH = "./" \ No newline at end of file diff --git a/packages/react-scripts/package.json b/packages/react-scripts/package.json index adf3bef6546..4916ed77ae3 100644 --- a/packages/react-scripts/package.json +++ b/packages/react-scripts/package.json @@ -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", diff --git a/packages/react-scripts/scripts/openPlayer.js b/packages/react-scripts/scripts/openPlayer.js new file mode 100644 index 00000000000..2c0b8dfb7c4 --- /dev/null +++ b/packages/react-scripts/scripts/openPlayer.js @@ -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; diff --git a/packages/react-scripts/scripts/start.js b/packages/react-scripts/scripts/start.js index 8b9a2c26b4d..a6dd5878f98 100644 --- a/packages/react-scripts/scripts/start.js +++ b/packages/react-scripts/scripts/start.js @@ -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'); @@ -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) {