Skip to content

Commit

Permalink
Other: Also lint cli utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Mar 6, 2017
1 parent 26d9fad commit 6e81fcb
Show file tree
Hide file tree
Showing 35 changed files with 379 additions and 288 deletions.
2 changes: 2 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ engines:
ratings:
paths:
- "src/**.js"
- "cli/**.js"
- "lib/aspromise/**.js"
- "lib/base64/**.js"
- "lib/eventemitter/**.js"
Expand All @@ -14,3 +15,4 @@ ratings:
- "lib/utf8/**.js"
exclude_paths:
- "**/tests/**"
- "cli/wrappers/**"
5 changes: 3 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
bench/*
bin/*
cli/*
cli/wrappers/*
coverage/*
dist/*
docs/*
Expand All @@ -10,6 +10,7 @@ lib/prelude.js
lib/polyfill.js
lib/tape-adapter.js
lib/tsd-jsdoc/*
lib/*/tests/*
sandbox/*
scripts/*
**/tests/*
tests/*
16 changes: 10 additions & 6 deletions cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var targets = util.requireAll("./targets");
* @param {function(?Error)} [callback] Optional completion callback
* @returns {number|undefined} Exit code, if known
*/
exports.main = function(args, callback) {
exports.main = function main(args, callback) {
var lintDefault = "eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins";
var argv = minimist(args, {
alias: {
Expand Down Expand Up @@ -59,16 +59,16 @@ exports.main = function(args, callback) {
return " " + util.pad(key, 14, true) + targets[key].description;
});
if (callback)
callback(Error("usage"));
callback(Error("usage")); // eslint-disable-line callback-return
else
console.error([
process.stderr.write([
"protobuf.js v" + pkg.version + " CLI for JavaScript",
"",
chalk.bold.white("Consolidates imports and converts between file formats."),
"",
" -t, --target Specifies the target format. Also accepts a path to require a custom target.",
"",
descs.join('\n'),
descs.join("\n"),
"",
" -p, --path Adds a directory to the include path.",
"",
Expand Down Expand Up @@ -184,8 +184,10 @@ exports.main = function(args, callback) {
}
callTarget();
} catch (err) {
if (callback)
return callback(err);
if (callback) {
callback(err);
return;
}
throw err;
}
});
Expand Down Expand Up @@ -280,4 +282,6 @@ exports.main = function(args, callback) {
: undefined;
});
}

return undefined;
};
29 changes: 14 additions & 15 deletions cli/pbts.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ exports.main = function(args, callback) {

if (!files.length) {
if (callback)
callback(Error("usage"));
callback(Error("usage")); // eslint-disable-line callback-return
else
console.error([
process.stderr.write([
"protobuf.js v" + pkg.version + " CLI for TypeScript",
"",
chalk.bold.white("Generates TypeScript definitions from annotated JavaScript files."),
Expand All @@ -59,8 +59,6 @@ exports.main = function(args, callback) {
"",
"usage: " + chalk.bold.green("pbts") + " [options] file1.js file2.js ..." + chalk.bold.gray(" (or) ") + "other | " + chalk.bold.green("pbts") + " [options] -"
].join("\n"));
if (callback)
callback(Error("usage"));
return 1;
}

Expand Down Expand Up @@ -113,17 +111,17 @@ exports.main = function(args, callback) {
child.stderr.pipe(process.stderr);
child.on("close", function(code) {
// clean up temporary files, no matter what
try { cleanup.forEach(fs.unlinkSync); } catch(e) {} cleanup = [];
try { cleanup.forEach(fs.unlinkSync); } catch(e) {/**/} cleanup = [];

if (code) {
out = out.join('').replace(/\s*JSDoc \d+\.\d+\.\d+ [^$]+/, "");
out = out.join("").replace(/\s*JSDoc \d+\.\d+\.\d+ [^$]+/, "");
process.stderr.write(out);
var err = Error("code " + code);
if (callback)
if (callback) {
callback(err);
else
throw err;
return;
return;
}
throw err;
}

var output = [];
Expand All @@ -137,20 +135,21 @@ exports.main = function(args, callback) {
"import * as $protobuf from \"protobufjs\";",
""
);
output = output.join('\n') + "\n" + out.join('');
output = output.join("\n") + "\n" + out.join("");

try {
if (argv.out)
fs.writeFileSync(argv.out, output);
else
process.stdout.write(output, "utf8");
if (callback)
callback(null);
callback(null); // eslint-disable-line callback-return
} catch (err) {
if (callback)
if (callback) {
callback(err);
else
throw err;
return;
}
throw err;
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion cli/targets/json.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
module.exports = json_target;

json_target.description = "JSON representation"
json_target.description = "JSON representation";

function json_target(root, options, callback) {
callback(null, JSON.stringify(root, null, 2));
Expand Down
38 changes: 22 additions & 16 deletions cli/targets/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ function proto_target(root, options, callback) {
first = false;
try {
buildRoot(root);
callback(null, out.join('\n'));
return callback(null, out.join("\n"));
} catch (err) {
callback(err);
return callback(err);
} finally {
out = [];
syntax = 3;
Expand All @@ -67,18 +67,18 @@ function push(line) {
}

function escape(str) {
return str.replace(/[\\"']/g, '\\$&')
.replace(/\u0000/g, '\\0');
return str.replace(/[\\"']/g, "\\$&")
.replace(/\u0000/g, "\\0");
}

function value(v) {
switch (typeof v) {
case 'boolean':
return v ? 'true' : 'false';
case 'number':
case "boolean":
return v ? "true" : "false";
case "number":
return v.toString();
default:
return '"' + escape(v + '') + '"';
return "\"" + escape(String(v)) + "\"";
}
}

Expand Down Expand Up @@ -178,10 +178,12 @@ function buildType(type) {
}

function buildField(field, passExtend) {
if (field.partOf || field.declaringField || (field.extend !== undefined && !passExtend))
if (field.partOf || field.declaringField || !(field.extend === undefined || passExtend))
return;
if (first)
first = false, push("");
if (first) {
first = false;
push("");
}
if (field.resolvedType && field.resolvedType.group) {
buildGroup(field);
return;
Expand Down Expand Up @@ -277,8 +279,10 @@ function buildOneOf(oneof) {
++indent; first = true;
oneof.oneof.forEach(function(fieldName) {
var field = oneof.parent.get(fieldName);
if (first)
push(""), first = false;
if (first) {
first = false;
push("");
}
var opts = buildFieldOptions(field);
push(field.type + " " + underScore(field.name) + " = " + field.id + (opts ? " " + opts : "") + ";");
});
Expand All @@ -301,11 +305,13 @@ function buildMethod(method) {

function buildOptions(object) {
if (!object.options)
return
return;
first = true;
Object.keys(object.options).forEach(function(key) {
if (first)
push(""), first = false;
if (first) {
first = false;
push("");
}
var val = object.options[key];
push("option " + key + " = " + JSON.stringify(val) + ";");
});
Expand Down
6 changes: 4 additions & 2 deletions cli/targets/static-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ static_module_target.description = "Static code without reflection as a module";

function static_module_target(root, options, callback) {
require("./static")(root, options, function(err, output) {
if (err)
return callback(err);
if (err) {
callback(err);
return;
}
try {
output = util.wrap(output, protobuf.util.merge({ dependency: "protobufjs/minimal" }, options));
} catch (e) {
Expand Down
15 changes: 8 additions & 7 deletions cli/util.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"use strict";
var fs = require("fs"),
path = require("path"),
child_process = require("child_process"),
semver;
child_process = require("child_process");

var semver;

var protobuf = require("..");

Expand All @@ -18,7 +19,7 @@ function basenameCompare(a, b) {
return 1;
}
return a.length < b.length ? -1 : 0;
};
}

exports.requireAll = function requireAll(dirname) {
dirname = path.join(__dirname, dirname);
Expand Down Expand Up @@ -110,7 +111,7 @@ function modExists(name, version) {
return semver
? semver.satisfies(pkg.version, version)
: parseInt(pkg.version, 10) === parseInt(version.replace(/^[\^~]/, ""), 10); // used for semver only
} catch (e) {}
} catch (e) {/**/}
}
return false;
}
Expand Down Expand Up @@ -156,8 +157,8 @@ exports.wrap = function(OUTPUT, options) {
// otherwise fetch the custom one
wrap = fs.readFileSync(path.resolve(process.cwd(), name)).toString("utf8");
}
wrap = wrap.replace(/%DEPENDENCY%/g, JSON.stringify(options.dependency || "protobufjs"));
wrap = wrap.replace(/( *)%OUTPUT%/, function($0, $1) {
wrap = wrap.replace(/\$DEPENDENCY/g, JSON.stringify(options.dependency || "protobufjs"));
wrap = wrap.replace(/( *)\$OUTPUT;/, function($0, $1) {
return $1.length ? OUTPUT.replace(/^/mg, $1) : OUTPUT;
});
if (options.lint !== "")
Expand All @@ -183,7 +184,7 @@ exports.safeProp = protobuf.util.safeProp = (function(safeProp) {
return !/^[$\w]+$/.test(name) || exports.reserved(name)
? safeProp(name)
: "." + name;
}
};
})(protobuf.util.safeProp);

exports.jsonSafeProp = function(json) {
Expand Down
2 changes: 1 addition & 1 deletion cli/wrappers/amd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
define(["protobuf"], function($protobuf) {
"use strict";

%OUTPUT%
$OUTPUT;

return $root;
});
4 changes: 2 additions & 2 deletions cli/wrappers/commonjs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var $protobuf = require(%DEPENDENCY%);
var $protobuf = require($DEPENDENCY);

%OUTPUT%
$OUTPUT;

module.exports = $root;
4 changes: 2 additions & 2 deletions cli/wrappers/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
define(["protobuf"], factory);

/* CommonJS */ else if (typeof require === 'function' && typeof module === 'object' && module && module.exports)
module.exports = factory(require(%DEPENDENCY%));
module.exports = factory(require($DEPENDENCY));

})(this, function($protobuf) {
"use strict";

%OUTPUT%
$OUTPUT;

return $root;
});
2 changes: 1 addition & 1 deletion cli/wrappers/es6.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as $protobuf from "protobufjs";

%OUTPUT%
$OUTPUT;

export { $root as default };
15 changes: 1 addition & 14 deletions dist/light/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/light/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/light/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/light/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/light/protobuf.min.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 6e81fcb

Please sign in to comment.