Last active
October 7, 2018 19:32
-
-
Save yokada/7ce71e00baa94a7d684743cb56c634a4 to your computer and use it in GitHub Desktop.
Revisions
-
yokada revised this gist
Oct 7, 2018 . 1 changed file with 6 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 */ const input = [ { "a": [ "a.txt", "b.txt", @@ -53,7 +52,7 @@ const input = { } ] }, { "g": [ "h.txt", { @@ -63,7 +62,7 @@ const input = { } ] } ] const createFileLayoutFromJson = (j, root) => { let ret = [] @@ -83,7 +82,7 @@ const createFileLayoutFromJson = (j, root) => { } }) } recur(j, root) } createFileLayoutFromJson(input, 'output') -
yokada created this gist
Oct 7, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 */