Skip to content

Commit

Permalink
allow resources to be specified in config
Browse files Browse the repository at this point in the history
If resources are not specified on the command line then if they are available in the config they will be used as default resources.

Resources specified on the command line will override any defined in the config file, thus those in the config file are ignored.
  • Loading branch information
jeffbski committed Dec 28, 2020
1 parent db0d080 commit 7e9618d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ waitOn(opts, [cb]) - function which triggers resource checks
- opts.delay - optional initial delay in ms, default 0
- opts.interval - optional poll resource interval in ms, default 250ms
- opts.log - optional flag which outputs to stdout, remaining resources waited on and when complete or errored
- opts.resources - optional array of string resources to wait for if none are specified via command line
- opts.reverse - optional flag to reverse operation so checks are for resources being NOT available, default false
- opts.simultaneous - optional count to limit concurrent connections per resource at a time, setting to 1 waits for previous connection to succeed, fail, or timeout before sending another, default infinity
- opts.timeout - optional timeout in ms, default Infinity. Aborts with error.
Expand Down
20 changes: 8 additions & 12 deletions bin/wait-on
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,22 @@ const minimistOpts = {
};

const argv = minimist(process.argv.slice(2), minimistOpts);
// if a js/json configuration file is provided require it
const configOpts = argv.config ? require(path.resolve(argv.config)) : {};
const hasResources = argv._.length || (configOpts.resources && configOpts.resources.length);

if (argv.help) {
if (argv.help || !hasResources) {
// help
fs.createReadStream(path.join(__dirname, '/usage.txt'))
.pipe(process.stdout)
.on('close', function () {
process.exit(1);
});
} else {
let opts = {};

// if a js/json configuration file is provided require it
const configFile = argv.config;
if (configFile) {
opts = require(path.resolve(configFile));
}

// add in the resources listed on the command line (if not present in config)
if (!opts.resources) {
opts.resources = argv._;
// if resources are present in the command line then they take
// precedence over those in the config file.
if (argv._.length) {
configOpts.resources = argv._;
}

// now check for specific options and set those
Expand Down
6 changes: 4 additions & 2 deletions exampleConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ module.exports = {
passphrase: 'yourpassphrase',
auth: {
user: 'yourusername',
pass: 'yourpassword',
pass: 'yourpassword'
},
strictSSL: false,
followRedirect: false,
headers: {
'x-custom': 'headers',
'x-custom': 'headers'
},
// optional default resources if not specified in command args
resources: ['http://foo/bar', 'http://cat/dog']
};
36 changes: 36 additions & 0 deletions test/cli.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,4 +533,40 @@ describe('cli', function () {
done();
});
});

context('resources are specified in config', () => {
it('should succeed when http resources become available later', function (done) {
setTimeout(function () {
httpServer = http.createServer().on('request', function (req, res) {
res.end('data');
});
httpServer.listen(8123, 'localhost');
}, 300);

execCLI(['--config', path.join(__dirname, 'config-http-resources.js')].concat(FAST_OPTS), {}).on(
'exit',
function (code) {
expect(code).toBe(0);
done();
}
);
});

it('should succeed when http resources from command line become available later (ignores config resources)', function (done) {
setTimeout(function () {
httpServer = http.createServer().on('request', function (req, res) {
res.end('data');
});
httpServer.listen(3030, 'localhost');
}, 300);

execCLI(
['--config', path.join(__dirname, 'config-http-resources.js'), 'http://localhost:3030/'].concat(FAST_OPTS),
{}
).on('exit', function (code) {
expect(code).toBe(0);
done();
});
});
});
});
3 changes: 3 additions & 0 deletions test/config-http-resources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
resources: ['http://localhost:8123', 'http://localhost:8123/foo'],
};

0 comments on commit 7e9618d

Please sign in to comment.