@@ -77,9 +77,15 @@ const mimeTypes = {
7777 ".7z" : "application/x-7z-compressed"
7878}
7979
80- module . exports = async function file ( CoCreateConfig , configPath ) {
80+ module . exports = async function file ( CoCreateConfig , configPath , match ) {
8181 let directories = CoCreateConfig . directories
8282 let sources = CoCreateConfig . sources
83+ let configDirectoryPath = path . dirname ( configPath )
84+
85+ if ( match && ! Array . isArray ( match ) )
86+ match = [ match ]
87+ else if ( ! match )
88+ match = [ ]
8389
8490 let config = await Config ( {
8591 organization_id : {
@@ -143,7 +149,7 @@ module.exports = async function file(CoCreateConfig, configPath) {
143149
144150 }
145151
146- console . log ( 'Uploading files...' )
152+ // console.log('Uploading files...')
147153
148154 /**
149155 * Store files by config directories
@@ -153,14 +159,15 @@ module.exports = async function file(CoCreateConfig, configPath) {
153159 async function runDirectories ( ) {
154160 for ( const directory of directories ) {
155161 const entry = directory . entry
156- const exclude = directory . exclude
162+ const exclude = directory . exclude || [ ]
157163 await runFiles ( directory , entry , exclude )
158164 }
159165 return
160166 }
161167
162168 async function runFiles ( directory , entry , exclude , parentDirectory = '' ) {
163- let files = fs . readdirSync ( entry ) ;
169+ const entryPath = path . resolve ( configDirectoryPath , entry )
170+ let files = fs . readdirSync ( entryPath ) ;
164171
165172 for ( let file of files ) {
166173 let skip = false
@@ -172,8 +179,19 @@ module.exports = async function file(CoCreateConfig, configPath) {
172179 }
173180 if ( skip ) continue
174181
182+ for ( let i = 0 ; i < match . length ; i ++ ) {
183+ skip = true
184+ const filePath = path . resolve ( entryPath , file ) ;
185+ if ( filePath . startsWith ( match [ i ] ) ) {
186+ skip = false
187+ console . log ( 'Uploaded: ' , filePath )
188+ break ;
189+ }
190+ }
175191
176- let isDirectory = fs . existsSync ( `${ entry } /${ file } ` ) && fs . lstatSync ( `${ entry } /${ file } ` ) . isDirectory ( ) ;
192+ if ( skip ) continue
193+
194+ let isDirectory = fs . existsSync ( `${ entryPath } /${ file } ` ) && fs . lstatSync ( `${ entryPath } /${ file } ` ) . isDirectory ( ) ;
177195 let name = file
178196 let source = ''
179197 let directoryName = parentDirectory || '' ;
@@ -203,7 +221,7 @@ module.exports = async function file(CoCreateConfig, configPath) {
203221 if ( isDirectory )
204222 mimeType = "text/directory"
205223 else
206- source = getSource ( `${ entry } /${ file } ` , mimeType )
224+ source = getSource ( `${ entryPath } /${ file } ` , mimeType )
207225
208226 let values = {
209227 '{{name}}' : name ,
@@ -304,6 +322,7 @@ module.exports = async function file(CoCreateConfig, configPath) {
304322 let source = { ...sources [ i ] } ;
305323 let keys = new Map ( )
306324 let response = { } ;
325+ let isMatch = false
307326
308327 try {
309328 if ( array ) {
@@ -316,7 +335,8 @@ module.exports = async function file(CoCreateConfig, configPath) {
316335
317336 let variables = object [ key ] . match ( / { { ( [ A - Z a - z 0 - 9 _ . , \[ \] \- \/ ] * ) } } / g) ;
318337 if ( variables ) {
319- keys . set ( key , `${ object [ key ] } ` )
338+ let originalValue = object [ key ]
339+ keys . set ( key , originalValue )
320340 let value = ""
321341 for ( let variable of variables ) {
322342 let entry = / { { \s * ( [ \w \W ] + ) \s * } } / g. exec ( variable ) ;
@@ -325,6 +345,17 @@ module.exports = async function file(CoCreateConfig, configPath) {
325345 if ( ! fs . existsSync ( entry ) )
326346 continue
327347
348+ if ( ! isMatch ) {
349+ const filePath = path . resolve ( configDirectoryPath , entry ) ;
350+ for ( let i = 0 ; i < match . length ; i ++ ) {
351+ if ( filePath . startsWith ( match [ i ] ) ) {
352+ console . log ( 'Source saved' , sources [ i ] )
353+ isMatch = true
354+ break ;
355+ }
356+ }
357+ }
358+
328359 let read_type = 'utf8'
329360 const fileExtension = path . extname ( entry ) ;
330361 let mime_type = mimeTypes [ fileExtension ] || 'text/html'
@@ -351,20 +382,20 @@ module.exports = async function file(CoCreateConfig, configPath) {
351382 query : [ { key : 'path' , value : object . path , operator : '$eq' } ]
352383 }
353384
354- response = await runStore ( data ) ;
385+ if ( match . length && isMatch )
386+ response = await runStore ( data ) ;
355387 }
356388 } catch ( err ) {
357389 console . log ( err )
358390 process . exit ( )
359391 }
392+
360393 if ( response . object && response . object [ 0 ] && response . object [ 0 ] . _id ) {
361- for ( const [ key , value ] of keys ) {
362- source . object [ key ] = value
363- }
364394 source . object . _id = response . object [ 0 ] . _id
365- } else {
366- console . log ( '_id could not be found' )
367- process . exit ( )
395+ }
396+
397+ for ( const [ key , value ] of keys ) {
398+ source . object [ key ] = value
368399 }
369400
370401 updatedSources . push ( source )
@@ -404,10 +435,10 @@ module.exports = async function file(CoCreateConfig, configPath) {
404435 if ( directories )
405436 await runDirectories ( )
406437
407- if ( sources ) {
438+ if ( sources && sources . length ) {
408439 let sources = await runSources ( )
409440 let newConfig = { ...CoCreateConfig }
410- if ( directories )
441+ if ( directories && directories . length )
411442 newConfig . directories = directories
412443
413444 newConfig . sources = sources
@@ -427,11 +458,13 @@ module.exports = async function file(CoCreateConfig, configPath) {
427458 fs . writeFileSync ( configPath , `module.exports = ${ JSON . stringify ( newConfig , null , 4 ) } ;` ) ;
428459 }
429460
430- console . log ( 'upload complete!' ) ;
461+ if ( ! match . length ) {
462+ console . log ( 'upload complete!' ) ;
431463
432- setTimeout ( function ( ) {
433- process . exit ( )
434- } , 2000 )
464+ setTimeout ( function ( ) {
465+ process . exit ( )
466+ } , 2000 )
467+ }
435468 }
436469
437470 run ( )
0 commit comments