Skip to content

Commit 1d981ef

Browse files
authored
feat: Better support for monorepos with node resolver (#30 by @morganick)
When you're working with monorepos, the scripts you are looking for may be hoisted into a parent directory. These updates take advantage of Node's resolver to locate the package and base our lookup for the scripts from that location.
1 parent fd937cb commit 1d981ef

File tree

9 files changed

+1940
-2487
lines changed

9 files changed

+1940
-2487
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
${{ runner.os }}-gradle-
2525
- uses: actions/setup-node@v2
2626
with:
27-
node-version: 18.x
27+
node-version: 20.x
2828
- run: yarn install --frozen-lockfile
2929
- run: yarn test test/install-colo-loco.test.js test/colo-loco-android.test.js
3030
test-ios:
@@ -34,6 +34,6 @@ jobs:
3434
- uses: actions/checkout@v2
3535
- uses: actions/setup-node@v2
3636
with:
37-
node-version: 18.x
37+
node-version: 20.x
3838
- run: yarn install --frozen-lockfile
3939
- run: yarn test test/colo-loco-ios.test.js

.github/workflows/publish.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ jobs:
1313
- name: Checkout
1414
uses: actions/checkout@v2
1515
- run: yarn install --frozen-lockfile
16-
- name: Debugging NPM_TOKEN
17-
run: echo -n "${{ secrets.NPM_TOKEN }}" | tail -c 4
1816
- name: Semantic Release
1917
run: "yarn ci:publish"
2018
env:

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ _NOTE: If this doesn't work or you have a non-standard project structure, try th
6161
For iOS, add this to your Podfile (`ios/Podfile`) (don't forget to change `MyApp` to your actual app name):
6262

6363
```ruby
64-
require_relative '../node_modules/react-native-colo-loco/scripts/ios.rb'
64+
require File.join(File.dirname(`node --print "require.resolve('react-native-colo-loco/package.json')"`), "scripts/ios.rb")
6565
link_colocated_native_files(app_name: 'MyApp', app_path: "../app")
6666
```
6767

@@ -153,7 +153,7 @@ Open up your `./android/settings.gradle` file and add this near the top (replace
153153
```groovy
154154
rootProject.name = 'MyApp'
155155
156-
apply from: '../node_modules/react-native-colo-loco/scripts/android.groovy'
156+
apply from: new File(["node", "--print", "require.resolve('react-native-colo-loco/package.json')"].execute(null, rootDir).text.trim(), "../scripts/android.groovy")
157157
linkColocatedNativeFiles([
158158
appName: rootProject.name,
159159
appPath: "../app",

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"devDependencies": {
2222
"@semantic-release/git": "^10.0.1",
2323
"fs-extra": "^11.1.1",
24-
"jest": "^27.3.1",
25-
"semantic-release": "^18.0.0"
24+
"jest": "^29.7.0",
25+
"semantic-release": "^22.0.7"
2626
},
2727
"scripts": {
2828
"test": "jest",

scripts/android.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// TL;DR: This file is included and executed from `./android/settings.gradle`
22
// like this:
33
//
4-
// apply from: '../node_modules/react-native-colo-loco/scripts/android.groovy'
4+
// apply from: new File(["node", "--print", "require.resolve('react-native-colo-loco/package.json')"].execute(null, rootDir).text.trim(), "../scripts/android.groovy")
55
// linkColocatedNativeFiles([ appName: rootProject.name, appPath: "../app", appPackageName: "com.myapp", androidPath: "./android/app/src/main/java/com/myapp" ])
66

77
import java.util.regex.Pattern

scripts/install-colo-loco

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function setupIOS(appName, sourceFolder, _packageName) {
116116
const coloLocoPodfileContents = `
117117
# React Native Colo Loco autolinks native files colocated next to your JS files
118118
# More info here: https://github.com/jamonholmgren/react-native-colo-loco
119-
require_relative '../node_modules/react-native-colo-loco/scripts/ios.rb'
119+
require File.join(File.dirname(\`node --print "require.resolve('react-native-colo-loco/package.json')"\`), "scripts/ios.rb")
120120
link_colocated_native_files(
121121
app_name: '${appName}',
122122
app_path: "../${sourceFolder}",
@@ -146,7 +146,7 @@ function setupAndroid(appName, sourceFolder, packageName) {
146146
const coloLocoSettingsGradleContents = `
147147
// React Native Colo Loco autolinks native files colocated next to your JS files
148148
// More info here: https://github.com/jamonholmgren/react-native-colo-loco
149-
apply from: '../node_modules/react-native-colo-loco/scripts/android.groovy'
149+
apply from: new File(["node", "--print", "require.resolve('react-native-colo-loco/package.json')"].execute(null, rootDir).text.trim(), "../scripts/android.groovy")
150150
linkColocatedNativeFiles([
151151
appName: "${appName}",
152152
appPath: "../${sourceFolder}",

test/helpers.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function runColoLoco({ appPath, input = [] } = {}, args = []) {
4747

4848
async function createTempApp({ initGit = false, setupAndroid = false, setupIOS = false, setupColoLoco = false } = {}) {
4949
// create temp folder
50-
const tempDir = path.join(os.tmpdir(), `colo-loco-test-${Math.random()}`)
50+
const tempDir = path.join(os.tmpdir(), `colo-loco-test-${Date.now()}-${Math.floor(Math.random() * 10000)}`)
5151
await fs.mkdir(tempDir)
5252

5353
// copy TestApp to temp folder
@@ -74,6 +74,11 @@ async function createTempApp({ initGit = false, setupAndroid = false, setupIOS =
7474
path.join(tempDir, "node_modules/react-native-colo-loco/scripts"),
7575
{ recursive: true }
7676
)
77+
// copy package.json too
78+
await fs.copy(
79+
path.join(__dirname, "../package.json"),
80+
path.join(tempDir, "node_modules/react-native-colo-loco/package.json")
81+
)
7782
}
7883
}
7984

test/install-colo-loco.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ describe("Checking for install-colo-loco. 🤪", () => {
3434
// iOS set up is correct
3535
// Podfile
3636
const podfile = await fs.readFile(`${appPath}/ios/Podfile`, "utf8")
37-
expect(podfile).toContain("react-native-colo-loco/scripts/ios.rb")
37+
expect(podfile).toContain("react-native-colo-loco/package.json")
38+
expect(podfile).toContain("scripts/ios.rb")
3839
expect(podfile).toContain("link_colocated_native_files")
3940

4041
// Android set up is correct
4142
// settings.gradle
4243
const settingsGradle = await fs.readFile(`${appPath}/android/settings.gradle`, "utf8")
43-
expect(settingsGradle).toContain("react-native-colo-loco/scripts/android.groovy")
44+
expect(settingsGradle).toContain("react-native-colo-loco/package.json")
45+
expect(settingsGradle).toContain("scripts/android.groovy")
4446
expect(settingsGradle).toContain("linkColocatedNativeFiles")
4547
expect(settingsGradle).toContain(PACKAGE_NAME)
4648

0 commit comments

Comments
 (0)