@@ -24,9 +24,10 @@ exports.main = function(args, callback) {
24
24
name : "n" ,
25
25
out : "o" ,
26
26
main : "m" ,
27
- global : "g"
27
+ global : "g" ,
28
+ import : "i"
28
29
} ,
29
- string : [ "name" , "out" , "global" ] ,
30
+ string : [ "name" , "out" , "global" , "import" ] ,
30
31
boolean : [ "comments" , "main" ] ,
31
32
default : {
32
33
comments : true ,
@@ -49,6 +50,8 @@ exports.main = function(args, callback) {
49
50
"" ,
50
51
" -g, --global Name of the global object in browser environments, if any." ,
51
52
"" ,
53
+ " -i, --import Comma delimited list of imports. Local names will equal camelCase of the basename." ,
54
+ "" ,
52
55
" --no-comments Does not output any JSDoc comments." ,
53
56
"" ,
54
57
chalk . bold . gray ( " Internal flags:" ) ,
@@ -135,18 +138,39 @@ exports.main = function(args, callback) {
135
138
return undefined ;
136
139
} ) ;
137
140
141
+ function getImportName ( importItem ) {
142
+ var result = path . basename ( importItem , ".js" )
143
+ return result . replace ( / ( [ - _ ~ . + ] \w ) / g, match => {
144
+ return match [ 1 ] . toUpperCase ( ) ;
145
+ } ) ;
146
+ }
147
+
138
148
function finish ( ) {
139
149
var output = [ ] ;
140
150
if ( argv . global )
141
151
output . push (
142
152
"export as namespace " + argv . global + ";" ,
143
153
""
144
154
) ;
145
- if ( ! argv . main )
146
- output . push (
147
- "import * as $protobuf from \"protobufjs\";" ,
148
- ""
149
- ) ;
155
+
156
+ if ( ! argv . main ) {
157
+ // Ensure we have a usable array of imports
158
+ var importArray = typeof argv . import === "string" ? argv . import . split ( "," ) : argv . import || [ ] ;
159
+
160
+ // Build an object of imports and paths
161
+ var imports = {
162
+ $protobuf : "protobufjs"
163
+ } ;
164
+ importArray . forEach ( function ( importItem ) {
165
+ imports [ getImportName ( importItem ) ] = importItem ;
166
+ } ) ;
167
+
168
+ // Write out the imports
169
+ Object . keys ( imports ) . forEach ( function ( key ) {
170
+ output . push ( "import * as " + key + " from \"" + imports [ key ] + "\";" ) ;
171
+ } ) ;
172
+ }
173
+
150
174
output = output . join ( "\n" ) + "\n" + out . join ( "" ) ;
151
175
152
176
try {
0 commit comments