-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathhelpers.ts
More file actions
133 lines (112 loc) · 4.19 KB
/
helpers.ts
File metadata and controls
133 lines (112 loc) · 4.19 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { platform } from 'os'
import { existsSync, readFileSync } from 'fs'
import { normalize, join } from 'path'
import { IPluginSettings, isNotDefaultPathToJest } from './Settings'
/**
* Known binary names of `react-scripts` forks
*/
const createReactAppBinaryNames = ['react-scripts', 'react-native-scripts', 'react-scripts-ts', 'react-app-rewired']
/**
* Tries to read the test command from the scripts section within `package.json`
*
* Returns the test command in case of success,
* `undefined` if there was an exception while reading and parsing `package.json`
* `null` if there is no test script
*/
export function getTestCommand(rootPath: string): string | undefined | null {
try {
const packagePath = join(rootPath, 'package.json')
const packageJSON = JSON.parse(readFileSync(packagePath, 'utf8'))
if (packageJSON && packageJSON.scripts && packageJSON.scripts.test) {
return packageJSON.scripts.test
}
return null
} catch {
return undefined
}
}
/**
* Checks if the supplied test command could have been generated by create-react-app
*/
export function isCreateReactAppTestCommand(testCommand: string): boolean {
return testCommand && createReactAppBinaryNames.some(binary => testCommand.indexOf(binary + ' test') === 0)
}
/**
* Checks if the project in `rootPath` was bootstrapped by `create-react-app`.
*/
function isBootstrappedWithCreateReactApp(rootPath: string): boolean {
const testCommand = getTestCommand(rootPath)
if (testCommand === undefined) {
// In case parsing `package.json` failed or was unconclusive,
// fallback to checking for the presence of the binaries in `./node_modules/.bin`
return createReactAppBinaryNames.some(binary => hasNodeExecutable(rootPath, binary))
}
return isCreateReactAppTestCommand(testCommand)
}
function hasNodeExecutable(rootPath: string, executable: string): boolean {
const ext = platform() === 'win32' ? '.cmd' : ''
const absolutePath = join(rootPath, 'node_modules', '.bin', executable + ext)
return existsSync(absolutePath)
}
/**
* Handles getting the jest runner, handling the OS and project specific work too
*
* @returns {string}
*/
export function pathToJest({ pathToJest, rootPath }: IPluginSettings) {
if (isNotDefaultPathToJest(pathToJest)) {
return normalize(pathToJest)
}
if (isBootstrappedWithCreateReactApp(rootPath)) {
return 'npm test --'
}
const localJestExecutable = pathToLocalJestExecutable(rootPath)
if (existsSync(localJestExecutable)) {
return localJestExecutable
}
return `jest${isWindows() ? '.cmd' : ''}`
}
function pathToLocalJestExecutable(rootDir) {
return normalize(join(rootDir, `node_modules/.bin/jest${isWindows() ? '.cmd' : ''}`))
}
function isWindows() {
return platform() === 'win32'
}
/**
* Handles getting the path to config file
*
* @returns {string}
*/
export function pathToConfig(pluginSettings: IPluginSettings) {
if (pluginSettings.pathToConfig !== '') {
return normalize(pluginSettings.pathToConfig)
}
return ''
}
export function pathToJestPackageJSON(pluginSettings: IPluginSettings): string | null {
let pathToNodeModules = join(pluginSettings.rootPath, 'node_modules')
if (pluginSettings.pathToJest) {
const relativeJestCmd = removeSurroundingQuotes(pluginSettings.pathToJest.split(' ')[0])
const relativePathToNodeModules = relativeJestCmd.replace(/node_modules.+$/i, 'node_modules')
pathToNodeModules = join(pluginSettings.rootPath, relativePathToNodeModules)
}
const defaultPath = normalize(join(pathToNodeModules, 'jest/package.json'))
const cliPath = normalize(join(pathToNodeModules, 'jest-cli/package.json'))
const craPath = normalize(join(pathToNodeModules, 'react-scripts/node_modules/jest/package.json'))
const paths = [defaultPath, cliPath, craPath]
for (const i in paths) {
if (existsSync(paths[i])) {
return paths[i]
}
}
return null
}
function removeSurroundingQuotes(str) {
return str.replace(/^['"`]/, '').replace(/['"`]$/, '')
}
/**
* Taken From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
*/
export function escapeRegExp(str: string): string {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') // $& means the whole matched string
}