-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
451 lines (422 loc) · 15.3 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
const { validateSchema } = require("webpack");
const schema = require("./options.json");
const fs = require("fs");
const { v4: uuidv4 } = require("uuid");
/**
* getFileScripts function will return the script object of the
* file for the meta json file
*
* @param {object} fileObject: Object having name and description info of file
* fileObject => {file: "setup.js", description: "Setup the user"}
*
* @return {string}
*
*/
function getFileScripts(fileObject) {
let fileWithoutExt = fileObject.fileName.replace(/\.[^/.]+$/, "");
return `{
"name": "${fileWithoutExt}",
"file": "${fileObject.fileName}",
"type": "action",
"description": "${fileObject.description}"
}`;
}
/**
* getFolderScripts function will return the script object of
* the folder for the meta json file
* @param {object} folderObject Object having name and description info of the folder
*
* folderObject => {folderName: "setup.js", description: "Setup the user"}
*
* @return {string}
*/
function getFolderScripts(folderObject) {
return `{
"name": "${folderObject.folderName}",
"file": "${folderObject.folderName}",
"type": "folder",
"description": "${folderObject.description}"
}`;
}
/**
* getMetaScripts function will return a complete script array
* including all files and folders information
*
* @param {Array<object>} dirFiles Array of file objects [{file: string, description: string}]
* @param {Array<object>} dirFolders Array of folder objects [{folderName: string, description: string}]
*
* @return {string}
*/
function getMetaScripts(dirFiles = [], dirFolders = []) {
let scripts = [];
dirFiles
.filter(fileObject => !fileObject.fileName.includes('.json'))
.forEach(fileObject => {
scripts.push(getFileScripts(fileObject));
});
dirFolders.forEach(folderObject => {
scripts.push(getFolderScripts(folderObject));
});
let scriptContent = `[`;
scripts.forEach(script => {
scriptContent += script + ',';
});
scriptContent = scriptContent.slice(0, -1); // removing ',' from the last item
scriptContent += ']';
return scriptContent;
}
/**
* getMetaJsonContent function will return complete meta json content to write into the file
*
* @param {Array<object>} dirFiles Array of file objects [{file: string, description: string}]
* @param {Array<object>} dirFolders Array of folder objects [{folderName: string, description: string}]
* @param {string} dirName Parent folder name
* @param {string} dirDesc Parent folder description
* @param {string} defaultAction Folder default action to be executed
* @param {string} icon URL of the icon to be used
*
* @return {string}
*
*/
function getMetaJsonContent(dirFiles, dirFolders, dirName, dirDesc, defaultAction, icon, sites) {
let scriptsContent = getMetaScripts(dirFiles, dirFolders);
let content = `{
"name": "${dirName}",
"description": "${dirDesc}",
"defaultAction": "${defaultAction}",`;
if (icon) {
content = `${content}
"icon": "${icon}",`;
}
return `${content}
"scripts": ${scriptsContent}
}`;
}
/**
* generateMetaJson function will return completeMetaJsonInfo Array with meta json file info
* (path: where to create the meta.json file, content: the content of meta.json file)
* This function uses the concept of recursion to explore all the inner folders.
*
* @param {object} dirObject Object containing all the info of directories anad files
* @param {string} dirName Directory Name to explore
* @param {Array<object>} completeMetaJsonInfo Array to be updated with meta json files information
*
* dirObject = {
* files: [Array<string>],
* folders: {folderName: <object>},
* parentPath: "/",
* description: string,
* defaultAction: "",
* icon: packageIcon,
* sites: sites
* }
*
* completeMetaJsonInfo = [{path: 'meta.json path', content: 'meta.json file content'}]
*
* @return {Array<object>} Array of meta.json file data (location, file content)
*/
function generateMetaJson(dirObject, dirName) {
let completeMetaJsonInfo = [];
// terminating condition for recursive function
if(!dirObject.files && !dirObject.folders) {
return completeMetaJsonInfo;
}
let folderObjects = Object.keys(dirObject.folders).map(folderName => {
return {
folderName,
description: dirObject.folders[folderName]["description"]
}
});
let fileObjects = dirObject.files;
let path = dirObject.parentPath;
let dirDesc = dirObject.description;
let icon = dirObject.icon;
let sites = dirObject.sites;
let defaultAction = dirObject.defaultAction;
let metaJsonContent = getMetaJsonContent(fileObjects, folderObjects, dirName, dirDesc, defaultAction, icon, sites);
let metaJsonObject = {
path,
content: metaJsonContent
}
completeMetaJsonInfo.push(metaJsonObject);
while(folderObjects.length > 0) {
let currentFolderObject = folderObjects.pop();
let metaInfo = generateMetaJson(dirObject.folders[currentFolderObject.folderName], currentFolderObject.folderName);
completeMetaJsonInfo = [...completeMetaJsonInfo, ...metaInfo];
}
return completeMetaJsonInfo;
}
/**
* generateDirectoryObject function will build and return the complete directory object
* having all the information of internal files, folders, and their description
*
* @param {object} compilationAssestsObject compilation.assets object of webpack
* @param {string} packageDescription description of the package
* @param {string} packageIcon Icon URL for package
* @param {string} folderIcon Icon URL for all folders inside the package
* @param {Array} sites Array of package sites
* @param {object} folderInfo object having folder information (path, description, iconPath)
* @param {string} environment webpack mode in which it is executed
*
* @return {object}
*
*/
function generateDirectoryObject(compilationAssestsObject, packageDescription, packageIcon, sites, folderInfo, environment) {
var directoryObject = {
files: [],
folders: {},
parentPath: "/",
description: packageDescription,
defaultAction: "",
icon: packageIcon,
sites: sites
};
for (var filename in compilationAssestsObject) {
if(!isValidFile(compilationAssestsObject, filename)) {
continue;
}
let content = compilationAssestsObject[filename].source();
let fileDesc = getFileDescription(content, environment);
let directory = directoryObject;
let path = filename.split("/");
let file = path.pop();
if(path.length > 0 && path[0] === '') {
path.shift();
}
let visitedPaths = ['']; // to build visited complete path and check if its description is available
while(path.length > 0) {
let folderDesc = "";
let folderIcon = "";
let folderDefaultAction = "";
let folder = path.shift();
let parent = directory.parentPath;
directory = directory.folders;
visitedPaths.push(folder);
if(!directory[folder]) {
let joinedPath = visitedPaths.join("/"); // building the visited path and checking if the description is available
if(folderInfo && folderInfo[joinedPath]) {
folderDefaultAction = folderInfo[joinedPath]["defaultAction"];
folderDesc = folderInfo[joinedPath]["description"];
folderIcon = folderInfo[joinedPath]["distIconPath"];
}
directory[folder] = {
files: [],
folders: {},
parentPath: `${parent}${folder}/`,
description: folderDesc,
defaultAction: folderDefaultAction ? folderDefaultAction : "",
icon: folderIcon
}
}
directory = directory[folder];
}
if(directory && directory.files) {
directory.files.push({
fileName: file,
description: fileDesc
});
}
}
return directoryObject;
}
/**
* isValidFile function checks whether the file and content is valid or not to be added
* in meta.json file
* Validity measures -
* should not be a .json file and should not include @ignore in the comments of the script
*
* @param {object} compilationAssestsObject compilation.assets object of webpack
* @param {string} fileName
*
* @return {boolean}
*/
function isValidFile(compilationAssestsObject, fileName) {
const invalidFilesRegex = /\.(gif|svg|jpe?g|png|PNG|json)$/
if(invalidFilesRegex.test(fileName)) {
return false;
}
let content = compilationAssestsObject[fileName].source();
let ignoreContent = content.split("\n").filter(line => line.includes("@ignore"));
return ignoreContent.length === 0;
}
/**
* getFileDescription function extracts the description from file content
* based on the environment (development | production)
*
* @param {string} fileContent : Content of minified file from webpack
* @param {string} environment: webpack mode in which is executed
*
* @return {string}
*/
function getFileDescription(fileContent, environment) {
let description = "";
let descriptionComment = fileContent.split("\n").filter(line => line.includes("@description"));
if(environment === "production") {
description = descriptionComment.length > 0 ? descriptionComment[0].split("@description").pop() : "";
} else {
if(descriptionComment.length > 0) {
let descriptionToken = descriptionComment[0].split("\\n").filter(token => token.includes("@description")).map(val => val.replace("\\r", ""));
description = descriptionToken.length > 0 ? descriptionToken[0].split("@description").pop() : "";
}
}
return description.trim();
}
/**
* getLocalIconData funciton extracts the actual contents of the icon file
* and generates the updated path of the icon w.r.t. dist folder and adds
* UUID with each file name.
*
* @param {string} compilerContext it is the webpacks compiler.context string which has
* the info of absolute path of the root directory
* @param {string} relativeIconPath relative path of the icon
*
* @returns {object} an object containing icon path w.r.t. dist/ folder
* example: {
* path: "icon path relative to dist/ folder"
* content: "BUFFER conatining actual content of icon file"
* }
*/
function getLocalIconData(compilerContext, relativeIconPath) {
const absoluteIconPath = `${compilerContext}/${relativeIconPath}`;
const fileContents = fs.readFileSync(absoluteIconPath);
// get an array of icon name and extension to append UUID
const iconFile = relativeIconPath.split("/").slice(-1).pop().split("."); // ["icon", "ext"]
const iconFileName = iconFile[0] + '-' + uuidv4() + `.${iconFile[1]}`
const writePath = `icons/${iconFileName}`;
return {
path: writePath,
content: fileContents
};
}
/**
* getFolderIconsInfo function will return information of all local icons that
* are mentioned in folderDescriptionList and update the respective iconPath
* w.r.t. dist directory
* @param {Array<object>} folderDescriptionList Array of folder descriptors object
* example: {
* path: relative path to the folder w.r.t. src/actions
* description: folder description
* iconPath: path of icon images used for folder w.r.t. root directory
* }
* @param {string} compilerContext it is the webpack's compiler.context string which has
* the info of absolute path of the root directory
*
* @returns {Array<object>} this function will return array of folder icon objects
* whose structure looks like this:
* [
* {
* path: the path of the icon relative to the dist directory
* content: icon images raw contents
* }
* ]
*/
function getFolderIconsInfo(folderDescriptionList, compilerContext) {
const folderIconData = [];
const httpUrlRegex = /(http(s?)):\/\//i; // to ignore the http | https icon urls
folderDescriptionList
.filter(folder => folder.iconPath && !httpUrlRegex.test(folder.iconPath))
.forEach(folderData => {
const iconDataObject = getLocalIconData(compilerContext, folderData.iconPath);
// add dist icon path for meta.json file w.r.t. dist directory
folderData["distIconPath"] = iconDataObject.path;
folderIconData.push(iconDataObject);
});
return folderIconData;
}
class WBMetaJsonGeneratorPlugin {
constructor(options = {}) {
validateSchema(schema, options, {
name: "WB MetaJson Generator",
baseDataPath: "options"
});
this.defaultEnv = "production";
this.environment = options.environment || this.defaultEnv;
this.package = options.package;
this.packageDescription = options.packageDescription;
this.packageIcon = options.packageIcon;
this.readmeFile = options.readmeFile;
this.sites = options.sites;
this.folderDescriptionList = options.folderDescriptionList;
}
apply(compiler) {
// emit is an asynchronous hook, tapping into it using tapAsync, you can use tapPromise/tap(synchronous) as well
compiler.hooks.emit.tapAsync('WBMetaJsonGeneratorPlugin', (compilation, callback) => {
// Loop through all compiled assets,
// adding a new line item for each filename.
let localIconInfo = [];
let folderInfo = {};
let distPackageIcon = this.packageIcon;
if(this.folderDescriptionList) {
// get all folder icons info (path of icon file, the content of icon file)
localIconInfo = getFolderIconsInfo(this.folderDescriptionList, compiler.context);
// creating folder info object with the path of a folder as property for faster lookup
this.folderDescriptionList.forEach(folder => {
folderInfo[folder.path] = folder;
});
}
// check if the package icon is remote URL or a local one
// if it is local then we copy the content of the icon file and update
// the distPackageIcon path w.r.t. dist/icons directory
const httpUrlRegex = /(http(s?)):\/\//i; // to ignore the http | https icon urls
if(distPackageIcon && !httpUrlRegex.test(distPackageIcon)) {
const iconDataObject = getLocalIconData(compiler.context, this.packageIcon);
// update icon path for meta.json file w.r.t. dist directory
distPackageIcon = iconDataObject.path;
localIconInfo.push(iconDataObject);
}
var directoryObject = generateDirectoryObject(compilation.assets, this.packageDescription, distPackageIcon,
this.sites, folderInfo, this.environment);
// get all meta.json files info (path: where to create, content: meta.json content)
var completeMetaJsonInfo = generateMetaJson(directoryObject, this.package);
// writing meta.json files in their respective locations in the dist folder
completeMetaJsonInfo.forEach(metaInfo => {
compilation.assets[`${metaInfo.path}/meta.json`] = {
source: function() {
return metaInfo.content;
},
size: function() {
return metaInfo.content.length;
},
};
});
// before copying the icon files make sure there should not be any
// existing image files in the dist folder if there is then delete
// because icon name will always be unique and whenever
// the build happens icon files will be accumulating
const distIconsDirPath = `${compiler.context}/dist/icons`;
if(fs.existsSync(distIconsDirPath)) {
fs.rmdirSync(distIconsDirPath, {recursive: true});
}
// copying file content in their respective predefined location
// dist/icons/ directory
localIconInfo.forEach(folderIcon => {
compilation.assets[`${folderIcon.path}`] = {
source: function() {
return folderIcon.content;
},
size: function() {
return folderIcon.content.size;
}
}
});
// Check if the readme file path exists or not and if the path is
// there read the contents of the README.md file that the user has
// passed and copy the contents to dist/ folder with
// filename as "README.md"
if(this.readmeFile) {
const absoluteReadmePath = `${compiler.context}/${this.readmeFile}`;
const readmeContent = fs.readFileSync(absoluteReadmePath);
compilation.assets['/README.md'] = {
source: function() {
return readmeContent;
},
size: function() {
return readmeContent.length;
},
};
}
callback();
});
}
}
module.exports = WBMetaJsonGeneratorPlugin;