react-native-sherpa-onnx 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +402 -0
  3. package/SherpaOnnx.podspec +84 -0
  4. package/android/build.gradle +193 -0
  5. package/android/src/main/AndroidManifest.xml +2 -0
  6. package/android/src/main/cpp/CMakeLists.txt +121 -0
  7. package/android/src/main/cpp/include/sherpa-onnx/c-api/c-api.h +1918 -0
  8. package/android/src/main/cpp/include/sherpa-onnx/c-api/cxx-api.h +841 -0
  9. package/android/src/main/cpp/jni/sherpa-onnx-jni.cpp +129 -0
  10. package/android/src/main/cpp/jni/sherpa-onnx-wrapper.cpp +649 -0
  11. package/android/src/main/cpp/jni/sherpa-onnx-wrapper.h +56 -0
  12. package/android/src/main/java/com/sherpaonnx/SherpaOnnxModule.kt +316 -0
  13. package/android/src/main/java/com/sherpaonnx/SherpaOnnxPackage.kt +33 -0
  14. package/ios/Frameworks/sherpa_onnx.xcframework.zip +0 -0
  15. package/ios/SherpaOnnx.h +5 -0
  16. package/ios/SherpaOnnx.mm +293 -0
  17. package/ios/SherpaOnnx.xcconfig +19 -0
  18. package/ios/include/sherpa-onnx/c-api/c-api.h +1918 -0
  19. package/ios/include/sherpa-onnx/c-api/cxx-api.h +841 -0
  20. package/ios/sherpa-onnx-wrapper.h +57 -0
  21. package/ios/sherpa-onnx-wrapper.mm +432 -0
  22. package/lib/module/NativeSherpaOnnx.js +5 -0
  23. package/lib/module/NativeSherpaOnnx.js.map +1 -0
  24. package/lib/module/diarization/index.js +54 -0
  25. package/lib/module/diarization/index.js.map +1 -0
  26. package/lib/module/enhancement/index.js +54 -0
  27. package/lib/module/enhancement/index.js.map +1 -0
  28. package/lib/module/index.js +25 -0
  29. package/lib/module/index.js.map +1 -0
  30. package/lib/module/package.json +1 -0
  31. package/lib/module/separation/index.js +54 -0
  32. package/lib/module/separation/index.js.map +1 -0
  33. package/lib/module/stt/index.js +79 -0
  34. package/lib/module/stt/index.js.map +1 -0
  35. package/lib/module/stt/types.js +4 -0
  36. package/lib/module/stt/types.js.map +1 -0
  37. package/lib/module/tts/index.js +54 -0
  38. package/lib/module/tts/index.js.map +1 -0
  39. package/lib/module/types.js +2 -0
  40. package/lib/module/types.js.map +1 -0
  41. package/lib/module/utils.js +93 -0
  42. package/lib/module/utils.js.map +1 -0
  43. package/lib/module/vad/index.js +54 -0
  44. package/lib/module/vad/index.js.map +1 -0
  45. package/lib/typescript/package.json +1 -0
  46. package/lib/typescript/src/NativeSherpaOnnx.d.ts +39 -0
  47. package/lib/typescript/src/NativeSherpaOnnx.d.ts.map +1 -0
  48. package/lib/typescript/src/diarization/index.d.ts +49 -0
  49. package/lib/typescript/src/diarization/index.d.ts.map +1 -0
  50. package/lib/typescript/src/enhancement/index.d.ts +47 -0
  51. package/lib/typescript/src/enhancement/index.d.ts.map +1 -0
  52. package/lib/typescript/src/index.d.ts +9 -0
  53. package/lib/typescript/src/index.d.ts.map +1 -0
  54. package/lib/typescript/src/separation/index.d.ts +48 -0
  55. package/lib/typescript/src/separation/index.d.ts.map +1 -0
  56. package/lib/typescript/src/stt/index.d.ts +53 -0
  57. package/lib/typescript/src/stt/index.d.ts.map +1 -0
  58. package/lib/typescript/src/stt/types.d.ts +39 -0
  59. package/lib/typescript/src/stt/types.d.ts.map +1 -0
  60. package/lib/typescript/src/tts/index.d.ts +47 -0
  61. package/lib/typescript/src/tts/index.d.ts.map +1 -0
  62. package/lib/typescript/src/types.d.ts +59 -0
  63. package/lib/typescript/src/types.d.ts.map +1 -0
  64. package/lib/typescript/src/utils.d.ts +53 -0
  65. package/lib/typescript/src/utils.d.ts.map +1 -0
  66. package/lib/typescript/src/vad/index.d.ts +48 -0
  67. package/lib/typescript/src/vad/index.d.ts.map +1 -0
  68. package/package.json +221 -0
  69. package/scripts/copy-headers.js +184 -0
  70. package/scripts/setup-assets.js +323 -0
  71. package/scripts/setup-ios-framework.sh +282 -0
  72. package/scripts/switch-registry.js +75 -0
  73. package/src/NativeSherpaOnnx.ts +44 -0
  74. package/src/diarization/index.ts +69 -0
  75. package/src/enhancement/index.ts +67 -0
  76. package/src/index.tsx +30 -0
  77. package/src/separation/index.ts +68 -0
  78. package/src/stt/index.ts +83 -0
  79. package/src/stt/types.ts +42 -0
  80. package/src/tts/index.ts +67 -0
  81. package/src/types.ts +73 -0
  82. package/src/utils.ts +97 -0
  83. package/src/vad/index.ts +70 -0
