-
-
Notifications
You must be signed in to change notification settings - Fork 813
/
Copy pathbuild.js
156 lines (126 loc) · 3.63 KB
/
build.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
/* eslint-disable no-sync */
'use strict';
// MODULES //
var logger = require( 'debug' );
var writeFile = require( 'fs' ).writeFileSync;
var resolvePath = require( 'path' ).resolve;
var getKeys = require( 'object-keys' ).shim();
var replace = require( '@stdlib/string/replace' );
var docs = require( '@stdlib/repl/help' );
// VARIABLES //
var debug = logger( 'repl:examples:build' );
// Examples output file path:
var EXAMPLES_OUTPUT = resolvePath( __dirname, '..', 'lib', 'db.js' );
// Output file preamble:
var PREAMBLE = '// This file is generated by scripts/build.js.\n\'use strict\';\n\n/* eslint-disable quotes, max-lines */\n\n';
// Identifier for inserting 'See Also' links:
var SEE_ALSO = '\n See Also\n --------\n';
// Identifier for an examples section:
var EXAMPLES = ' Examples';
// Identifier for an examples heading underline:
var EXAMPLES_UNDERLINE = ' --------';
// Regular expression for a line that starts with a space:
var RE_SPACE = /^\s/;
// Regular expression for a command prompt:
var RE_PROMPT = /^(\s{4}>\s*)/;
// Regular expression for a command continuation:
var RE_CONTINUATION = /^(\s{4}\.{3}\s)/;
// Regular expression for command input:
var RE_COMMAND_INPUT = /^(\s{4}(?:>|\.{3})\s*)/;
// Regular expression for a comment:
var RE_COMMENT = /^(\s{4}\/\/.+)/;
// Regular expression for indentation:
var RE_IDENTATION = /^(\s{4})/;
// Regular expression for indentation (global):
var RE_IDENTATION_GLOBAL = /^(\s{4})/g;
// Regular expression for variable declaration:
var RE_VAR_DECLARATION = /^(var\s+)/;
// FUNCTIONS //
/**
* Generates a hash for REPL examples.
*
* @private
* @param {Object} docs - hash containing REPL help text
*/
function createExamples( docs ) {
var aliases;
var alias;
var fopts;
var lines;
var line;
var acc;
var txt;
var out;
var flg;
var i;
var j;
fopts = {
'encoding': 'utf8'
};
out = {};
aliases = getKeys( docs );
for ( i = 0; i < aliases.length; i++ ) {
alias = aliases[ i ];
debug( 'Loading REPL text: %s', alias );
txt = docs[ alias ];
debug( 'Extracting examples section.' );
lines = txt.split( '\n' );
flg = false;
acc = '';
// NOTE: the following only extracts the first example!!!
for ( j = 0; j < lines.length; j++ ) {
line = lines[ j ];
if (
line === EXAMPLES &&
lines[ j+1 ] === EXAMPLES_UNDERLINE
) {
debug( 'Found an examples section.' );
flg = true;
acc = '';
j += 1; // skip the next line
continue;
}
if ( flg === false ) {
continue;
}
if (
line !== SEE_ALSO &&
RE_SPACE.test( line )
) {
if ( RE_COMMAND_INPUT.test( line ) ) {
line = replace( line, RE_PROMPT, '' );
line = replace( line, RE_CONTINUATION, '' );
line = replace( line, RE_IDENTATION_GLOBAL, ' ' );
line = replace( line, RE_VAR_DECLARATION, '' ); // NOTE: the REPL silences output when declaring a variable. So we remove any variable declarations here.
acc += line + '\n';
} else if ( RE_COMMENT.test( line ) ) {
line = replace( line, RE_IDENTATION, '' );
acc += '\n' + line + '\n';
}
} else if ( line !== '' ) {
debug( 'Finished extracting section.' );
flg = false;
break;
}
}
if ( acc === '' ) {
debug( 'Unable to extract an examples section.' );
} else {
out[ alias ] = acc;
}
}
debug( 'Writing REPL example hash to file.' );
out = PREAMBLE+'module.exports = '+JSON.stringify( out, null, '\t' )+';\n';
writeFile( EXAMPLES_OUTPUT, out, fopts );
} // end FUNCTION createExamples()
/**
* Main execution sequence.
*
* @private
*/
function main() {
debug( 'Generating REPL examples.' );
createExamples( docs() );
} // end FUNCTION main()
// MAIN //
main();