@@ -2,10 +2,13 @@ const fs = require('fs');
22const path = require ( 'path' ) ;
33const config = require ( './config' ) ;
44const yaml = require ( 'read-yaml' ) ;
5+ const Hashids = require ( 'hashids' ) ;
6+ const hashids = new Hashids ( ) ;
57
68const globalConfigDir = process . env [ ( process . platform == 'win32' ) ? 'USERPROFILE' : 'HOME' ] + '/.gtt' ;
79const globalConfigFile = globalConfigDir + '/config.yml' ;
810const frameDir = globalConfigDir + '/frames' ;
11+ const cacheDir = globalConfigDir + '/cache' ;
912const localConfigFile = '/.gtt.yml' ;
1013
1114/**
@@ -21,6 +24,10 @@ class fileConfig extends config {
2124 this . assertGlobalConfig ( ) ;
2225 this . workDir = workDir ;
2326 this . data = Object . assign ( this . data , this . localExists ( ) ? this . parseLocal ( ) : this . parseGlobal ( ) ) ;
27+ this . cache = {
28+ get : this . _cacheGet ,
29+ set : this . _cacheSet
30+ } ;
2431 }
2532
2633 /**
@@ -66,13 +73,33 @@ class fileConfig extends config {
6673 assertGlobalConfig ( ) {
6774 if ( ! fs . existsSync ( this . globalDir ) ) fs . mkdirSync ( this . globalDir , '0644' , true ) ;
6875 if ( ! fs . existsSync ( this . frameDir ) ) fs . mkdirSync ( this . frameDir , '0744' , true ) ;
76+ if ( ! fs . existsSync ( this . cacheDir ) ) fs . mkdirSync ( this . cacheDir , '0744' , true ) ;
6977 if ( ! fs . existsSync ( this . global ) ) fs . appendFileSync ( this . global , '' ) ;
7078 }
7179
7280 assertLocalConfig ( ) {
7381 if ( ! this . localExists ( ) ) fs . appendFileSync ( this . local , '' ) ;
7482 }
7583
84+ _cacheGet ( key ) {
85+ let file = this . cacheDir + '/' + hashids . encode ( key ) ;
86+ if ( ! fs . existsSync ( file ) ) return false ;
87+
88+ return JSON . parse ( fs . readFileSync ( file ) ) ;
89+ }
90+
91+ _cacheSet ( key , value ) {
92+ let file = this . cacheDir + '/' + hashids . encode ( key ) ;
93+ if ( fs . existsSync ( file ) ) fs . unlinkSync ( file ) ;
94+ fs . appendFileSync ( file , JSON . stringify ( value ) ) ;
95+
96+ return value ;
97+ }
98+
99+ get cacheDir ( ) {
100+ return cacheDir ;
101+ }
102+
76103 get frameDir ( ) {
77104 return frameDir ;
78105 }
0 commit comments