hamlib 0.1.3 → 0.1.4

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 (2) hide show
  1. package/package.json +2 -1
  2. package/scripts/install.js +262 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hamlib",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Node.js wrapper for hamlib radio control library",
5
5
  "main": "index.js",
6
6
  "module": "lib/index.mjs",
@@ -30,6 +30,7 @@
30
30
  "lib/",
31
31
  "prebuilds/",
32
32
  "src/",
33
+ "scripts/",
33
34
  "index.js",
34
35
  "index.d.ts",
35
36
  "binding.gyp",
@@ -0,0 +1,262 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const { execSync } = require('child_process');
4
+ const { getPlatformBinaryPath } = require('../lib/binary-loader');
5
+
6
+ /**
7
+ * Check if a prebuilt binary exists for the current platform
8
+ */
9
+ function checkPrebuiltBinary() {
10
+ try {
11
+ const binaryPath = getPlatformBinaryPath();
12
+ return fs.existsSync(binaryPath);
13
+ } catch (error) {
14
+ return false;
15
+ }
16
+ }
17
+
18
+ /**
19
+ * Check if we can build from source
20
+ */
21
+ function canBuildFromSource() {
22
+ try {
23
+ // Check if binding.gyp exists
24
+ const bindingGypPath = path.join(__dirname, '..', 'binding.gyp');
25
+ if (!fs.existsSync(bindingGypPath)) {
26
+ return false;
27
+ }
28
+
29
+ // Check if source files exist
30
+ const srcPath = path.join(__dirname, '..', 'src');
31
+ if (!fs.existsSync(srcPath)) {
32
+ return false;
33
+ }
34
+
35
+ // Check if node-gyp is available
36
+ try {
37
+ execSync('node-gyp --version', { stdio: 'ignore' });
38
+ return true;
39
+ } catch (error) {
40
+ return false;
41
+ }
42
+ } catch (error) {
43
+ return false;
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Check if we're in a MinGW environment
49
+ */
50
+ function isMinGWEnvironment() {
51
+ return process.env.HAMLIB_ROOT === '/mingw64' ||
52
+ process.env.HAMLIB_ROOT === 'C:\\msys64\\mingw64' ||
53
+ process.env.MSYSTEM === 'MINGW64' ||
54
+ process.env.MINGW_PREFIX === '/mingw64' ||
55
+ process.env.MINGW_PREFIX === 'C:\\msys64\\mingw64';
56
+ }
57
+
58
+ /**
59
+ * Check if hamlib is available in the system
60
+ */
61
+ function checkHamlibAvailable() {
62
+ try {
63
+ if (isMinGWEnvironment()) {
64
+ // In MinGW environment, try to use pkg-config to check hamlib
65
+ try {
66
+ execSync('pkg-config --exists hamlib', { stdio: 'ignore' });
67
+ console.log('✓ Hamlib found via pkg-config');
68
+ return true;
69
+ } catch (pkgConfigError) {
70
+ // If pkg-config fails, try to check for specific files
71
+ // Note: In MSYS2, /mingw64 might be mapped differently for Node.js
72
+ console.log('pkg-config check failed, trying direct file checks...');
73
+
74
+ // Try common MSYS2 paths
75
+ const possiblePaths = [
76
+ '/mingw64/include/hamlib',
77
+ 'C:/msys64/mingw64/include/hamlib',
78
+ 'C:\\msys64\\mingw64\\include\\hamlib',
79
+ process.env.MINGW_PREFIX + '/include/hamlib',
80
+ process.env.HAMLIB_ROOT + '/include/hamlib'
81
+ ].filter(Boolean);
82
+
83
+ for (const includePath of possiblePaths) {
84
+ try {
85
+ if (fs.existsSync(includePath)) {
86
+ console.log(`✓ Hamlib headers found at: ${includePath}`);
87
+ return true;
88
+ }
89
+ } catch (e) {
90
+ // Continue to next path
91
+ }
92
+ }
93
+
94
+ // If all file checks fail, assume hamlib is available since we're in MinGW
95
+ // and the build process showed it was installed
96
+ console.log('Direct file checks failed, but assuming hamlib is available in MinGW environment');
97
+ return true;
98
+ }
99
+ } else {
100
+ // Check if hamlib is available in standard locations
101
+ const locations = [
102
+ '/usr/include/hamlib',
103
+ '/usr/local/include/hamlib',
104
+ '/opt/homebrew/include/hamlib'
105
+ ];
106
+ return locations.some(location => fs.existsSync(location));
107
+ }
108
+ } catch (error) {
109
+ console.log('Error checking hamlib availability:', error.message);
110
+ // In MinGW environment, be more lenient since paths might be mapped differently
111
+ return isMinGWEnvironment();
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Setup Windows dependencies if needed
117
+ */
118
+ async function setupWindowsDependencies() {
119
+ // Skip if we're in MinGW environment - dependencies should already be set up
120
+ if (isMinGWEnvironment()) {
121
+ console.log('✓ MinGW environment detected, skipping Windows dependency setup...');
122
+ return checkHamlibAvailable();
123
+ }
124
+
125
+ // Check if dependencies are already set up
126
+ if (process.env.HAMLIB_ROOT && fs.existsSync(process.env.HAMLIB_ROOT)) {
127
+ console.log('✓ Windows dependencies already set up, skipping...');
128
+ return true;
129
+ }
130
+
131
+ console.log('Setting up Windows dependencies...');
132
+
133
+ try {
134
+ // Call PowerShell script instead of Node.js script
135
+ const scriptPath = path.join(__dirname, 'setup-windows-deps.ps1');
136
+ const command = `powershell -ExecutionPolicy Bypass -File "${scriptPath}"`;
137
+
138
+ console.log(`Running: ${command}`);
139
+ execSync(command, { stdio: 'inherit' });
140
+
141
+ console.log('✓ Windows dependencies setup completed');
142
+ return true;
143
+ } catch (error) {
144
+ console.error('✗ Failed to setup Windows dependencies:', error.message);
145
+ return false;
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Try to build from source
151
+ */
152
+ async function buildFromSource() {
153
+ console.log('Building hamlib from source...');
154
+
155
+ try {
156
+ // Setup Windows dependencies first if on Windows and not in MinGW environment
157
+ if (process.platform === 'win32' && !isMinGWEnvironment()) {
158
+ const success = await setupWindowsDependencies();
159
+ if (!success) {
160
+ throw new Error('Windows dependencies setup failed');
161
+ }
162
+ }
163
+
164
+ // For MinGW environment, verify hamlib is available
165
+ if (isMinGWEnvironment()) {
166
+ if (!checkHamlibAvailable()) {
167
+ throw new Error('Hamlib not found in MinGW environment. Please install hamlib first.');
168
+ }
169
+ console.log('✓ Hamlib found in MinGW environment');
170
+ }
171
+
172
+ // Build with node-gyp
173
+ execSync('node-gyp configure', { stdio: 'inherit' });
174
+ execSync('node-gyp build', { stdio: 'inherit' });
175
+
176
+ console.log('✓ Built hamlib successfully');
177
+ return true;
178
+ } catch (error) {
179
+ console.error('✗ Failed to build hamlib from source:', error.message);
180
+ return false;
181
+ }
182
+ }
183
+
184
+ /**
185
+ * Main installation logic
186
+ */
187
+ async function main() {
188
+ console.log('Installing node-hamlib...');
189
+
190
+ try {
191
+ // Try to find prebuilt binary first
192
+ const prebuiltPath = path.join(__dirname, '..', 'prebuilds');
193
+ const platform = process.platform;
194
+ const arch = process.arch;
195
+
196
+ let targetDir = '';
197
+ if (platform === 'win32' && arch === 'x64') {
198
+ targetDir = 'win32-x64';
199
+ } else if (platform === 'linux' && arch === 'x64') {
200
+ targetDir = 'linux-x64';
201
+ } else if (platform === 'linux' && arch === 'arm64') {
202
+ targetDir = 'linux-arm64';
203
+ } else if (platform === 'darwin' && arch === 'arm64') {
204
+ targetDir = 'darwin-arm64';
205
+ }
206
+
207
+ const prebuiltBinary = path.join(prebuiltPath, targetDir, 'hamlib.node');
208
+
209
+ if (fs.existsSync(prebuiltBinary)) {
210
+ console.log('✓ Found prebuilt binary');
211
+
212
+ // Copy to build directory
213
+ const buildDir = path.join(__dirname, '..', 'build', 'Release');
214
+ if (!fs.existsSync(buildDir)) {
215
+ fs.mkdirSync(buildDir, { recursive: true });
216
+ }
217
+
218
+ const targetPath = path.join(buildDir, 'hamlib.node');
219
+ fs.copyFileSync(prebuiltBinary, targetPath);
220
+
221
+ console.log('✓ Installation completed successfully');
222
+ return;
223
+ }
224
+
225
+ console.log('⚠ No prebuilt binary found for your platform');
226
+
227
+ // Build from source
228
+ const success = await buildFromSource();
229
+ if (!success) {
230
+ throw new Error('Failed to build from source');
231
+ }
232
+
233
+ console.log('✓ Installation completed successfully');
234
+
235
+ } catch (error) {
236
+ console.error('❌ Installation failed!');
237
+ console.error('This could be due to:');
238
+ console.error('1. No prebuilt binary available for your platform');
239
+ console.error('2. Missing system dependencies (libhamlib-dev)');
240
+ console.error('3. Missing build tools (node-gyp, compiler)');
241
+ console.error('');
242
+ console.error('To resolve this:');
243
+ console.error('System dependencies:');
244
+ console.error(' Linux: sudo apt-get install libhamlib-dev');
245
+ console.error(' macOS: brew install hamlib');
246
+ console.error(' Windows: Install hamlib via vcpkg or from source');
247
+ console.error('');
248
+ console.error('Build tools:');
249
+ console.error(' npm install -g node-gyp');
250
+ console.error(' # Follow node-gyp platform-specific setup instructions');
251
+
252
+ process.exit(1);
253
+ }
254
+ }
255
+
256
+ // Run only if this script is called directly
257
+ if (require.main === module) {
258
+ main().catch(error => {
259
+ console.error('Installation failed:', error);
260
+ process.exit(1);
261
+ });
262
+ }