diff --git a/bin/cake b/bin/cake index 067cabbad3..fa52dea7eb 100755 --- a/bin/cake +++ b/bin/cake @@ -1,25 +1,60 @@ #!/usr/bin/env node -try { - new Function('var {a} = {a: 1}')(); -} catch (error) { - console.error('Your JavaScript runtime does not support some features used by the cake command. Please use Node 6 or later.'); +// More comprehensive feature detection +const testES6Support = () => { + try { + // Test multiple ES6 features + new Function('let [a] = [1]; const {b} = {b: 2}; class Test {};'); + return true; + } catch (e) { + return false; + } +}; + +if (!testES6Support()) { + console.error('Your JavaScript runtime does not support ES6 features required by the cake command. Please use Node 6 or later.'); process.exit(1); } -var path = require('path'); -var fs = require('fs'); +const path = require('path'); +const fs = require('fs').promises; // Use promises API for async operations -var potentialPaths = [ - path.join(process.cwd(), 'node_modules/coffeescript/lib/coffeescript'), - path.join(process.cwd(), 'node_modules/coffeescript/lib/coffee-script'), - path.join(process.cwd(), 'node_modules/coffee-script/lib/coffee-script'), - path.join(__dirname, '../lib/coffeescript') -]; +// More comprehensive search paths +const potentialPaths = [ + path.join(process.cwd(), 'node_modules', 'coffeescript', 'lib', 'coffeescript'), + path.join(process.cwd(), 'node_modules', 'coffeescript', 'lib', 'coffee-script'), + path.join(process.cwd(), 'node_modules', 'coffee-script', 'lib', 'coffee-script'), + path.join(__dirname, '..', 'lib', 'coffeescript'), + // Also check global installations + path.join(process.env.NODE_PATH || '', 'coffeescript', 'lib', 'coffeescript'), + path.join(process.env.NODE_PATH || '', 'coffee-script', 'lib', 'coffee-script'), +].filter(Boolean); // Remove any empty paths -for (var i = 0, len = potentialPaths.length; i < len; i++) { - if (fs.existsSync(potentialPaths[i])) { - require(potentialPaths[i] + '/cake').run(); - break; +// Async function to find and run cake +async function findAndRunCake() { + for (const potentialPath of potentialPaths) { + try { + // Use access instead of exists for better error handling + await fs.access(potentialPath); + const cakePath = path.join(potentialPath, 'cake'); + require(cakePath).run(); + return; // Exit on success + } catch (error) { + // Continue to next path if this one doesn't work + continue; + } } + + // If we get here, no path was found + console.error('Could not find CoffeeScript installation. Please install it with:'); + console.error(' npm install -g coffeescript'); + console.error(' or'); + console.error(' npm install --save-dev coffeescript'); + process.exit(1); } + +// Run the async function +findAndRunCake().catch(error => { + console.error('Unexpected error:', error.message); + process.exit(1); +}); diff --git a/bin/coffee b/bin/coffee index 9ebc45efa8..dd6bb73823 100755 --- a/bin/coffee +++ b/bin/coffee @@ -1,25 +1,64 @@ #!/usr/bin/env node -try { - new Function('var {a} = {a: 1}')(); -} catch (error) { - console.error('Your JavaScript runtime does not support some features used by the coffee command. Please use Node 6 or later.'); +// More comprehensive ES6 feature detection +const testES6Support = () => { + try { + // Test multiple ES6 features, not just destructuring + new Function(` + let [a] = [1]; + const {b} = {b: 2}; + class Test {}; + () => {}; + for (let x of [1,2,3]) {}; + Promise.resolve(); + `)(); + return true; + } catch (e) { + return false; + } +}; + +if (!testES6Support()) { + console.error('Your JavaScript runtime does not support ES6 features required by the coffee command. Please use Node 6 or later.'); process.exit(1); } -var path = require('path'); -var fs = require('fs'); +const path = require('path'); +const fs = require('fs').promises; -var potentialPaths = [ - path.join(process.cwd(), 'node_modules/coffeescript/lib/coffeescript'), - path.join(process.cwd(), 'node_modules/coffeescript/lib/coffee-script'), - path.join(process.cwd(), 'node_modules/coffee-script/lib/coffee-script'), - path.join(__dirname, '../lib/coffeescript') -]; +// More comprehensive search paths including global installs +const potentialPaths = [ + path.join(process.cwd(), 'node_modules', 'coffeescript', 'lib', 'coffeescript'), + path.join(process.cwd(), 'node_modules', 'coffeescript', 'lib', 'coffee-script'), + path.join(process.cwd(), 'node_modules', 'coffee-script', 'lib', 'coffee-script'), + path.join(__dirname, '..', 'lib', 'coffeescript'), + // Global installation paths + path.join(process.env.npm_config_prefix || '', 'lib', 'node_modules', 'coffeescript', 'lib', 'coffeescript'), + path.join(process.env.HOME || process.env.USERPROFILE, '.npm', 'lib', 'node_modules', 'coffeescript', 'lib', 'coffeescript'), +].filter(Boolean); -for (var i = 0, len = potentialPaths.length; i < len; i++) { - if (fs.existsSync(potentialPaths[i])) { - require(potentialPaths[i] + '/command').run(); - break; +async function findAndRunCoffee() { + for (const potentialPath of potentialPaths) { + try { + await fs.access(potentialPath); + const commandPath = path.join(potentialPath, 'command'); + require(commandPath).run(); + return; // Exit on success + } catch (error) { + // Continue to next path + continue; + } } + + // If no path was found + console.error('CoffeeScript not found. Please install it with:'); + console.error(' npm install -g coffeescript'); + console.error(' or'); + console.error(' npm install --save-dev coffeescript'); + process.exit(1); } + +findAndRunCoffee().catch(error => { + console.error('Failed to start coffee command:', error.message); + process.exit(1); +});