Skip to content

Commit 6dc8826

Browse files
committed
Add wakatime-plugin #2810
1 parent 78c00b1 commit 6dc8826

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

browser/components/CodeEditor.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import TurndownService from 'turndown'
2626
import {
2727
gfm
2828
} from 'turndown-plugin-gfm'
29+
import { findStorage } from 'browser/lib/findStorage'
30+
import { sendWakatimeHeartBeat } from 'browser/lib/wakatime-plugin'
2931

3032
CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js'
3133

@@ -741,8 +743,14 @@ export default class CodeEditor extends React.Component {
741743
this.updateHighlight(editor, changeObject)
742744

743745
this.value = editor.getValue()
746+
747+
const { storageKey, noteKey } = this.props
748+
const storage = findStorage(storageKey)
744749
if (this.props.onChange) {
745750
this.props.onChange(editor)
751+
if (storage) sendWakatimeHeartBeat(storage.path, noteKey, storage.name, true, true, false)
752+
} else {
753+
if (storage) sendWakatimeHeartBeat(storage.path, noteKey, storage.name, false, false, false)
746754
}
747755
}
748756

@@ -846,6 +854,11 @@ export default class CodeEditor extends React.Component {
846854
}
847855

848856
reload () {
857+
// wakatime
858+
const { storageKey, noteKey } = this.props
859+
const storage = findStorage(storageKey)
860+
if (storage) sendWakatimeHeartBeat(storage.path, noteKey, storage.name, false, false, true)
861+
849862
// Change event shouldn't be fired when switch note
850863
this.editor.off('change', this.changeHandler)
851864
this.value = this.props.value

browser/lib/wakatime-plugin.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const exec = require('child_process').exec
2+
const path = require('path')
3+
let lastHeartbeat = 0
4+
5+
function sendWakatimeHeartBeat (storagePath, noteKey, storageName, isWrite, hasFileChanges, isFileChange) {
6+
7+
if (new Date().getTime() - lastHeartbeat > 120000 || isFileChange) {
8+
const notePath = path.join(storagePath, 'notes', noteKey + '.cson')
9+
10+
if (!isWrite && !hasFileChanges && !isFileChange) {
11+
return
12+
}
13+
14+
// TODO: add --key sdasdsa-sdsad-asdasd-asdsa-asdasdadas from configuration UI or use ~/.wakatime.conf
15+
exec(`wakatime --file ${notePath} --project ${storageName} --plugin Boostnote-wakatime`, (error, stdOut, stdErr) => {
16+
if (error) {
17+
console.log(error)
18+
} else {
19+
lastHeartbeat = new Date()
20+
console.log('wakatime', 'isWrite', isWrite, 'hasChanges', hasFileChanges)
21+
}
22+
})
23+
}
24+
}
25+
26+
export { sendWakatimeHeartBeat }

0 commit comments

Comments
 (0)