forked from nodejs/node-core-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.js
More file actions
42 lines (36 loc) · 976 Bytes
/
file.js
File metadata and controls
42 lines (36 loc) · 976 Bytes
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
'use strict';
const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
exports.appendFile = function(file, content) {
const parts = path.parse(file);
if (!fs.existsSync(parts.dir)) {
mkdirp.sync(parts.dir);
}
// TODO(joyeecheung): what if the file is a dir?
fs.appendFileSync(file, content, 'utf8');
};
exports.writeFile = function(file, content) {
const parts = path.parse(file);
if (!fs.existsSync(parts.dir)) {
mkdirp.sync(parts.dir);
}
// TODO(joyeecheung): what if the file is a dir?
fs.writeFileSync(file, content, 'utf8');
};
exports.writeJson = function(file, obj) {
exports.writeFile(file, JSON.stringify(obj, null, 2));
};
exports.readFile = function(file) {
if (fs.existsSync(file)) {
return fs.readFileSync(file, 'utf8');
}
return '';
};
exports.readJson = function(file) {
const content = exports.readFile(file);
if (content) {
return JSON.parse(content);
}
return {};
};