Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ internal fun detectedEntryFile(config: ReactExtension, envVariableOverride: Stri
*/
internal fun detectedCliFile(config: ReactExtension): File =
detectCliFile(
project = config.project,
reactNativeRoot = config.root.get().asFile,
preconfiguredCliFile = config.cliFile.asFile.orNull,
)
Expand Down Expand Up @@ -71,7 +72,11 @@ private fun detectEntryFile(
else -> File(reactRoot, "index.js")
}

private fun detectCliFile(reactNativeRoot: File, preconfiguredCliFile: File?): File {
private fun detectCliFile(
project: Project,
reactNativeRoot: File,
preconfiguredCliFile: File?
): File {
// 1. preconfigured path
if (preconfiguredCliFile != null) {
if (preconfiguredCliFile.exists()) {
Expand All @@ -81,14 +86,12 @@ private fun detectCliFile(reactNativeRoot: File, preconfiguredCliFile: File?): F

// 2. node module path
val nodeProcess =
Runtime.getRuntime()
.exec(
arrayOf("node", "--print", "require.resolve('react-native/cli');"),
emptyArray(),
reactNativeRoot,
)

val nodeProcessOutput = nodeProcess.inputStream.use { it.bufferedReader().readText().trim() }
project.providers.exec {
it.commandLine("node", "--print", "require.resolve('react-native/cli');")
it.workingDir(reactNativeRoot)
}

val nodeProcessOutput = nodeProcess.standardOutput.asText.get().trim()

if (nodeProcessOutput.isNotEmpty()) {
val nodeModuleCliJs = File(nodeProcessOutput)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.facebook.react.tests.WithOs
import java.io.File
import org.assertj.core.api.Assertions.assertThat
import org.gradle.testfixtures.ProjectBuilder
import org.gradle.process.ProcessExecutionException
import org.junit.Assume.assumeTrue
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -99,7 +100,7 @@ class PathUtilsTest {
assertThat(actual.readText()).isEqualTo("<!-- nothing to see here -->")
}

@Test(expected = IllegalStateException::class)
@Test(expected = ProcessExecutionException::class)
fun detectedCliPath_failsIfNotFound() {
val project = ProjectBuilder.builder().build()
val extension = TestReactExtension(project)
Expand Down
Loading