Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom pbts type imports #1038

Merged
merged 1 commit into from
May 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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