-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit_diff.js
54 lines (48 loc) · 1.87 KB
/
commit_diff.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
const { detailedDiff } = require('deep-object-diff')
const { spawnSync } = require('child_process')
const { isEqual, isEmpty } = require('lodash')
const current = require('./custom.json')
const git = require('simple-git')()
const rawOld = spawnSync('git', 'show master:custom.json'.split(' ')).stdout.toString()
const old = JSON.parse(rawOld)
const escape = string => {
return `${string}`
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t')
}
const commitDiff = () => {
const reallyCurrent = require('./custom.json')
if (!isEqual(reallyCurrent, current)) {
console.log('Exiting early because additional changes were made.')
return // Exit early. Another instance of the script will get the change.
}
const difference = detailedDiff(old, current)
if (Object.keys(difference).every(changeType => isEmpty(difference[changeType]))) {
console.log('No changes detected.')
return
}
const strokeToString = dictionary => stroke => `${stroke}: ${dictionary[stroke]}`
const added =
Object.keys(difference.added).map(strokeToString(current)).join(', ')
const deleted =
Object.keys(difference.deleted).map(strokeToString(old)).join(', ')
const updated =
Object.keys(difference.updated).map(stroke =>
`${stroke}: ${old[stroke]} → ${current[stroke]}`
).join(', ')
const commitMessage = [
added && `${added}`,
updated && `♻️${updated}`,
deleted && `🗑️${deleted}`
].filter(x => x).map(escape).join(' ')
console.log(commitMessage)
git.add('.').commit(commitMessage).push('origin', 'master')
}
module.exports = {
commitDiff,
debouncedCommitDiff: () => {
console.log('Waiting 60 seconds before committing changes to allow undos or additional changes.')
setTimeout(commitDiff, 60 * 1000)
}
}