2016-10-08 12:01:58 +00:00
|
|
|
import fs from 'mz/fs'
|
|
|
|
import resolve from './resolve'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* resolve a file like `require.resolve`,
|
|
|
|
* and read and cache the file content
|
|
|
|
*/
|
|
|
|
|
2016-10-17 02:19:42 +00:00
|
|
|
async function read (path) {
|
2016-10-15 19:49:42 +00:00
|
|
|
const f = await resolve(path)
|
|
|
|
if (cache.hasOwnProperty(f)) {
|
|
|
|
return cache[f]
|
2016-10-08 12:01:58 +00:00
|
|
|
}
|
2016-10-14 15:05:08 +00:00
|
|
|
|
2016-10-15 19:49:42 +00:00
|
|
|
const data = fs.readFile(f, 'utf8')
|
|
|
|
cache[f] = data
|
|
|
|
return data
|
2016-10-08 12:01:58 +00:00
|
|
|
}
|
|
|
|
|
2016-10-15 19:49:42 +00:00
|
|
|
export default read
|
|
|
|
export const cache = {}
|
2016-10-08 12:01:58 +00:00
|
|
|
|
2016-10-15 19:49:42 +00:00
|
|
|
read.cache = cache
|