-
Notifications
You must be signed in to change notification settings - Fork 104
/
getReactScriptsPath.ts
58 lines (52 loc) · 1.56 KB
/
getReactScriptsPath.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { readFileSync, realpathSync } from 'fs';
import { join } from 'path';
const getReactScriptsPath = (): string => {
const cwd = process.cwd();
const scriptsBinPath = join(cwd, '/node_modules/.bin/react-scripts');
if (process.platform === 'win32') {
/*
* Try to find the scripts package on Windows by following the `react-scripts` CMD file.
* https://github.com/storybookjs/storybook/issues/5801
*/
try {
const content = readFileSync(scriptsBinPath, 'utf8');
// eslint-disable-next-line @typescript-eslint/prefer-regexp-exec
const packagePathMatch = content.match(
/"\$basedir[\\/](\S+?)[\\/]bin[\\/]react-scripts\.js"/i,
);
if (packagePathMatch && packagePathMatch.length > 1) {
const scriptsPath = join(
cwd,
'/node_modules/.bin/',
packagePathMatch[1],
);
return scriptsPath;
}
} catch (e) {
// NOOP
}
} else {
/*
* Try to find the scripts package by following the `react-scripts` symlink.
* This won't work for Windows users, unless within WSL.
*/
try {
const resolvedBinPath = realpathSync(scriptsBinPath);
const scriptsPath = join(resolvedBinPath, '..', '..');
return scriptsPath;
} catch (e) {
// NOOP
}
}
/*
* Try to find the `react-scripts` package by name (won't catch forked scripts packages).
*/
try {
const scriptsPath = require.resolve('react-scripts');
return scriptsPath;
} catch (e) {
// NOOP
}
return '';
};
export { getReactScriptsPath };