Skip to content

Commit

Permalink
CLI: Added an option to pbts to allow custom imports (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
thegecko authored and dcodeIO committed May 18, 2018
1 parent 996b3fa commit 9fceaa6
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions cli/pbts.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ exports.main = function(args, callback) {
name: "n",
out : "o",
main: "m",
global: "g"
global: "g",
import: "i"
},
string: [ "name", "out", "global" ],
string: [ "name", "out", "global", "import" ],
boolean: [ "comments", "main" ],
default: {
comments: true,
Expand All @@ -49,6 +50,8 @@ exports.main = function(args, callback) {
"",
" -g, --global Name of the global object in browser environments, if any.",
"",
" -i, --import Comma delimited list of imports. Local names will equal camelCase of the basename.",
"",
" --no-comments Does not output any JSDoc comments.",
"",
chalk.bold.gray(" Internal flags:"),
Expand Down Expand Up @@ -135,18 +138,39 @@ exports.main = function(args, callback) {
return undefined;
});

function getImportName(importItem) {
var result = path.basename(importItem, ".js")
return result.replace(/([-_~.+]\w)/g, match => {
return match[1].toUpperCase();
});
}

function finish() {
var output = [];
if (argv.global)
output.push(
"export as namespace " + argv.global + ";",
""
);
if (!argv.main)
output.push(
"import * as $protobuf from \"protobufjs\";",
""
);

if (!argv.main) {
// Ensure we have a usable array of imports
var importArray = typeof argv.import === "string" ? argv.import.split(",") : argv.import || [];

// Build an object of imports and paths
var imports = {
$protobuf: "protobufjs"
};
importArray.forEach(function(importItem) {
imports[getImportName(importItem)] = importItem;
});

// Write out the imports
Object.keys(imports).forEach(function(key) {
output.push("import * as " + key + " from \"" + imports[key] + "\";");
});
}

output = output.join("\n") + "\n" + out.join("");

try {
Expand Down

0 comments on commit 9fceaa6

Please sign in to comment.