UNPKG

spur-common

Version:

A Node.JS library of common modules used as a base to most Node.JS applications.

33 lines (22 loc) 895 B
module.exports = (path, Promise, fsPromise, FixtureCache, Logger)-> new class FixtureUtil constructor:(@fixturesPath)-> @cache = FixtureCache setFixturesPath:(@fixturesPath)-> Logger.log "Fixtures path changed to: #{@fixturesPath}" get:(name)=> @cache.getOrPromise name, ()=> @readAndProcessFile(name) readAndProcessFile:(name)=> @startFileRead(name) .then @processText startFileRead:(name)=> if not @fixturesPath return Promise.reject(new Error("fixtures path is not defined")) filePath = path.join(@fixturesPath, "#{name}.json") Logger.log "Using file fixture: #{filePath}" fsPromise .readFileAsync(filePath, {encoding: 'utf8'}) .catch (err)-> Promise.reject(new Error("#{filePath} not found")) processText:(text)-> JSON.parse(text) if text and text.length > 0