1- const crud = require ( '@cocreate/crud-client' )
2- const mime = require ( 'mime-types' )
3- const fs = require ( 'fs' ) ;
4- const path = require ( 'path' ) ;
5-
6- // TODO: throwing error
7- process . env [ 'NODE_TLS_REJECT_UNAUTHORIZED' ] = 0
8-
9- let CoCreateConfig ;
10- let configFile = path . resolve ( process . cwd ( ) , 'CoCreate.config.js' ) ;
11- if ( fs . existsSync ( configFile ) ) {
12- CoCreateConfig = require ( configFile ) ;
13- } else {
14- console . log ( 'config not found.' )
15- process . exit ( )
16- }
17-
18- const { config, directories, sources } = CoCreateConfig ;
19-
20- if ( ! config )
21- config = process . env . config || { }
22- if ( ! config . organization_id )
23- config . organization_id = process . env . organization_id
24- if ( ! config . key )
25- config . key = process . env . key
26- if ( ! config . host )
27- config . host = process . env . host
28-
29- crud . socket . create ( config )
30- config . broadcast = false
31-
32- /**
33- * Store files by config directories
34- **/
35- let errorLog = [ ] ;
36-
37- async function runDirectories ( ) {
38- for ( const directory of directories ) {
39- const entry = directory . entry
40- const exclude = directory . exclude
41- await runFiles ( directory , entry , exclude )
42- }
43- return
44- }
45-
46- async function runFiles ( directory , entry , exclude , parentDirectory = '' ) {
47- let files = fs . readdirSync ( entry ) ;
48-
49- for ( let file of files ) {
50- if ( exclude && exclude . includes ( file ) ) continue
51-
52- let isDirectory = fs . existsSync ( `${ entry } /${ file } ` ) && fs . lstatSync ( `${ entry } /${ file } ` ) . isDirectory ( ) ;
53- let name = file
54- let source = ''
55- let directoryName = parentDirectory || '' ;
56- let parentDirectoryOnly = parentDirectory || '' ;
57- let index = parentDirectoryOnly . lastIndexOf ( '/' ) + 1
58- if ( parentDirectoryOnly && index ) {
59- parentDirectoryOnly = parentDirectoryOnly . substring ( index )
60- }
61- let mimeType = mime . lookup ( `${ file } ` )
62- let pathName = '' ;
63-
64- if ( ! directoryName && directory . document && directory . document . directory )
65- directoryName = directory . document . directory . replace ( '{{directory}}' , '' ) . trim ( )
66- else if ( ! directoryName )
67- directoryName = '/'
68-
69- if ( exclude && exclude . includes ( directoryName ) ) continue
70-
71- if ( directoryName . endsWith ( "/" ) )
72- pathName = directoryName + name
73- else if ( directoryName )
74- pathName = directoryName + '/' + name
75- else
76- pathName = '/' + name
77-
78- if ( exclude && exclude . includes ( pathName ) ) continue
79-
80- if ( isDirectory )
81- mimeType = "text/directory"
82- else
83- source = getSource ( `${ entry } /${ file } ` , mimeType )
84-
85- let values = {
86- '{{name}}' : name ,
87- '{{source}}' : source ,
88- '{{directory}}' : directoryName ,
89- '{{parentDirectory}}' : parentDirectoryOnly ,
90- '{{path}}' : pathName ,
91- '{{content-type}}' : mimeType
92- }
93-
94- let document = { ...directory . document }
95- if ( ! document . name )
96- document . name = "{{name}}"
97- if ( ! document . src )
98- document . src = "{{source}}"
99- if ( ! document . directory )
100- document . directory = "/{{directory}}"
101- if ( ! document . parentDirectory )
102- document . parentDirectory = "{{parentDirectory}}"
103- if ( ! document . path )
104- document . path = "{{path}}"
105- if ( ! document [ "content-type" ] )
106- document [ "content-type" ] = '{{content-type}}'
107- if ( ! document . public && document . public != false && document . public != 'false' )
108- document . public = 'true'
109-
110- let object = {
111- collection : directory . collection || 'files' ,
112- document
113- }
114- for ( const key of Object . keys ( directory . document ) ) {
115- if ( typeof directory . document [ key ] == 'string' ) {
116-
117- let variables = directory . document [ key ] . match ( / { { ( [ A - Z a - z 0 - 9 _ . , \[ \] \- \/ ] * ) } } / g) ;
118- if ( variables ) {
119- for ( let variable of variables ) {
120- if ( variable == '{{directory}}' ) {
121- if ( parentDirectory )
122- object . document [ key ] = values [ variable ]
123- else
124- object . document [ key ] = object . document [ key ] . replace ( variable , '' ) ;
125- }
126- else if ( isDirectory && variable == '{{source}}' )
127- delete object . document [ key ]
128- else
129- object . document [ key ] = object . document [ key ] . replace ( variable , values [ variable ] ) ;
130- }
131- }
132-
133- }
134- }
135-
136- if ( ! object . document . _id )
137- object . filter = {
138- query : [ { name : 'path' , value : pathName , operator : '$eq' } ]
139- }
140-
141- response = await runStore ( object ) ;
142- if ( response . error )
143- errorLog . push ( response . error )
144-
145- if ( isDirectory && pathName ) {
146- let newEntry
147- if ( entry . endsWith ( "/" ) )
148- newEntry = entry + name
149- else
150- newEntry = entry + '/' + name
151-
152- await runFiles ( directory , newEntry , exclude , pathName )
153- }
154- }
155- if ( errorLog . length )
156- console . log ( ...errorLog )
157-
158- }
159-
160-
161- function getSource ( path , mimeType ) {
162- let readType = 'utf8'
163- if ( / ^ ( i m a g e | a u d i o | v i d e o ) \/ [ - + . \w ] + / . test ( mimeType ) )
164- readType = 'base64'
165-
166- let binary = fs . readFileSync ( path ) ;
167- let content = new Buffer . from ( binary ) . toString ( readType ) ;
168-
169- return content
170- }
171-
172- /**
173- * Store files by config sources
174- **/
175- async function runSources ( ) {
176- let updatedSources = [ ] ;
177-
178- for ( let i = 0 ; i < sources . length ; i ++ ) {
179- const { collection, document } = sources [ i ] ;
180-
181- let source = { ...sources [ i ] } ;
182- let keys = new Map ( )
183- let response = { } ;
184-
185- try {
186- if ( collection ) {
187- if ( ! document )
188- document = { } ;
189- else
190- for ( const key of Object . keys ( document ) ) {
191- if ( typeof document [ key ] != 'string' )
192- continue
193-
194- let variables = document [ key ] . match ( / { { ( [ A - Z a - z 0 - 9 _ . , \[ \] \- \/ ] * ) } } / g) ;
195- if ( variables ) {
196- keys . set ( key , `${ document [ key ] } ` )
197- let value = ""
198- for ( let variable of variables ) {
199- let entry = / { { \s * ( [ \w \W ] + ) \s * } } / g. exec ( variable ) ;
200- entry = entry [ 1 ] . trim ( )
201- if ( entry ) {
202- if ( ! fs . existsSync ( entry ) )
203- continue
204-
205- let read_type = 'utf8'
206- let mime_type = mime . lookup ( entry ) || 'text/html' ;
207- if ( / ^ ( i m a g e | a u d i o | v i d e o ) \/ [ - + . \w ] + / . test ( mime_type ) ) {
208- read_type = 'base64'
209- }
210-
211- let binary = fs . readFileSync ( entry ) ;
212- let content = new Buffer . from ( binary ) . toString ( read_type ) ;
213- if ( content )
214- value += content
215- // document[key] = document[key].replace(variable, content);
216- }
217- }
218- document [ key ] = value
219- }
220-
221- }
222-
223- let data = { collection, document }
224- if ( ! document . _id && document . path )
225- data . filter = {
226- query : [ { name : 'path' , value : document . path , operator : '$eq' } ]
227- }
228-
229- response = await runStore ( data ) ;
230- }
231- } catch ( err ) {
232- console . log ( err )
233- process . exit ( )
234- }
235- if ( response . document && response . document [ 0 ] && response . document [ 0 ] . _id ) {
236- for ( const [ key , value ] of keys ) {
237- source . document [ key ] = value
238- }
239- source . document . _id = response . document [ 0 ] . _id
240- } else {
241- console . log ( '_id could not be found' )
242- process . exit ( )
243- }
244-
245- updatedSources . push ( source )
246- }
247-
248- return updatedSources
249- }
250-
251-
252- async function runStore ( data ) {
253- try {
254- let response ;
255- if ( ! data . document . _id && ! data . filter ) {
256- response = await crud . createDocument ( {
257- ...config ,
258- ...data
259- } )
260- } else {
261- response = await crud . updateDocument ( {
262- ...config ,
263- ...data ,
264- upsert : true
265- } )
266- }
267- if ( response ) {
268- return response ;
269- }
270- } catch ( err ) {
271- console . log ( err ) ;
272- return null ;
273- }
274- }
275-
276- async function run ( ) {
277- if ( directories )
278- await runDirectories ( )
279-
280- if ( sources ) {
281- let sources = await runSources ( )
282- let newConfig = { ...CoCreateConfig }
283- if ( directories )
284- newConfig . directories = directories
285-
286- newConfig . sources = sources
287-
288- delete newConfig . config . url
289- delete newConfig . config . broadcast
290- let write_str = JSON . stringify ( newConfig , null , 4 )
291- write_str = "module.exports = " + write_str ;
292-
293- fs . writeFileSync ( configFile , write_str ) ;
294- }
295-
296- setTimeout ( function ( ) {
297- process . exit ( )
298- } , 2000 )
299- }
300-
301- run ( )
1+ const file = require ( '@cocreate/file' )
2+ module . exports = file ( )
0 commit comments