| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var |
| 2 | fs = require('fs'), | |
| 3 | path = require('path'); | |
| 4 | ||
| 5 | 1 | function FileAnalyzer(root) { |
| 6 | 8 | this.root = root; |
| 7 | } | |
| 8 | ||
| 9 | 1 | FileAnalyzer.prototype.getFiles = function(globbedFiles) { |
| 10 | 1 | var files = []; |
| 11 | ||
| 12 | 1 | globbedFiles.forEach(function(file) { |
| 13 | 1 | files = file.src.filter(function(filepath) { |
| 14 | 1 | return fs.lstatSync(filepath).isFile(); |
| 15 | }).map(function(filepath) { | |
| 16 | 0 | return filepath; |
| 17 | }); | |
| 18 | }); | |
| 19 | ||
| 20 | 1 | return files; |
| 21 | }; | |
| 22 | ||
| 23 | 1 | FileAnalyzer.prototype.duplicate = function(file, hash) { |
| 24 | 1 | var |
| 25 | _extension = path.extname(file), | |
| 26 | _directory = path.dirname(file), | |
| 27 | _base = path.basename(file, _extension); | |
| 28 | ||
| 29 | 1 | fs.writeFileSync( |
| 30 | _directory + '/' + | |
| 31 | _base + '-' + | |
| 32 | hash + _extension, | |
| 33 | fs.readFileSync(file) | |
| 34 | ); | |
| 35 | }; | |
| 36 | ||
| 37 | 1 | module.exports = FileAnalyzer; |
| 38 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var |
| 2 | path = require('path'), | |
| 3 | fs = require('fs'), | |
| 4 | _ = require('lodash'), | |
| 5 | crypto = require('crypto'), | |
| 6 | FileAnalyzer = require('./file-analyzer'), | |
| 7 | hash = require('string-hash'); | |
| 8 | ||
| 9 | 1 | function Fingerprinter(root, devPaths) { |
| 10 | 7 | this.devPaths = devPaths; |
| 11 | 7 | this.fileAnalyzer = new FileAnalyzer(root); |
| 12 | }; | |
| 13 | ||
| 14 | 1 | Fingerprinter.prototype.hashFiles = function(files) { |
| 15 | 1 | var |
| 16 | _that = this, | |
| 17 | _prints = {}; | |
| 18 | ||
| 19 | 1 | files.forEach(function(file) { |
| 20 | 1 | var |
| 21 | _code = fs.readFileSync(file).toString(), | |
| 22 | _hash = crypto.createHash('sha256'), | |
| 23 | _digest; | |
| 24 | ||
| 25 | 1 | _hash.setEncoding('hex'); |
| 26 | 1 | _hash.update(fs.readFileSync(file)); |
| 27 | ||
| 28 | 1 | _digest = _hash.digest('hex'); |
| 29 | ||
| 30 | 1 | _prints[_digest] = { |
| 31 | id: hash(_code), | |
| 32 | hash: _digest, | |
| 33 | file: path.basename(file), | |
| 34 | path: _.difference( | |
| 35 | path.dirname(file).split('/'), | |
| 36 | this.devPaths | |
| 37 | ).join('/'), | |
| 38 | extension: path.extname(file) | |
| 39 | }; | |
| 40 | ||
| 41 | 1 | _that.fileAnalyzer.duplicate(file, _digest); |
| 42 | }, this); | |
| 43 | ||
| 44 | 1 | return _prints; |
| 45 | }; | |
| 46 | ||
| 47 | 1 | Fingerprinter.prototype.hashManifest = function(manifest) { |
| 48 | 6 | var |
| 49 | _hash = crypto.createHash('sha256'), | |
| 50 | _digest; | |
| 51 | ||
| 52 | 6 | _hash.setEncoding('hex'); |
| 53 | 6 | _hash.update(manifest); |
| 54 | ||
| 55 | 6 | return _hash.digest('hex'); |
| 56 | }; | |
| 57 | ||
| 58 | 1 | module.exports = Fingerprinter; |
| 59 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var |
| 2 | fs = require('fs'), | |
| 3 | Fingerprinter = require('./fingerprinter'); | |
| 4 | ||
| 5 | 1 | function Persister(packageMeta, root, location, rootUrl, packageUrl) { |
| 6 | 5 | this._packageMeta = packageMeta; |
| 7 | 5 | this._root = root; |
| 8 | 5 | this._location = location; |
| 9 | 5 | this._rootUrl = rootUrl; |
| 10 | 5 | this._packageUrl = packageUrl; |
| 11 | ||
| 12 | 5 | this.fingerprinter = new Fingerprinter(root, []); |
| 13 | } | |
| 14 | ||
| 15 | 1 | Persister.prototype.write = function(hashes) { |
| 16 | 5 | var manifest = {}; |
| 17 | ||
| 18 | 5 | manifest.package = this._packageMeta; |
| 19 | 5 | manifest.hashes = hashes; |
| 20 | 5 | manifest.rootUrl = this._rootUrl; |
| 21 | 5 | manifest.packageUrl = this._packageUrl; |
| 22 | 5 | manifest.version = this.fingerprinter.hashManifest(JSON.stringify(manifest)); |
| 23 | 5 | var manifestAsJson = JSON.stringify(manifest, null, 2); |
| 24 | ||
| 25 | 5 | fs.writeFileSync(this._root + '/' + this._location, manifestAsJson); |
| 26 | ||
| 27 | 5 | return manifestAsJson; |
| 28 | }; | |
| 29 | ||
| 30 | 1 | Persister.prototype.read = function() { |
| 31 | 4 | var json = fs.readFileSync(this._root + '/' + this._location); |
| 32 | ||
| 33 | 4 | return JSON.parse(json); |
| 34 | }; | |
| 35 | ||
| 36 | 1 | module.exports = Persister; |
| 37 |