|
1 | 1 | #!/usr/bin/env node
|
2 | 2 |
|
3 |
| -try { |
4 |
| - new Function('var {a} = {a: 1}')(); |
5 |
| -} catch (error) { |
6 |
| - console.error('Your JavaScript runtime does not support some features used by the cake command. Please use Node 6 or later.'); |
| 3 | +// More comprehensive feature detection |
| 4 | +const testES6Support = () => { |
| 5 | + try { |
| 6 | + // Test multiple ES6 features |
| 7 | + new Function('let [a] = [1]; const {b} = {b: 2}; class Test {};'); |
| 8 | + return true; |
| 9 | + } catch (e) { |
| 10 | + return false; |
| 11 | + } |
| 12 | +}; |
| 13 | + |
| 14 | +if (!testES6Support()) { |
| 15 | + console.error('Your JavaScript runtime does not support ES6 features required by the cake command. Please use Node 6 or later.'); |
7 | 16 | process.exit(1);
|
8 | 17 | }
|
9 | 18 |
|
10 |
| -var path = require('path'); |
11 |
| -var fs = require('fs'); |
| 19 | +const path = require('path'); |
| 20 | +const fs = require('fs').promises; // Use promises API for async operations |
12 | 21 |
|
13 |
| -var potentialPaths = [ |
14 |
| - path.join(process.cwd(), 'node_modules/coffeescript/lib/coffeescript'), |
15 |
| - path.join(process.cwd(), 'node_modules/coffeescript/lib/coffee-script'), |
16 |
| - path.join(process.cwd(), 'node_modules/coffee-script/lib/coffee-script'), |
17 |
| - path.join(__dirname, '../lib/coffeescript') |
18 |
| -]; |
| 22 | +// More comprehensive search paths |
| 23 | +const potentialPaths = [ |
| 24 | + path.join(process.cwd(), 'node_modules', 'coffeescript', 'lib', 'coffeescript'), |
| 25 | + path.join(process.cwd(), 'node_modules', 'coffeescript', 'lib', 'coffee-script'), |
| 26 | + path.join(process.cwd(), 'node_modules', 'coffee-script', 'lib', 'coffee-script'), |
| 27 | + path.join(__dirname, '..', 'lib', 'coffeescript'), |
| 28 | + // Also check global installations |
| 29 | + path.join(process.env.NODE_PATH || '', 'coffeescript', 'lib', 'coffeescript'), |
| 30 | + path.join(process.env.NODE_PATH || '', 'coffee-script', 'lib', 'coffee-script'), |
| 31 | +].filter(Boolean); // Remove any empty paths |
19 | 32 |
|
20 |
| -for (var i = 0, len = potentialPaths.length; i < len; i++) { |
21 |
| - if (fs.existsSync(potentialPaths[i])) { |
22 |
| - require(potentialPaths[i] + '/cake').run(); |
23 |
| - break; |
| 33 | +// Async function to find and run cake |
| 34 | +async function findAndRunCake() { |
| 35 | + for (const potentialPath of potentialPaths) { |
| 36 | + try { |
| 37 | + // Use access instead of exists for better error handling |
| 38 | + await fs.access(potentialPath); |
| 39 | + const cakePath = path.join(potentialPath, 'cake'); |
| 40 | + require(cakePath).run(); |
| 41 | + return; // Exit on success |
| 42 | + } catch (error) { |
| 43 | + // Continue to next path if this one doesn't work |
| 44 | + continue; |
| 45 | + } |
24 | 46 | }
|
| 47 | + |
| 48 | + // If we get here, no path was found |
| 49 | + console.error('Could not find CoffeeScript installation. Please install it with:'); |
| 50 | + console.error(' npm install -g coffeescript'); |
| 51 | + console.error(' or'); |
| 52 | + console.error(' npm install --save-dev coffeescript'); |
| 53 | + process.exit(1); |
25 | 54 | }
|
| 55 | + |
| 56 | +// Run the async function |
| 57 | +findAndRunCake().catch(error => { |
| 58 | + console.error('Unexpected error:', error.message); |
| 59 | + process.exit(1); |
| 60 | +}); |
0 commit comments