-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemo.js
64 lines (50 loc) · 1.54 KB
/
memo.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
const sha256 = require('js-sha256');
const common = require('./common');
const fs = require('fs');
const makeDutsfilePath = common.makeDutsfilePath
const execCmd = common.execCmd
const seperator = common.seperator
const lineSeperator = common.lineSeperator
function memo(file, memo) {
const convertedMemo = convertNewline(memo)
const now = NOW()
const beforeHash = `${file}${convertedMemo}${now}`
const hash = sha256(beforeHash)
const content = [file, now, convertedMemo, hash].join(seperator)
fs.appendFile(makeDutsfilePath(file), content + lineSeperator, function (err) {
if (err) {
console.log(`cannot memo. error: ${err}`)
} else {
const filelines = fs.readFileSync(makeDutsfilePath(file)).toString().split(lineSeperator).slice(0, -1);
execCmd(`processed. total ${filelines.length} memos`)
}
})
}
function convertNewline(content) {
content = content.replace(/(?:\r\n|\r|\n)/g, '|||');
return content
}
function NOW() {
var date = new Date();
var aaaa = date.getUTCFullYear();
var gg = date.getUTCDate();
var mm = (date.getUTCMonth() + 1);
if (gg < 10)
gg = "0" + gg;
if (mm < 10)
mm = "0" + mm;
var cur_day = aaaa + "-" + mm + "-" + gg;
var hours = date.getUTCHours()
var minutes = date.getUTCMinutes()
var seconds = date.getUTCSeconds();
if (hours < 10)
hours = "0" + hours;
if (minutes < 10)
minutes = "0" + minutes;
if (seconds < 10)
seconds = "0" + seconds;
return cur_day + " " + hours + ":" + minutes + ":" + seconds;
}
module.exports = {
memo: memo
}