@@ -19,107 +19,231 @@ setTimeout(function(){
1919 process . exit ( )
2020} , 120000 )
2121
22- const { config, sources , crud } = CoCreateConfig ;
22+ const { config, directories , sources } = CoCreateConfig ;
2323
2424CoCreateCrud . socket . create ( config )
2525config . broadcast = false
2626
2727
2828/**
29- * update and create document by config crud
30- */
29+ * Store files by config directories
30+ **/
31+ let errorLog = [ ] ;
3132
32- if ( crud ) {
33- crud . forEach ( async ( data ) => {
34- await runStore ( data )
35- } )
33+ async function runDirectories ( ) {
34+ for ( const directory of directories ) {
35+ const entry = directory . entry
36+ const exclude = directory . exclude
37+ await runFiles ( directory , entry , exclude )
38+ }
39+ return
3640}
3741
42+ async function runFiles ( directory , entry , exclude , parentDirectory = '' ) {
43+ let files = fs . readdirSync ( entry ) ;
44+
45+ for ( let file of files ) {
46+ if ( exclude && exclude . includes ( file ) ) continue
47+
48+ let isDirectory = fs . existsSync ( `${ entry } /${ file } ` ) && fs . lstatSync ( `${ entry } /${ file } ` ) . isDirectory ( ) ;
49+ let name = file
50+ let source = ''
51+ let directoryName = parentDirectory || '' ;
52+ let mimeType = mime . lookup ( `${ file } ` )
53+ let pathName = '' ;
54+
55+ if ( ! directoryName && directory . document && directory . document . directory )
56+ directoryName = directory . document . directory . replace ( '{{directory}}' , '' ) . trim ( )
57+ else if ( ! directoryName )
58+ directoryName = '/'
59+
60+ if ( exclude && exclude . includes ( directoryName ) ) continue
61+
62+ if ( directoryName . endsWith ( "/" ) )
63+ pathName = directoryName + name
64+ else if ( directoryName )
65+ pathName = directoryName + '/' + name
66+ else
67+ pathName = '/' + name
68+
69+ if ( exclude && exclude . includes ( pathName ) ) continue
70+
71+ if ( isDirectory )
72+ mimeType = "text/directory"
73+ else
74+ source = getSource ( `${ entry } /${ file } ` , mimeType )
75+
76+ let values = {
77+ '{{name}}' : name ,
78+ '{{source}}' : source ,
79+ '{{directory}}' : directoryName ,
80+ '{{path}}' : pathName ,
81+ '{{content-type}}' : mimeType
82+ }
83+
84+ let document = { ...directory . document }
85+ if ( ! document . name )
86+ document . name = "{{name}}"
87+ if ( ! document . src )
88+ document . src = "{{source}}"
89+ if ( ! document . directory )
90+ document . directory = "/{{directory}}"
91+ if ( ! document . path )
92+ document . path = "{{path}}"
93+ if ( ! document [ "content-type" ] )
94+ document [ "content-type" ] = '{{content-type}}'
95+ if ( ! document . public && document . public != false && document . public != 'false' )
96+ document . public = 'true'
97+
98+ let object = {
99+ collection : directory . collection || 'files' ,
100+ document
101+ }
102+ for ( const key of Object . keys ( directory . document ) ) {
103+ if ( typeof directory . document [ key ] == 'string' ) {
104+
105+ let variables = directory . document [ key ] . match ( / { { ( [ A - Z a - z 0 - 9 _ . , \[ \] \- \/ ] * ) } } / g) ;
106+ if ( variables ) {
107+ for ( let variable of variables ) {
108+ if ( variable == '{{directory}}' ) {
109+ if ( parentDirectory )
110+ object . document [ key ] = values [ variable ]
111+ else
112+ object . document [ key ] = object . document [ key ] . replace ( variable , '' ) ;
113+ }
114+ else if ( isDirectory && variable == '{{source}}' )
115+ delete object . document [ key ]
116+ else
117+ object . document [ key ] = object . document [ key ] . replace ( variable , values [ variable ] ) ;
118+ }
119+ }
120+
121+ }
122+ }
123+
124+ if ( ! object . document . _id )
125+ object . filter = {
126+ query : [ { name : 'path' , value : pathName , operator : '$eq' } ]
127+ }
128+
129+ response = await runStore ( object ) ;
130+ if ( response . error )
131+ errorLog . push ( response . error )
132+
133+ if ( isDirectory && pathName ) {
134+ let newEntry
135+ if ( entry . endsWith ( "/" ) )
136+ newEntry = entry + name
137+ else
138+ newEntry = entry + '/' + name
139+
140+ await runFiles ( directory , newEntry , exclude , pathName )
141+ }
142+ }
143+ if ( errorLog . length )
144+ console . log ( ...errorLog )
145+
146+ }
147+
148+
149+ function getSource ( path , mimeType ) {
150+ let readType = 'utf8'
151+ if ( / ^ ( i m a g e | a u d i o | v i d e o ) \/ [ - + . \w ] + / . test ( mimeType ) )
152+ readType = 'base64'
153+
154+ let binary = fs . readFileSync ( path ) ;
155+ let content = new Buffer . from ( binary ) . toString ( readType ) ;
156+
157+ return content
158+ }
38159
39160/**
40- * Store html files by config sources
161+ * Store files by config sources
41162 **/
42- if ( sources ) {
43- let new_sources_list = [ ] ;
163+ async function runSources ( ) {
164+ let updatedSources = [ ] ;
44165
45- async function runSources ( ) {
46- for ( let i = 0 ; i < sources . length ; i ++ ) {
47- const { entry, collection, document } = sources [ i ] ;
48-
49- let new_source = { ...sources [ i ] } ;
50- let Key
51- let response = { } ;
52- if ( entry ) {
53- try {
54- let read_type = 'utf8'
55- let mime_type = mime . lookup ( entry ) || 'text/html' ;
56- if ( / ^ ( i m a g e | a u d i o | v i d e o ) \/ [ - + . \w ] + / . test ( mime_type ) ) {
57- read_type = 'base64'
58- }
59-
60- let binary = fs . readFileSync ( entry ) ;
61-
62- let content = new Buffer . from ( binary ) . toString ( read_type ) ;
63-
64- if ( content && collection ) {
65- if ( ! document )
66- document = { } ;
67- else
68- for ( const key of Object . keys ( document ) ) {
69- if ( document [ key ] == '{{source}}' ) {
70- document [ key ] = content
71- Key = key
72- break ;
166+ for ( let i = 0 ; i < sources . length ; i ++ ) {
167+ const { collection, document } = sources [ i ] ;
168+
169+ let source = { ...sources [ i ] } ;
170+ let keys = new Map ( )
171+ let response = { } ;
172+
173+ try {
174+ if ( collection ) {
175+ if ( ! document )
176+ document = { } ;
177+ else
178+ for ( const key of Object . keys ( document ) ) {
179+ if ( typeof document [ key ] != 'string' )
180+ continue
181+
182+ let variables = document [ key ] . match ( / { { ( [ A - Z a - z 0 - 9 _ . , \[ \] \- \/ ] * ) } } / g) ;
183+ if ( variables ) {
184+ keys . set ( key , `${ document [ key ] } ` )
185+ let value = ""
186+ for ( let variable of variables ) {
187+ let entry = / { { \s * ( [ \w \W ] + ) \s * } } / g. exec ( variable ) ;
188+ entry = entry [ 1 ] . trim ( )
189+ if ( entry ) {
190+ let read_type = 'utf8'
191+ let mime_type = mime . lookup ( entry ) || 'text/html' ;
192+ if ( / ^ ( i m a g e | a u d i o | v i d e o ) \/ [ - + . \w ] + / . test ( mime_type ) ) {
193+ read_type = 'base64'
194+ }
195+
196+ let binary = fs . readFileSync ( entry ) ;
197+ let content = new Buffer . from ( binary ) . toString ( read_type ) ;
198+ if ( content )
199+ value += content
200+ // document[key] = document[key].replace(variable, content);
73201 }
74202 }
203+ document [ key ] = value
204+ }
205+
206+ }
75207
76- response = await runStore ( { collection, document} ) ;
208+ let data = { collection, document}
209+ if ( ! document . _id && document . path )
210+ data . filter = {
211+ query : [ { name : 'path' , value : document . path , operator : '$eq' } ]
77212 }
78- } catch ( err ) {
79- console . log ( err )
80- process . exit ( )
81- }
82- if ( response . document && response . document [ 0 ] && response . document [ 0 ] . _id ) {
83- new_source . document [ Key ] = '{{source}}'
84- new_source . document . _id = response . document [ 0 ] . _id
85- } else {
86- console . log ( '_id could not be found' )
87- process . exit ( )
88- }
213+
214+ response = await runStore ( data ) ;
215+ }
216+ } catch ( err ) {
217+ console . log ( err )
218+ process . exit ( )
219+ }
220+ if ( response . document && response . document [ 0 ] && response . document [ 0 ] . _id ) {
221+ for ( const [ key , value ] of keys ) {
222+ source . document [ key ] = value
89223 }
90- new_sources_list . push ( new_source )
224+ source . document . _id = response . document [ 0 ] . _id
225+ } else {
226+ console . log ( '_id could not be found' )
227+ process . exit ( )
91228 }
92229
93- return new_sources_list
230+ updatedSources . push ( source )
94231 }
95-
96- runSources ( ) . then ( ( ) => {
97- let new_config = {
98- config,
99- sources : new_sources_list ,
100- crud : crud ,
101- }
102- delete new_config . config . broadcast
103- let write_str = JSON . stringify ( new_config , null , 4 )
104- write_str = "module.exports = " + write_str ;
105232
106- fs . writeFileSync ( configFile , write_str ) ;
107- setTimeout ( function ( ) {
108- process . exit ( )
109- } , 2000 )
110- } )
233+ return updatedSources
111234}
235+
112236
113237async function runStore ( data ) {
114238 try {
115239 let response ;
116- if ( ! data . document . _id ) {
240+ if ( ! data . document . _id && ! data . filter ) {
117241 response = await CoCreateCrud . createDocument ( {
118242 ...config ,
119243 ...data
120244 } )
121245 } else {
122- response = await CoCreateCrud . updateDocument ( {
246+ response = await CoCreateCrud . updateDocument ( {
123247 ...config ,
124248 ...data ,
125249 upsert : true
@@ -132,4 +256,29 @@ async function runStore(data) {
132256 console . log ( err ) ;
133257 return null ;
134258 }
135- }
259+ }
260+
261+ async function run ( ) {
262+ if ( directories )
263+ await runDirectories ( )
264+
265+ if ( sources ) {
266+ let sources = await runSources ( )
267+ let new_config = {
268+ config,
269+ directories,
270+ sources,
271+ }
272+ delete new_config . config . broadcast
273+ let write_str = JSON . stringify ( new_config , null , 4 )
274+ write_str = "module.exports = " + write_str ;
275+
276+ fs . writeFileSync ( configFile , write_str ) ;
277+ }
278+
279+ setTimeout ( function ( ) {
280+ process . exit ( )
281+ } , 2000 )
282+ }
283+
284+ run ( )
0 commit comments