UNPKG

699 BJavaScriptView Raw
1'use strict'
2
3const path = require('path')
4
5// note: filePath needs to be relative to the module root
6module.exports = function loadFixtures (filePath, module) {
7 if (module) {
8 filePath = path.join(module, filePath)
9 }
10
11 const fs = require('fs')
12 const paths = [
13 path.join(process.cwd(), filePath),
14 path.join(process.cwd(), 'node_modules', filePath),
15 resolve(filePath)
16 ]
17
18 const resourcePath = paths.find(path => fs.existsSync(path))
19
20 if (!resourcePath) {
21 throw new Error(`Could not load ${filePath}`)
22 }
23
24 return fs.readFileSync(resourcePath)
25}
26
27function resolve (filePath) {
28 try {
29 return require.resolve(filePath)
30 } catch (error) {
31 // ignore error
32 }
33}