check-connection.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. const { Client } = require('@elastic/elasticsearch');
  2. const { elasticsearchHost, elasticsearchPort } = require('./constants');
  3. const esClient = new Client({
  4. node: `${elasticsearchHost}:${elasticsearchPort}`,
  5. });
  6. /**
  7. * When contributing to Vendure, developers who made changes unrelated to
  8. * this plugin should not be expected to set up an Elasticsearch instance
  9. * locally just so they can get the pre-push hook to pass. So if no
  10. * instance is available, we skip the tests.
  11. */
  12. async function checkConnection() {
  13. try {
  14. await esClient.ping({}, { requestTimeout: 1000 });
  15. // If the connection is available, we exit with 1 in order to invoke the
  16. // actual e2e test script (since we are using the `||` operator in the "e2e" script)
  17. return 1;
  18. } catch (e) {
  19. console.log(
  20. `Could not connect to Elasticsearch instance at "${elasticsearchHost}:${elasticsearchPort}"`,
  21. );
  22. console.log(`Skipping e2e tests for ElasticsearchPlugin`);
  23. process.env.SKIP_ELASTICSEARCH_E2E_TESTS = true;
  24. // If no elasticsearch available, we exit with 0 so that the npm script
  25. // exits
  26. return 0;
  27. }
  28. }
  29. checkConnection().then((result) => {
  30. process.exit(result);
  31. });