This repository has been archived by the owner on Oct 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
metadata-file.js
executable file
·154 lines (138 loc) · 5.54 KB
/
metadata-file.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"use strict";
var File = require('vinyl');
var path = require('path');
var _ = require('underscore');
var metadataUtils = require('./utils');
var Manifest = require('./manifest');
var MetadataComponent = require('./metadata-component');
var describeMetadataService = new(require('./describe-metadata-service'))();
var config = new(require("./config"))();
var folderToMetadataType = config.get("folderBasedMetadataMap");
var metadataTypeToFolder = _.invert(folderToMetadataType);
/**
* MetadataFile represents a file on the filesystem.
* It inherits from vinyl.
* Note that the path should be relative to `src`.
*/
var MetadataFile = module.exports = function(opts) {
File.call(this, opts);
};
MetadataFile.prototype = Object.create(File.prototype);
MetadataFile.prototype.constructor = MetadataFile;
MetadataFile.prototype.basenameDirname = function() {
var self = this;
return path.basename(path.dirname(self.path));
};
MetadataFile.prototype.waveTemplateDirname = function() {
// Metadata like WaveTemplateBundles are deeply nested and contain ambiguous subfolders like 'dashboard'.
var self = this;
var pathComponents = self.path.split(path.sep);
return pathComponents[1];
};
MetadataFile.prototype.parentDirname = function() {
var self = this;
return path.dirname(path.dirname(self.path)) !== '.' ? path.basename(path.dirname(path.dirname(self.path))) : null;
};
MetadataFile.prototype.extnameWithoutDot = function() {
var self = this;
return path.basename(self.path).indexOf('.') === 0 ? path.basename(self.path).replace('.', '') : path.extname(self.path).replace('.', '');
};
MetadataFile.prototype.filename = function() {
var self = this;
return path.basename(self.path).replace(path.extname(self.path), '');
};
MetadataFile.prototype.diff = function(other) {
var self = this;
var added = new Manifest();
var modified = new Manifest();
var deleted = new Manifest();
var isDeleted = other === undefined || other.isNull() || !other.path || other.path === '/dev/null' || other.path === '\\dev\\null';
var isNew = self.path === undefined || self.isNull() || !self.path || self.path === '/dev/null' || self.path === '\\dev\\null';
var isModified = !self.isNull() && !other.isNull() && self.path === other.path;
var componentFrom = self.getComponent();
var componentTo = other.getComponent();
if (isDeleted && componentFrom) {
deleted.add(componentFrom);
} else if (isNew && componentTo) {
added.add(componentTo);
} else if (isModified && componentTo) {
modified.add(componentTo);
}
return {
added: added,
modified: modified,
deleted: deleted
};
};
MetadataFile.prototype.getMetadataFilename = function() {
var self = this;
var metadataFilepath = self.path;
if (!metadataUtils.isValidFilename(metadataFilepath) && !metadataUtils.isValidMetaFilename(metadataFilepath)) {
return null;
}
if (metadataUtils.isValidMetaFilename(metadataFilepath)) {
return metadataUtils.getMetadataFilenameForMetaFilename(metadataFilepath);
}
return metadataFilepath;
};
MetadataFile.prototype.getMetadataType = function() {
var self = this;
var metadataFilename = self.getMetadataFilename();
return metadataFilename ? describeMetadataService.getTypeForFilepath(metadataFilename) : null;
};
MetadataFile.prototype.getComponent = function() {
var self = this;
var metadataFile = new MetadataFile({
path: self.getMetadataFilename() // in case it was a -meta.xml
});
var metadataType = metadataFile.getMetadataType();
if (!metadataType) {
return null;
}
var result = {
type: metadataType.xmlName,
fileName: path.join(metadataType.directoryName, metadataFile.basename),
fullName: metadataFile.filename()
};
if (metadataType.directoryName === 'aura') {
if (!metadataFile.extnameWithoutDot()) {
result.fileName = path.join(metadataType.directoryName, metadataFile.basename);
result.fullName = metadataFile.basename;
} else {
result.fileName = path.join(metadataType.directoryName, metadataFile.basenameDirname());
result.fullName = metadataFile.basenameDirname();
}
}
if (metadataType.directoryName === 'lwc') {
if (!metadataFile.extnameWithoutDot()) {
result.fileName = path.join(metadataType.directoryName, metadataFile.basename);
result.fullName = metadataFile.basename;
} else {
result.fileName = path.join(metadataType.directoryName, metadataFile.basenameDirname());
result.fullName = metadataFile.basenameDirname();
}
} else if (metadataType.directoryName === 'waveTemplates') {
result.fileName = path.join(metadataType.directoryName, metadataFile.waveTemplateDirname());
result.fullName = metadataFile.waveTemplateDirname();
} else if (metadataType.inFolder) {
var fileNameParts = [metadataFile.basename];
var fullNameParts = [metadataType.xmlName === 'Document' ? metadataFile.basename : metadataFile.filename()];
if (metadataFile.parentDirname()) {
// not a Folder, then prepend folder name
fileNameParts.unshift(metadataFile.basenameDirname());
fullNameParts.unshift(metadataFile.basenameDirname());
} else {
// return the folder type
result.type = metadataTypeToFolder[metadataType.xmlName];
}
var fileNamePartsPath = path.join.apply('', fileNameParts);
result.fileName = path.join(metadataType.directoryName, fileNamePartsPath);
result.fullName = fullNameParts.join('/');
}
return new MetadataComponent(result);
};
// STATIC
var builtInProps = ['basenameDirname', 'waveTemplateDirname', 'parentDirname', 'extnameWithoutDot', 'filename', 'diff', 'getMetadataFilename', 'getMetadataType', 'getComponent'];
MetadataFile.isCustomProp = function(name) {
return File.isCustomProp(name) && builtInProps.indexOf(name) === -1;
};