Remove argv and its huge yargs dependancy
This commit is contained in:
parent
aad2c1e449
commit
455103a330
7 changed files with 2 additions and 6567 deletions
|
@ -21,7 +21,7 @@ nconf.version = require('../package.json').version;
|
|||
//
|
||||
// Setup all stores as lazy-loaded getters.
|
||||
//
|
||||
['argv', 'env', 'file', 'literal', 'memory'].forEach(function (store) {
|
||||
['env', 'file', 'literal', 'memory'].forEach(function (store) {
|
||||
var name = common.capitalize(store);
|
||||
|
||||
nconf.__defineGetter__(name, function () {
|
||||
|
|
|
@ -1,128 +0,0 @@
|
|||
/*
|
||||
* 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);
|
||||
}
|
6234
package-lock.json
generated
6234
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -19,8 +19,7 @@
|
|||
"plugabble"
|
||||
],
|
||||
"dependencies": {
|
||||
"ini": "^1.3.0",
|
||||
"yargs": "^14.0.0"
|
||||
"ini": "^1.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"async": "^2.6.1",
|
||||
|
|
|
@ -36,101 +36,6 @@ describe('nconf/hierarchy, When using nconf', () => {
|
|||
|
||||
});
|
||||
|
||||
it("configured with .argv(), .env() and .file() should not persist information passed in to process.env and process.argv to disk",
|
||||
done => {
|
||||
var configFile = path.join(__dirname, 'fixtures', 'load-save.json');
|
||||
var script = path.join(__dirname, 'fixtures', 'scripts', 'nconf-hierarchical-load-save.js');
|
||||
var argv = ['--foo', 'foo', '--bar', 'bar'];
|
||||
var data = '';
|
||||
|
||||
try {
|
||||
fs.unlinkSync(configFile)
|
||||
}
|
||||
catch (ex) {
|
||||
// No-op
|
||||
}
|
||||
|
||||
var child = spawn('node', [script].concat(argv));
|
||||
|
||||
child.stdout.on('data', function (d) {
|
||||
data += d;
|
||||
});
|
||||
|
||||
child.on('close', function () {
|
||||
fs.readFile(configFile, 'utf8', (err, ondisk) => {
|
||||
expect(data).toEqual('foo');
|
||||
expect(JSON.parse(ondisk)).toEqual({
|
||||
database: {
|
||||
host: '127.0.0.1',
|
||||
port: 5984
|
||||
}
|
||||
});
|
||||
});
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it("configured with .argv(), .file() and invoked with nested command line options, should merge nested objects",
|
||||
done => {
|
||||
var script = path.join(__dirname, 'fixtures', 'scripts', 'nconf-hierarchical-load-merge.js');
|
||||
var argv = ['--candy:something', 'foo', '--candy:something5:second', 'bar'];
|
||||
var data = '';
|
||||
|
||||
var child = spawn('node', [script].concat(argv));
|
||||
|
||||
child.stdout.on('data', function (d) {
|
||||
data += d;
|
||||
});
|
||||
|
||||
child.on('close', function () {
|
||||
expect(JSON.parse(data)).toEqual({
|
||||
apples: true,
|
||||
candy: {
|
||||
something: 'foo',
|
||||
something1: true,
|
||||
something2: true,
|
||||
something5: {
|
||||
first: 1,
|
||||
second: 'bar'
|
||||
}
|
||||
}
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
it("configured with .argv() and separator, .file() and invoked with nested command line options should merge nested objects", done => {
|
||||
|
||||
var script = path.join(__dirname, 'fixtures',
|
||||
'scripts', 'nconf-hierarchical-load-merge-with-separator.js');
|
||||
var argv = ['--candy--something', 'foo', '--candy--something5--second', 'bar'];
|
||||
var data = '';
|
||||
process.env.candy__bonbon = 'sweet';
|
||||
var child = spawn('node', [script].concat(argv), {env: process.env});
|
||||
delete process.env.candy__bonbon;
|
||||
child.stdout.on('data', function (d) {
|
||||
data += d;
|
||||
});
|
||||
|
||||
child.on('close', function () {
|
||||
console.log(data);
|
||||
expect(JSON.parse(data)).toEqual({
|
||||
apples: true,
|
||||
candy: {
|
||||
bonbon: 'sweet',
|
||||
something: 'foo',
|
||||
something1: true,
|
||||
something2: true,
|
||||
something5: {
|
||||
first: 1,
|
||||
second: 'bar'
|
||||
}
|
||||
}
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it("configured with .file(), .defaults() should deep merge objects should merge nested objects ", done => {
|
||||
var script = path.join(__dirname, 'fixtures', 'scripts', 'nconf-hierarchical-defaults-merge.js');
|
||||
var data = '';
|
||||
|
|
|
@ -29,11 +29,6 @@ describe('nconf/provider When using nconf', () => {
|
|||
expect(provider.stores.file.file).toEqual(files[1]);
|
||||
})
|
||||
});
|
||||
it("respond with correct arg when 'argv' is true",
|
||||
helpers.assertSystemConf({
|
||||
script: path.join(fixturesDir, 'scripts', 'provider-argv.js'),
|
||||
argv: ['--something', 'foobar']
|
||||
}));
|
||||
it("respond with correct arg when 'env' is true", helpers.assertSystemConf({
|
||||
script: path.join(fixturesDir, 'scripts', 'provider-env.js'),
|
||||
env: {SOMETHING: 'foobar'}
|
||||
|
@ -70,36 +65,6 @@ describe('nconf/provider When using nconf', () => {
|
|||
expect(provider.get('SOMEBAD')).toEqual('5.1a');
|
||||
});
|
||||
|
||||
describe("the default nconf provider", () => {
|
||||
|
||||
it("respond with correct arg when 'argv' is set to true", helpers.assertSystemConf({
|
||||
script: path.join(fixturesDir, 'scripts', 'nconf-argv.js'),
|
||||
argv: ['--something', 'foobar'],
|
||||
env: {SOMETHING: true}
|
||||
}));
|
||||
|
||||
it("respond with correct arg when 'env' is set to true", helpers.assertSystemConf({
|
||||
script: path.join(fixturesDir, 'scripts', 'nconf-env.js'),
|
||||
env: {SOMETHING: 'foobar'}
|
||||
}));
|
||||
|
||||
it("respond with correct arg when 'argv' is set to true and process.argv is modified", helpers.assertSystemConf({
|
||||
script: path.join(fixturesDir, 'scripts', 'nconf-change-argv.js'),
|
||||
argv: ['--something', 'badValue', 'evenWorse', 'OHNOEZ', 'foobar']
|
||||
}));
|
||||
|
||||
it("respond with correct arg when hierarchical 'argv' get", helpers.assertSystemConf({
|
||||
script: path.join(fixturesDir, 'scripts', 'nconf-hierarchical-file-argv.js'),
|
||||
argv: ['--something', 'foobar'],
|
||||
env: {SOMETHING: true}
|
||||
}));
|
||||
|
||||
it("respond with correct arg when 'env' is set to true with a nested separator", helpers.assertSystemConf({
|
||||
script: path.join(fixturesDir, 'scripts', 'nconf-nested-env.js'),
|
||||
env: {SOME_THING: 'foobar'}
|
||||
}));
|
||||
});
|
||||
|
||||
describe("an instance of 'nconf.Provider'", () => {
|
||||
describe("the merge() method", () => {
|
||||
it("should have the result merged in", () => {
|
||||
|
|
|
@ -1,72 +0,0 @@
|
|||
/*
|
||||
* argv-test.js: Tests for the nconf argv store.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins and the Contributors.
|
||||
*
|
||||
*/
|
||||
|
||||
var yargs = require('yargs');
|
||||
var nconf = require('../../lib/nconf');
|
||||
|
||||
describe('nconf/stores/argv, An instance of nconf.Argv', () => {
|
||||
|
||||
it("should have the correct methods defined", () => {
|
||||
var argv = new nconf.Argv();
|
||||
expect(typeof argv.loadSync).toBe('function');
|
||||
expect(typeof argv.loadArgv).toBe('function');
|
||||
expect(argv.options).toEqual({});
|
||||
});
|
||||
|
||||
describe("can be created with a custom yargs", () => {
|
||||
var yargsInstance = yargs.alias('s', 'somearg').default('s', 'false');
|
||||
|
||||
it("and can give access to them", () => {
|
||||
var argv = new nconf.Argv(yargsInstance);
|
||||
expect(argv.options).toBe(yargsInstance);
|
||||
});
|
||||
|
||||
it("values are the one from the custom yargv", () => {
|
||||
var argv = new nconf.Argv(yargsInstance);
|
||||
argv.loadSync();
|
||||
expect(argv.get('somearg')).toBe('false');
|
||||
expect(argv.get('s')).toBe('false');
|
||||
});
|
||||
});
|
||||
|
||||
describe("can be created with a nconf yargs", () => {
|
||||
var options = {somearg: {alias: 's', default: 'false'}};
|
||||
it("and can give access to them", () => {
|
||||
var argv = new nconf.Argv(options);
|
||||
expect(argv.options).toEqual({somearg: {alias: 's', default: 'false'}});
|
||||
});
|
||||
|
||||
it("values are the one from the custom yargv", () => {
|
||||
var argv = new nconf.Argv(options);
|
||||
argv.loadSync();
|
||||
expect(argv.get('somearg')).toBe('false');
|
||||
expect(argv.get('s')).toBe('false');
|
||||
});
|
||||
|
||||
it("values cannot be altered with set when readOnly:true", () => {
|
||||
var argv = new nconf.Argv(options);
|
||||
argv.loadSync();
|
||||
argv.set('somearg', 'true');
|
||||
expect(argv.get('somearg')).toBe('false');
|
||||
});
|
||||
});
|
||||
describe("can be created with readOnly set to be false", () => {
|
||||
|
||||
it("readOnly is actually false", () => {
|
||||
var argv = new nconf.Argv({readOnly: false});
|
||||
expect(argv.readOnly).toBe(false);
|
||||
});
|
||||
|
||||
it("values can be changed by calling .set", () => {
|
||||
var argv = new nconf.Argv({somearg: {alias: 's', default: 'false'}, readOnly: false});
|
||||
argv.loadSync();
|
||||
expect(argv.get('somearg')).toBe('false');
|
||||
argv.set('somearg', 'true');
|
||||
expect(argv.get('somearg')).toBe('true');
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue