From 012bfa736481f076e8b40c07838c4757c8648f5b Mon Sep 17 00:00:00 2001 From: igor-dv Date: Sat, 24 Nov 2018 20:04:37 +0200 Subject: [PATCH] Use "fs" to check if the r-s/pkg.json exists --- app/react/src/server/cra-config.js | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/app/react/src/server/cra-config.js b/app/react/src/server/cra-config.js index 403ece7a536..d5875e26ca9 100644 --- a/app/react/src/server/cra-config.js +++ b/app/react/src/server/cra-config.js @@ -14,23 +14,16 @@ let reactScriptsPath; export function getReactScriptsPath({ noCache } = {}) { if (reactScriptsPath && !noCache) return reactScriptsPath; - try { - const appDirectory = fs.realpathSync(process.cwd()); - const reactScriptsScriptPath = fs.realpathSync( - path.join(appDirectory, '/node_modules/.bin/react-scripts') - ); + const appDirectory = fs.realpathSync(process.cwd()); + const reactScriptsScriptPath = fs.realpathSync( + path.join(appDirectory, '/node_modules/.bin/react-scripts') + ); - reactScriptsPath = path.join(reactScriptsScriptPath, '../..'); - const scriptsPkgJson = path.join(reactScriptsPath, 'package.json'); + reactScriptsPath = path.join(reactScriptsScriptPath, '../..'); + const scriptsPkgJson = path.join(reactScriptsPath, 'package.json'); - // eslint-disable-next-line import/no-dynamic-require,global-require - require(scriptsPkgJson); - } catch { + if (!fs.existsSync(scriptsPkgJson)) { reactScriptsPath = 'react-scripts'; - const scriptsPkgJson = path.join(reactScriptsPath, 'package.json'); - - // eslint-disable-next-line import/no-dynamic-require,global-require - require(scriptsPkgJson); } return reactScriptsPath; @@ -39,8 +32,8 @@ export function getReactScriptsPath({ noCache } = {}) { export function isReactScriptsInstalled(requiredVersion = '2.0.0') { try { // eslint-disable-next-line import/no-dynamic-require,global-require - const { version } = require(path.join(getReactScriptsPath(), 'package.json')); - return !semver.lt(version, requiredVersion); + const reactScriptsJson = require(path.join(getReactScriptsPath(), 'package.json')); + return !semver.lt(reactScriptsJson.version, requiredVersion); } catch (e) { return false; }