UNPKG

2.3 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getReactScriptsPath = getReactScriptsPath;
7exports.isReactScriptsInstalled = isReactScriptsInstalled;
8
9var _fs = _interopRequireDefault(require("fs"));
10
11var _path = _interopRequireDefault(require("path"));
12
13var _semver = _interopRequireDefault(require("@storybook/semver"));
14
15var _nodeLogger = require("@storybook/node-logger");
16
17function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
19var appDirectory = _fs.default.realpathSync(process.cwd());
20
21var reactScriptsPath;
22
23function getReactScriptsPath({
24 noCache: noCache
25} = {}) {
26 if (reactScriptsPath && !noCache) return reactScriptsPath;
27
28 var reactScriptsScriptPath = _fs.default.realpathSync(_path.default.join(appDirectory, '/node_modules/.bin/react-scripts'));
29
30 try {
31 // Note: Since there is no symlink for .bin/react-scripts on Windows
32 // we'll parse react-scripts file to find actual package path.
33 // This is important if you use fork of CRA.
34 var pathIsNotResolved = /node_modules[\\/]\.bin[\\/]react-scripts/i.test(reactScriptsScriptPath);
35
36 if (pathIsNotResolved) {
37 var content = _fs.default.readFileSync(reactScriptsScriptPath, 'utf8');
38
39 var packagePathMatch = content.match(/"\$basedir[\\/]([^\s]+?[\\/]bin[\\/]react-scripts\.js")/i);
40
41 if (packagePathMatch && packagePathMatch.length > 1) {
42 reactScriptsScriptPath = _path.default.join(appDirectory, '/node_modules/.bin/', packagePathMatch[1]);
43 }
44 }
45 } catch (e) {
46 _nodeLogger.logger.warn(`Error occurred during react-scripts package path resolving: ${e}`);
47 }
48
49 reactScriptsPath = _path.default.join(reactScriptsScriptPath, '../..');
50
51 var scriptsPkgJson = _path.default.join(reactScriptsPath, 'package.json');
52
53 if (!_fs.default.existsSync(scriptsPkgJson)) {
54 reactScriptsPath = 'react-scripts';
55 }
56
57 return reactScriptsPath;
58}
59
60function isReactScriptsInstalled(requiredVersion = '2.0.0') {
61 try {
62 // eslint-disable-next-line import/no-dynamic-require,global-require
63 var reactScriptsJson = require(_path.default.join(getReactScriptsPath(), 'package.json'));
64
65 return !_semver.default.gtr(requiredVersion, reactScriptsJson.version);
66 } catch (e) {
67 return false;
68 }
69}
\No newline at end of file