Skip to content

Instantly share code, notes, and snippets.

@yokada
Last active October 7, 2018 19:32
Show Gist options
  • Select an option

  • Save yokada/7ce71e00baa94a7d684743cb56c634a4 to your computer and use it in GitHub Desktop.

Select an option

Save yokada/7ce71e00baa94a7d684743cb56c634a4 to your computer and use it in GitHub Desktop.

Revisions

  1. yokada revised this gist Oct 7, 2018. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions createFileLayoutFromJson.js
    Original file line number Diff line number Diff line change
    @@ -22,12 +22,11 @@ import fs from 'fs'
    - i:
    - j.json
    ## 2. Convert source yaml to json using config.js
    [ config - npm ]( https://www.npmjs.com/package/config )
    ## 2. Convert source yaml to json
    */

    const input = {
    "0": {
    const input = [
    {
    "a": [
    "a.txt",
    "b.txt",
    @@ -53,7 +52,7 @@ const input = {
    }
    ]
    },
    "1": {
    {
    "g": [
    "h.txt",
    {
    @@ -63,7 +62,7 @@ const input = {
    }
    ]
    }
    }
    ]

    const createFileLayoutFromJson = (j, root) => {
    let ret = []
    @@ -83,7 +82,7 @@ const createFileLayoutFromJson = (j, root) => {
    }
    })
    }
    recur(Object.keys(j).map(k=>j[k]), root)
    recur(j, root)
    }
    createFileLayoutFromJson(input, 'output')

  2. yokada created this gist Oct 7, 2018.
    110 changes: 110 additions & 0 deletions createFileLayoutFromJson.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,110 @@
    import path from 'path'
    import fs from 'fs'

    /**
    # Creating Input data
    ## 1. write source yaml
    - a:
    - a.txt
    - b.txt
    - c.txt
    - d:
    - e.txt
    - f.txt
    - asdf:
    - asoidfu:
    - wiej:
    - owiejfowef.com
    - g:
    - h.txt
    - i:
    - j.json
    ## 2. Convert source yaml to json using config.js
    [ config - npm ]( https://www.npmjs.com/package/config )
    */

    const input = {
    "0": {
    "a": [
    "a.txt",
    "b.txt",
    "c.txt",
    {
    "d": [
    "e.txt",
    "f.txt",
    {
    "asdf": [
    {
    "asoidfu": [
    {
    "wiej": [
    "owiejfowef.com"
    ]
    }
    ]
    }
    ]
    }
    ]
    }
    ]
    },
    "1": {
    "g": [
    "h.txt",
    {
    "i": [
    "j.json"
    ]
    }
    ]
    }
    }

    const createFileLayoutFromJson = (j, root) => {
    let ret = []
    const recur = (a, i) => {
    if (i.length > 0 && !fs.existsSync(i)) {
    fs.mkdirSync(i)
    }
    a.forEach(o => {
    if (typeof o == 'object') {
    for (const k in o) {
    recur(o[k], path.join(i, k))
    }
    } else if (o == 'array') {
    recur(o, i)
    } else {
    fs.writeFileSync(path.join(i, o), '')
    }
    })
    }
    recur(Object.keys(j).map(k=>j[k]), root)
    }
    createFileLayoutFromJson(input, 'output')

    /** the result
    output
    ├── a
    │   ├── a.txt
    │   ├── b.txt
    │   ├── c.txt
    │   └── d
    │   ├── asdf
    │   │   └── asoidfu
    │   │   └── wiej
    │   │   └── owiejfowef.com
    │   ├── e.txt
    │   └── f.txt
    └── g
    ├── h.txt
    └── i
    └── j.json
    7 directories, 8 files
    */