-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathobject-info.js
142 lines (127 loc) · 3.28 KB
/
object-info.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
'use strict';
const path = require('path');
module.exports = {
/**
* Returns info object
*
* @param {object} config
* @param {array} config.addonPaths, config.includeAddons
* @param {string} filename
* @returns {object}
* @private
*/
get(config, filepath) {
let addonInfo = this.addonInfo(config, filepath);
let fullName = this.objectName(config, filepath);
let topLevelName = fullName.split('/')[0];
let info = {
key: fullName,
name: fullName,
type: this.objectType(config, filepath),
parentKey: topLevelName,
subComponentKeys: {},
isSubComponent: topLevelName != fullName,
stats: {
count: 0,
js: 0,
curly: 0,
angle: 0,
componentHelper: 0,
},
occurrences: [],
filePaths: [],
fileType: path.extname(filepath),
};
if (addonInfo) {
info.addonName = addonInfo.name;
info.addonPath = addonInfo.sourcePath;
info.key = `[${addonInfo.name}] ${fullName}`;
info.parentKey = `[${addonInfo.name}] ${topLevelName}`;
}
return info;
},
/**
* Returns object type
*
* @param {object} config
* @param {array} config.addonPaths, config.componentPaths
* @param {string} filename
* @returns {object}
* @private
*/
objectType: function(config, filename) {
if (['.hbs', '.js'].includes(path.extname(filename))) {
if (path.basename(filename).indexOf('-test.js') > -1) {
return 'test';
} else if (filename.split('/').includes('components')) {
return 'component';
}
}
},
/**
* Returns object name from path
*
* @param {object} config
* @param {array} config.addonPaths, config.componentPaths
* @param {string} filename
* @returns {object}
* @private
*/
objectName: function(config, filename) {
let name = filename;
name = _removeLeadingPath(name, 'components/', '/');
name = _removeLeadingPath(name, 'templates/', '/');
return name
.replace('component.js', '')
.replace('template.hbs', '')
.replace('index.js', '')
.replace('index.ts', '')
.replace('index.hbs', '')
.replace('.js', '')
.replace('.ts', '')
.replace('.hbs', '')
.replace(/^[\\/\\.]+/, '')
.replace(/[\\/\\.]$/, '')
.trim();
},
/**
* Returns addon info object
*
* @param {object} config
* @param {array} config.addonPaths, config.includeAddons
* @param {string} filename
* @returns {object}
* @private
*/
addonInfo: function(config, filename) {
let sourcePath, name;
if (config.includeAddons) {
for (let addonPath of config.addonPaths) {
if (filename.indexOf(addonPath) > -1) {
sourcePath = addonPath;
name = path.basename(addonPath);
break;
}
}
}
if (sourcePath && name) {
return {
sourcePath,
name,
};
}
},
};
/**
* Returns leading path.
* we might know that its from an addon at /node_modules/company-addon so we can chop out everything
* that comes before that path component
*
* @param {string} filepath
* @param {string} subpath
* @returns {string}
* @private
*/
function _removeLeadingPath(filepath, subpath) {
return filepath.replace(new RegExp(`^.+${subpath}`), '');
}