Browse Source

Merge pull request #2300 from svaarala/js-tooling-util-trivia

JS tooling util trivia fixes
pull/2301/head
Sami Vaarala 5 years ago
committed by GitHub
parent
commit
52aadda3e3
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      src-tools/lib/util/cmdline.js
  2. 17
      src-tools/lib/util/fs.js

21
src-tools/lib/util/cmdline.js

@ -97,6 +97,17 @@ function addOptionValue(optSpec, opts, name, value) {
}
}
// Check required options.
function checkRequiredOptions(spec, opts) {
let options = spec.options || {};
for (let optName of Object.getOwnPropertyNames(options || {}).sort()) {
let opt = options[optName];
if (opt.required && !(optName in opts)) {
throw new TypeError('missing required option --' + optName);
}
}
}
// Parse command line given a clean argv (excluding nodejs etc) and a
// global command/option specification object.
function parseCommandLine(argv, argSpec) {
@ -104,7 +115,7 @@ function parseCommandLine(argv, argSpec) {
var command = void 0;
var commandOpts = void 0;
var commandPositional = [];
var genericSpec = argSpec.options;
var genericSpec = assert(argSpec);
var commandSpec = void 0;
function parseLongOption(i, t) {
@ -232,6 +243,14 @@ function parseCommandLine(argv, argSpec) {
}
}
// Check required options.
if (genericSpec) {
checkRequiredOptions(genericSpec, genericOpts);
}
if (commandSpec) {
checkRequiredOptions(commandSpec, commandOpts);
}
return { genericOpts, command, commandOpts, commandPositional };
}
exports.parseCommandLine = parseCommandLine;

17
src-tools/lib/util/fs.js

@ -61,6 +61,10 @@ function pathJoinTest() {
// > require('path').join('foo/bar/')
// 'foo/bar/'
if (!isDuktape()) {
return;
}
assert(pathJoin('foo/bar/quux.c', '..', 'baz') === 'foo/bar/baz');
assert(pathJoin('foo/bar/', '..', 'baz') === 'foo/baz');
assert(pathJoin('foo/bar', '..', 'baz') === 'foo/baz');
@ -135,6 +139,10 @@ function dirnameTest() {
// > require('path').dirname('foo')
// '.'
if (!isDuktape()) {
return;
}
assert(dirname('foo/bar') === 'foo');
assert(dirname('foo/bar/') === 'foo');
assert(dirname('foo/bar//') === 'foo');
@ -186,6 +194,10 @@ function basenameTest() {
// > require('path').basename('foo/..')
// '..'
if (!isDuktape()) {
return;
}
assert(basename('foo') === 'foo');
assert(basename('foo/') === 'foo');
assert(basename('foo/bar') === 'bar');
@ -248,6 +260,11 @@ function readFileYaml(fn) {
}
exports.readFileYaml = readFileYaml;
function writeFileJson(fn, doc) {
writeFileUtf8(fn, JSON.stringify(doc) + '\n');
}
exports.writeFileJson = writeFileJson;
function writeFileJsonPretty(fn, doc) {
writeFileUtf8(fn, JSON.stringify(doc, null, 4) + '\n');
}

Loading…
Cancel
Save