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
/
utils.js
executable file
·96 lines (86 loc) · 2.34 KB
/
utils.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
"use strict";
var path = require('path');
var _ = require('underscore');
var describeMetadataService = new(require('./describe-metadata-service'))();
exports.ensureArray = function(objectOrArray) {
if (!objectOrArray) {
return [];
}
return _.isArray(objectOrArray) ? objectOrArray : [objectOrArray];
};
exports.getFileNameWithoutExtension = function(filename) {
return path.basename(filename).replace(path.extname(filename), '');
};
exports.getFileExtension = function(filename) {
filename = filename || "";
if (path.basename(filename).indexOf('.') === 0) {
return path.basename(filename).replace('.', '');
}
return path.extname(filename).replace('.', '');
};
exports.isValidFilename = function(filename) {
if (filename === undefined || filename === null || filename === '' || filename === '/dev/null' || filename === '\\dev\\null') {
return false;
}
if (path.basename(filename).match(/.*-meta\.xml$/)) {
return false;
}
// walk up and see if directory name matches a known type
var dirname = path.dirname(filename);
while (dirname && dirname !== '.') {
var matchedTypes = describeMetadataService.getTypesForDirectoryName(path.basename(dirname));
if (matchedTypes && matchedTypes.length) {
return true;
}
dirname = path.dirname(dirname);
}
return false;
};
exports.isValidMetaFilename = function(filename) {
var self = this;
if (filename === undefined || filename === null || filename === '' || filename === '/dev/null' || filename === '\\dev\\null') {
return false;
}
var metadataFilename = self.getMetadataFilenameForMetaFilename(filename);
return self.getFileExtension(filename) === 'xml' && self.isValidFilename(metadataFilename);
};
exports.getMetadataFilenameForMetaFilename = function(metaFilename) {
return metaFilename.replace('-meta.xml', '');
};
// always put Setings at the end
// otherwise alphanumeric
exports.compareMetadataTypeNames = function(a, b) {
if (a === 'Settings' && b !== 'Settings') {
return 1;
}
if (a !== 'Settings' && b === 'Settings') {
return -1;
}
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
};
// alphanumeric
exports.compareMetadataFileNames = function(a, b) {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
};
// alphanumeric
exports.compareMetadataFullNames = function(a, b) {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
};