@@ -0,0 +1,323 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const { execSync } = require('child_process');
6
+
7
+ const isWindows = process.platform === 'win32';
8
+
9
+ /**
10
+ * Setup script that runs after npm/yarn install
11
+ * Handles:
12
+ * 1. Copying C++ header files from sherpa-onnx submodule
13
+ * 2. Downloading iOS framework from GitHub releases
14
+ */
15
+
16
+ function log(message, type = 'info') {
17
+ const colors = {
18
+ info: '\x1b[36m', // cyan
19
+ success: '\x1b[32m', // green
20
+ warning: '\x1b[33m', // yellow
21
+ error: '\x1b[31m', // red
22
+ reset: '\x1b[0m',
23
+ };
24
+
25
+ const color = colors[type] || colors.info;
26
+ console.log(`${color}[setup-assets] ${message}${colors.reset}`);
27
+ }
28
+
29
+ /**
30
+ * Run a command and return output
31
+ */
32
+ function runCommand(cmd, options = {}) {
33
+ const { silent = false, allowFailure = false } = options;
34
+
35
+ try {
36
+ if (!silent) {
37
+ log(`Running: ${cmd}`);
38
+ }
39
+
40
+ const output = execSync(cmd, {
41
+ encoding: 'utf-8',
42
+ stdio: silent ? 'pipe' : 'inherit',
43
+ });
44
+
45
+ return { ok: true, output: (output || '').trim() };
46
+ } catch (error) {
47
+ if (!silent) {
48
+ log(
49
+ `${allowFailure ? 'Warning' : 'Failed'}: ${cmd}`,
50
+ allowFailure ? 'warning' : 'error'
51
+ );
52
+ }
53
+
54
+ if (!allowFailure) {
55
+ throw error;
56
+ }
57
+
58
+ return { ok: false, output: '' };
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Verify header files are in the correct locations
64
+ */
65
+ function verifyHeaders() {
66
+ log('Verifying header files...', 'info');
67
+
68
+ const requiredHeaders = [
69
+ path.join(
70
+ __dirname,
71
+ '..',
72
+ 'ios',
73
+ 'include',
74
+ 'sherpa-onnx',
75
+ 'c-api',
76
+ 'c-api.h'
77
+ ),
78
+ path.join(
79
+ __dirname,
80
+ '..',
81
+ 'ios',
82
+ 'include',
83
+ 'sherpa-onnx',
84
+ 'c-api',
85
+ 'cxx-api.h'
86
+ ),
87
+ path.join(
88
+ __dirname,
89
+ '..',
90
+ 'android',
91
+ 'src',
92
+ 'main',
93
+ 'cpp',
94
+ 'include',
95
+ 'sherpa-onnx',
96
+ 'c-api',
97
+ 'c-api.h'
98
+ ),
99
+ path.join(
100
+ __dirname,
101
+ '..',
102
+ 'android',
103
+ 'src',
104
+ 'main',
105
+ 'cpp',
106
+ 'include',
107
+ 'sherpa-onnx',
108
+ 'c-api',
109
+ 'cxx-api.h'
110
+ ),
111
+ ];
112
+
113
+ const missingHeaders = requiredHeaders.filter(
114
+ (header) => !fs.existsSync(header)
115
+ );
116
+
117
+ if (missingHeaders.length === 0) {
118
+ log('All header files verified at correct locations', 'success');
119
+ return true;
120
+ } else {
121
+ log(
122
+ `Missing header files found (${missingHeaders.length}/${requiredHeaders.length}):`,
123
+ 'error'
124
+ );
125
+ missingHeaders.forEach((header) => {
126
+ log(` - ${header}`, 'error');
127
+ });
128
+ return false;
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Step 1: Copy header files
134
+ */
135
+ function copyHeaders() {
136
+ log('Step 1: Copying header files...', 'info');
137
+
138
+ try {
139
+ // Call the script directly to avoid depending on yarn availability in consumers
140
+ const copyResult = runCommand('node scripts/copy-headers.js', {
141
+ allowFailure: true,
142
+ });
143
+
144
+ if (copyResult.ok) {
145
+ log('Header files copied successfully', 'success');
146
+ } else {
147
+ log(
148
+ 'Warning: Header copy command reported a failure; verifying files...',
149
+ 'warning'
150
+ );
151
+ }
152
+
153
+ // Verify headers after copy
154
+ console.log('');
155
+ const headersOk = verifyHeaders();
156
+ if (!headersOk) {
157
+ log('Header verification failed after copy', 'error');
158
+ }
159
+ return headersOk;
160
+ } catch (error) {
161
+ log('Warning: Header copy failed, may already exist', 'warning');
162
+ // Still try to verify if headers exist
163
+ console.log('');
164
+ return verifyHeaders();
165
+ }
166
+ }
167
+
168
+ /**
169
+ * Verify iOS framework is downloaded and extracted correctly
170
+ */
171
+ function verifyIOSFramework() {
172
+ log('Verifying iOS framework...', 'info');
173
+
174
+ const frameworkPath = path.join(
175
+ __dirname,
176
+ '..',
177
+ 'ios',
178
+ 'Frameworks',
179
+ 'sherpa_onnx.xcframework'
180
+ );
181
+
182
+ // Check if framework directory exists
183
+ if (!fs.existsSync(frameworkPath)) {
184
+ log(`Framework directory not found at ${frameworkPath}`, 'error');
185
+ return false;
186
+ }
187
+
188
+ log(`Framework directory exists`, 'success');
189
+
190
+ // Check for required framework files
191
+ const requiredFiles = [
192
+ path.join(frameworkPath, 'Info.plist'),
193
+ path.join(frameworkPath, 'ios-arm64', 'libsherpa-onnx.a'),
194
+ path.join(frameworkPath, 'ios-arm64_x86_64-simulator', 'libsherpa-onnx.a'),
195
+ ];
196
+
197
+ const missingFiles = requiredFiles.filter((file) => !fs.existsSync(file));
198
+
199
+ if (missingFiles.length === 0) {
200
+ // Calculate framework size
201
+ let totalSize = 0;
202
+ const getDirectorySize = (dirPath) => {
203
+ try {
204
+ const files = fs.readdirSync(dirPath);
205
+ files.forEach((file) => {
206
+ const filePath = path.join(dirPath, file);
207
+ const stats = fs.statSync(filePath);
208
+ if (stats.isDirectory()) {
209
+ totalSize += getDirectorySize(filePath);
210
+ } else {
211
+ totalSize += stats.size;
212
+ }
213
+ });
214
+ } catch (e) {
215
+ // Silently ignore errors
216
+ }
217
+ return totalSize;
218
+ };
219
+
220
+ getDirectorySize(frameworkPath);
221
+ const frameworkSizeMB = (totalSize / (1024 * 1024)).toFixed(2);
222
+
223
+ log(`All framework files present (${frameworkSizeMB}MB)`, 'success');
224
+ return true;
225
+ } else {
226
+ log(
227
+ `Missing framework files (${missingFiles.length}/${requiredFiles.length}):`,
228
+ 'error'
229
+ );
230
+ missingFiles.forEach((file) => {
231
+ log(` - ${file}`, 'error');
232
+ });
233
+ return false;
234
+ }
235
+ }
236
+
237
+ /**
238
+ * Step 2: Download iOS framework (only on macOS)
239
+ */
240
+ function downloadIOSFramework() {
241
+ // Only download on macOS since it's iOS-specific
242
+ if (process.platform !== 'darwin') {
243
+ log('Skipping iOS framework download (not on macOS)', 'info');
244
+ return true;
245
+ }
246
+
247
+ log('Step 2: Downloading iOS framework...', 'info');
248
+
249
+ const frameworkPath = path.join(
250
+ __dirname,
251
+ '..',
252
+ 'ios',
253
+ 'Frameworks',
254
+ 'sherpa_onnx.xcframework'
255
+ );
256
+
257
+ // Check if framework already exists
258
+ if (fs.existsSync(frameworkPath)) {
259
+ log('iOS framework already present, verifying...', 'success');
260
+ console.log('');
261
+ return verifyIOSFramework();
262
+ }
263
+
264
+ try {
265
+ // Call the shell script directly to avoid yarn dependency in consuming apps
266
+ const downloadResult = runCommand('bash scripts/setup-ios-framework.sh');
267
+
268
+ if (downloadResult.ok) {
269
+ log('iOS framework downloaded successfully', 'success');
270
+
271
+ // Verify framework after download
272
+ console.log('');
273
+ return verifyIOSFramework();
274
+ }
275
+
276
+ log('iOS framework download command reported failure', 'error');
277
+ return false;
278
+ } catch (error) {
279
+ log('iOS framework download failed', 'error');
280
+ return false;
281
+ }
282
+ }
283
+
284
+ /**
285
+ * Main setup function
286
+ */
287
+ function setup() {
288
+ console.log('');
289
+ log('='.repeat(60), 'info');
290
+ log('SherpaOnnx SDK Setup Starting', 'info');
291
+ log('='.repeat(60), 'info');
292
+ console.log('');
293
+
294
+ let success = true;
295
+
296
+ // Step 1: Copy headers (critical for both Android and iOS)
297
+ if (!copyHeaders()) {
298
+ success = false;
299
+ }
300
+
301
+ console.log('');
302
+
303
+ // Step 2: Download iOS framework (macOS only)
304
+ if (!downloadIOSFramework()) {
305
+ success = false;
306
+ }
307
+
308
+ console.log('');
309
+ if (success) {
310
+ log('='.repeat(60), 'success');
311
+ log('Setup completed successfully!', 'success');
312
+ log('='.repeat(60), 'success');
313
+ console.log('');
314
+ process.exit(0);
315
+ } else {
316
+ log('Setup failed (see logs above)', 'error');
317
+ console.log('');
318
+ process.exit(1);
319
+ }
320
+ }
321
+
322
+ // Run setup
323
+ setup();
@@ -0,0 +1,282 @@
1
+ #!/bin/bash
2
+
3
+ # Script to download and manage iOS Framework
4
+ # Can be called manually or by Podfile during pod install
5
+ # Usage:
6
+ # ./scripts/setup-ios-framework.sh # Downloads/updates framework (auto mode, no interactive)
7
+ # ./scripts/setup-ios-framework.sh 1.12.23 # Downloads specific version
8
+ # ./scripts/setup-ios-framework.sh --interactive # Interactive mode with prompts
9
+
10
+ set -e
11
+
12
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
13
+ PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
14
+ FRAMEWORKS_DIR="$PROJECT_ROOT/ios/Frameworks"
15
+ VERSION_FILE="$FRAMEWORKS_DIR/.framework-version"
16
+
17
+ # Colors for output
18
+ RED='\033[0;31m'
19
+ GREEN='\033[0;32m'
20
+ YELLOW='\033[1;33m'
21
+ BLUE='\033[0;34m'
22
+ NC='\033[0m' # No Color
23
+
24
+ # Detect if running in interactive mode (terminal)
25
+ INTERACTIVE=false
26
+ [ -t 0 ] && INTERACTIVE=true
27
+
28
+ # Check for explicit flags
29
+ if [ "$1" = "--interactive" ]; then
30
+ INTERACTIVE=true
31
+ shift
32
+ fi
33
+
34
+ # Only print header if interactive
35
+ if [ "$INTERACTIVE" = true ]; then
36
+ echo -e "${BLUE}iOS Framework Setup Script${NC}"
37
+ echo "Project root: $PROJECT_ROOT"
38
+ echo ""
39
+ fi
40
+
41
+ # Create frameworks directory if it doesn't exist
42
+ mkdir -p "$FRAMEWORKS_DIR"
43
+
44
+ # Function to compare semantic versions (e.g., "1.12.23" vs "1.12.24")
45
+ compare_versions() {
46
+ local v1=$1
47
+ local v2=$2
48
+
49
+ # Convert to arrays
50
+ IFS='.' read -ra v1_parts <<< "$v1"
51
+ IFS='.' read -ra v2_parts <<< "$v2"
52
+
53
+ # Pad arrays to same length
54
+ local max_len=${#v1_parts[@]}
55
+ [ ${#v2_parts[@]} -gt $max_len ] && max_len=${#v2_parts[@]}
56
+
57
+ # Compare each part
58
+ for ((i=0; i<max_len; i++)); do
59
+ local v1_num=${v1_parts[$i]:-0}
60
+ local v2_num=${v2_parts[$i]:-0}
61
+
62
+ if [ "$v1_num" -lt "$v2_num" ]; then
63
+ echo "-1" # v1 < v2
64
+ return 0
65
+ elif [ "$v1_num" -gt "$v2_num" ]; then
66
+ echo "1" # v1 > v2
67
+ return 0
68
+ fi
69
+ done
70
+
71
+ echo "0" # v1 == v2
72
+ }
73
+
74
+ # Function to get latest framework version from GitHub
75
+ get_latest_framework_version() {
76
+ echo -e "${YELLOW}Fetching latest framework release from GitHub...${NC}" >&2
77
+
78
+ local releases_json=$(curl -s -H "Accept: application/vnd.github+json" "https://api.github.com/repos/XDcobra/react-native-sherpa-onnx/releases" 2>/dev/null || echo "")
79
+
80
+ if [ -z "$releases_json" ]; then
81
+ echo -e "${RED}Error: Could not fetch releases from GitHub API${NC}" >&2
82
+ return 1
83
+ fi
84
+
85
+ # Avoid jq errors on rate-limit HTML or plain-text responses
86
+ if ! echo "$releases_json" | grep -q '"tag_name"'; then
87
+ echo -e "${RED}Error: GitHub API response did not contain release data (possible rate limit).${NC}" >&2
88
+ echo "Response (truncated):" >&2
89
+ echo "$releases_json" | head -5 >&2
90
+ return 1
91
+ fi
92
+
93
+ local version=""
94
+
95
+ if command -v jq &> /dev/null; then
96
+ if echo "$releases_json" | jq -e . > /dev/null 2>&1; then
97
+ version=$(echo "$releases_json" | jq -r '.[] | select(.tag_name | startswith("framework-v")) | .tag_name' | head -1 | sed 's/framework-v//')
98
+ else
99
+ echo -e "${RED}Error: GitHub releases response is not valid JSON${NC}" >&2
100
+ echo "$releases_json" | head -5 >&2
101
+ return 1
102
+ fi
103
+ else
104
+ version=$(echo "$releases_json" | grep -o '"tag_name": "framework-v[0-9.]*' | head -1 | sed 's/.*framework-v//')
105
+ fi
106
+
107
+ if [ -z "$version" ]; then
108
+ echo -e "${RED}Error: No framework releases found with tag format 'framework-vX.Y.Z'${NC}" >&2
109
+ return 1
110
+ fi
111
+
112
+ echo "$version"
113
+ }
114
+
115
+ # Function to get local framework version
116
+ get_local_framework_version() {
117
+ if [ -f "$VERSION_FILE" ]; then
118
+ cat "$VERSION_FILE"
119
+ else
120
+ echo ""
121
+ fi
122
+ }
123
+
124
+ # Function to download and extract framework
125
+ download_and_extract_framework() {
126
+ local version=$1
127
+ local tag="framework-v$version"
128
+
129
+ echo -e "${YELLOW}Downloading framework version $version...${NC}" >&2
130
+
131
+ # Get download URL from GitHub API
132
+ local release_json=$(curl -s -H "Accept: application/vnd.github+json" "https://api.github.com/repos/XDcobra/react-native-sherpa-onnx/releases/tags/$tag" 2>/dev/null || echo "")
133
+
134
+ if [ -z "$release_json" ]; then
135
+ echo -e "${RED}Error: Could not fetch release information for tag $tag${NC}" >&2
136
+ return 1
137
+ fi
138
+
139
+ if ! echo "$release_json" | grep -q '"assets"'; then
140
+ echo -e "${RED}Error: GitHub API response for $tag did not contain assets (possible rate limit).${NC}" >&2
141
+ echo "Response (truncated):" >&2
142
+ echo "$release_json" | head -5 >&2
143
+ return 1
144
+ fi
145
+
146
+ # Extract download URL using jq if available, otherwise grep/sed
147
+ local download_url
148
+ if command -v jq &> /dev/null; then
149
+ if echo "$release_json" | jq -e . > /dev/null 2>&1; then
150
+ download_url=$(echo "$release_json" | jq -r '.assets[] | select(.name == "sherpa_onnx.xcframework.zip") | .browser_download_url' | head -1)
151
+ else
152
+ echo -e "${RED}Error: Release response is not valid JSON${NC}" >&2
153
+ echo "$release_json" | head -5 >&2
154
+ return 1
155
+ fi
156
+ else
157
+ download_url=$(echo "$release_json" | grep -o '"browser_download_url": "[^"]*' | grep 'xcframework.zip' | head -1 | sed 's/.*: "//' | sed 's/"$//')
158
+ fi
159
+
160
+ if [ -z "$download_url" ]; then
161
+ echo -e "${RED}Error: Could not find download URL for version $version${NC}" >&2
162
+ echo -e "${RED}Available assets:${NC}" >&2
163
+ if command -v jq &> /dev/null; then
164
+ echo "$release_json" | jq -r '.assets[].name' | sed 's/^/ - /' >&2 || true
165
+ fi
166
+ return 1
167
+ fi
168
+
169
+ echo "Downloading from: $download_url" >&2
170
+
171
+ # Download the zip file
172
+ local zip_path="$FRAMEWORKS_DIR/sherpa_onnx.xcframework.zip"
173
+
174
+ if ! curl -L -f -o "$zip_path" "$download_url" 2>/dev/null; then
175
+ echo -e "${RED}Error: Failed to download framework from $download_url${NC}" >&2
176
+ rm -f "$zip_path"
177
+ return 1
178
+ fi
179
+
180
+ # Check if zip file is valid
181
+ if ! file "$zip_path" 2>/dev/null | grep -q "Zip archive"; then
182
+ echo -e "${RED}Error: Downloaded file is not a valid zip archive${NC}" >&2
183
+ echo "File type: $(file "$zip_path" 2>/dev/null || echo "unknown")" >&2
184
+ rm -f "$zip_path"
185
+ return 1
186
+ fi
187
+
188
+ # Remove old framework if it exists
189
+ if [ -d "$FRAMEWORKS_DIR/sherpa_onnx.xcframework" ]; then
190
+ echo -e "${YELLOW}Removing old framework...${NC}" >&2
191
+ rm -rf "$FRAMEWORKS_DIR/sherpa_onnx.xcframework"
192
+ fi
193
+
194
+ # Extract the zip
195
+ echo -e "${YELLOW}Extracting framework...${NC}" >&2
196
+ unzip -q -o "$zip_path" -d "$FRAMEWORKS_DIR"
197
+
198
+ if [ ! -d "$FRAMEWORKS_DIR/sherpa_onnx.xcframework" ]; then
199
+ echo -e "${RED}Error: Framework extraction failed${NC}" >&2
200
+ echo "Contents of $FRAMEWORKS_DIR:" >&2
201
+ ls -la "$FRAMEWORKS_DIR" 2>/dev/null | head -20 >&2 || true
202
+ rm -f "$zip_path"
203
+ return 1
204
+ fi
205
+
206
+ # Remove zip file
207
+ rm -f "$zip_path"
208
+
209
+ # Write version file
210
+ echo "$version" > "$VERSION_FILE"
211
+
212
+ echo -e "${GREEN}Framework v$version downloaded and extracted successfully${NC}" >&2
213
+ return 0
214
+ }
215
+
216
+ # Main logic
217
+ if [ -n "$1" ]; then
218
+ # User provided a specific version
219
+ download_and_extract_framework "$1"
220
+ else
221
+ # Auto mode: check version and download only if needed
222
+ [ "$INTERACTIVE" = true ] && echo -e "${YELLOW}Checking framework version...${NC}" >&2
223
+
224
+ local_version=$(get_local_framework_version)
225
+
226
+ if ! latest_version=$(get_latest_framework_version); then
227
+ if [ -d "$FRAMEWORKS_DIR/sherpa_onnx.xcframework" ]; then
228
+ echo -e "${YELLOW}Warning: Could not fetch latest framework version, using existing local framework.${NC}" >&2
229
+ exit 0
230
+ fi
231
+ exit 1
232
+ fi
233
+
234
+ if [ -z "$latest_version" ]; then
235
+ echo -e "${RED}Error: Could not fetch framework version from GitHub${NC}" >&2
236
+ exit 1
237
+ fi
238
+
239
+ [ "$INTERACTIVE" = true ] && echo -e "${GREEN}Latest framework version: $latest_version${NC}" >&2
240
+
241
+ # Check if framework exists
242
+ if [ ! -d "$FRAMEWORKS_DIR/sherpa_onnx.xcframework" ]; then
243
+ [ "$INTERACTIVE" = true ] && echo "Framework not found locally, downloading..." >&2
244
+ download_and_extract_framework "$latest_version" || exit 1
245
+ elif [ -z "$local_version" ]; then
246
+ # Framework exists but no version file
247
+ [ "$INTERACTIVE" = true ] && echo "Framework exists but no version info, updating version file..." >&2
248
+ echo "$latest_version" > "$VERSION_FILE"
249
+ else
250
+ # Compare versions
251
+ local version_cmp=$(compare_versions "$local_version" "$latest_version")
252
+
253
+ if [ "$version_cmp" = "0" ]; then
254
+ [ "$INTERACTIVE" = true ] && echo -e "${GREEN}Framework is up to date (v$local_version)${NC}" >&2
255
+ elif [ "$version_cmp" = "-1" ]; then
256
+ [ "$INTERACTIVE" = true ] && echo -e "${YELLOW}Update available: v$local_version → v$latest_version${NC}" >&2
257
+ if [ "$INTERACTIVE" = true ]; then
258
+ read -p "Do you want to update? (y/N): " -n 1 -r
259
+ echo
260
+ if [[ $REPLY =~ ^[Yy]$ ]]; then
261
+ download_and_extract_framework "$latest_version" || exit 1
262
+ fi
263
+ else
264
+ # Auto-update in non-interactive mode (e.g., from Podfile)
265
+ download_and_extract_framework "$latest_version" || exit 1
266
+ fi
267
+ else
268
+ [ "$INTERACTIVE" = true ] && echo -e "${YELLOW}Local version (v$local_version) is newer than latest release (v$latest_version)${NC}" >&2
269
+ fi
270
+ fi
271
+ fi
272
+
273
+ if [ "$INTERACTIVE" = true ]; then
274
+ echo "" >&2
275
+ echo -e "${GREEN}Framework setup complete!${NC}" >&2
276
+ echo "Framework location: $FRAMEWORKS_DIR/sherpa_onnx.xcframework" >&2
277
+ echo "" >&2
278
+ echo "Next steps:" >&2
279
+ echo " 1. cd example" >&2
280
+ echo " 2. pod install" >&2
281
+ echo " 3. Open ios/SherpaOnnxExample.xcworkspace in Xcode" >&2
282
+ fi
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Script to switch npm registry configuration
5
+ * Updates both .npmrc and publishConfig in package.json
6
+ * This ensures both npm publish and release-it use the correct registry
7
+ * Usage: node scripts/switch-registry.js [local|public]
8
+ */
9
+
10
+ const fs = require('fs');
11
+ const path = require('path');
12
+
13
+ const rootDir = path.join(__dirname, '..');
14
+ const npmrcPath = path.join(rootDir, '.npmrc');
15
+ const npmrcLocalPath = path.join(rootDir, '.npmrc.local');
16
+ const npmrcPublicPath = path.join(rootDir, '.npmrc.public');
17
+ const packageJsonPath = path.join(rootDir, 'package.json');
18
+
19
+ const mode = process.argv[2] || 'local';
20
+
21
+ // Read package.json
22
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
23
+
24
+ if (mode === 'local') {
25
+ if (!fs.existsSync(npmrcLocalPath)) {
26
+ console.error(' .npmrc.local not found');
27
+ process.exit(1);
28
+ }
29
+
30
+ // Update .npmrc
31
+ fs.copyFileSync(npmrcLocalPath, npmrcPath);
32
+
33
+ // Update publishConfig in package.json
34
+ if (!packageJson.publishConfig) {
35
+ packageJson.publishConfig = {};
36
+ }
37
+ packageJson.publishConfig.registry = 'http://localhost:4873';
38
+
39
+ // Write updated package.json
40
+ fs.writeFileSync(
41
+ packageJsonPath,
42
+ JSON.stringify(packageJson, null, 2) + '\n'
43
+ );
44
+
45
+ console.log(' Switched to Verdaccio (local registry)');
46
+ console.log(' Registry: http://localhost:4873');
47
+ console.log(' Updated .npmrc and package.json publishConfig');
48
+ } else if (mode === 'public') {
49
+ if (!fs.existsSync(npmrcPublicPath)) {
50
+ console.error(' .npmrc.public not found');
51
+ process.exit(1);
52
+ }
53
+
54
+ // Update .npmrc
55
+ fs.copyFileSync(npmrcPublicPath, npmrcPath);
56
+
57
+ // Update publishConfig in package.json
58
+ if (!packageJson.publishConfig) {
59
+ packageJson.publishConfig = {};
60
+ }
61
+ packageJson.publishConfig.registry = 'https://registry.npmjs.org/';
62
+
63
+ // Write updated package.json
64
+ fs.writeFileSync(
65
+ packageJsonPath,
66
+ JSON.stringify(packageJson, null, 2) + '\n'
67
+ );
68
+
69
+ console.log(' Switched to npm (public registry)');
70
+ console.log(' Registry: https://registry.npmjs.org/');
71
+ console.log(' Updated .npmrc and package.json publishConfig');
72
+ } else {
73
+ console.error(' Invalid mode. Use "local" or "public"');
74
+ process.exit(1);
75
+ }