forked from evanx/redexutil
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathYamlFiles.js
38 lines (33 loc) · 1023 Bytes
/
YamlFiles.js
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
// Copyright (c) 2015, Evan Summers (twitter.com/evanxsummers)
// ISC license, see http://github.com/evanx/redexutil/LICENSE
import fs from 'fs';
import yaml from 'js-yaml';
import Errors from './Errors';
import * as Files from './Files';
import Loggers from './Loggers';
import Maybe from './Maybe';
const logger = Loggers.create(__filename, 'info');
const YamlFiles = {
readFileSyncMaybe(file) {
logger.debug('readFileSyncMaybe', file);
if (Files.existsFileSync(file)) {
return Maybe.resolve(YamlFiles.readFileSync(file));
} else {
return Maybe.reject(file);
}
},
readFileSyncDir(dir, file) {
return YamlFiles.readFileSync(Paths.join(dir, file));
},
readFile(file) {
return Files.readFile(file).then(content => yaml.safeLoad(content));
},
readFileSync(file) {
try {
return yaml.safeLoad(fs.readFileSync(file, 'utf8'));
} catch (e) {
throw Errors.decorate(e, {file});
}
}
};
module.exports = YamlFiles;