-
Notifications
You must be signed in to change notification settings - Fork 2
/
yaml_adapter.ts
137 lines (112 loc) · 4.17 KB
/
yaml_adapter.ts
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import * as fs from 'fs';
import * as path from 'path';
import { DatabaseAdapter, JSObject, SyncDatabaseAdapter } from './database';
import { mkdirs, mkdirsSync } from './fs_help';
// js-yaml import. Throw an error if it is not installed
let yaml: typeof import("js-yaml");
try {
yaml = require('js-yaml');
} catch(e) {
let err = new Error("You should have installed js-yaml if you want to use the yaml-adapter");
err.stack += '\nCaused by: '+ err.message + err.stack;
throw e;
}
/**
* YamlFileAdapter is an async adapter for working with Yaml Files
*
* @author Nicolas Schmidt <[@nsc-de](https://github.com/nsc-de)>
*/
export class YamlFileAdapter implements DatabaseAdapter {
/**
* The constructor for [YamlFileAdapter](https://nsc-de.github.io/js-database/classes/_yaml_adapter_.yamlfileadapter.html)
*
* @author Nicolas Schmidt <[@nsc-de](https://github.com/nsc-de)>
* @param path the path to a yaml file to save to and load from
*
* @see [YamlFileAdapter](https://nsc-de.github.io/js-database/classes/_yaml_adapter_.yamlfileadapter.html) - 👩👦 the parent class
*/
constructor(
readonly path: string
) {}
/**
* Saves the given data to the file the adapter refers to
*
* @author Nicolas Schmidt <[@nsc-de](https://github.com/nsc-de)>
* @param data The data to save
* @returns ⌛ Promise (Ready when saved) >> ⛔ void
*
* @see [YamlFileAdapter](https://nsc-de.github.io/js-database/classes/_yaml_adapter_.yamlfileadapter.html) - 👩👦 the parent class
*/
save(data: JSObject): Promise<void> {
return new Promise((rs, rj) =>
mkdirs(path.dirname(this.path)).then(() =>
fs.writeFile(this.path, yaml.dump(data), err => {
if(err) rj(err);
else rs();
})));
}
/**
* Loads data from the file the adapter refers to
*
* @author Nicolas Schmidt <[@nsc-de](https://github.com/nsc-de)>
* @returns ⌛ Promise (Ready when saved) >> Object
*
* @see [YamlFileAdapter](https://nsc-de.github.io/js-database/classes/_yaml_adapter_.yamlfileadapter.html) - 👩👦 the parent class
*/
load(): Promise<JSObject> {
return new Promise((rs, rj) => {
fs.exists(this.path, exists => {
if(exists) {
fs.readFile(this.path, (err, data) => {
if(err) rj(err);
else rs(yaml.load(data.toString()));
});
}
else rs({});
})
});
}
}
/**
* [SyncYamlFileAdapter](https://nsc-de.github.io/js-database/classes/_yaml_adapter_.syncyamlfileadapter.html) is an sync adapter for working with Yaml Files
*
* @author Nicolas Schmidt <[@nsc-de](https://github.com/nsc-de)>
*/
export class SyncYamlFileAdapter implements SyncDatabaseAdapter {
/**
* The constructor for [SyncYamlFileAdapter](https://nsc-de.github.io/js-database/classes/_yaml_adapter_.syncyamlfileadapter.html)
*
* @author Nicolas Schmidt <[@nsc-de](https://github.com/nsc-de)>
* @param path the path to a yaml file to save to and load from
*
* @see [SyncYamlFileAdapter](https://nsc-de.github.io/js-database/classes/_yaml_adapter_.syncyamlfileadapter.html) - 👩👦 the parent class
*/
constructor(
readonly path: string
) {}
/**
* Saves the given data to the file the adapter refers to
*
* @author Nicolas Schmidt <[@nsc-de](https://github.com/nsc-de)>
* @param data The data to save
* @returns ⌛ Promise (Ready when saved) >> ⛔ void
*
* @see [SyncYamlFileAdapter](https://nsc-de.github.io/js-database/classes/_yaml_adapter_.syncyamlfileadapter.html) - 👩👦 the parent class
*/
save(data: JSObject): void {
mkdirsSync(path.dirname(this.path));
fs.writeFileSync(this.path, yaml.dump(data));
}
/**
* Loads data from the file the adapter refers to
*
* @author Nicolas Schmidt <[@nsc-de](https://github.com/nsc-de)>
* @returns ⌛ Promise (Ready when saved) >> Object
*
* @see [SyncYamlFileAdapter](https://nsc-de.github.io/js-database/classes/_yaml_adapter_.syncyamlfileadapter.html) - 👩👦 the parent class
*/
load(): JSObject {
if(fs.existsSync(this.path)) return yaml.load(fs.readFileSync(this.path).toString());
return {};
}
}