signalk-automation-plugin 0.0.8 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/engine.js +39 -0
- package/lib/git.js +14 -10
- package/lib/runtime.js +2 -8
- package/package.json +1 -1
package/lib/engine.js
CHANGED
|
@@ -74,6 +74,23 @@ function evalClause (clause, ctx) {
|
|
|
74
74
|
clause: { not: true }
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
|
+
if (clause.triggered) {
|
|
78
|
+
const want = clause.triggered
|
|
79
|
+
const fired = ctx.trigger && ctx.trigger.path
|
|
80
|
+
let expected = null
|
|
81
|
+
if (want && typeof want === 'object') {
|
|
82
|
+
if (want.helper) expected = helperPath(want.helper)
|
|
83
|
+
else if (want.path) expected = want.path
|
|
84
|
+
} else if (typeof want === 'string') {
|
|
85
|
+
expected = want
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
pass: expected != null && fired === expected,
|
|
89
|
+
error: expected == null ? 'triggered needs helper or path' : null,
|
|
90
|
+
actual: fired || null,
|
|
91
|
+
expected
|
|
92
|
+
}
|
|
93
|
+
}
|
|
77
94
|
if (Array.isArray(clause.all)) {
|
|
78
95
|
const parts = clause.all.map((c) => withLabel(c, evalClause(c, ctx)))
|
|
79
96
|
return {
|
|
@@ -212,6 +229,12 @@ function fmtVal (value) {
|
|
|
212
229
|
function clauseLabel (clause, result) {
|
|
213
230
|
if (!clause || typeof clause !== 'object') return ''
|
|
214
231
|
if (clause.not) return 'not ' + clauseLabel(clause.not, result)
|
|
232
|
+
if (clause.triggered) {
|
|
233
|
+
const want = clause.triggered
|
|
234
|
+
if (want && want.helper) return 'triggered helper ' + want.helper
|
|
235
|
+
if (want && want.path) return 'triggered path ' + want.path
|
|
236
|
+
return 'triggered ' + fmtVal(want)
|
|
237
|
+
}
|
|
215
238
|
if (Array.isArray(clause.all)) return 'all'
|
|
216
239
|
if (Array.isArray(clause.any)) return 'any'
|
|
217
240
|
if (clause.zone) {
|
|
@@ -636,6 +659,20 @@ function sameValue (a, b) {
|
|
|
636
659
|
return false
|
|
637
660
|
}
|
|
638
661
|
|
|
662
|
+
function asSwitch (v) {
|
|
663
|
+
if (v === true || v === 1 || v === '1' || v === 'on' || v === 'ON') return 1
|
|
664
|
+
if (v === false || v === 0 || v === '0' || v === 'off' || v === 'OFF') return 0
|
|
665
|
+
return null
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
function valueUnchanged (previous, next) {
|
|
669
|
+
if (previous === undefined) return false
|
|
670
|
+
if (sameValue(previous, next)) return true
|
|
671
|
+
const a = asSwitch(previous)
|
|
672
|
+
const b = asSwitch(next)
|
|
673
|
+
return a !== null && a === b
|
|
674
|
+
}
|
|
675
|
+
|
|
639
676
|
function makeRunScript (opts) {
|
|
640
677
|
return function (spec) {
|
|
641
678
|
return runScript({
|
|
@@ -664,6 +701,8 @@ module.exports = {
|
|
|
664
701
|
makeRunScript,
|
|
665
702
|
parseDuration,
|
|
666
703
|
sameValue,
|
|
704
|
+
asSwitch,
|
|
705
|
+
valueUnchanged,
|
|
667
706
|
clauseLabel,
|
|
668
707
|
triggerFiredLabel,
|
|
669
708
|
triggerListMatched,
|
package/lib/git.js
CHANGED
|
@@ -46,9 +46,18 @@ function currentSha (dir) {
|
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
function canonicalPath (p) {
|
|
50
|
+
const resolved = path.resolve(p)
|
|
51
|
+
try {
|
|
52
|
+
return fs.realpathSync(resolved)
|
|
53
|
+
} catch (_) {
|
|
54
|
+
return resolved
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
49
58
|
function repoRelPath (repoDir) {
|
|
50
|
-
const root =
|
|
51
|
-
const dir =
|
|
59
|
+
const root = canonicalPath(runGit(['rev-parse', '--show-toplevel'], repoDir))
|
|
60
|
+
const dir = canonicalPath(repoDir)
|
|
52
61
|
const rel = path.relative(root, dir).replace(/\\/g, '/')
|
|
53
62
|
if (!rel || rel === '.') return { root, rel: '' }
|
|
54
63
|
if (rel.startsWith('..') || path.isAbsolute(rel)) {
|
|
@@ -78,13 +87,8 @@ function diffRange (repoDir, fromSha, toSha) {
|
|
|
78
87
|
}
|
|
79
88
|
|
|
80
89
|
function archiveTreeish (repoDir, sha) {
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
const rel = path.relative(root, dir).replace(/\\/g, '/')
|
|
84
|
-
if (!rel || rel === '.') return sha
|
|
85
|
-
if (rel.startsWith('..') || path.isAbsolute(rel)) {
|
|
86
|
-
throw new Error('automationsDir is outside the git repository')
|
|
87
|
-
}
|
|
90
|
+
const { rel } = repoRelPath(repoDir)
|
|
91
|
+
if (!rel) return sha
|
|
88
92
|
return sha + ':' + rel
|
|
89
93
|
}
|
|
90
94
|
|
|
@@ -93,7 +97,7 @@ function materializeCommit (repoDir, sha, destDir) {
|
|
|
93
97
|
if (!isGitRepo(repoDir)) throw new Error('automationsDir is not a git repository')
|
|
94
98
|
runGit(['cat-file', '-t', sha], repoDir)
|
|
95
99
|
const treeish = archiveTreeish(repoDir, sha)
|
|
96
|
-
const root =
|
|
100
|
+
const root = repoRelPath(repoDir).root
|
|
97
101
|
const parent = path.dirname(path.resolve(destDir))
|
|
98
102
|
fs.mkdirSync(parent, { recursive: true })
|
|
99
103
|
const tmp = fs.mkdtempSync(path.join(parent, 'live-tmp-'))
|
package/lib/runtime.js
CHANGED
|
@@ -151,6 +151,7 @@ class Runtime {
|
|
|
151
151
|
: undefined
|
|
152
152
|
this.setPathValue(p, value, { $source: source })
|
|
153
153
|
await this.applyLatches(p, source, previous)
|
|
154
|
+
if (engine.valueUnchanged(previous, value)) return
|
|
154
155
|
await this.kickPath(p, value)
|
|
155
156
|
}
|
|
156
157
|
|
|
@@ -159,8 +160,7 @@ class Runtime {
|
|
|
159
160
|
if (fromSelf) return
|
|
160
161
|
if (previous === undefined) return
|
|
161
162
|
const next = this.values[p]
|
|
162
|
-
if (engine.
|
|
163
|
-
if (asSwitch(previous) !== null && asSwitch(previous) === asSwitch(next)) return
|
|
163
|
+
if (engine.valueUnchanged(previous, next)) return
|
|
164
164
|
for (const def of Object.values(this.doc.helpers)) {
|
|
165
165
|
if (def.latch_on_external_put === p && def.type === 'boolean') {
|
|
166
166
|
await this.setHelper(def.id, true)
|
|
@@ -507,12 +507,6 @@ function newAbort () {
|
|
|
507
507
|
return abort
|
|
508
508
|
}
|
|
509
509
|
|
|
510
|
-
function asSwitch (v) {
|
|
511
|
-
if (v === true || v === 1 || v === '1' || v === 'on' || v === 'ON') return 1
|
|
512
|
-
if (v === false || v === 0 || v === '0' || v === 'off' || v === 'OFF') return 0
|
|
513
|
-
return null
|
|
514
|
-
}
|
|
515
|
-
|
|
516
510
|
function holdMs (auto) {
|
|
517
511
|
for (const t of auto.trigger || []) {
|
|
518
512
|
if (t.for) {
|