UNPKG

@resin/pinejs

Version:

Pine.js is a sophisticated rules-driven API engine that enables you to define rules in a structured subset of English. Those rules are used in order for Pine.js to generate a database schema and the associated [OData](http://www.odata.org/) API. This make

50 lines (46 loc) 1 kB
import * as _fs from 'fs' import * as _ from 'lodash' const cacheFile = process.env.PINEJS_CACHE_FILE || '.pinejs-cache.json' let cache: null | { [ name: string ]: { [ version: string ]: { [ srcJson: string ]: any } } } = null let fs: undefined | typeof _fs try { fs = require('fs') } catch (e) {} const saveCache = _.debounce( () => { if (fs != null) { fs.writeFile(cacheFile, JSON.stringify(cache), 'utf8', (err) => { if (err) { console.warn('Error saving pinejs cache:', err) } }) } }, 5000 ) export const cachedCompile = (name: string, version: string, src: any, fn: () => any) => { if (cache == null) { if (fs != null) { try { cache = JSON.parse(fs.readFileSync(cacheFile, 'utf8')) } catch (e) {} } if (cache == null) { cache = {} } } const key = [ name, version, JSON.stringify(src) ] let result = _.get(cache, key) if (result == null) { result = fn() _.set(cache, key, result) saveCache() } return _.cloneDeep(result) }