1
1
var fs = require ( 'fs' ) ;
2
+ var async = require ( 'async' ) ;
2
3
3
- fs . writeFile (
4
- 'README.md' , ' # sublime-react\n\n' +
4
+ var DOCUMENTATION_TOP =
5
+ '# sublime-react\n\n' +
5
6
'Snippets and syntax highlighting for React.js / JSX.\n\n' +
6
7
'\n\n' +
7
8
'## Installation\n\n' +
@@ -21,38 +22,70 @@ fs.writeFile(
21
22
'It\'s easy! Simply activate snippets by typing a mnemonic followed by TAB.\n\n' +
22
23
'\n\n' +
23
24
'#### Available snippets:\n\n' +
24
- '```\n' , function ( err ) {
25
- if ( err ) {
26
- throw err ;
27
- }
28
- } ) ;
25
+ '```\n' ;
26
+
27
+ var DOCUMENTATION_BOTTOM =
28
+ '```\n\n' +
29
+ '## Contributing\n\n' +
30
+ '### Rebuilding the docs\n\n' +
31
+ 'After making changes to snippet files, run `npm install && npm run build-docs` to automatically generate this document from source.\n\n' ;
29
32
30
33
fs . readdir ( './snippets' , function ( err , files ) {
31
- files
32
- . filter ( function ( file ) {
33
- return file . substr ( - 16 ) === '.sublime-snippet' ;
34
- } )
35
- . forEach ( function ( file ) {
36
- fs . readFile ( './snippets/' + file , 'utf-8' , function ( err , contents ) {
37
- if ( ! err ) {
38
- inspectFile ( contents ) ;
39
- }
40
- } ) ;
34
+ var snippets = files . filter ( function ( file ) {
35
+ return file . substr ( - 16 ) === '.sublime-snippet' ;
36
+ } ) . map ( function ( file ) {
37
+ return './snippets/' + file ;
38
+ } ) ;
39
+ async . map ( snippets , readAndInspect , function ( err , results ) {
40
+ if ( err ) {
41
+ console . error ( 'error mapping snippets' , err ) ;
42
+ }
43
+ var snippetDocs =
44
+ DOCUMENTATION_TOP +
45
+ results . map ( function ( snippet ) {
46
+ return inspectFile ( snippet ) ;
47
+ } ) . sort ( function ( a , b ) {
48
+ return a . abbreviation > b . abbreviation
49
+ ? 1
50
+ : a . abbreviation === b . abbreviation
51
+ ? 0
52
+ : - 1 ;
53
+ } ) . map ( function ( snippet ) {
54
+ return snippet . docBlock ;
55
+ } ) . join ( '' ) +
56
+ DOCUMENTATION_BOTTOM ;
57
+ fs . writeFile ( 'README.md' , snippetDocs , function ( err ) {
58
+ if ( err ) {
59
+ console . error ( 'error appending README:' , err ) ;
60
+ }
41
61
} ) ;
62
+ } ) ;
42
63
} ) ;
43
64
65
+ function readAndInspect ( fileName , cb ) {
66
+ fs . readFile ( fileName , 'utf-8' , function ( err , contents ) {
67
+ if ( ! err ) {
68
+ cb ( null , contents ) ;
69
+ }
70
+ } ) ;
71
+ }
72
+
44
73
function inspectFile ( contents ) {
45
74
var match = contents . match (
46
75
/ [ \s \S ] * < t a b T r i g g e r > ( .* ?) < \/ t a b T r i g g e r > [ \s \S ] * ?< d e s c r i p t i o n > ( R e a c t : ) ? ( .* ?) < \/ d e s c r i p t i o n > [ \s \S ] * / i
47
76
) ;
48
77
var docBlock = '' ;
78
+ var abbreviation = '' ;
79
+ var description = '' ;
49
80
if ( match !== null ) {
50
- var shortCut = ' ' . substring ( 0 , 5 - match [ 1 ] . length ) + match [ 1 ] ;
51
- docBlock = ' ' + shortCut + '→ ' + match [ 3 ] + '\n\n' ;
81
+ abbreviation = match [ 1 ] ;
82
+ description = match [ 3 ] ;
83
+ var shortCut = ' ' . substring ( 0 , 5 - abbreviation . length ) + abbreviation ;
84
+ docBlock = ' ' + shortCut + '→ ' + description + '\n\n' ;
52
85
}
53
- fs . appendFile ( 'README.md' , docBlock , function ( err ) {
54
- if ( err ) {
55
- console . error ( 'error appending README:' , err ) ;
56
- }
57
- } ) ;
86
+ return {
87
+ docBlock : docBlock ,
88
+ abbreviation : abbreviation ,
89
+ description : description
90
+ } ;
58
91
}
0 commit comments