Skip to content

Commit 9fceaa6

Browse files
thegeckodcodeIO
authored andcommitted
CLI: Added an option to pbts to allow custom imports (#1038)
1 parent 996b3fa commit 9fceaa6

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

cli/pbts.js

+31-7
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ exports.main = function(args, callback) {
2424
name: "n",
2525
out : "o",
2626
main: "m",
27-
global: "g"
27+
global: "g",
28+
import: "i"
2829
},
29-
string: [ "name", "out", "global" ],
30+
string: [ "name", "out", "global", "import" ],
3031
boolean: [ "comments", "main" ],
3132
default: {
3233
comments: true,
@@ -49,6 +50,8 @@ exports.main = function(args, callback) {
4950
"",
5051
" -g, --global Name of the global object in browser environments, if any.",
5152
"",
53+
" -i, --import Comma delimited list of imports. Local names will equal camelCase of the basename.",
54+
"",
5255
" --no-comments Does not output any JSDoc comments.",
5356
"",
5457
chalk.bold.gray(" Internal flags:"),
@@ -135,18 +138,39 @@ exports.main = function(args, callback) {
135138
return undefined;
136139
});
137140

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+
138148
function finish() {
139149
var output = [];
140150
if (argv.global)
141151
output.push(
142152
"export as namespace " + argv.global + ";",
143153
""
144154
);
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+
150174
output = output.join("\n") + "\n" + out.join("");
151175

152176
try {

0 commit comments

Comments
 (0)