(opts, args)
| 63 | main(program.opts(), program.args) |
| 64 | |
| 65 | async function main(opts, args) { |
| 66 | const texts = [args.join(' ')] |
| 67 | if (!opts.elasticsearchUrl && !process.env.ELASTICSEARCH_URL) { |
| 68 | throw new Error( |
| 69 | 'Must passed the elasticsearch URL option or ' + |
| 70 | 'set the environment variable ELASTICSEARCH_URL' |
| 71 | ) |
| 72 | } |
| 73 | let node = opts.elasticsearchUrl || process.env.ELASTICSEARCH_URL |
| 74 | |
| 75 | // Allow the user to lazily set it to `localhost:9200` for example. |
| 76 | if (!node.startsWith('http') && !node.startsWith('://') && node.split(':').length === 2) { |
| 77 | node = `http://${node}` |
| 78 | } |
| 79 | |
| 80 | try { |
| 81 | const parsed = new URL(node) |
| 82 | if (!parsed.hostname) throw new Error('no valid hostname') |
| 83 | } catch (err) { |
| 84 | console.error(chalk.bold('URL for Elasticsearch not a valid URL', err)) |
| 85 | return |
| 86 | } |
| 87 | |
| 88 | const { verbose, language, notLanguage } = opts |
| 89 | |
| 90 | // The notLanguage is useful you want to, for example, index all languages |
| 91 | // *except* English. |
| 92 | if (language && notLanguage) { |
| 93 | throw new Error("Can't combine --language and --not-language") |
| 94 | } |
| 95 | |
| 96 | if (verbose) { |
| 97 | console.log(`Connecting to ${chalk.bold(safeUrlDisplay(node))}`) |
| 98 | } |
| 99 | |
| 100 | const client = new Client({ node }) |
| 101 | |
| 102 | // This will throw if it can't ping |
| 103 | await client.ping() |
| 104 | |
| 105 | const versionKey = opts.version || 'dotcom' |
| 106 | if (verbose) { |
| 107 | console.log(`Analyzing on version ${chalk.bold(versionKey)}`) |
| 108 | } |
| 109 | const languageKey = opts.language || 'en' |
| 110 | if (verbose) { |
| 111 | console.log(`Analyzing on language ${chalk.bold(languageKey)}`) |
| 112 | } |
| 113 | |
| 114 | const { indexPrefix } = opts |
| 115 | const prefix = indexPrefix ? `${indexPrefix}_` : '' |
| 116 | |
| 117 | const indexName = `${prefix}github-docs-${versionKey}-${languageKey}` |
| 118 | console.log(chalk.yellow(`Analyzing in ${chalk.bold(indexName)}`)) |
| 119 | await analyzeVersion(client, texts, indexName, verbose) |
| 120 | } |
| 121 | |
| 122 | function safeUrlDisplay(url) { |
no test coverage detected