Skip to content

Commit 1132236

Browse files
committed
json file patch
1 parent 6cb7c28 commit 1132236

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dataparty/api",
33
"private": false,
4-
"version": "1.2.8",
4+
"version": "1.2.9",
55
"main": "dist/dataparty.js",
66
"browser": "src/index-browser.js",
77
"source": "src/index.js",

src/config/json-file.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,45 @@
33
const fs = require('fs')
44
const deepSet = require('deep-set')
55
const reach = require('../utils/reach')
6-
const logger = require('debug')('dataparty.config.memory');
6+
const logger = require('debug')('dataparty.config.json-file');
77

88
/**
99
* @class
1010
* @implements {Config}
1111
*/
1212
class JsonFileConfig {
1313

14-
constructor(defaults){
14+
constructor(defaults={}){
1515
this.path = reach(defaults, 'basePath') +'/config.json'
1616
this.defaults = defaults || {}
1717
this.content = Object.assign({}, this.defaults)
1818
}
1919

2020
async load(){
21-
if(!fs.existsSync(this.path)){return}
22-
23-
let rawdata = fs.readFileSync(this.path)
21+
logger('check exists', this.path)
22+
if(fs.existsSync(this.path)){
23+
24+
logger('does exist ... reading')
25+
26+
let rawdata = fs.readFileSync(this.path, {encoding: 'utf8'})
27+
28+
logger('read raw data')
29+
30+
if(rawdata ){
31+
logger('reading content')
32+
this.content = JSON.parse(rawdata)
33+
}
2434

25-
if(rawdata && rawdata.length > 0){
26-
this.content = JSON.parse(rawdata)
35+
}
36+
else{
37+
logger('does not exist')
38+
await this.save()
2739
}
2840
}
2941

3042
async start () {
3143
await this.load()
32-
return this
44+
logger('started')
3345
}
3446

3547
clear () {

0 commit comments

Comments
 (0)