/ src / server / diff.js
diff.js
 1  const log = require('loglevel')
 2  const join = require('path').join
 3  
 4  const DB = require('./db')
 5  
 6  const TEMP_PATH = '/var/tmp'
 7  const JQUERY_URL = 'https://code.jquery.com/jquery-3.3.1.min.js'
 8  /* 0 disables limits on report size */
 9  const MAX_REPORT_SIZE = 0
10  
11  class Diff {
12    constructor (diffoscope) {
13      this.dos = diffoscope
14    }
15  
16    async manual (obj) {
17      const diff = await DB.Diff.query().insertGraph({
18        name: obj.name,
19        type: 'manual',
20        status: 'new',
21        builds: [
22          { name: obj.name, type: 'manual', fileUrl: obj.east },
23          { name: obj.name, type: 'manual', fileUrl: obj.west },
24        ]
25      })
26      return await this.run(diff)
27    }
28  
29    async builds (builds) {
30      const newest = builds[0]
31      builds = builds.slice(1)
32      builds.forEach(async build => {
33        const diff = this.db.addDiff({east: newest, west: build})
34        return await this.run(diff)
35      })
36    }
37  
38    async updateDiffStatus(id, status) {
39      return await DB.Diff.query().update({status}).where('id', id)
40    }
41  
42    async run (diff) {
43      let rval
44      /* generate the diff report */
45      try {
46        rval = await this.dos.gen(
47          diff.name, [diff.builds[0].fileUrl, diff.builds[1].fileUrl]
48        )
49      } catch(ex) {
50        log.error('Diff failed: %s', ex.message)
51        this.updateDiffStatus(diff.id, 'failure')
52        throw ex
53      }
54      this.updateDiffStatus(diff.id, 'success')
55      return rval
56    }
57  
58  }
59  
60  module.exports = Diff