Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cliPath should handle absolute paths #32983

Closed
wants to merge 5 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -68,7 +68,18 @@ private fun detectCliPath(
): String {
// 1. preconfigured path
if (preconfiguredCliPath != null) {
return File(projectDir, preconfiguredCliPath).toString()
val preconfiguredCliJsAbsolute = File(preconfiguredCliPath)
if (preconfiguredCliJsAbsolute.exists()) {
return preconfiguredCliJsAbsolute.absolutePath
}
val preconfiguredCliJsRelativeToReactRoot = File(reactRoot, preconfiguredCliPath)
if (preconfiguredCliJsRelativeToReactRoot.exists()) {
return preconfiguredCliJsRelativeToReactRoot.absolutePath
}
val preconfiguredCliJsRelativeToProject = File(projectDir, preconfiguredCliPath)
if (preconfiguredCliJsRelativeToProject.exists()) {
return preconfiguredCliJsRelativeToProject.absolutePath
}
}

// 2. node module path
Expand All @@ -82,7 +93,10 @@ private fun detectCliPath(
val nodeProcessOutput = nodeProcess.inputStream.use { it.bufferedReader().readText().trim() }

if (nodeProcessOutput.isNotEmpty()) {
return nodeProcessOutput
val nodeModuleCliJs = File(nodeProcessOutput)
if (nodeModuleCliJs.exists()) {
return nodeModuleCliJs.absolutePath
}
}

// 3. cli.js in the root folder
Expand Down
Expand Up @@ -52,10 +52,44 @@ class PathUtilsTest {
}

@Test
fun detectedCliPath_withCliPathFromExtension() {
fun detectedCliPath_withCliPathFromExtensionAbsolute() {
val project = ProjectBuilder.builder().build()
val extension = TestReactExtension(project)
val expected = File(project.projectDir, "fake-cli.sh")
val expected = File(project.projectDir, "abs/fake-cli.sh").apply {
parentFile.mkdirs()
writeText("<!-- nothing to see here -->")
}
extension.cliPath.set(project.projectDir + "/abs/fake-cli.sh")

val actual = detectedCliPath(project.projectDir, extension)

assertEquals(expected.toString(), actual)
}

@Test
fun detectedCliPath_withCliPathFromExtensionInReactFolder() {
val project = ProjectBuilder.builder().build()
val extension = TestReactExtension(project)
val expected = File(project.projectDir, "/react-root/fake-cli.sh").apply {
parentFile.mkdirs()
writeText("<!-- nothing to see here -->")
}
extension.cliPath.set("fake-cli.sh")
extension.reactRoot.set(project.projectDir + "/react-root")

val actual = detectedCliPath(project.projectDir, extension)

assertEquals(expected.toString(), actual)
}

@Test
fun detectedCliPath_withCliPathFromExtensionInProjectFolder() {
val project = ProjectBuilder.builder().build()
val extension = TestReactExtension(project)
val expected = File(project.projectDir, "fake-cli.sh").apply {
parentFile.mkdirs()
writeText("<!-- nothing to see here -->")
}
extension.cliPath.set("fake-cli.sh")

val actual = detectedCliPath(project.projectDir, extension)
Expand Down
53 changes: 38 additions & 15 deletions react.gradle
Expand Up @@ -21,20 +21,6 @@ def detectEntryFile(config) {
return "index.js";
}

/**
* Detects CLI location in a similar fashion to the React Native CLI
*/
def detectCliPath(config) {
if (config.cliPath) {
return "${projectDir}/${config.cliPath}"
}
if (new File("${projectDir}/../../node_modules/react-native/cli.js").exists()) {
return "${projectDir}/../../node_modules/react-native/cli.js"
}
throw new Exception("Couldn't determine CLI location. " +
"Please set `project.ext.react.cliPath` to the path of the react-native cli.js file. This file typically resides in `node_modules/react-native/cli.js`");
}

def composeSourceMapsPath = config.composeSourceMapsPath ?: "node_modules/react-native/scripts/compose-source-maps.js"
def bundleAssetName = config.bundleAssetName ?: "index.android.bundle"
def entryFile = detectEntryFile(config)
Expand All @@ -45,6 +31,43 @@ def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" :
def enableVmCleanup = config.enableVmCleanup == null ? true : config.enableVmCleanup
def hermesCommand = config.hermesCommand ?: "../../node_modules/hermes-engine/%OS-BIN%/hermesc"

/**
* Detects CLI location in a similar fashion to the React Native CLI
*/
def detectCliPath(config, reactRoot) {
// 1. preconfigured path
if (config.cliPath) {
def cliJsAbsolute = new File(config.cliPath)
if (cliJsAbsolute.exists()) {
return cliJsAbsolute.getAbsolutePath()
}
def cliJsRelativeToRoot = new File("${rootDir}/${config.cliPath}")
if (cliJsRelativeToRoot.exists()) {
return cliJsRelativeToRoot.getAbsolutePath()
}
def cliJsRelativeToProject = new File("${projectDir}/${config.cliPath}")
if (cliJsRelativeToProject.exists()) {
return cliJsRelativeToProject.getAbsolutePath()
}
}

// 2. node module path
def cliJsFromNode = new File(["node", "--print", "require.resolve('react-native/cli').bin"].execute(null, rootDir).text.trim())
if (cliJsFromNode.exists()) {
return cliJsFromNode.getAbsolutePath()
}

// 3. cli.js in the root folder
def rootCliJs = new File(reactRoot, "node_modules/react-native/cli.js")
if (rootCliJs.exists()) {
return rootCliJs.getAbsolutePath()
}

throw new Exception("Couldn't determine CLI location. " +
"Please set `project.ext.react.cliPath` to the path of the react-native cli.js file. " +
"This file typically resides in `node_modules/react-native/cli.js`");
}

def reactNativeDevServerPort() {
def value = project.getProperties().get("reactNativeDevServerPort")
return value != null ? value : "8081"
Expand Down Expand Up @@ -150,7 +173,7 @@ afterEvaluate {

// Additional node and packager commandline arguments
def nodeExecutableAndArgs = config.nodeExecutableAndArgs ?: ["node"]
def cliPath = detectCliPath(config)
def cliPath = detectCliPath(config, reactRoot)

def execCommand = []

Expand Down