1- const file = require ( '@cocreate/file' )
2- module . exports = file ( )
1+ const extract = require ( 'extract-comments' ) ;
2+ const fs = require ( 'fs' ) ;
3+ const path = require ( "path" )
4+ const glob = require ( 'glob' ) ;
5+ const parseHtmlComments = require ( 'parse-html-comments' )
6+ const crud = require ( '@cocreate/crud' )
7+ const { config } = require ( '@cocreate/cli' )
8+
9+ /**
10+ * "extract": {
11+ "directory": "./src/",
12+ "extensions": [
13+ "js",
14+ "css",
15+ "html"
16+ ],
17+ "ignores": [
18+ "node_modules",
19+ "vendor",
20+ "bower_components",
21+ "archive"
22+ ]
23+ }
24+
25+ */
26+ class ExtractComment {
27+ constructor ( ) {
28+
29+ }
30+
31+ run ( filePath , collection , name ) {
32+ let content = fs . readFileSync ( filePath , 'utf8' ) ;
33+ let extension = path . extname ( filePath )
34+
35+ let comments = [ ] ;
36+ let docItems = [ ] ;
37+
38+ if ( extension == '.html' ) {
39+ comments = this . extractHtml ( content )
40+ } else {
41+ comments = extract ( content )
42+ }
43+
44+ comments . forEach ( ( { value } ) => {
45+ let ret_value = this . extractValue ( value )
46+ if ( ret_value ) {
47+ docItems . push ( {
48+ collection,
49+ data : {
50+ [ name ] : ret_value ,
51+ extension,
52+ file_path : filePath
53+ } ,
54+ metaData : filePath
55+ } )
56+ }
57+ } )
58+ return docItems ;
59+ }
60+
61+ extractValue ( valueStr ) {
62+ var regResult = / @ v a l u e _ s t a r t (?< value > .* ?) @ v a l u e _ e n d / gs. exec ( valueStr ) ;
63+ if ( regResult ) {
64+ return regResult . groups . value . trim ( )
65+ } else {
66+ return null
67+ }
68+ }
69+
70+ extractHtml ( content ) {
71+ let htmlComments = parseHtmlComments ( content )
72+ let result_comment = [ ] ;
73+
74+ htmlComments . matches . forEach ( ( { groups } ) => {
75+ let comment_value = groups . commentOnly ;
76+ comment_value = comment_value . replace ( / < ! - - | - - > / gs, '' ) ;
77+
78+ result_comment . push ( {
79+ value : comment_value
80+ } )
81+ } )
82+ return result_comment ;
83+ }
84+ }
85+
86+ const extractInstance = new ExtractComment ( )
87+
88+ function CoCreateExtract ( directory , ignoreFolders , extensions ) {
89+ let extensionsStr = "*" ;
90+ let ignoreFolderStr = "" ;
91+
92+ if ( extensions && extensions . length > 0 ) {
93+ extensionsStr = extensions . join ( ',' ) ;
94+ }
95+
96+ if ( ignoreFolders && ignoreFolders . length > 0 ) {
97+ ignoreFolderStr = ignoreFolders . join ( '|' ) ;
98+ }
99+
100+ let result = [ ] ;
101+
102+ if ( ! directory ) {
103+ directory = "." ;
104+ }
105+
106+ const files = glob . sync ( directory + `/**/*.{${ extensionsStr } }` , { } ) ;
107+
108+ files . forEach ( ( file ) => {
109+ var regex = new RegExp ( ignoreFolderStr , 'g' ) ;
110+ if ( ! regex . test ( file ) ) {
111+ const docData = extractInstance . run ( file , 'docs' , 'description' ) ;
112+ if ( docData . length > 0 ) {
113+ const fileName = path . basename ( file ) ;
114+ result . push ( docData )
115+ }
116+ }
117+ } )
118+
119+ return result ;
120+ }
121+
122+
123+
124+ module . exports = CoCreateExtract
125+
0 commit comments