forked from larsvanbraam/block-documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
612 lines (536 loc) · 13.1 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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/*!
* blockDocumentation
* @copyright 2017 Lars van Braam <[email protected]>
* MIT Licensed
*/
'use strict';
/**
* @private
* @description Module dependencies
*/
const fs = require( 'fs-extra' );
const path = require( 'path' );
const upperCamelCase = require( 'uppercamelcase' );
const camelCase = require( 'camelcase' );
const typhen = require( 'typhen' );
const stripExtension = require( 'strip-extension' );
/**
* @private
* @description Default configuration for generating the documentation
*/
const config = {
input: '',
output: '',
jsonFile: 'data.json',
interfaceName: 'I{blockId}Options.ts',
exampleBlockIdLabel: 'componentId',
silent: false,
placeholderValues: {
string: 'Lorem ipsum dolor sit amet',
boolean: true,
number: 1
}
};
/**
* @private
* @description The global result object
* @type {{blocks: Array, references: Array, enums: Array}}
*/
let result;
/**
* @private
* @method generateData
* @returns void
*/
function generateData( options )
{
// Empty the result
result = {blocks: [], references: [], enums: [], stringLiterals:[], files: []};
// Merge the provided config with the default config
Object.assign( config, options );
const inputDirectoryPath = getInputDirectoryPath();
// Check for provided input / output
if( (!inputDirectoryPath && !config.input.files) || !config.output.length )
{
console.error( 'Please provide an input and an output path' );
return;
}
if( inputDirectoryPath )
{
// Parse the input directory path
parseInputDirectoryPath( inputDirectoryPath )
}
// Parse the other files
if( typeof config.input === 'object' )
{
parseInputFiles( config.input.files )
}
if( !config.silent )
{
console.log( '[Info] All blocks have been parsed, writing to file..' );
}
writeOutputFile();
}
/**
* @private
* @method parseInputDirectoryPath
* @param inputDirectoryPath
*/
function parseInputDirectoryPath( inputDirectoryPath )
{
// Get all the directories
const blockDirectories = getDirectories( inputDirectoryPath );
// Loop through all the blocks
blockDirectories.forEach( function( blockDirectory, index )
{
const blockId = blockDirectoryToBlockId( blockDirectory );
const interfacePath = blockDirectoryToInterfacePath( blockDirectory );
const properties = parseBlock( interfacePath ).reverse();
// Log the progress
if( !config.silent )
{
const progress = Math.round( (index + 1) / blockDirectories.length * 100 ) + '%';
console.log( '[' + progress + '] Parsing block with id: ' + blockId );
}
// Store the result
result.blocks.push( {
name: blockId,
properties: properties,
example: JSON.stringify( {
[config.exampleBlockIdLabel]: blockId,
data: generateExampleJSON( properties, {} )
}, null, 4 )
} );
} );
}
/**
* @private
* @method parseInputFiles
* @param inputFiles
*/
function parseInputFiles( inputFiles )
{
var files = [];
// Only one file has been defined
if( typeof inputFiles === 'string' )
{
files.push( inputFiles );
}
// Multiple files have been defined
else if( Array.isArray( inputFiles ) )
{
files = files.concat( inputFiles );
}
files.forEach( function( file, index )
{
const properties = parseBlock( file ).reverse();
const fileName = stripExtension( file.split( '/' ).pop() );
// Log the progress
if( !config.silent )
{
const progress = Math.round( (index + 1) / files.length * 100 ) + '%';
console.log( '[' + progress + '] Parsing file with name: ' + fileName );
}
// Store the result
result.files.push( {
name: fileName,
properties: properties,
example: JSON.stringify( generateExampleJSON( properties, {} ), null, 4 )
} );
} )
}
/**
* @private
* @method generate
* @returns void
*/
function generate( options )
{
// First generate the data
generateData( options );
// Copy the index.html from the src folder to the output folder
fs.copySync( __dirname + '/src/index.html', config.output + 'index.html' );
if( !config.silent )
{
console.log( '[Success] Copied the index.html file to the output directory' );
}
}
/**
* @method generateExampleJSON
* @description This method recursively generated mock data for the types
* @returns {Object}
*/
function generateExampleJSON( properties, base )
{
properties.forEach( function( property )
{
let reference;
if( hasReference( property.type, result.references ) )
{
reference = hasReference( property.type, result.references );
// If we found a reference object, start generating the example JSON
base[property.name] = generateExampleJSON( reference.properties, {} );
}
else if( hasReference( property.type, result.enums ) )
{
reference = hasReference( property.type, result.enums );
// If we found a reference Enum, we always choose the first option as default
base[property.name] = reference.properties[0].value;
}
else if( hasReference( property.type, result.stringLiterals ) )
{
reference = hasReference( property.type, result.stringLiterals );
// If we found a reference Enum, we always choose the first option as default
base[property.name] = reference.properties[0].value;
}
else
{
switch( property.type )
{
case 'string':
{
base[property.name] = property.placeholder || config.placeholderValues.string;
break;
}
case 'boolean':
{
base[property.name] = config.placeholderValues.boolean;
break;
}
case 'number':
{
base[property.name] = config.placeholderValues.number;
break;
}
case 'Object':
{
base[property.name] = generateExampleJSON( property.properties, {} );
break;
}
case 'Array':
{
base[property.name] = [];
base[property.name].push( generateExampleJSON( property.properties, {} ) );
break;
}
default:
{
base[property.name] = 'TODO: ' + property.type;
}
}
}
} );
return base;
}
/**
* @private
* @method parseBlock
* @param path
* @returns {Array}
*/
function parseBlock( path )
{
// Parse the options file with typhen to get all the properties
const typhenResult = typhen.parse( path );
// Find the correct exported module for the file
const typhenTypes = typhenResult.types.find( function( type, index )
{
return type.rawName == path.split( '/' ).pop().split( '.' ).shift();
} );
const properties = typhenTypes.properties || typhenTypes.type.properties;
return parseProperties( properties );
}
/**
* @private
* @method parseProperties
* @param properties
* @returns {Array}
*/
function parseProperties( properties )
{
if( !properties )
{
properties = []
}
// Keep track of the parsed properties
let parsedProperties = [];
// Parse all the properties
properties.forEach( function( property )
{
// If the @ignore comment was added we will skip the property
if( !getDocComment( property.docComment || [], '@ignore' ) )
{
parsedProperties.push( parseProperty( property ) );
}
} );
// Return the parsed properties
return parsedProperties;
}
/**
* @private
* @method parseProperty
* @description Parse the properties and return the new parsed object
* @returns {Object}
*/
function parseProperty( property )
{
let childProperties = null;
if( property.type.rawName === 'Array' )
{
childProperties = property.type.type.properties;
}
else if( property.type.rawName === '' && property.type.properties ) // If the rawName == '' the interface was an object, it's super werid!
{
childProperties = property.type.properties;
}
return {
name: property.rawName,
type: getType( property.type, getDocComment(property.docComment, '@rawName') ),
required: !property.isOptional,
defaultValue: getDocComment( property.docComment, '@defaultValue' ) || 'null',
description: getDocComment( property.docComment, '@description' ),
placeholder: getDocComment( property.docComment, '@placeholder' ),
properties: parseProperties( childProperties )
}
}
/**
* @private
* @method getDocComment
* @description Fetch a desired doc comment based on the @property
* @param docComment
* @param property
*/
function getDocComment( docComment, property )
{
if( Array.isArray( docComment ) )
{
for( let i = 0; i < docComment.length; i++ )
{
if( docComment[i].indexOf( property ) > -1 )
{
return docComment[i].replace( property + ' ', '' ).toString();
}
}
}
// No match was found
return '';
}
/**
* @private
* @method getType
* @param PrimitiveType
* @param rawName
* @description Get type from the type object
*/
function getType( PrimitiveType, rawName )
{
if( PrimitiveType.properties && PrimitiveType.rawName.indexOf( 'I' ) === 0 )
{
parseObjectReference( PrimitiveType.rawName, PrimitiveType.properties );
}
else if( PrimitiveType.members )
{
parseEnumReference( PrimitiveType.rawName, PrimitiveType.members );
}
else if(PrimitiveType.types)
{
PrimitiveType.rawName = rawName
parseStringLiteralReference(rawName, PrimitiveType.types);
}
// No name means it's a custom Object
if( PrimitiveType.rawName === '' )
{
return 'Object';
}
else
{
return PrimitiveType.rawName;
}
}
/**
* @private
* @method hasReference
* @param {string} name
* @param {Array} array
* @returns {boolean}
*/
function hasReference( name, array )
{
return array.find( function( item )
{
return item.name === name
} );
}
/**
* @private
* @method parseReference
* @param {string} name
* @param {Object} properties
* @returns void
*/
function parseObjectReference( name, properties )
{
if( !hasReference( name, result.references ) && Array.isArray( properties ) )
{
// Keep track of the parsed properties
let parsedProperties = [];
// Parse all the properties
properties.forEach( function( property )
{
// If the @ignore comment was added we will skip the property
if( !getDocComment( property.docComment || [], '@ignore' ) )
{
parsedProperties.push( parseProperty( property ) );
}
} );
result.references.push( {
name: name,
properties: parsedProperties
} );
}
}
/**
* @private
* @method parseEnum
* @description Parse an enum reference
* @param {string} name
* @param {Array} members
* @returns void
*/
function parseEnumReference( name, members )
{
if( !hasReference( name, result.enums ) && Array.isArray( members ) )
{
// Keep track of the parsed properties
let parsedProperties = [];
// Parse all the properties
members.forEach( function( member )
{
// If the @ignore comment was added we will skip the property
if( !getDocComment( member.docComment || [], '@ignore' ) )
{
parsedProperties.push( {
name: member.rawName,
value: member.value
} );
}
} );
result.enums.push( {
name: name,
properties: parsedProperties
} );
}
}
/**
* @private
* @method parseStringLiteralReference
* @description Parse a string literal reference
* @param {string} name
* @param {Array} stringLiterals
* @returns void
*/
function parseStringLiteralReference( name, stringLiterals )
{
if( !hasReference( name, result.stringLiterals ) && Array.isArray( stringLiterals ) )
{
// Keep track of the parsed properties
let parsedProperties = [];
// Parse all the properties
stringLiterals.forEach( function( stringLiteral )
{
// If the @ignore comment was added we will skip the property
if( !getDocComment( stringLiteral.docComment || [], '@ignore' ) )
{
// Strip out the " characters
const text = stringLiteral.text.replace(/"/g, "");
parsedProperties.push( {
name: text,
value: text
} );
}
} );
result.stringLiterals.push( {
name: name,
properties: parsedProperties
} );
}
}
/**
* @private
* @writeOutputFile
* @returns void
*/
function writeOutputFile()
{
// Create the output folder if it's not there
if( !fs.existsSync( config.output ) )
{
fs.mkdirSync( config.output );
}
// Write the *.json file
fs.outputJsonSync(
config.output + config.jsonFile,
result,
{
spaces: 4
}
);
if( !config.silent )
{
console.log( '[Success] Writing to file is done! See: ', config.output + config.jsonFile );
}
}
/**
* @private
* @method getDirectories
* @description Get all the folders within a desired folder
* @param {string} src
* @returns {string}
*/
function getDirectories( src )
{
return fs.readdirSync( src ).filter( function( file )
{
return fs.statSync( path.join( src, file ) ).isDirectory();
} );
}
/**
* @private
* @method blockDirectoryToBlockId
* @param blockDirectory
* @description Parse the block directory name to the internally used block id's
* @returns {string}
*/
function blockDirectoryToBlockId( blockDirectory )
{
return camelCase( blockDirectory.split( '-' ).slice( 1 ).join( '-' ) );
}
/**
* @private
* @method blockDirectoryToInterfacePath
* @param blockDirectory
* @returns {string}
*/
function blockDirectoryToInterfacePath( blockDirectory )
{
// Parse the configured with the correct blockId
const fileName = config.interfaceName.replace( '{blockId}', upperCamelCase( blockDirectory ) );
return getInputDirectoryPath() + blockDirectory + '/' + fileName;
}
/**
* @private
* @method getInputDirectoryPath
* @returns {string}
*/
function getInputDirectoryPath()
{
return typeof config.input === 'object' ? config.input.folder : config.input;
}
/**
* @public
* @description Module exports
*/
module.exports = {
generateData: generateData,
generate: generate
};