forked from saltfactory/front-matter-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (75 loc) · 2.07 KB
/
index.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
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
'use strict';
let fs = require('fs');
let matter = require('gray-matter');
let extend = require('util')._extend;
let fsi = require('fs-filesysteminfo');
let path = require('path');
const mkdirp = require('mkdirp');
class FrontMatterService {
constructor() {
}
_printObject(obj){
console.log(JSON.stringify(obj, null, 2));
}
create(filePath) {
let directoryName = path.dirname(filePath);
mkdirp.sync(directoryName);
fs.writeFileSync(filePath);
return this.read(filePath);
}
read(filePath) {
// this.sourcePath = filePath;
this.file = fs.readFileSync(filePath);
this.fileSystemInfo = new fsi.FileSystemInfo(filePath);
this.matter = matter(String(this.file));
if (this.matter.orig === 'undefined') {
this.matter.orig = '';
this.matter.content = '';
}
return this;
}
show(flag) {
let output = flag ? this.matter[flag] : this.matter;
this._printObject(output);
return this;
}
extend(func) {
func(this.matter.data, this.matter.content, this.matter);
return this;
}
data(func) {
// this.matter = extend(this.matter, obj);
func(this.matter.data, this.matter);
return this;
}
content(func) {
func(this.matter.content, this.matter);
return this;
}
fileInfo(){
// let info = new fsi.FileSystemInfo(this.sourcePath);
this._printObject(this.fileSystemInfo);
return this;
}
save(dirPath, options, callback){
let filename = this.fileSystemInfo.name;
if (options) {
if (options.filename) filename = options.filename;
if (options.prefix) filename = `${options.prefix}${filename}`;
if (options.postfix) filename = `${filename}${options.postfix}`;
}
let dest = path.join(dirPath, filename);
let data = matter.stringify(this.matter.content, this.matter.data);
fs.writeFile(dest, data, (err) => {
callback(err, data);
});
}
}
module.exports = new FrontMatterService();
// module.exports = function(filePath){
//
// return this;
// }
// module.exports.prototype.show = function(){
// console.log(this.)
// }