forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-directory.js
More file actions
88 lines (82 loc) · 2.54 KB
/
data-directory.js
File metadata and controls
88 lines (82 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import fs from 'fs'
import os from 'os'
import path from 'path'
import yaml from 'js-yaml'
// This helper class exists so you can create a temporary root directory
// full of data files.
// For example, if you want to unit test with files that are not part
// of the git repo but should only "temporarily" exist for the duration
// of the tests.
// This class takes an object and generates that as files on disk. E.g.
//
// const dataDirectory = new DataDirectory({
// data: {
// reusables: {
// example: 'a rose by any other name\nwould smell as sweet',
// },
// },
// })
// process.env.ROOT = dataDirectory.root
// ...
// try {
// ...unit tests here...
// } finally {
// dataDirectory.destroy()
// }
//
// Note that it's very specific about keys. For example, if the nested
// object has a key called 'ui' it doesn't create a depper nested structure
// but takes the nested structure and writes it to a single .yml file.
// For example:
//
// const dataDirectory = new DataDirectory({
// data: {
// ui: {
// key: "Value",
// deep: {
// er: "Stuff"
//
// will create a single <tempdir>/data/ui.yml file.
//
export class DataDirectory {
constructor(data, root) {
this.root = root || this.createTempRoot('data-directory')
this.create(data)
}
createTempRoot(prefix) {
const fullPath = path.join(os.tmpdir(), prefix)
return fs.mkdtempSync(fullPath)
}
create(data, root = null, isVariables = false, isReusables = false) {
const here = root || this.root
for (const [key, value] of Object.entries(data)) {
if (isReusables) {
if (typeof value === 'string') {
fs.writeFileSync(path.join(here, `${key}.md`), value, 'utf-8')
} else {
fs.mkdirSync(path.join(here, key))
this.create(value, path.join(here, key), false, true)
}
} else if (isVariables) {
fs.writeFileSync(path.join(here, `${key}.yml`), yaml.dump(value), 'utf-8')
} else {
if (key === 'ui') {
fs.writeFileSync(path.join(here, `${key}.yml`), yaml.dump(value), 'utf-8')
} else {
const there = path.join(here, key)
fs.mkdirSync(there)
if (key === 'reusables') {
this.create(value, there, false, true)
} else if (key === 'variables') {
this.create(value, there, true, false)
} else {
this.create(value, there)
}
}
}
}
}
destroy() {
fs.rmSync(this.root, { recursive: true })
}
}