1- let glob = require ( "glob" ) ;
2- let fs = require ( 'fs' ) ;
1+ const fs = require ( "fs" ) ;
32const path = require ( "path" ) ;
43
5- function globUpdater ( er , files ) {
6- if ( er )
7- console . log ( files , 'glob resolving issue' ) ;
8- else
9- files . forEach ( filename => update ( filename ) ) ;
10- }
4+ function findDirectories ( startPath , callback , fileName ) {
5+ // Resolve relative paths to absolute paths if needed
6+ const resolvedPath =
7+ startPath . startsWith ( "./" ) || startPath . startsWith ( "../" )
8+ ? path . resolve ( startPath )
9+ : startPath ;
1110
11+ const segments = resolvedPath . split ( "/" ) ; // Split path by '/'
12+ let currentPath = "/" ; // Start from root
1213
14+ for ( let i = 0 ; i < segments . length ; i ++ ) {
15+ const segment = segments [ i ] ;
16+ const isWildcard = segment === "*" ;
1317
18+ if ( isWildcard ) {
19+ // Get all directories at this level
20+ const directories = fs
21+ . readdirSync ( currentPath )
22+ . filter ( ( file ) =>
23+ fs . statSync ( path . join ( currentPath , file ) ) . isDirectory ( )
24+ ) ;
1425
15- function update ( MdPath ) {
16- let name = path . basename ( path . resolve ( path . dirname ( MdPath ) , './' ) ) . substring ( 9 ) ;
17- let object = '' ;
18- let replaceContent = fs . readFileSync ( MdPath ) . toString ( ) ;
26+ // Process each directory and continue along the path
27+ directories . forEach ( ( dir ) => {
28+ findDirectories (
29+ path . join ( currentPath , dir , ...segments . slice ( i + 1 ) ) ,
30+ callback ,
31+ fileName
32+ ) ;
33+ } ) ;
34+ return ; // Stop further processing in the loop for wildcard case
35+ } else {
36+ // Continue to the next part of the path
37+ currentPath = path . join ( currentPath , segment ) ;
1938
39+ // If a segment doesn’t exist or isn’t a directory, log an error and stop
40+ if (
41+ ! fs . existsSync ( currentPath ) ||
42+ ! fs . statSync ( currentPath ) . isDirectory ( )
43+ ) {
44+ console . log ( `Directory not found: ${ currentPath } ` ) ;
45+ return ;
46+ }
47+ }
48+ }
2049
21- let content_source = replaceContent . substring ( replaceContent . indexOf ( "sources" ) ) ;
22- let content1 = content_source . substring ( content_source . indexOf ( "object" ) ) ;
23- let content2 = content1 . substring ( content1 . indexOf ( ':' ) ) ;
24- object = content2 . substring ( 3 , content2 . indexOf ( ',' ) - 4 ) ;
50+ // If we reach the end of the path without wildcards, we have a valid directory
51+ callback ( currentPath , fileName ) ;
52+ }
2553
54+ function createOrUpdateFile ( directoryPath , fileName ) {
55+ let name = path
56+ . basename ( path . resolve ( path . dirname ( directoryPath ) , "./" ) )
57+ . substring ( 9 ) ;
58+ let object = "" ;
59+ let replaceContent = fs . readFileSync ( directoryPath ) . toString ( ) ;
2660
27- let fileContent = `module.exports = {
61+ // Parse content to extract `object`
62+ let content_source = replaceContent . substring (
63+ replaceContent . indexOf ( "sources" )
64+ ) ;
65+ let content1 = content_source . substring ( content_source . indexOf ( "object" ) ) ;
66+ let content2 = content1 . substring ( content1 . indexOf ( ":" ) ) ;
67+ object = content2 . substring ( 3 , content2 . indexOf ( "," ) - 4 ) ;
68+
69+ let fileContent = `module.exports = {
2870 "config": {
2971 "organization_id": "5ff747727005da1c272740ab",
3072 "key": "2061acef-0451-4545-f754-60cf8160",
@@ -37,7 +79,7 @@ function update(MdPath) {
3779 "object": {
3880 "_id": "${ object } ",
3981 "name": "index.html",
40- "path": "/docs/${ name } ",
82+ "path": "/docs/${ name } ",
4183 "pathname": "/docs/${ name } /index.html",
4284 "src": "{{./docs/index.html}}",
4385 "host": [
@@ -50,28 +92,42 @@ function update(MdPath) {
5092 }
5193 ]
5294}
53-
5495` ;
5596
56- if ( ! object . length )
57- console . log ( "object Undefined: " , MdPath ) ;
58- if ( object . length != 24 && object . length != 0 )
59- console . log ( "object not valid! please check your config: " , MdPath ) ;
60- else {
61- console . log ( MdPath , " -> object : " , object ) ;
62- if ( fs . existsSync ( MdPath ) )
63- fs . unlinkSync ( MdPath ) ;
64- fs . writeFileSync ( MdPath , fileContent ) ;
65-
66- }
67-
97+ if ( ! object . length ) {
98+ console . log ( "object Undefined: " , directoryPath ) ;
99+ } else if ( object . length !== 24 ) {
100+ console . log ( "object not valid! Please check your config: " , directoryPath ) ;
101+ } else {
102+ const filePath = path . join ( directoryPath , fileName ) ;
103+ if ( fs . existsSync ( filePath ) ) fs . unlinkSync ( filePath ) ;
104+ fs . writeFileSync ( filePath , fileContent ) ;
105+ }
106+ const filePath = path . join ( directoryPath , fileName ) ;
107+ // Create or update the file
108+ if ( fs . existsSync ( filePath ) ) fs . unlinkSync ( filePath ) ;
109+ fs . writeFileSync ( filePath , fileContent ) ;
68110}
69111
70- // glob("../../CoCreate-components/CoCreate-filter/CoCreate.config.js", globUpdater);
71- glob ( "../../CoCreate-components/*/CoCreate.config.js" , globUpdater ) ;
72- glob ( "../../CoCreate-apps/*/CoCreate.config.js" , globUpdater ) ;
73- glob ( "../../CoCreate-plugins/*/CoCreate.config.js" , globUpdater ) ;
74- // glob("../CoCreateCSS/CoCreate.config.js", globUpdater);
75- // glob("../CoCreateJS/CoCreate.config.js", globUpdater);
112+ // Define the directories with wildcards
113+ const directories = [
114+ "../../../../../CoCreate-components/*/" ,
115+ "../../../../../CoCreate-apps/*/" ,
116+ "../../../../../CoCreate-plugins/*/" ,
117+ "../../../../../CoCreateCSS/" ,
118+ "../../../../../CoCreateJS/" ,
119+ "../../../../../CoCreateWS/" ,
120+ "../../../../../YellowOracle/" ,
121+ "../../../../../CoCreate-website/" ,
122+ "../../../../../CoCreate-admin/" ,
123+ "../../../../../CoCreate-website-old/" ,
124+ "../../../../../CoCreate-superadmin/" ,
125+ ] ;
126+ const fileName = "CoCreate.config.js" ;
127+
128+ // Execute directory search and create/update file if the directory exists
129+ directories . forEach ( ( directory ) => {
130+ findDirectories ( directory , createOrUpdateFile , fileName ) ;
131+ } ) ;
76132
77- console . log ( 'finished' ) ;
133+ console . log ( "Finished" ) ;
0 commit comments