Function to return levels of an object in Javascript

Asked

Viewed 155 times

1

I am 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 change in the structure I just change this file and not the whole system.

To do that, I thought I’d ride a .config that would return an object with all the directories in 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’s not working yet 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:""          
  }                   
}                     

So from this variable I would return an object with all the "mappings" available 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’m not being able to think about how to do this function so that it enters at all levels and assembles this structure. Does anyone have any matter of recursive functions or any solution?

  • 1

    what you want to do is something to read the file-Structure and pass it to an object ? If yes, and you are using Node, you can do this with the fs.readdir where each folder is pushed to the object and the value of that key is the full-path of it. The trick here is to recur in case you have N folders inside folders, but it’s not Rocket Science :)

  • is something similar to this but I don’t use Node. my app is about Cordova and to maintain a design pattern I need this kind of control

  • 1

    All right, I just googled "Cordova readdir," but I got a really cool link that might help you with your quest - even just a little

  • A key value wouldn’t be enough?

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( %pre%{
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

Your end result will be something like this:

[
  {
   "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
    },
[...]

Source: adapted version of https://stackoverflow.com/questions/15990113/cmd-tree-to-json
(The original version lists only subdirectories. The adapted version also includes files.)

  • I know there’s no way to do the listing of directories with javascript so I’ll already have a json with the directories defined as shown in my Return, what I need is that using this json I generate something like ["app/", "app/controllers/", "app/controllers/Cad"]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.