(opts, args)
| 67 | main(program.opts(), program.args) |
| 68 | |
| 69 | async function main(opts, args) { |
| 70 | if (!args.length) { |
| 71 | throw new Error('Must pass the source as the first argument') |
| 72 | } |
| 73 | |
| 74 | const { verbose, language, notLanguage, elasticsearchUrl } = opts |
| 75 | |
| 76 | if (!elasticsearchUrl && !process.env.ELASTICSEARCH_URL) { |
| 77 | throw new Error( |
| 78 | 'Must passed the elasticsearch URL option or ' + |
| 79 | 'set the environment variable ELASTICSEARCH_URL' |
| 80 | ) |
| 81 | } |
| 82 | let node = elasticsearchUrl || process.env.ELASTICSEARCH_URL |
| 83 | |
| 84 | // Allow the user to lazily set it to `localhost:9200` for example. |
| 85 | if (!node.startsWith('http') && !node.startsWith('://') && node.split(':').length === 2) { |
| 86 | node = `http://${node}` |
| 87 | } |
| 88 | |
| 89 | try { |
| 90 | const parsed = new URL(node) |
| 91 | if (!parsed.hostname) throw new Error('no valid hostname') |
| 92 | } catch (err) { |
| 93 | console.error(chalk.bold('URL for Elasticsearch not a valid URL', err)) |
| 94 | throw err |
| 95 | } |
| 96 | |
| 97 | // The notLanguage is useful you want to, for example, index all languages |
| 98 | // *except* English. |
| 99 | if (language && notLanguage) { |
| 100 | throw new Error("Can't combine --language and --not-language") |
| 101 | } |
| 102 | |
| 103 | if (verbose) { |
| 104 | console.log(`Connecting to ${chalk.bold(safeUrlDisplay(node))}`) |
| 105 | } |
| 106 | const sourceDirectory = args[0] |
| 107 | try { |
| 108 | await fs.stat(sourceDirectory) |
| 109 | } catch (error) { |
| 110 | if (error.code === 'ENOENT') { |
| 111 | throw new Error(`The specified directory '${sourceDirectory}' does not exist.`) |
| 112 | } |
| 113 | throw error |
| 114 | } |
| 115 | |
| 116 | try { |
| 117 | await indexAll(node, sourceDirectory, opts) |
| 118 | } catch (error) { |
| 119 | // If any error is thrown from within the SDK, that error object will |
| 120 | // contain a `Connection` object which, when printed, can reveal the |
| 121 | // username/password or the base64 Basic auth credentials. |
| 122 | // So we want to carefully re-throw it so it only contains the minimal |
| 123 | // information for debugging without exposing the Connection credentials |
| 124 | // in Actions logs. |
| 125 | if (error instanceof errors.ElasticsearchClientError) { |
| 126 | // All ElasticsearchClientError error subclasses have a `name` and |
no test coverage detected