source: trip-planner-front/node_modules/stylus/lib/cache/fs.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.6 KB
Line 
1/**
2 * Module dependencies.
3 */
4
5var crypto = require('crypto')
6 , fs = require('fs')
7 , join = require('path').join
8 , version = require('../../package').version
9 , nodes = require('../nodes');
10
11var FSCache = module.exports = function(options) {
12 options = options || {};
13 this._location = options['cache location'] || '.styl-cache';
14 if (!fs.existsSync(this._location)) fs.mkdirSync(this._location);
15};
16
17/**
18 * Set cache item with given `key` to `value`.
19 *
20 * @param {String} key
21 * @param {Object} value
22 * @api private
23 */
24
25FSCache.prototype.set = function(key, value) {
26 fs.writeFileSync(join(this._location, key), JSON.stringify(value));
27};
28
29/**
30 * Get cache item with given `key`.
31 *
32 * @param {String} key
33 * @return {Object}
34 * @api private
35 */
36
37FSCache.prototype.get = function(key) {
38 var data = fs.readFileSync(join(this._location, key), 'utf-8');
39 return JSON.parse(data, FSCache.fromJSON);
40};
41
42/**
43 * Check if cache has given `key`.
44 *
45 * @param {String} key
46 * @return {Boolean}
47 * @api private
48 */
49
50FSCache.prototype.has = function(key) {
51 return fs.existsSync(join(this._location, key));
52};
53
54/**
55 * Generate key for the source `str` with `options`.
56 *
57 * @param {String} str
58 * @param {Object} options
59 * @return {String}
60 * @api private
61 */
62
63FSCache.prototype.key = function(str, options) {
64 var hash = crypto.createHash('sha1');
65 hash.update(str + version + options.prefix);
66 return hash.digest('hex');
67};
68
69/**
70 * JSON to Stylus nodes converter.
71 *
72 * @api private
73 */
74
75FSCache.fromJSON = function(key, val) {
76 if (val && val.__type) {
77 val.__proto__ = nodes[val.__type].prototype;
78 }
79 return val;
80};
Note: See TracBrowser for help on using the repository browser.