kibana/scripts/read_jest_help.mjs
Gerard Soldevila 5acb5506ac
[8.x] SKA: Relocate "platform" packages that remain on /packages (#208704) (#212474)
# Backport

This will backport the following commits from `main` to `8.x`:
- [SKA: Relocate "platform" packages that remain on `/packages`
(#208704)](https://github.com/elastic/kibana/pull/208704)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Gerard
Soldevila","email":"gerard.soldevila@elastic.co"},"sourceCommit":{"committedDate":"2025-02-24T11:03:30Z","message":"SKA:
Relocate \"platform\" packages that remain on `/packages`
(#208704)\n\n## Summary\n\nThe `/packages` folder at the root of the
Kibana repository used to\ncontain a lot of packages.\nIn the context of
SKA, they have been gradually moved to various\nlocations:\n*
`src/platform/packages`\n* `x-pack/platform/packages`\n*
`src/core/packages`\n\nCurrently, only `devOnly: true` packages are left
in this folder. This\ncomprises libraries for CLI scripts as well as
testing utilities.\n\nWith this PR, we are moving ~half of these
packages under\n`src/platform/packages/(private|shared)/`.\nIn
particular, we are moving those packages that are being used
from\nplatform and/or solutions.\n\nSince they are `\"devOnly\": true`,
this means they are ONLY used from\ntests, cypress tests, storybook
configs, ./scripts/ folders inside some\nmodules, or other non-prod-time
logic. Nonetheless, they are effectively\nreferenced from platform
and/or solutions code, hence I decided they\nshould be placed under
`platform` folders.\n\n---------\n\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"6a7c904f921434fe21dfa00eceabfb5e64e915dc","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Core","release_note:skip","v9.0.0","ci:project-deploy-observability","Team:obs-ux-infra_services","backport:version","v8.18.0","v9.1.0","v8.19.0"],"title":"SKA:
Relocate \"platform\" packages that remain on
`/packages`","number":208704,"url":"https://github.com/elastic/kibana/pull/208704","mergeCommit":{"message":"SKA:
Relocate \"platform\" packages that remain on `/packages`
(#208704)\n\n## Summary\n\nThe `/packages` folder at the root of the
Kibana repository used to\ncontain a lot of packages.\nIn the context of
SKA, they have been gradually moved to various\nlocations:\n*
`src/platform/packages`\n* `x-pack/platform/packages`\n*
`src/core/packages`\n\nCurrently, only `devOnly: true` packages are left
in this folder. This\ncomprises libraries for CLI scripts as well as
testing utilities.\n\nWith this PR, we are moving ~half of these
packages under\n`src/platform/packages/(private|shared)/`.\nIn
particular, we are moving those packages that are being used
from\nplatform and/or solutions.\n\nSince they are `\"devOnly\": true`,
this means they are ONLY used from\ntests, cypress tests, storybook
configs, ./scripts/ folders inside some\nmodules, or other non-prod-time
logic. Nonetheless, they are effectively\nreferenced from platform
and/or solutions code, hence I decided they\nshould be placed under
`platform` folders.\n\n---------\n\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"6a7c904f921434fe21dfa00eceabfb5e64e915dc"}},"sourceBranch":"main","suggestedTargetBranches":["8.18","8.x"],"targetPullRequestStates":[{"branch":"9.0","label":"v9.0.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/212224","number":212224,"state":"OPEN"},{"branch":"8.18","label":"v8.18.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/208704","number":208704,"mergeCommit":{"message":"SKA:
Relocate \"platform\" packages that remain on `/packages`
(#208704)\n\n## Summary\n\nThe `/packages` folder at the root of the
Kibana repository used to\ncontain a lot of packages.\nIn the context of
SKA, they have been gradually moved to various\nlocations:\n*
`src/platform/packages`\n* `x-pack/platform/packages`\n*
`src/core/packages`\n\nCurrently, only `devOnly: true` packages are left
in this folder. This\ncomprises libraries for CLI scripts as well as
testing utilities.\n\nWith this PR, we are moving ~half of these
packages under\n`src/platform/packages/(private|shared)/`.\nIn
particular, we are moving those packages that are being used
from\nplatform and/or solutions.\n\nSince they are `\"devOnly\": true`,
this means they are ONLY used from\ntests, cypress tests, storybook
configs, ./scripts/ folders inside some\nmodules, or other non-prod-time
logic. Nonetheless, they are effectively\nreferenced from platform
and/or solutions code, hence I decided they\nshould be placed under
`platform` folders.\n\n---------\n\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"6a7c904f921434fe21dfa00eceabfb5e64e915dc"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
2025-02-28 10:12:01 +00:00

132 lines
4 KiB
JavaScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import Fsp from 'fs/promises';
import Path from 'path';
import { createFailError } from '@kbn/dev-cli-errors';
import { run } from '@kbn/dev-cli-runner';
import { REPO_ROOT } from '@kbn/repo-info';
const FLAGS_FILE = 'src/platform/packages/shared/kbn-test/src/jest/jest_flags.json';
function readStdin() {
return new Promise((resolve, reject) => {
let buffer = '';
let timer = setTimeout(() => {
reject(
createFailError('you must pipe the output of `yarn jest --help` to this script', {
showHelp: true,
})
);
}, 1000);
process.stdin
.on('data', (chunk) => {
if (timer) {
clearTimeout(timer);
timer = undefined;
}
buffer += chunk;
})
.on('end', () => resolve(buffer))
.on('error', reject);
});
}
run(
async ({ log }) => {
const lines = (await readStdin()).split('\n');
/** @type {{ string: string[], boolean: string[], alias: Record<string, string> }} */
const flags = { string: [], boolean: [], alias: {} };
/** @type {string | undefined} */
let currentFlag;
for (const line of lines) {
const flagMatch = line.match(/^\s+(?:-(\w), )?--(\w+)\s+/);
const typeMatch = line.match(/\[(boolean|string|array|number|choices: [^\]]+)\]/);
if (flagMatch && currentFlag) {
throw createFailError(`unable to determine type for flag [${currentFlag}]`);
}
if (flagMatch) {
currentFlag = flagMatch[2];
if (flagMatch[1]) {
flags.alias[flagMatch[1]] = flagMatch[2];
}
}
if (currentFlag && typeMatch) {
switch (typeMatch[1]) {
case 'string':
case 'array':
case 'number':
flags.string.push(currentFlag);
break;
case 'boolean':
flags.boolean.push(currentFlag);
break;
default:
if (typeMatch[1].startsWith('choices: ')) {
flags.string.push(currentFlag);
break;
}
throw createFailError(`unexpected flag type [${typeMatch[1]}]`);
}
currentFlag = undefined;
}
}
await Fsp.writeFile(
Path.resolve(REPO_ROOT, FLAGS_FILE),
JSON.stringify(
{
string: flags.string.sort(function (a, b) {
return a.localeCompare(b);
}),
boolean: flags.boolean.sort(function (a, b) {
return a.localeCompare(b);
}),
alias: Object.fromEntries(
Object.entries(flags.alias).sort(function (a, b) {
return a[0].localeCompare(b[0]);
})
),
},
null,
2
)
);
log.success('wrote jest flag info to', FLAGS_FILE);
log.warning('make sure you bootstrap to rebuild @kbn/test');
},
{
usage: `yarn jest --help | node scripts/read_jest_help.mjs`,
description: `
Jest no longer exposes the ability to parse CLI flags externally, so we use this
script to read the help output and convert it into parameters we can pass to getopts()
which will parse the flags similar to how Jest does it.
getopts() doesn't support things like enums, or number flags, but if we use the generated
config then it will at least interpret which flags are expected, which are invalid, and
allow us to determine the correct config path based on the provided path while passing
the rest of the args directly to jest.
`,
flags: {
allowUnexpected: true,
guessTypesForUnexpectedFlags: false,
},
}
);