|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/* global $ argv */ |
| 21 | + |
| 22 | +'use strict' |
| 23 | + |
| 24 | +import 'zx/globals' |
| 25 | + |
| 26 | +import { readFile, writeFile } from 'fs/promises' |
| 27 | +import assert from 'assert' |
| 28 | +import { join } from 'desm' |
| 29 | +import semver from 'semver' |
| 30 | + |
| 31 | +assert(typeof argv.task === 'string', 'Missing task parameter') |
| 32 | + |
| 33 | +switch (argv.task) { |
| 34 | + case 'release': |
| 35 | + release(argv._).catch(onError) |
| 36 | + break |
| 37 | + case 'bump': |
| 38 | + bump(argv._).catch(onError) |
| 39 | + break |
| 40 | + case 'codegen': |
| 41 | + codegen(argv._).catch(onError) |
| 42 | + break |
| 43 | + default: |
| 44 | + console.log(`Unknown task: ${argv.task}`) |
| 45 | + process.exit(1) |
| 46 | +} |
| 47 | + |
| 48 | +async function release (args) { |
| 49 | + assert(args.length === 2, 'Release task expects two parameters') |
| 50 | + let [version, outputFolder] = args |
| 51 | + |
| 52 | + if (process.env.WORKFLOW === 'snapshot' && !version.endsWith('SNAPSHOT')) { |
| 53 | + version = `${version}-SNAPSHOT` |
| 54 | + } |
| 55 | + |
| 56 | + await bump([version]) |
| 57 | + |
| 58 | + const packageJson = JSON.parse(await readFile( |
| 59 | + join(import.meta.url, '..', 'package.json'), |
| 60 | + 'utf8' |
| 61 | + )) |
| 62 | + |
| 63 | + await $`npm run build` |
| 64 | + await $`npm pack` |
| 65 | + await $`zip elasticsearch-js-${version}.zip elastic-elasticsearch-${packageJson.version}.tgz` |
| 66 | + await $`rm elastic-elasticsearch-${packageJson.version}.tgz` |
| 67 | + await $`mv ${join(import.meta.url, '..', `elasticsearch-js-${version}.zip`)} ${join(import.meta.url, '..', outputFolder, `elasticsearch-js-${version}.zip`)}` |
| 68 | +} |
| 69 | + |
| 70 | +async function bump (args) { |
| 71 | + assert(args.length === 1, 'Bump task expects one parameter') |
| 72 | + const [version] = args |
| 73 | + const packageJson = JSON.parse(await readFile( |
| 74 | + join(import.meta.url, '..', 'package.json'), |
| 75 | + 'utf8' |
| 76 | + )) |
| 77 | + |
| 78 | + const cleanVersion = semver.clean(version.includes('SNAPSHOT') ? version.split('-')[0] : version) |
| 79 | + assert(semver.valid(cleanVersion)) |
| 80 | + packageJson.version = cleanVersion |
| 81 | + packageJson.versionCanary = `${cleanVersion}-canary.0` |
| 82 | + |
| 83 | + await writeFile( |
| 84 | + join(import.meta.url, '..', 'package.json'), |
| 85 | + JSON.stringify(packageJson, null, 2), |
| 86 | + 'utf8' |
| 87 | + ) |
| 88 | + |
| 89 | + const testMatrix = await readFile(join(import.meta.url, 'test-matrix.yml'), 'utf8') |
| 90 | + await writeFile( |
| 91 | + join(import.meta.url, 'test-matrix.yml'), |
| 92 | + testMatrix.replace(/STACK_VERSION:\s+\- "[0-9]+[0-9\.]*[0-9](?:\-SNAPSHOT)?"/, `STACK_VERSION:\n - "${cleanVersion}-SNAPSHOT"`), // eslint-disable-line |
| 93 | + 'utf8' |
| 94 | + ) |
| 95 | +} |
| 96 | + |
| 97 | +// this command can only be executed locally for now |
| 98 | +async function codegen (args) { |
| 99 | + assert(args.length === 1, 'Bump task expects one parameter') |
| 100 | + const clientGeneratorPath = join(import.meta.url, '..', '..', 'elastic-client-generator-js') |
| 101 | + const [version] = args |
| 102 | + |
| 103 | + const isGeneratorCloned = await $`[[ -d ${clientGeneratorPath} ]]`.exitCode === 0 |
| 104 | + assert(isGeneratorCloned, 'You must clone the elastic-client-generator-js first') |
| 105 | + |
| 106 | + await $`npm install --prefix ${clientGeneratorPath}` |
| 107 | + // this command will take a while! |
| 108 | + if (version === 'main') { |
| 109 | + await $`npm run elasticsearch --prefix ${clientGeneratorPath} -- --version main` |
| 110 | + } else { |
| 111 | + await $`npm run elasticsearch --prefix ${clientGeneratorPath} -- --version ${version.split('.').slice(0, 2).join('.')}` |
| 112 | + } |
| 113 | + await $`npm run lint --prefix ${clientGeneratorPath}` |
| 114 | + |
| 115 | + await $`rm -rf ${join(import.meta.url, '..', 'src', 'api')}` |
| 116 | + await $`mkdir ${join(import.meta.url, '..', 'src', 'api')}` |
| 117 | + await $`cp -R ${join(import.meta.url, '..', '..', 'elastic-client-generator-js', 'output')}/* ${join(import.meta.url, '..', 'src', 'api')}` |
| 118 | + await $`mv ${join(import.meta.url, '..', 'src', 'api', 'reference.asciidoc')} ${join(import.meta.url, '..', 'docs', 'reference.asciidoc')}` |
| 119 | + await $`npm run build` |
| 120 | +} |
| 121 | + |
| 122 | +function onError (err) { |
| 123 | + console.log(err) |
| 124 | + process.exit(1) |
| 125 | +} |
0 commit comments