Created
          August 7, 2019 09:39 
        
      - 
      
- 
        Save danieltorscho/9e86e5f3f0aa137f751c47e3e0d4ed4e to your computer and use it in GitHub Desktop. 
    Check module existence within Front end
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | /** | |
| * Check if required module exists physically | |
| * | |
| * Checking if a requested filename is existing physically | |
| * without having to load it before for performance purpose. | |
| * This check should be made before requiring the script. | |
| * | |
| * @param {*} filename Name of the file to check for | |
| */ | |
| moduleExists (filename) { | |
| // Search for .js files inside a specific folder | |
| let req = require.context('./drivers', false, /\.js$/i) | |
| // Get their cleaned up filepath names | |
| const files = req.keys().map(item => item.slice(2, (item.length - 3))) | |
| // Return results as an array or an object | |
| return files.includes(filename) | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment