-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtyped-export.js
More file actions
133 lines (118 loc) · 4.52 KB
/
typed-export.js
File metadata and controls
133 lines (118 loc) · 4.52 KB
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
var path = require('path');
var string = require('underscore.string');
// TODO(Tony) bring this into prototype
function TypedExport(options) {
// Each export module needs a type
this.type = options.type || 'unknown';
// The ast nodes that will go into this export module
this.astNodes = [];
// Output directory for module files
this.outputDirectory = options.outputDirectory;
// Set the export name for the module
this.exportName = options.exportName;
this.fileName = options.fileName;
}
TypedExport.prototype = Object.create(null);
TypedExport.knownTypes = ['model', 'serializer', 'controller', 'view', 'mixin', 'transform', 'adapter'];
TypedExport.pluralizeType = function(type) {
return type + 's';
};
TypedExport.determineType = function(filePath, className) {
// First check to see if any class matches
var type = 'unknown';
if (!className) {
return type;
}
TypedExport.knownTypes.forEach(function(testType) {
var r = new RegExp(string.titleize(testType));
if (r.test(className)) {
type = testType;
}
}, this);
// Check to see if filename provides type, if we did not find it from classname
if (type === 'unknown') {
TypedExport.knownTypes.forEach(function(testType) {
var r = new RegExp(TypedExport.pluralizeType(testType));
if (r.test(filePath)) {
type = testType;
}
}, this);
}
return type;
};
// TODO(Tony) handle path and name
TypedExport.prototype.exportPath = function(appName) {
return '/' + appName + '/' + this.fileName;
};
TypedExport.prototype.outputFolderPath = function(appName) {
return path.join(this.outputDirectory, path.dirname(this.exportPath(appName)));
};
TypedExport.prototype.outputFilePath = function(appName) {
var fileName = path.basename(this.fileName);
var folderPath = this.outputFolderPath(appName);
return path.join(folderPath, string.dasherize(fileName));
};
TypedExport.convertToOutputFilename = function(stringInput) {
var filename = [];
var chars = string.chars(stringInput);
function isUpperCase(str) { return (str === str.toUpperCase() && !isLowerCase(str)); }
function isLowerCase(str) { return (str === str.toLowerCase()); }
chars.forEach(function(c, i) {
if (i>0 && isLowerCase(chars[i-1]) && isUpperCase(c)) {
filename.push('-');
}
filename.push(c);
});
return filename.join('').toLowerCase();
}
TypedExport.filePathForClassname = function(className, type, filePath, exportFiles) {
var newFilePath;
if (!className) {
// className = null means we are not exporting anything for this filepath
// so we should send it to special globals folder
newFilePath = path.join('globals', filePath);
} else if (type === 'unknown') {
newFilePath = filePath;
} else {
var filename = TypedExport.convertToOutputFilename(className);
var fileParts = filename.split('-');
var shouldPop = false;
TypedExport.knownTypes.forEach(function(testType) {
var r = new RegExp(testType);
// If we are a known type and the type is on the last part of the filename remove it
if (type === testType && r.test(fileParts[fileParts.length-1])) {
shouldPop = true;
}
});
if (shouldPop) {
fileParts.pop();
}
filename = fileParts.join('-');
newFilePath = TypedExport.pluralizeType(type) + "/" + filename + ".js";
}
// Check to see if we are colliding with previous export filenames
// TODO(Tony) - className is null if we are not actually trying to export and
// therefore we want it to map to an existing filename
if (className && newFilePath in exportFiles) {
if (type === "unknown") {
var splitPath = filePath.split("/");
var filename = TypedExport.convertToOutputFilename(className);
splitPath[splitPath.length - 1] = filename + ".js";
newFilePath = splitPath.join("/");
}
if (newFilePath in exportFiles) {
// In the rare case that we have a className, i.e., we were trying to put
// something on the global App namespace, but it is going to be same to
// the same split file (probably have same dasherized name, e.g., dupName,
// and DupName).
var nameWithoutExt = newFilePath.slice(0,newFilePath.indexOf(".js"));
newFilePath = nameWithoutExt + "-x.js";
}
// Don't know why we would have more than two classNames trying to map to the same export file
if (newFilePath in exportFiles) {
console.log('Bad things happening, multiple redundant global export for className:', className);
}
}
return newFilePath;
}
module.exports = TypedExport;