-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.js
More file actions
96 lines (81 loc) · 1.59 KB
/
File.js
File metadata and controls
96 lines (81 loc) · 1.59 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
89
90
91
92
93
94
95
96
/*
Spark API – File Class Definition
This file was created by Spark
Licensed under CC BY-NC-SA 4.0 (https://creativecommons.org/licenses/by-nc-sa/4.0/)
*/
// import required dependencies
const path = require("path")
const fs = require("fs")
class File {
constructor(filename_, project_, options) {
this.filename = filename_
this.project = project_
this.isPage = options.isPage
this.data = {}
if(this.isPage)
this.path = `${__dirname}/pages/${this.project}/${this.filename}.json`
else
this.path = null
}
/*
@method create
create the file if it does not exist already
@return {Null}
*/
create() {
try {
if(!fs.existsSync(this.path)) {
fs.writeFileSync(this.path, JSON.stringify(this.data, null, 3))
}
} catch(err) {
throw err
}
}
/*
@method delete
delete the file
@return {Null}
*/
delete() {
try {
fs.unlinkSync(this.path)
} catch(err) {
throw err
}
}
/*
@method loadData
make sure the current data matches the file's data
@return {Null}
*/
loadData() {
const rawData = fs.readFileSync(this.path)
const json = JSON.parse(rawData)
this.data = json
}
/*
@method save
save & write the file to disk
@return {Null}
*/
save() {
try {
fs.writeFileSync(this.path, JSON.stringify(this.data, null, 3))
} catch(err) {
throw err
}
}
/*
@method update
@param {String} the key in the JSON to update
@param {Object} the new value for the specified key
@return {Null}
*/
update(component, data) {
this.loadData()
this.data[component] = data
this.save()
}
}
// export class
module.exports = File