Last active
October 7, 2018 19:32
-
-
Save yokada/7ce71e00baa94a7d684743cb56c634a4 to your computer and use it in GitHub Desktop.
Create file layout from json which is written in yaml.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
*/ | |
const input = [ | |
{ | |
"a": [ | |
"a.txt", | |
"b.txt", | |
"c.txt", | |
{ | |
"d": [ | |
"e.txt", | |
"f.txt", | |
{ | |
"asdf": [ | |
{ | |
"asoidfu": [ | |
{ | |
"wiej": [ | |
"owiejfowef.com" | |
] | |
} | |
] | |
} | |
] | |
} | |
] | |
} | |
] | |
}, | |
{ | |
"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(j, 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 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment