nconf-lite/lib/nconf/stores/argv.js
Adrien Becchis b8686aeff0 Migrating test to jest (#292)
* set up jest dependencies

* add parser option to eslint to support es6

* migrate first test to jest

* migrate the argv test to shpec

* convert the env and literal store tests

* convert the file-store tests

* convert the memory-store tests

* convert the hierarchy tests

* convert the provider-save test

* convert the complete test

* convert the provider test

* convert the conf test

* tweak a test that was no longer working (context changed)

* replace in place the helpers file

* remove vows dependency

* update the test invocation to rely on jest

* update the argv test to be able to use the jest --verbose option

* Some tweaks to the test to have them working

* Update node version tested (+10 +12 -9)

* Replace const by var until we drop 0.10/0.12/4 node

* Replace let by var until we drop 0.10/0.12/4 node
2019-04-29 22:26:02 -04:00

128 lines
3 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* argv.js: Simple memory-based store for command-line arguments.
*
* (C) 2011, Charlie Robbins and the Contributors.
*
*/
var util = require('util'),
common = require('../common'),
Memory = require('./memory').Memory;
//
// ### function Argv (options)
// #### @options {Object} Options for this instance.
// Constructor function for the Argv nconf store, a simple abstraction
// around the Memory store that can read command-line arguments.
//
var Argv = exports.Argv = function (options, usage) {
Memory.call(this, options);
options = options || {};
this.type = 'argv';
this.readOnly = options.readOnly !== undefined? options.readOnly : true;
this.options = options;
this.usage = usage;
if(typeof options.readOnly === 'boolean') {
this.readOnly = options.readOnly;
delete options.readOnly;
// FIXME; should not mutate options!!!!
} else {
this.readOnly = true;
}
if(typeof options.parseValues === 'boolean') {
this.parseValues = options.parseValues;
delete options.parseValues;
} else {
this.parseValues = false;
}
if (typeof options.transform === 'function') {
this.transform = options.transform;
delete options.transform;
} else {
this.transform = false;
}
if (typeof options.separator === 'string' || options.separator instanceof RegExp) {
this.separator = options.separator;
delete options.separator;
} else {
this.separator = '';
}
};
// Inherit from the Memory store
util.inherits(Argv, Memory);
//
// ### function loadSync ()
// Loads the data passed in from `process.argv` into this instance.
//
Argv.prototype.loadSync = function () {
this.loadArgv();
return this.store;
};
//
// ### function loadArgv ()
// Loads the data passed in from the command-line arguments
// into this instance.
//
Argv.prototype.loadArgv = function () {
var self = this,
yargs, argv;
yargs = isYargs(this.options) ?
this.options :
typeof this.options === 'object' ?
require('yargs')(process.argv.slice(2)).options(this.options) :
require('yargs')(process.argv.slice(2));
if (typeof this.usage === 'string') { yargs.usage(this.usage) }
argv = yargs.argv
if (!argv) {
return;
}
if (this.transform) {
argv = common.transform(argv, this.transform);
}
var tempWrite = false;
if(this.readOnly) {
this.readOnly = false;
tempWrite = true;
}
Object.keys(argv).forEach(function (key) {
var val = argv[key];
if (typeof val !== 'undefined') {
if (self.parseValues) {
val = common.parseValues(val);
}
if (self.separator) {
self.set(common.key.apply(common, key.split(self.separator)), val);
}
else {
self.set(key, val);
}
}
});
this.showHelp = yargs.showHelp
this.help = yargs.help
if (tempWrite) {
this.readOnly = true;
}
return this.store;
};
function isYargs(obj) {
return (typeof obj === 'function' || typeof obj === 'object') && ('argv' in obj);
}