[bazel/packages] make package discovery dirs easier to extend (#127836)

This commit is contained in:
Spencer 2022-03-16 08:57:29 -06:00 committed by GitHub
parent 5641dcc12c
commit cc3d190e1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 8 deletions

View file

@ -0,0 +1,17 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/
/**
* This is a list of repo-relative paths to directories containing packages. Do not
* include `**` in these, one or two `*` segments is acceptable, we need this search
* to be super fast so please avoid deep recursive searching.
*
* eg. src/vis-editors => would find a package at src/vis-editors/foo/package.json
* src/vis-editors/* => would find a package at src/vis-editors/foo/bar/package.json
*/
export const BAZEL_PACKAGE_DIRS = ['packages'];

View file

@ -13,20 +13,24 @@ import { REPO_ROOT } from '@kbn/utils';
import { asyncMapWithLimit } from '@kbn/std';
import { BazelPackage } from './bazel_package';
import { BAZEL_PACKAGE_DIRS } from './bazel_package_dirs';
/**
* Search the local Kibana repo for bazel packages and return an array of BazelPackage objects
* representing each package found.
*/
export async function discoverBazelPackages() {
const packageJsons = globby.sync('*/package.json', {
cwd: Path.resolve(REPO_ROOT, 'packages'),
absolute: true,
});
const packageJsons = globby.sync(
BAZEL_PACKAGE_DIRS.map((dir) => `${dir}/*/package.json`),
{
cwd: REPO_ROOT,
absolute: true,
}
);
return await asyncMapWithLimit(
packageJsons.sort((a, b) => a.localeCompare(b)),
10,
(path) => BazelPackage.fromDir(Path.dirname(path))
100,
async (path) => await BazelPackage.fromDir(Path.dirname(path))
);
}

View file

@ -6,5 +6,6 @@
* Side Public License, v 1.
*/
export * from './bazel_package_dirs';
export * from './discover_packages';
export type { BazelPackage } from './bazel_package';

View file

@ -30,6 +30,7 @@ RUNTIME_DEPS = [
"//packages/kbn-bazel-packages",
"//packages/kbn-utils",
"@npm//ejs",
"@npm//micromatch",
"@npm//normalize-path",
]
@ -37,7 +38,9 @@ TYPES_DEPS = [
"//packages/kbn-dev-utils:npm_module_types",
"//packages/kbn-bazel-packages:npm_module_types",
"//packages/kbn-utils:npm_module_types",
"@npm//@types/micromatch",
"@npm//ejs",
"@npm//micromatch",
"@npm//normalize-path",
"@npm//@types/ejs",
]

View file

@ -12,8 +12,9 @@ import Path from 'path';
import normalizePath from 'normalize-path';
import globby from 'globby';
import micromatch from 'micromatch';
import { REPO_ROOT } from '@kbn/utils';
import { discoverBazelPackages } from '@kbn/bazel-packages';
import { discoverBazelPackages, BAZEL_PACKAGE_DIRS } from '@kbn/bazel-packages';
import { createFailError, createFlagError, isFailError, sortPackageJson } from '@kbn/dev-utils';
import { TEMPLATE_DIR, ROOT_PKG_DIR, PKG_TEMPLATE_DIR } from '../paths';
@ -30,8 +31,10 @@ export const PackageCommand: GenerateCommand = {
--dev Generate a package which is intended for dev-only use and can access things like devDependencies
--web Build webpack-compatible version of sources for this package. If your package is intended to be
used in the browser and Node.js then you need to opt-into these sources being created.
--dir Directory where this package will live, defaults to [./packages]
--force If the packageDir already exists, delete it before generation
--dir Directory where this package will live, defaults to [./packages]
Valid Options:
${BAZEL_PACKAGE_DIRS.map((rel) => ` ${rel}\n`).join('')}
`,
},
async run({ log, flags, render }) {
@ -48,6 +51,13 @@ export const PackageCommand: GenerateCommand = {
const dev = !!flags.dev;
const containingDir = flags.dir ? Path.resolve(`${flags.dir}`) : ROOT_PKG_DIR;
const relContainingDir = Path.relative(REPO_ROOT, containingDir);
if (!micromatch.isMatch(relContainingDir, BAZEL_PACKAGE_DIRS)) {
throw createFlagError(
'Invalid --dir selection. To setup a new --dir option extend the `BAZEL_PACKAGE_DIRS` const in `@kbn/bazel-packages` and make sure to rebuild.'
);
}
const packageDir = Path.resolve(containingDir, name.slice(1).replace('/', '-'));
const normalizedRepoRelativeDir = normalizePath(Path.relative(REPO_ROOT, packageDir));