@knapsack-pro/core
Version:
Knapsack Pro Core library splits tests across CI nodes and makes sure that tests will run in optimal time on each CI node. This library gives core features like communication with KnapsackPro.com API. This library is a dependency for other projects specif
55 lines (54 loc) • 1.98 kB
JavaScript
import { spawnSync } from 'child_process';
export const logDiagnostics = (logger, endpoint) => {
commands(endpoint).forEach((command) => logCommand(logger, command));
logEnv(logger);
};
const commands = (endpoint) => {
const url = new URL(endpoint);
const port = url.port || (url.protocol === 'https:' ? '443' : '80');
return [
{
label: `dig ${url.hostname}`,
command: 'dig',
args: [url.hostname],
},
{
label: `nslookup ${url.hostname}`,
command: 'nslookup',
args: [url.hostname],
},
{
label: `curl -v ${url.protocol}//${url.hostname}:${port}`,
command: 'curl',
args: ['-v', `${url.protocol}//${url.hostname}:${port}`],
},
{
label: `nc -vz ${url.hostname} ${port}`,
command: 'nc',
args: ['-vz', url.hostname, port],
},
{
label: `openssl s_client -connect ${url.hostname}:${port} < /dev/null`,
command: 'openssl',
args: ['s_client', '-connect', `${url.hostname}:${port}`],
options: { input: '' },
},
];
};
const logCommand = (logger, diagnostic) => {
logger.warn(diagnostic.label);
logger.warn('='.repeat(diagnostic.label.length));
const result = spawnSync(diagnostic.command, diagnostic.args, diagnostic.options);
logger.warn(`Exit status: ${result.status}`);
((result.stderr ?? '').toString() + (result.stdout ?? '').toString())
.split('\n')
.forEach((line) => logger.warn(line));
};
const logEnv = (logger) => {
logger.warn('KNAPSACK_PRO_*');
logger.warn('==============');
Object.entries(process.env)
.filter(([key]) => key.startsWith('KNAPSACK_PRO') && !key.includes('TOKEN'))
.sort(([leftKey], [rightKey]) => leftKey.localeCompare(rightKey))
.forEach(([key, value]) => logger.warn(`${key}=${value ?? ''}`));
};