Function to return the levels of an object in Javascript

1

I'm creating an app with AngularJS where I need to do a "mapping" of the system folder structure, so when it is necessary to make some changes in the structure I only change this file and not the entire system.

To do this, I thought about putting together a .config that would return an object with all the directories of my app. The directory structure is something like this:

app/
|- controllers/
   |- cad/
|- directives/
|- views/

The problem is that if I write directory by directory it still does not get functional so I thought I could create an object with the multi-level structure and then return another object already with the mounted directories, something like:

var path = {          
  app: {              
    controllers:{     
      cad: ""         
    },                
    directives: "",   
    views:""          
  }                   
}                     

Then from this variable I would return an object with all the available "mappings" based on the object, like this:

return {
  app:{
    _: "app/"
    controllers: {
      _: "app/controllers/",
      cad: "app/controllers/cad/"
    },
    directives: {
      _:"app/directives/"
    },
    views: {
      _:"app/views/"
    }
  }
}

The problem is that I can not think of how to do this function so that it enters at all levels and builds this structure. Does anyone have any recursive function matter or solution?

    
asked by anonymous 04.04.2016 / 16:49

1 answer

0
Assuming you are using Windows, you can use a batch file to generate a JSON representing your directory structure:

dir2json.bat [diretório] >[arquivo destino]

Example:

dir2json.bat . >lista.json
  

dir2json.bat

@echo off &setlocal

setlocal EnableDelayedExpansion

if "%~1"=="" (set "root=.") else set "root=%~1"
set "pre0=                                    "

pushd %root%
echo([
call:dirtree "%CD%" "1" "1"
popd
echo(]
goto:eof

:dirtree
setlocal
call set "pre=%%pre0:~-%~2%%
set /a ccount=%~3
set /a tcount=%~2+2
set /a dcount=0
set /a fcount=0
for /d %%i in (*) do set /a dcount+=1
echo( 
[
  {
   "type": "folder",
   "name": "libs",
   "path": "C:/teste/libs",
   "childno": 1,
   "files": [
       {"type":"file","name": "dir.txt"},
       {"type":"file","name": "tree2json.bat"}
   ],
   "subchilds": 5,
   "children": [
    {
     "type": "folder",
     "name": "bf",
     "path": "C:/teste/libs/bf/bf",
     "childno": 5,
     "files": [
         {"name": "authentication.js"},
         {"name": "item.js"},
         {"name": "menu.js"},
         {"name": "person.js"}
     ],
     "subchilds": 0
    },
[...]
{ echo(
dir2json.bat [diretório] >[arquivo destino]
"type": "folder", echo(
dir2json.bat . >lista.json
"name": "%~nx1", set "fpath=%~f1" set "fpath=%fpath:\=/%" echo(
@echo off &setlocal

setlocal EnableDelayedExpansion

if "%~1"=="" (set "root=.") else set "root=%~1"
set "pre0=                                    "

pushd %root%
echo([
call:dirtree "%CD%" "1" "1"
popd
echo(]
goto:eof

:dirtree
setlocal
call set "pre=%%pre0:~-%~2%%
set /a ccount=%~3
set /a tcount=%~2+2
set /a dcount=0
set /a fcount=0
for /d %%i in (*) do set /a dcount+=1
echo( 
[
  {
   "type": "folder",
   "name": "libs",
   "path": "C:/teste/libs",
   "childno": 1,
   "files": [
       {"type":"file","name": "dir.txt"},
       {"type":"file","name": "tree2json.bat"}
   ],
   "subchilds": 5,
   "children": [
    {
     "type": "folder",
     "name": "bf",
     "path": "C:/teste/libs/bf/bf",
     "childno": 5,
     "files": [
         {"name": "authentication.js"},
         {"name": "item.js"},
         {"name": "menu.js"},
         {"name": "person.js"}
     ],
     "subchilds": 0
    },
[...]
{ echo( %pre%"type": "folder", echo( %pre%"name": "%~nx1", set "fpath=%~f1" set "fpath=%fpath:\=/%" echo( %pre%"path": "%fpath%", echo( %pre%"childno": %ccount%, for %%i in (*) do set /a fcount+=1 if %fcount% gtr 0 ( echo( %pre%"files": [ for %%i in (*) do ( echo( %pre%{"name": "%%i"} set /A fcount-=1 if !fcount! gtr 0 (echo %pre%,) ) echo( %pre%], ) ) if %dcount% equ 0 echo( %pre%"subchilds": %dcount% if %dcount% gtr 0 ( echo( %pre%"subchilds": %dcount%, echo( %pre%"children": [ for /d %%i in (*) do ( for /f %%j in ('call echo "%%dcount%%"') do ( cd "%%i" call:dirtree "%%i" "%tcount%" "%%j" cd .. ) set /a dcount-=1 ) echo( %pre%] ) if %ccount% equ 1 (echo %pre%}) else echo( %pre%}, endlocal goto:eof
"path": "%fpath%", echo( %pre%"childno": %ccount%, for %%i in (*) do set /a fcount+=1 if %fcount% gtr 0 ( echo( %pre%"files": [ for %%i in (*) do ( echo( %pre%{"name": "%%i"} set /A fcount-=1 if !fcount! gtr 0 (echo %pre%,) ) echo( %pre%], ) ) if %dcount% equ 0 echo( %pre%"subchilds": %dcount% if %dcount% gtr 0 ( echo( %pre%"subchilds": %dcount%, echo( %pre%"children": [ for /d %%i in (*) do ( for /f %%j in ('call echo "%%dcount%%"') do ( cd "%%i" call:dirtree "%%i" "%tcount%" "%%j" cd .. ) set /a dcount-=1 ) echo( %pre%] ) if %ccount% equ 1 (echo %pre%}) else echo( %pre%}, endlocal goto:eof

Your end result will look something like this:

%pre%

Source: Adapted version of link
(The original version lists only subdirectories. The adapted version also includes files.)

    
04.04.2016 / 18:02