Comparing version 1.0.2 to 2.0.0
{ | ||
"name": "md.daily", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "for daily store", | ||
@@ -26,2 +26,3 @@ "main": "src/index.js", | ||
"chokidar": "^2.0.3", | ||
"glob": "^7.1.2", | ||
"md5": "^2.2.1", | ||
@@ -28,0 +29,0 @@ "then-fs": "^2.0.0" |
@@ -1,81 +0,3 @@ | ||
const path = require('path') | ||
, fs = require('then-fs') | ||
, parse = require('./parse') | ||
, chokidar = require('chokidar') | ||
, MetaProxy = require('./MetaProxy') | ||
class Daily extends MetaProxy { | ||
/** | ||
* @description Daily constructor | ||
* @param { String } file_path 文件绝对路径 | ||
*/ | ||
constructor(file_path) { | ||
super(); | ||
// 参数初始化 | ||
this.file_path = file_path; | ||
this.data = null; | ||
} | ||
/** | ||
* @description 带缓存的 getData | ||
* @returns { Promise<Daily> } | ||
*/ | ||
getData() { | ||
if (this.data) { | ||
return Promise.resolve(this); | ||
} else { | ||
return this.read().then(data => { | ||
data.file_path = this.file_path; | ||
this.data = data; | ||
return this; | ||
}); | ||
} | ||
} | ||
/** | ||
* @description 从磁盘中读取 file_path 返回 Promise | ||
* @returns { Promise<Object> } 文章经过 parse 的结果 | ||
*/ | ||
read() { | ||
return fs.readFile(this.file_path) | ||
.then(e => e.toString()) | ||
.then(parse) | ||
} | ||
/** | ||
* @description watch | ||
* @returns { Daily } 链式调用 | ||
*/ | ||
beginWatch() { | ||
if (this.watcher) return this; | ||
// 绑定 watcher | ||
this.watcher = chokidar.watch(this.file_path); | ||
// !!! 热更新 | ||
this.watcher.on('change', path => { | ||
this.data = null; | ||
this.emit('change', this); | ||
}); | ||
// 当文件被删除 | ||
// 被动的取消绑定 | ||
this.watcher.on('unlink', path => { | ||
this.endWatch(); | ||
}); | ||
return this; | ||
} | ||
/** | ||
* @description 主动取消绑定 | ||
* @returns { Daily } 链式调用 | ||
*/ | ||
endWatch() { | ||
this.watcher.close(); | ||
this.watcher = null; | ||
return this; | ||
} | ||
module.exports = { | ||
Daily: require('./Daily') | ||
} | ||
module.exports = Daily; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14067
14
314
4
1
+ Addedglob@^7.1.2
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedwrappy@1.0.2(transitive)