All files utils.js

100% Statements 14/14
100% Branches 4/4
100% Functions 2/2
100% Lines 13/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 261x 1x   1x 6x 6x   6x       4x 4x   1093x   2x 1x     1x   1x      
import fs from 'fs'
import Module from 'module'
 
export const listRequiredModules = (packageName) => {
  try {
    const module = new Module()
 
    const mainFilePath = Module._resolveFilename(packageName, {
      ...module, paths: Module._nodeModulePaths(process.cwd())
    })
 
    const mainFile = fs.readFileSync(mainFilePath, 'utf-8')
    const matches = mainFile.match(/\.\/([^']+)/g)
 
    return matches ? matches.map((item) => item.match(/\.\/([^']+)/)[1]) : []
  } catch (e) {
    if (e.code !== 'MODULE_NOT_FOUND') {
      throw e
    }
 
    console.warn(e.message)
 
    return []
  }
}