Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow resources to be defined in config #94

Merged
merged 6 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
tabWidth: 2,
singleQuote: true,
printWidth: 120,
trailingComma: 'none',
};
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ Description:
once resources are available. ex: wait-on f1 && NEXT_CMD

resources types are defined by their prefix, if no prefix is
present, the resource is assumed to be of type 'file'
present, the resource is assumed to be of type 'file'. Resources
can also be provided in the config file.

resource prefixes are:

Expand All @@ -86,7 +87,7 @@ Standard Options:

-c, --config

js or json config file, useful for http(s) options
js or json config file, useful for http(s) options and resources

-d, --delay

Expand Down Expand Up @@ -155,7 +156,7 @@ var opts = {
'tcp:foo.com:8000',
'socket:/my/sock',
'http://unix:/my/sock:/my/url',
'http-get://unix:/my/sock:/my/url',
'http-get://unix:/my/sock:/my/url'
],
delay: 1000, // initial delay in ms, default 0
interval: 100, // poll interval in ms, default 250ms
Expand Down Expand Up @@ -188,16 +189,16 @@ var opts = {
} */,
auth: {
user: 'theuser', // or username
pass: 'thepassword', // or password
pass: 'thepassword' // or password
},
strictSSL: false,
followRedirect: true,
headers: {
'x-custom': 'headers',
'x-custom': 'headers'
},
validateStatus: function (status) {
return status >= 200 && status < 300; // default if not provided
},
}
};

// Usage with callback function
Expand Down Expand Up @@ -232,6 +233,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
28 changes: 13 additions & 15 deletions bin/wait-on
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,31 @@ const minimistOpts = {
t: 'timeout',
v: 'verbose',
w: 'window',
h: 'help',
},
h: 'help'
}
};

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 || !argv._.length) {
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));
// if resources are present in the command line then they take
// precedence over those in the config file.
if (argv._.length) {
configOpts.resources = argv._;
}

// add in the resources listed on the command line
opts.resources = argv._;

// now check for specific options and set those
opts = [
const opts = [
'delay',
'httpTimeout',
'interval',
Expand All @@ -54,13 +52,13 @@ if (argv.help || !argv._.length) {
'timeout',
'tcpTimeout',
'verbose',
'window',
'window'
].reduce(function (accum, x) {
if (argv[x]) {
accum[x] = argv[x];
}
return accum;
}, opts);
}, configOpts);

waitOn(opts, function (err) {
if (err) {
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']
};
26 changes: 15 additions & 11 deletions lib/wait-on.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ const WAIT_ON_SCHEMA = Joi.object({
proxy: [Joi.boolean(), Joi.object()],
auth: Joi.object({
username: Joi.string(),
password: Joi.string(),
password: Joi.string()
}),
strictSSL: Joi.boolean().default(false),
followRedirect: Joi.boolean().default(true), // HTTP 3XX responses
headers: Joi.object(),
headers: Joi.object()
});

/**
Expand Down Expand Up @@ -112,7 +112,7 @@ function waitOnImpl(opts, cbFunc) {
...validResult.value, // use defaults
// window needs to be at least interval
...(validResult.value.window < validResult.value.interval ? { window: validResult.value.interval } : {}),
...(validResult.value.verbose ? { log: true } : {}), // if debug logging then normal log is also enabled
...(validResult.value.verbose ? { log: true } : {}) // if debug logging then normal log is also enabled
};

const { resources, log: shouldLog, timeout, verbose, reverse } = validatedOpts;
Expand All @@ -125,10 +125,14 @@ function waitOnImpl(opts, cbFunc) {
let lastResourcesState = resources; // the last state we had recorded

const timeoutError$ =
timeout !== Infinity ? timer(timeout).pipe(mergeMap(() => {
const resourcesWaitingFor = determineRemainingResources(resources, lastResourcesState).join(', ')
return throwError(Error(`${TIMEOUT_ERR_MSG}: ${resourcesWaitingFor}`))
})) : NEVER;
timeout !== Infinity
? timer(timeout).pipe(
mergeMap(() => {
const resourcesWaitingFor = determineRemainingResources(resources, lastResourcesState).join(', ');
return throwError(Error(`${TIMEOUT_ERR_MSG}: ${resourcesWaitingFor}`));
})
)
: NEVER;

function cleanup(err) {
if (err) {
Expand Down Expand Up @@ -159,7 +163,7 @@ function waitOnImpl(opts, cbFunc) {
logWaitingForWDeps(resourceStates);
},
error: cleanup,
complete: cleanup,
complete: cleanup
});
}

Expand Down Expand Up @@ -270,7 +274,7 @@ function createHTTP$({ validatedOpts, output }, resource) {
proxy,
reverse,
simultaneous,
strictSSL: rejectUnauthorized,
strictSSL: rejectUnauthorized
} = validatedOpts;
const method = HTTP_GET_RE.test(resource) ? 'get' : 'head';
const url = resource.replace('-get:', ':');
Expand All @@ -283,13 +287,13 @@ function createHTTP$({ validatedOpts, output }, resource) {
...pick(['auth', 'headers', 'validateStatus'], validatedOpts),
httpsAgent: new https.Agent({
rejectUnauthorized,
...pick(['ca', 'cert', 'key', 'passphrase'], validatedOpts),
...pick(['ca', 'cert', 'key', 'passphrase'], validatedOpts)
}),
...(followRedirect ? {} : { maxRedirects: 0 }), // defaults to 5 (enabled)
proxy, // can be undefined, false, or object
...(timeout && { timeout }),
...urlSocketOptions,
method,
method
// by default it provides full response object
// validStatus is 2xx unless followRedirect is true (default)
};
Expand Down
Loading