UNPKG

1.56 kBJavaScriptView Raw
1var utils = require("../utils");
2
3/**
4 * Contains the mapping between partial file path to git path.
5 */
6function FilesContainer(logger) {
7 this.logger = logger;
8 this.partialToGitMap = {};
9}
10
11
12/**
13 * Add fille to filess map, each files has keys per each optional sub path.
14 * For example: given file 'foo/src/index.js', it gets the following keys:
15 * 1. 'foo/src/index.js'
16 * 2. 'src/index.js'
17 * 3. 'index.js'
18 * @param scmFile
19 */
20FilesContainer.prototype.addFile = function(scmFile) {
21 scmFile = utils.adjustPathSlashes(scmFile);
22 var parts = scmFile.split("/");
23 for (var i = parts.length; i > 0; i--) {
24 var partsToSlice = parts.length - i;
25 var key = parts.slice(partsToSlice).join("/");
26 this.partialToGitMap[key] = this.partialToGitMap[key] || [];
27 this.partialToGitMap[key].push(scmFile);
28 }
29};
30
31
32/**
33 *
34 * @param partialPath
35 * @returns {[]}
36 */
37FilesContainer.prototype.getScmPath = function(partialPath){
38 partialPath = utils.adjustPathSlashes(partialPath);
39 var parts = partialPath.split("/");
40 for (var i = parts.length; i > 0; i--) {
41 var partsToSice = parts.length - i;
42 var key = parts.slice(partsToSice).join("/");
43 var gitPathArr = this.partialToGitMap[key];
44 if(gitPathArr && gitPathArr.length === 1){
45 break;
46 }
47 }
48 if(gitPathArr == null){
49 this.logger.warn(partialPath + " has no match to any git file, consider using SL_sourceRoot");
50 return null;
51 }
52 return gitPathArr;
53}
54module.exports = FilesContainer;
\No newline at end of file