1313require ( 'loadenv' ) ( ) ;
1414
1515var fs = require ( 'fs' ) ;
16- var Build = require ( 'models/mongo/build' ) ;
1716var Context = require ( 'models/mongo/context' ) ;
1817var ContextVersion = require ( 'models/mongo/context-version' ) ;
1918var InfraCodeVersion = require ( 'models/mongo/infra-code-version' ) ;
@@ -65,55 +64,32 @@ var createdBy = {
6564 github : process . env . HELLO_RUNNABLE_GITHUB_ID
6665} ;
6766
68- function removeCurrentSourceTemplates ( name , cb ) {
69- Context . find ( { 'name' : name , 'isSource' : true } , function ( err , docs ) {
70- if ( err ) { return cb ( err ) ; }
71- async . each ( docs , function ( doc , cb ) {
72- // we don't ever want to delete old contexts, once the are in use by users
73- console . log ( 'UN-SOURCING OLD SOURCES' ) ;
74- doc . update ( {
75- $set : {
76- name : doc . name + Date . now ( ) ,
77- lowerName : doc . lowerName + Date . now ( ) ,
78- isSource : false ,
79- oldSource : Date . now ( )
80- }
81- } , cb ) ;
82- } , function ( ) {
83- cb ( ) ;
84- } ) ;
85- } ) ;
86- }
8767
88- function createBlankSourceContext ( cb ) {
68+ function createBlankSourceContext ( thisCb ) {
8969 async . waterfall ( [
9070 function ( cb ) {
91- Instance . find ( {
92- 'lowerName' : ( 'Blank' ) . toLowerCase ( ) ,
93- 'owner' : createdBy
94- } , function ( err , docs ) {
95- console . log ( 'REMOVING existing instance for (BLANK)' ) ;
71+ Context . findOne ( { 'name' : 'Blank' , 'isSource' : true } , function ( err , doc ) {
9672 if ( err ) { return cb ( err ) ; }
97- async . each ( docs , function ( doc , cb ) {
98- console . log ( 'REMOVING INSTANCES' , docs ) ;
99- doc . remove ( cb ) ;
100- } , function ( ) {
101- cb ( null , 'Blank' ) ;
102- } ) ;
103- } ) ;
104- } ,
105- removeCurrentSourceTemplates ,
106- function newContext ( cb ) {
107- console . log ( 'newContext (blank)' ) ;
108- var context = new Context ( {
109- owner : createdBy ,
110- name : 'Blank' ,
111- description : 'An empty template!' ,
112- isSource : true
73+ if ( doc ) {
74+ console . log ( 'Context (blank)' ) ;
75+ ctx . blankIcv = doc . infraCodeVersion ;
76+ thisCb ( ) ;
77+ }
78+ else {
79+ console . log ( 'newContext (blank)' ) ;
80+ var context = new Context ( {
81+ owner : createdBy ,
82+ name : 'Blank' ,
83+ description : 'An empty template!' ,
84+ isSource : true
85+ } ) ;
86+ context . save ( function ( err , doc ) {
87+ cb ( err , doc ) ;
88+ } ) ;
89+ }
11390 } ) ;
114- context . save ( cb ) ;
11591 } ,
116- function newICV ( context , count , cb ) {
92+ function newICV ( context , cb ) {
11793 console . log ( 'newICV (blank)' ) ;
11894 var icv = new InfraCodeVersion ( {
11995 context : context . _id
@@ -125,11 +101,11 @@ function createBlankSourceContext (cb) {
125101 ] , function ( err ) { cb ( err , context , icv ) ; } ) ;
126102 } ,
127103 function ( context , icv , cb ) {
128- ctx . blankIcv = icv ;
104+ ctx . blankIcv = icv . _id ;
129105 cb ( null , context , icv ) ;
130106 } ,
131107 newCV
132- ] , cb ) ;
108+ ] , thisCb ) ;
133109}
134110
135111/**
@@ -155,13 +131,13 @@ function createFirstSourceContext(finalCB) {
155131 if ( err ) { return cb ( err ) ; }
156132 async . each ( docs , function ( doc , cb ) {
157133 console . log ( 'REMOVING INSTANCES' , docs ) ;
158- doc . remove ( cb ) ;
159- } , function ( ) {
160- cb ( null , model . name ) ;
161- } ) ;
134+ doc . remove ( next ) ;
135+ } , next ) ;
162136 } ) ;
137+ function next ( err ) {
138+ cb ( err ) ;
139+ }
163140 } ,
164- removeCurrentSourceTemplates ,
165141 function ( cb ) {
166142 if ( model . isTemplate ) {
167143 return cb ( ) ;
@@ -193,7 +169,7 @@ function createFirstSourceContext(finalCB) {
193169 console . log ( 'newICV (' , model . name , ')' ) ;
194170 var icv = new InfraCodeVersion ( {
195171 context : context . _id ,
196- parent : ctx . blankIcv . _id
172+ parent : ctx . blankIcv
197173 } ) ;
198174 async . series ( [
199175 icv . initWithDefaults . bind ( icv ) ,
@@ -221,11 +197,11 @@ function createFirstSourceContext(finalCB) {
221197
222198 function buildBuild ( build , cb ) {
223199 console . log ( 'buildBuild (' , model . name , ')' ) ;
224- build . build ( { message : 'seed instance script' , noCache : true } , function ( err , buildDoc ) {
200+ build . build ( { message : 'seed instance script' , noCache : true } , function ( err ) {
225201 setTimeout ( function ( ) {
226202 cb ( err , build ) ;
227- } , 500 )
228- } )
203+ } , 500 ) ;
204+ } ) ;
229205 }
230206
231207 function createInstance ( build , cb ) {
0 commit comments