"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/season/lib/cson.js" (8 Mar 2017, 7943 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 (function() {
    2   var CSON, crypto, csonCache, detectDuplicateKeys, fs, getCachePath, isAllCommentsAndWhitespace, parseCacheContents, parseContents, parseContentsSync, parseObject, path, stats, writeCacheFile, writeCacheFileSync;
    3 
    4   crypto = require('crypto');
    5 
    6   path = require('path');
    7 
    8   fs = require('fs-plus');
    9 
   10   CSON = null;
   11 
   12   csonCache = null;
   13 
   14   stats = {
   15     hits: 0,
   16     misses: 0
   17   };
   18 
   19   getCachePath = function(cson) {
   20     var digest;
   21     digest = crypto.createHash('sha1').update(cson, 'utf8').digest('hex');
   22     return path.join(csonCache, "" + digest + ".json");
   23   };
   24 
   25   writeCacheFileSync = function(cachePath, object) {
   26     try {
   27       return fs.writeFileSync(cachePath, JSON.stringify(object));
   28     } catch (_error) {}
   29   };
   30 
   31   writeCacheFile = function(cachePath, object) {
   32     return fs.writeFile(cachePath, JSON.stringify(object), function() {});
   33   };
   34 
   35   parseObject = function(objectPath, contents, options) {
   36     var error, parsed;
   37     if (path.extname(objectPath) === '.cson') {
   38       if (CSON == null) {
   39         CSON = require('cson-parser');
   40       }
   41       try {
   42         parsed = CSON.parse(contents, (options != null ? options.allowDuplicateKeys : void 0) === false ? detectDuplicateKeys : void 0);
   43         stats.misses++;
   44         return parsed;
   45       } catch (_error) {
   46         error = _error;
   47         if (isAllCommentsAndWhitespace(contents)) {
   48           return null;
   49         } else {
   50           throw error;
   51         }
   52       }
   53     } else {
   54       return JSON.parse(contents);
   55     }
   56   };
   57 
   58   parseCacheContents = function(contents) {
   59     var parsed;
   60     parsed = JSON.parse(contents);
   61     stats.hits++;
   62     return parsed;
   63   };
   64 
   65   parseContentsSync = function(objectPath, cachePath, contents, options) {
   66     var object, parseError;
   67     try {
   68       object = parseObject(objectPath, contents, options);
   69     } catch (_error) {
   70       parseError = _error;
   71       if (parseError.path == null) {
   72         parseError.path = objectPath;
   73       }
   74       if (parseError.filename == null) {
   75         parseError.filename = objectPath;
   76       }
   77       throw parseError;
   78     }
   79     if (cachePath) {
   80       writeCacheFileSync(cachePath, object);
   81     }
   82     return object;
   83   };
   84 
   85   isAllCommentsAndWhitespace = function(contents) {
   86     var line, lines;
   87     lines = contents.split('\n');
   88     while (lines.length > 0) {
   89       line = lines[0].trim();
   90       if (line.length === 0 || line[0] === '#') {
   91         lines.shift();
   92       } else {
   93         return false;
   94       }
   95     }
   96     return true;
   97   };
   98 
   99   parseContents = function(objectPath, cachePath, contents, options, callback) {
  100     var object, parseError;
  101     try {
  102       object = parseObject(objectPath, contents, options);
  103     } catch (_error) {
  104       parseError = _error;
  105       parseError.path = objectPath;
  106       if (parseError.filename == null) {
  107         parseError.filename = objectPath;
  108       }
  109       parseError.message = "" + objectPath + ": " + parseError.message;
  110       if (typeof callback === "function") {
  111         callback(parseError);
  112       }
  113       return;
  114     }
  115     if (cachePath) {
  116       writeCacheFile(cachePath, object);
  117     }
  118     if (typeof callback === "function") {
  119       callback(null, object);
  120     }
  121   };
  122 
  123   module.exports = {
  124     setCacheDir: function(cacheDirectory) {
  125       return csonCache = cacheDirectory;
  126     },
  127     isObjectPath: function(objectPath) {
  128       var extension;
  129       if (!objectPath) {
  130         return false;
  131       }
  132       extension = path.extname(objectPath);
  133       return extension === '.cson' || extension === '.json';
  134     },
  135     resolve: function(objectPath) {
  136       var csonPath, jsonPath;
  137       if (objectPath == null) {
  138         objectPath = '';
  139       }
  140       if (!objectPath) {
  141         return null;
  142       }
  143       if (this.isObjectPath(objectPath) && fs.isFileSync(objectPath)) {
  144         return objectPath;
  145       }
  146       jsonPath = "" + objectPath + ".json";
  147       if (fs.isFileSync(jsonPath)) {
  148         return jsonPath;
  149       }
  150       csonPath = "" + objectPath + ".cson";
  151       if (fs.isFileSync(csonPath)) {
  152         return csonPath;
  153       }
  154       return null;
  155     },
  156     readFileSync: function(objectPath, options) {
  157       var cachePath, contents;
  158       contents = fs.readFileSync(objectPath, 'utf8');
  159       if (contents.trim().length === 0) {
  160         return null;
  161       }
  162       if (csonCache && path.extname(objectPath) === '.cson') {
  163         cachePath = getCachePath(contents);
  164         if (fs.isFileSync(cachePath)) {
  165           try {
  166             return parseCacheContents(fs.readFileSync(cachePath, 'utf8'));
  167           } catch (_error) {}
  168         }
  169       }
  170       return parseContentsSync(objectPath, cachePath, contents, options);
  171     },
  172     readFile: function(objectPath, options, callback) {
  173       if (arguments.length < 3) {
  174         callback = options;
  175         options = null;
  176       }
  177       return fs.readFile(objectPath, 'utf8', (function(_this) {
  178         return function(error, contents) {
  179           var cachePath;
  180           if (error != null) {
  181             return typeof callback === "function" ? callback(error) : void 0;
  182           }
  183           if (contents.trim().length === 0) {
  184             return typeof callback === "function" ? callback(null, null) : void 0;
  185           }
  186           if (csonCache && path.extname(objectPath) === '.cson') {
  187             cachePath = getCachePath(contents);
  188             return fs.stat(cachePath, function(error, stat) {
  189               if (stat != null ? stat.isFile() : void 0) {
  190                 return fs.readFile(cachePath, 'utf8', function(error, cached) {
  191                   var parsed;
  192                   try {
  193                     parsed = parseCacheContents(cached);
  194                   } catch (_error) {
  195                     error = _error;
  196                     try {
  197                       parseContents(objectPath, cachePath, contents, options, callback);
  198                     } catch (_error) {}
  199                     return;
  200                   }
  201                   return typeof callback === "function" ? callback(null, parsed) : void 0;
  202                 });
  203               } else {
  204                 return parseContents(objectPath, cachePath, contents, options, callback);
  205               }
  206             });
  207           } else {
  208             return parseContents(objectPath, null, contents, options, callback);
  209           }
  210         };
  211       })(this));
  212     },
  213     writeFile: function(objectPath, object, callback) {
  214       var contents, error;
  215       if (callback == null) {
  216         callback = function() {};
  217       }
  218       try {
  219         contents = this.stringifyPath(objectPath, object);
  220       } catch (_error) {
  221         error = _error;
  222         callback(error);
  223         return;
  224       }
  225       return fs.writeFile(objectPath, "" + contents + "\n", callback);
  226     },
  227     writeFileSync: function(objectPath, object) {
  228       return fs.writeFileSync(objectPath, "" + (this.stringifyPath(objectPath, object)) + "\n");
  229     },
  230     stringifyPath: function(objectPath, object, visitor, space) {
  231       if (path.extname(objectPath) === '.cson') {
  232         return this.stringify(object, visitor, space);
  233       } else {
  234         return JSON.stringify(object, void 0, 2);
  235       }
  236     },
  237     stringify: function(object, visitor, space) {
  238       if (space == null) {
  239         space = 2;
  240       }
  241       if (CSON == null) {
  242         CSON = require('cson-parser');
  243       }
  244       return CSON.stringify(object, visitor, space);
  245     },
  246     parse: function(str, reviver) {
  247       if (CSON == null) {
  248         CSON = require('cson-parser');
  249       }
  250       return CSON.parse(str, reviver);
  251     },
  252     getCacheHits: function() {
  253       return stats.hits;
  254     },
  255     getCacheMisses: function() {
  256       return stats.misses;
  257     },
  258     resetCacheStats: function() {
  259       return stats = {
  260         hits: 0,
  261         misses: 0
  262       };
  263     }
  264   };
  265 
  266   detectDuplicateKeys = function(key, value) {
  267     if (this.hasOwnProperty(key) && this[key] !== value) {
  268       throw new Error("Duplicate key '" + key + "'");
  269     } else {
  270       return value;
  271     }
  272   };
  273 
  274 }).call(this);