Skip to content

Commit 82b37b2

Browse files
committed
fix(ccusage): derive native package target from metadata
Avoid duplicating the native package platform map in the Nushell prepack check. The script now scans the native package manifests and selects the package whose os and cpu metadata match the current platform. This addresses cubic feedback about drift risk while preserving the same fast path and cargo fallback behavior.
1 parent 98c3abe commit 82b37b2

1 file changed

Lines changed: 23 additions & 16 deletions

File tree

‎apps/ccusage/scripts/ensure-native-binary.nu‎

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
#!/usr/bin/env nu
22

3-
const native_package_dirs = {
4-
'darwin-arm64': 'ccusage-darwin-arm64',
5-
'darwin-x64': 'ccusage-darwin-x64',
6-
'linux-arm64': 'ccusage-linux-arm64',
7-
'linux-x64': 'ccusage-linux-x64',
8-
'win32-arm64': 'ccusage-win32-arm64',
9-
'win32-x64': 'ccusage-win32-x64',
10-
}
11-
123
const system_dylib_prefixes = [
134
'/usr/lib/',
145
'/System/Library/',
@@ -18,14 +9,8 @@ def main [] {
189
let repo_root = ($env.CURRENT_FILE | path dirname | path join ../../.. | path expand)
1910
let target_platform = (node_platform)
2011
let target_arch = (node_arch)
21-
let target_key = $"($target_platform)-($target_arch)"
22-
let native_package_dir = ($native_package_dirs | get --optional $target_key)
2312
let binary_name = if $target_platform == 'win32' { 'ccusage.exe' } else { 'ccusage' }
24-
let native_package_root = if $native_package_dir == null {
25-
null
26-
} else {
27-
$repo_root | path join packages $native_package_dir
28-
}
13+
let native_package_root = (matching_native_package_root $repo_root $target_platform $target_arch)
2914
let native_binary = if $native_package_root == null {
3015
null
3116
} else {
@@ -70,6 +55,28 @@ def node_arch [] {
7055
}
7156
}
7257

58+
def matching_native_package_root [repo_root: path, target_platform: string, target_arch: string] {
59+
let candidates = (
60+
glob ($repo_root | path join packages 'ccusage-*' package.json)
61+
| each {|package_json_path|
62+
let package_json = (open $package_json_path)
63+
let os = $package_json.os?
64+
let cpu = $package_json.cpu?
65+
if (list_contains $os $target_platform) and (list_contains $cpu $target_arch) {
66+
$package_json_path | path dirname
67+
} else {
68+
null
69+
}
70+
}
71+
| where {|package_root| $package_root != null }
72+
)
73+
$candidates | get --optional 0
74+
}
75+
76+
def list_contains [value: any, needle: string] {
77+
(($value | describe) =~ '^list') and ($value | any {|item| $item == $needle })
78+
}
79+
7380
def expected_version [repo_root: path] {
7481
let package_json = (open ($repo_root | path join apps ccusage package.json))
7582
if (($package_json.version? | describe) != 'string') {

0 commit comments

Comments
 (0)