-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
267 lines (231 loc) · 6.91 KB
/
index.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
let walk = require('walk');
let path = require('path');
let fs = require('fs');
let Output = require('./src/js/Output');
const pluginName = 'TypescriptClassMetaInfoGeneratorPlugin';
const extName = '.ts';
const encoding = 'utf8';
/**
* Webpack plugin that generates a container object and associates the class name with its reference for easy access
*
* @version 1.0.6
*/
class TypescriptClassMetaInfoGeneratorPlugin {
/**
* @param options - Object properties below
*
* srcFolder: string - The directory with ts source files you want walked
* [Default]: ./src/ts
* siteName: string - The class name / namespace you want to use for your container name
* [Default]: Site
* siteMetaFileName: string - The file name that holds all the meta information about the classes.
* [Default]: site-meta.ts
* siteMetaPath: string - The location the meta file should be generated.
* [Default]: .
* importPath: string - If siteMetaPath is not the root sometimes the import path needs to change
* [Default]: ''
* ignoreFiles: string[] - List of file names (case-sensitive) without the ts extension.
* [Default]: []
* ignoreFolders: string[] - List of folder names (case-sensitive) to ignore.
*/
constructor(options) {
if(options) {
this.srcFolder = (options.srcFolder) ? options.srcFolder : './src/ts';
this.siteName = (options.siteName) ? options.siteName : 'Site';
this.siteMetaFileName = (options.siteMetaFileName) ? options.siteMetaFileName : 'site-meta.ts';
this.siteMetaPath = (options.siteMetaPath) ? options.siteMetaPath : '.';
this.importPath = (options.importPath) ? options.importPath : '';
this.ignoreFiles = (options.ignoreFiles) ? options.ignoreFiles : [];
this.ignoreFolders = (options.ignoreFolders) ? options.ignoreFolders: [];
}
this.files = [];
this.output = new Output();
}
/**
* Function called by webpack to kick off the plugin. Compiler hook entryOption is called and tap'ed so we can walk
* the typescript files and generate what we need before watch/building
*
* @param compiler
*/
apply(compiler) {
compiler.hooks.entryOption.tap(pluginName, this.walkAndMake.bind(this));
}
/**
* Walks the defined src folder and gets a file list based on files not in the ignore folders or files list. Once
* the list has been gathered we export that data into a TS form where the classes are associated with their string
* names and added to a container object to be accessed within the project scope
*/
walkAndMake() {
// Walker options
let walker = walk.walk(this.srcFolder, {
followLinks: false,
filters: this.ignoreFolders
});
walker.on('file', (root, stat, next) => {
if(path.extname(stat.name) === extName) {
let nameWithoutExt = stat.name.substr(0, stat.name.length - extName.length);
let inIgnore = this.ignoreFiles.indexOf(nameWithoutExt) >= 0;
if(!inIgnore) {
// Add this file to the list of files
this.files.push({
name: nameWithoutExt,
path: root + `/${nameWithoutExt}`
});
}
}
next();
});
walker.on('end', () => {
this.output.container = `\nexport let ${this.siteName}: any = {};\n`;
this.files.forEach(file => {
this.output.imports.push(`import { ${file.name} } from "${this.createImportPath(file.path)}";\n`);
this.output.associations.push(`\n${this.siteName}.${file.name} = ${file.name};`);
});
fs.writeFileSync(this.siteMetaFullPath, this.output.combined(), encoding);
});
}
/**
* Gets the absolute path to file and removes the srcFolder path part to leave just the relative path to the file.
* Once obtained it appends it to the specified importPath
*
* @param {string} filePath
*
* @returns {string}
*/
createImportPath(filePath) {
let importPath = filePath;
if(this.importPath !== "") {
let relativePath = this.arrayDif(filePath.split(path.sep), this.srcFolder.split(path.sep)).join(path.sep);
importPath = `${this.importPath}${path.sep}${relativePath}`;
}
return importPath;
}
/**
* Finds the differences from the first array in the second
*
* @param {array} a1
* @param {array} a2
*
* @returns {array}
*/
arrayDif(a1, a2) {
return a1.filter(item => a2.indexOf(item) < 0);
}
/**
* Returns the siteMetaPath joined with the siteMetaFileName
*
* @returns {string}
*/
get siteMetaFullPath() {
return `${this.siteMetaPath}/${this.siteMetaFileName}`;
}
/**
* @returns {string}
*/
get siteName() {
return this._siteName;
}
/**
* @param {string} value
*/
set siteName(value) {
this._siteName = value;
}
/**
* @returns {string}
*/
get srcFolder() {
return this._srcFolder;
}
/**
* @param {string} value
*/
set srcFolder(value) {
this._srcFolder = value;
}
/**
* @returns {string}
*/
get siteMetaPath() {
return this._siteMetaPath;
}
/**
* @param {string} value
*/
set siteMetaPath(value) {
this._siteMetaPath = value;
}
/**
* @returns {string}
*/
get importPath() {
return this._importPath;
}
/**
* @param {string} value
*/
set importPath(value) {
this._importPath = value;
}
/**
* @returns {string}
*/
get siteMetaFileName() {
return this._siteMetaFileName;
}
/**
* @param {string} value
*/
set siteMetaFileName(value) {
this._siteMetaFileName = value;
}
/**
* @returns {string[]}
*/
get ignoreFiles() {
return this._ignoreFiles;
}
/**
* @param {string[]} value
*/
set ignoreFiles(value) {
this._ignoreFiles = value;
}
/**
* @returns {string[]}
*/
get ignoreFolders() {
return this._ignoreFolders;
}
/**
* @param {string[]} value
*/
set ignoreFolders(value) {
this._ignoreFolders = value;
}
/**
* @returns {string[]}
*/
get files() {
return this._files;
}
/**
* @param {string[]} value
*/
set files(value) {
this._files = value;
}
/**
* @returns {Output}
*/
get output() {
return this._output;
}
/**
* @param {Output} value
*/
set output(value) {
this._output = value;
}
}
module.exports = TypescriptClassMetaInfoGeneratorPlugin;