Skip to content
Merged
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
36 changes: 33 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,52 @@ func readJSONObject(atPath path: String) -> [String: Any] {
// searching up through parent directories (starting one level above this
// package, i.e. outside node_modules/@op-engineering/op-sqlite). In the
// monorepo/dev checkout it's simply example/package.json.
// #filePath keeps the autolinker's symlink path (see the header), which is not
// inside node_modules, so the search starts from the real directory.
let resolvedPackageRoot = packageRoot.resolvingSymlinksInPath()

let appPackageJSONPath: String = {
guard isUserApp else {
guard resolvedPackageRoot.path.contains("node_modules") else {
return packageRoot.appendingPathComponent("example/package.json").path
}
var currentDir = packageRoot.deletingLastPathComponent()
var packageJSONPath: String
var currentDir = resolvedPackageRoot.deletingLastPathComponent()
while true {
let candidate = currentDir.appendingPathComponent("package.json").path
if FileManager.default.fileExists(atPath: candidate) {
return candidate
packageJSONPath = candidate
break
}
let parentDir = currentDir.deletingLastPathComponent()
if parentDir.path == currentDir.path {
fatalError("[OP-SQLITE] package.json not found")
}
currentDir = parentDir
}

// Under pnpm, or when hoisted to a monorepo root, the search above ends at
// the workspace root and skips the app. Prefer the app, the folder above the
// one holding build/generated/autolinking (the same package.json Android
// reads): walk up from it, no further than the folder found above, and take
// the first package.json with an "op-sqlite" key. Without the autolinker's
// symlink the app is unknown and the search above stands.
guard let marker = packageRoot.path.range(of: "/build/generated/autolinking/libs/") else {
return packageJSONPath
}
let stopDir = URL(fileURLWithPath: packageJSONPath).deletingLastPathComponent().path
var appDir = URL(fileURLWithPath: String(packageRoot.path[..<marker.lowerBound]))
.resolvingSymlinksInPath().deletingLastPathComponent()
while appDir.path == stopDir || appDir.path.hasPrefix(stopDir + "/") {
let candidate = appDir.appendingPathComponent("package.json").path
if readJSONObject(atPath: candidate)["op-sqlite"] != nil {
return candidate
}
if appDir.path == stopDir {
break
}
appDir = appDir.deletingLastPathComponent()
}
return packageJSONPath
}()

let appPackage = readJSONObject(atPath: appPackageJSONPath)
Expand Down
7 changes: 3 additions & 4 deletions docs/docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ Required response headers (for worker/OPFS setup):
SQLite is very customizable on compilation level. op-sqlite also allows you add extensions or even change the base implementation. You can do this by adding the following to your `package.json`:

> [!IMPORTANT]
> When using a monorepo, be sure to add the "op-sqlite" config to the monorepo root `package.json` file.
> This is necessary because the `node_modules/@op-engineering/op-sqlite/op-sqlite.podspec` will search for first the `package.json` file in a parent directory.
> Alternative, you may be able to solve this by blocking this package from being hoisted to the root `node_modules`.
> Check your ios/Podfile.lock to see where it's being installed.
> In a monorepo, put the "op-sqlite" config in your app's `package.json` (the folder above `ios/` and `android/`).
> On iOS (CocoaPods and SwiftPM) the build walks up from that folder and uses the first `package.json` with an "op-sqlite" key, so a config in the monorepo root `package.json` is still picked up.
> The build prints which file it used (`[OP-SQLITE] Configuration found at ...` from `pod install`, `[OP-SQLITE] SPM configuration found at ...` from SwiftPM).

```json
{
Expand Down
26 changes: 26 additions & 0 deletions op-sqlite.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,32 @@ if is_user_app
end

raise "package.json not found" if package_json_path.nil?

# Under pnpm, or when hoisted to a monorepo root, the search above ends at
# the workspace root and skips the app. Prefer the app, the folder above the
# Podfile (the same package.json Android reads): walk up from it, no further
# than the folder found above, and take the first package.json with an
# "op-sqlite" key. `pod install` finds the Podfile before evaluating any
# podspec; when none was found, podfile_path is nil.
podfile_path = defined?(Pod::Config) ? Pod::Config.instance.podfile_path : nil
if podfile_path
stop_dir = File.dirname(package_json_path)
current_dir = File.dirname(File.dirname(podfile_path.to_s))
while current_dir == stop_dir || current_dir.start_with?(File.join(stop_dir, ""))
package_path = File.join(current_dir, "package.json")
has_config = File.exist?(package_path) && begin
JSON.parse(File.read(package_path)).key?("op-sqlite")
rescue JSON::ParserError
false
end
if has_config
package_json_path = package_path
break
end
break if current_dir == stop_dir
current_dir = File.dirname(current_dir)
end
end
else
package_json_path = File.join(__dir__, "example", "package.json")
end
Expand Down
Loading