@@ -5,11 +5,22 @@ import path from "path";
55import _ from "underscore" ;
66
77class LibraryDb {
8- loadTemplates ( ) {
8+ constructor ( ) {
9+ this . _items = null ;
10+ this . _all = null ;
11+ }
12+
13+ isDevelopment ( ) {
14+ return process . env . NODE_ENV === "development" ;
15+ }
16+
17+ readTemplatesFromDisk ( ) {
918 const templatePath = path . join ( __dirname , "step-templates.json" ) ;
10- const stepTemplates = JSON . parse ( fs . readFileSync ( templatePath , "utf8" ) ) ;
19+ return JSON . parse ( fs . readFileSync ( templatePath , "utf8" ) ) ;
20+ }
1121
12- return _ . chain ( stepTemplates . items )
22+ hydrateTemplates ( stepTemplates ) {
23+ const items = _ . chain ( stepTemplates . items )
1324 . map ( function ( t ) {
1425 if ( t . Properties ) {
1526 var script = t . Properties [ "Octopus.Action.Script.ScriptBody" ] ;
@@ -44,14 +55,40 @@ class LibraryDb {
4455 return t . Name . toLowerCase ( ) ;
4556 } )
4657 . value ( ) ;
58+
59+ return {
60+ items,
61+ all : _ . indexBy ( items , "Id" ) ,
62+ } ;
63+ }
64+
65+ loadTemplates ( ) {
66+ return this . hydrateTemplates ( this . readTemplatesFromDisk ( ) ) ;
67+ }
68+
69+ getTemplates ( ) {
70+ if ( this . isDevelopment ( ) ) {
71+ return this . loadTemplates ( ) ;
72+ }
73+
74+ if ( ! this . _items || ! this . _all ) {
75+ const templates = this . loadTemplates ( ) ;
76+ this . _items = templates . items ;
77+ this . _all = templates . all ;
78+ }
79+
80+ return {
81+ items : this . _items ,
82+ all : this . _all ,
83+ } ;
4784 }
4885
4986 list ( cb ) {
50- cb ( null , this . loadTemplates ( ) ) ;
87+ cb ( null , this . getTemplates ( ) . items ) ;
5188 }
5289
5390 get ( id , cb ) {
54- var item = _ . indexBy ( this . loadTemplates ( ) , "Id" ) [ id ] ;
91+ var item = this . getTemplates ( ) . all [ id ] ;
5592 cb ( null , item ) ;
5693 }
5794}
0 commit comments