From 6b0bd5b5709f2006617bd29ac2a1638b5bd88a41 Mon Sep 17 00:00:00 2001 From: Spencer Date: Tue, 23 Aug 2022 14:46:52 -0700 Subject: [PATCH] [kbn/bazel-packages] convert to js (#139235) --- packages/BUILD.bazel | 142 +++++++++--------- packages/kbn-bazel-packages/BUILD.bazel | 14 +- packages/kbn-bazel-packages/src/async.js | 107 +++++++++++++ packages/kbn-bazel-packages/src/async.test.ts | 61 ++++++++ .../{bazel_package.ts => bazel_package.js} | 43 ++++-- ..._package_dirs.ts => bazel_package_dirs.js} | 26 ++-- .../src/discover_packages.js | 44 ++++++ .../src/discover_packages.ts | 56 ------- packages/kbn-bazel-packages/src/find_files.js | 114 ++++++++++++++ packages/kbn-bazel-packages/src/index.js | 19 +++ packages/kbn-bazel-packages/src/index.ts | 11 -- ..._package_json.ts => parse_package_json.js} | 40 ++--- packages/kbn-bazel-packages/src/types.ts | 31 ++++ packages/kbn-bazel-packages/tsconfig.json | 2 + .../src/commands/package_command.ts | 2 +- .../packages_build_manifest_command.ts | 2 +- src/dev/build/tasks/build_packages_task.ts | 3 +- src/dev/build/tasks/clean_tasks.ts | 3 +- 18 files changed, 509 insertions(+), 211 deletions(-) create mode 100644 packages/kbn-bazel-packages/src/async.js create mode 100644 packages/kbn-bazel-packages/src/async.test.ts rename packages/kbn-bazel-packages/src/{bazel_package.ts => bazel_package.js} (67%) rename packages/kbn-bazel-packages/src/{bazel_package_dirs.ts => bazel_package_dirs.js} (68%) create mode 100644 packages/kbn-bazel-packages/src/discover_packages.js delete mode 100644 packages/kbn-bazel-packages/src/discover_packages.ts create mode 100644 packages/kbn-bazel-packages/src/find_files.js create mode 100644 packages/kbn-bazel-packages/src/index.js delete mode 100644 packages/kbn-bazel-packages/src/index.ts rename packages/kbn-bazel-packages/src/{parse_package_json.ts => parse_package_json.js} (57%) create mode 100644 packages/kbn-bazel-packages/src/types.ts diff --git a/packages/BUILD.bazel b/packages/BUILD.bazel index b084fa8617929..812d73e82826f 100644 --- a/packages/BUILD.bazel +++ b/packages/BUILD.bazel @@ -14,110 +14,110 @@ filegroup( "//packages/analytics/shippers/elastic_v3/common:build", "//packages/analytics/shippers/elastic_v3/server:build", "//packages/analytics/shippers/fullstory:build", + "//packages/core/analytics/core-analytics-browser:build", "//packages/core/analytics/core-analytics-browser-internal:build", "//packages/core/analytics/core-analytics-browser-mocks:build", - "//packages/core/analytics/core-analytics-browser:build", + "//packages/core/analytics/core-analytics-server:build", "//packages/core/analytics/core-analytics-server-internal:build", "//packages/core/analytics/core-analytics-server-mocks:build", - "//packages/core/analytics/core-analytics-server:build", "//packages/core/base/core-base-browser-internal:build", "//packages/core/base/core-base-browser-mocks:build", - "//packages/core/base/core-base-common-internal:build", "//packages/core/base/core-base-common:build", + "//packages/core/base/core-base-common-internal:build", "//packages/core/base/core-base-server-internal:build", "//packages/core/base/core-base-server-mocks:build", "//packages/core/capabilities/core-capabilities-common:build", + "//packages/core/capabilities/core-capabilities-server:build", "//packages/core/capabilities/core-capabilities-server-internal:build", "//packages/core/capabilities/core-capabilities-server-mocks:build", - "//packages/core/capabilities/core-capabilities-server:build", "//packages/core/config/core-config-server-internal:build", + "//packages/core/deprecations/core-deprecations-browser:build", "//packages/core/deprecations/core-deprecations-browser-internal:build", "//packages/core/deprecations/core-deprecations-browser-mocks:build", - "//packages/core/deprecations/core-deprecations-browser:build", "//packages/core/deprecations/core-deprecations-common:build", + "//packages/core/doc-links/core-doc-links-browser:build", "//packages/core/doc-links/core-doc-links-browser-internal:build", "//packages/core/doc-links/core-doc-links-browser-mocks:build", - "//packages/core/doc-links/core-doc-links-browser:build", + "//packages/core/doc-links/core-doc-links-server:build", "//packages/core/doc-links/core-doc-links-server-internal:build", "//packages/core/doc-links/core-doc-links-server-mocks:build", - "//packages/core/doc-links/core-doc-links-server:build", "//packages/core/elasticsearch/core-elasticsearch-client-server-internal:build", "//packages/core/elasticsearch/core-elasticsearch-client-server-mocks:build", + "//packages/core/elasticsearch/core-elasticsearch-server:build", "//packages/core/elasticsearch/core-elasticsearch-server-internal:build", "//packages/core/elasticsearch/core-elasticsearch-server-mocks:build", - "//packages/core/elasticsearch/core-elasticsearch-server:build", "//packages/core/environment/core-environment-server-internal:build", "//packages/core/environment/core-environment-server-mocks:build", + "//packages/core/execution-context/core-execution-context-browser:build", "//packages/core/execution-context/core-execution-context-browser-internal:build", "//packages/core/execution-context/core-execution-context-browser-mocks:build", - "//packages/core/execution-context/core-execution-context-browser:build", "//packages/core/execution-context/core-execution-context-common:build", + "//packages/core/execution-context/core-execution-context-server:build", "//packages/core/execution-context/core-execution-context-server-internal:build", "//packages/core/execution-context/core-execution-context-server-mocks:build", - "//packages/core/execution-context/core-execution-context-server:build", + "//packages/core/fatal-errors/core-fatal-errors-browser:build", "//packages/core/fatal-errors/core-fatal-errors-browser-internal:build", "//packages/core/fatal-errors/core-fatal-errors-browser-mocks:build", - "//packages/core/fatal-errors/core-fatal-errors-browser:build", + "//packages/core/http/core-http-browser:build", "//packages/core/http/core-http-browser-internal:build", "//packages/core/http/core-http-browser-mocks:build", - "//packages/core/http/core-http-browser:build", "//packages/core/http/core-http-common:build", "//packages/core/http/core-http-context-server-internal:build", "//packages/core/http/core-http-context-server-mocks:build", "//packages/core/http/core-http-router-server-internal:build", "//packages/core/http/core-http-router-server-mocks:build", + "//packages/core/http/core-http-server:build", "//packages/core/http/core-http-server-internal:build", "//packages/core/http/core-http-server-mocks:build", - "//packages/core/http/core-http-server:build", + "//packages/core/i18n/core-i18n-browser:build", "//packages/core/i18n/core-i18n-browser-internal:build", "//packages/core/i18n/core-i18n-browser-mocks:build", - "//packages/core/i18n/core-i18n-browser:build", + "//packages/core/injected-metadata/core-injected-metadata-browser:build", "//packages/core/injected-metadata/core-injected-metadata-browser-internal:build", "//packages/core/injected-metadata/core-injected-metadata-browser-mocks:build", - "//packages/core/injected-metadata/core-injected-metadata-browser:build", "//packages/core/injected-metadata/core-injected-metadata-common-internal:build", "//packages/core/integrations/core-integrations-browser-internal:build", "//packages/core/integrations/core-integrations-browser-mocks:build", + "//packages/core/logging/core-logging-server:build", "//packages/core/logging/core-logging-server-internal:build", "//packages/core/logging/core-logging-server-mocks:build", - "//packages/core/logging/core-logging-server:build", "//packages/core/metrics/core-metrics-collectors-server-internal:build", "//packages/core/metrics/core-metrics-collectors-server-mocks:build", + "//packages/core/metrics/core-metrics-server:build", "//packages/core/metrics/core-metrics-server-internal:build", "//packages/core/metrics/core-metrics-server-mocks:build", - "//packages/core/metrics/core-metrics-server:build", - "//packages/core/mount-utils/core-mount-utils-browser-internal:build", "//packages/core/mount-utils/core-mount-utils-browser:build", + "//packages/core/mount-utils/core-mount-utils-browser-internal:build", + "//packages/core/node/core-node-server:build", "//packages/core/node/core-node-server-internal:build", "//packages/core/node/core-node-server-mocks:build", - "//packages/core/node/core-node-server:build", + "//packages/core/notifications/core-notifications-browser:build", "//packages/core/notifications/core-notifications-browser-internal:build", "//packages/core/notifications/core-notifications-browser-mocks:build", - "//packages/core/notifications/core-notifications-browser:build", + "//packages/core/overlays/core-overlays-browser:build", "//packages/core/overlays/core-overlays-browser-internal:build", "//packages/core/overlays/core-overlays-browser-mocks:build", - "//packages/core/overlays/core-overlays-browser:build", + "//packages/core/preboot/core-preboot-server:build", "//packages/core/preboot/core-preboot-server-internal:build", "//packages/core/preboot/core-preboot-server-mocks:build", - "//packages/core/preboot/core-preboot-server:build", "//packages/core/saved-objects/core-saved-objects-api-browser:build", "//packages/core/saved-objects/core-saved-objects-api-server:build", "//packages/core/saved-objects/core-saved-objects-base-server-internal:build", "//packages/core/saved-objects/core-saved-objects-base-server-mocks:build", + "//packages/core/saved-objects/core-saved-objects-browser:build", "//packages/core/saved-objects/core-saved-objects-browser-internal:build", "//packages/core/saved-objects/core-saved-objects-browser-mocks:build", - "//packages/core/saved-objects/core-saved-objects-browser:build", "//packages/core/saved-objects/core-saved-objects-common:build", "//packages/core/saved-objects/core-saved-objects-server:build", "//packages/core/saved-objects/core-saved-objects-utils-server:build", "//packages/core/test-helpers/core-test-helpers-deprecations-getters:build", "//packages/core/test-helpers/core-test-helpers-http-setup-browser:build", + "//packages/core/theme/core-theme-browser:build", "//packages/core/theme/core-theme-browser-internal:build", "//packages/core/theme/core-theme-browser-mocks:build", - "//packages/core/theme/core-theme-browser:build", + "//packages/core/ui-settings/core-ui-settings-browser:build", "//packages/core/ui-settings/core-ui-settings-browser-internal:build", "//packages/core/ui-settings/core-ui-settings-browser-mocks:build", - "//packages/core/ui-settings/core-ui-settings-browser:build", "//packages/core/ui-settings/core-ui-settings-common:build", "//packages/home/sample_data_card:build", "//packages/home/sample_data_tab:build", @@ -141,11 +141,11 @@ filegroup( "//packages/kbn-ci-stats-reporter:build", "//packages/kbn-cli-dev-mode:build", "//packages/kbn-coloring:build", + "//packages/kbn-config:build", "//packages/kbn-config-mocks:build", "//packages/kbn-config-schema:build", - "//packages/kbn-config:build", - "//packages/kbn-crypto-browser:build", "//packages/kbn-crypto:build", + "//packages/kbn-crypto-browser:build", "//packages/kbn-datemath:build", "//packages/kbn-dev-cli-errors:build", "//packages/kbn-dev-cli-runner:build", @@ -154,10 +154,10 @@ filegroup( "//packages/kbn-doc-links:build", "//packages/kbn-docs-utils:build", "//packages/kbn-ebt-tools:build", + "//packages/kbn-es:build", "//packages/kbn-es-archiver:build", "//packages/kbn-es-errors:build", "//packages/kbn-es-query:build", - "//packages/kbn-es:build", "//packages/kbn-eslint-config:build", "//packages/kbn-eslint-plugin-disable:build", "//packages/kbn-eslint-plugin-eslint:build", @@ -170,8 +170,8 @@ filegroup( "//packages/kbn-get-repo-files:build", "//packages/kbn-handlebars:build", "//packages/kbn-hapi-mocks:build", - "//packages/kbn-i18n-react:build", "//packages/kbn-i18n:build", + "//packages/kbn-i18n-react:build", "//packages/kbn-import-resolver:build", "//packages/kbn-interpreter:build", "//packages/kbn-io-ts-utils:build", @@ -179,21 +179,21 @@ filegroup( "//packages/kbn-jsonc:build", "//packages/kbn-kibana-manifest-parser:build", "//packages/kbn-kibana-manifest-schema:build", - "//packages/kbn-logging-mocks:build", "//packages/kbn-logging:build", - "//packages/kbn-managed-vscode-config-cli:build", + "//packages/kbn-logging-mocks:build", "//packages/kbn-managed-vscode-config:build", + "//packages/kbn-managed-vscode-config-cli:build", "//packages/kbn-mapbox-gl:build", "//packages/kbn-monaco:build", - "//packages/kbn-optimizer-webpack-helpers:build", "//packages/kbn-optimizer:build", + "//packages/kbn-optimizer-webpack-helpers:build", "//packages/kbn-performance-testing-dataset-extractor:build", "//packages/kbn-plugin-discovery:build", "//packages/kbn-plugin-generator:build", "//packages/kbn-plugin-helpers:build", "//packages/kbn-react-field:build", - "//packages/kbn-repo-source-classifier-cli:build", "//packages/kbn-repo-source-classifier:build", + "//packages/kbn-repo-source-classifier-cli:build", "//packages/kbn-rule-data-utils:build", "//packages/kbn-safer-lodash-set:build", "//packages/kbn-securitysolution-autocomplete:build", @@ -222,23 +222,23 @@ filegroup( "//packages/kbn-storybook:build", "//packages/kbn-synthetic-package-map:build", "//packages/kbn-telemetry-tools:build", + "//packages/kbn-test:build", "//packages/kbn-test-jest-helpers:build", "//packages/kbn-test-subj-selector:build", - "//packages/kbn-test:build", "//packages/kbn-timelion-grammar:build", "//packages/kbn-tinymath:build", "//packages/kbn-tooling-log:build", + "//packages/kbn-type-summarizer:build", "//packages/kbn-type-summarizer-cli:build", "//packages/kbn-type-summarizer-core:build", - "//packages/kbn-type-summarizer:build", "//packages/kbn-typed-react-router-config:build", "//packages/kbn-ui-framework:build", "//packages/kbn-ui-shared-deps-npm:build", "//packages/kbn-ui-shared-deps-src:build", "//packages/kbn-ui-theme:build", "//packages/kbn-user-profile-components:build", - "//packages/kbn-utility-types-jest:build", "//packages/kbn-utility-types:build", + "//packages/kbn-utility-types-jest:build", "//packages/kbn-utils:build", "//packages/kbn-yarn-lock-validator:build", "//packages/shared-ux/avatar/solution:build", @@ -290,110 +290,110 @@ filegroup( "//packages/analytics/shippers/elastic_v3/common:build_types", "//packages/analytics/shippers/elastic_v3/server:build_types", "//packages/analytics/shippers/fullstory:build_types", + "//packages/core/analytics/core-analytics-browser:build_types", "//packages/core/analytics/core-analytics-browser-internal:build_types", "//packages/core/analytics/core-analytics-browser-mocks:build_types", - "//packages/core/analytics/core-analytics-browser:build_types", + "//packages/core/analytics/core-analytics-server:build_types", "//packages/core/analytics/core-analytics-server-internal:build_types", "//packages/core/analytics/core-analytics-server-mocks:build_types", - "//packages/core/analytics/core-analytics-server:build_types", "//packages/core/base/core-base-browser-internal:build_types", "//packages/core/base/core-base-browser-mocks:build_types", - "//packages/core/base/core-base-common-internal:build_types", "//packages/core/base/core-base-common:build_types", + "//packages/core/base/core-base-common-internal:build_types", "//packages/core/base/core-base-server-internal:build_types", "//packages/core/base/core-base-server-mocks:build_types", "//packages/core/capabilities/core-capabilities-common:build_types", + "//packages/core/capabilities/core-capabilities-server:build_types", "//packages/core/capabilities/core-capabilities-server-internal:build_types", "//packages/core/capabilities/core-capabilities-server-mocks:build_types", - "//packages/core/capabilities/core-capabilities-server:build_types", "//packages/core/config/core-config-server-internal:build_types", + "//packages/core/deprecations/core-deprecations-browser:build_types", "//packages/core/deprecations/core-deprecations-browser-internal:build_types", "//packages/core/deprecations/core-deprecations-browser-mocks:build_types", - "//packages/core/deprecations/core-deprecations-browser:build_types", "//packages/core/deprecations/core-deprecations-common:build_types", + "//packages/core/doc-links/core-doc-links-browser:build_types", "//packages/core/doc-links/core-doc-links-browser-internal:build_types", "//packages/core/doc-links/core-doc-links-browser-mocks:build_types", - "//packages/core/doc-links/core-doc-links-browser:build_types", + "//packages/core/doc-links/core-doc-links-server:build_types", "//packages/core/doc-links/core-doc-links-server-internal:build_types", "//packages/core/doc-links/core-doc-links-server-mocks:build_types", - "//packages/core/doc-links/core-doc-links-server:build_types", "//packages/core/elasticsearch/core-elasticsearch-client-server-internal:build_types", "//packages/core/elasticsearch/core-elasticsearch-client-server-mocks:build_types", + "//packages/core/elasticsearch/core-elasticsearch-server:build_types", "//packages/core/elasticsearch/core-elasticsearch-server-internal:build_types", "//packages/core/elasticsearch/core-elasticsearch-server-mocks:build_types", - "//packages/core/elasticsearch/core-elasticsearch-server:build_types", "//packages/core/environment/core-environment-server-internal:build_types", "//packages/core/environment/core-environment-server-mocks:build_types", + "//packages/core/execution-context/core-execution-context-browser:build_types", "//packages/core/execution-context/core-execution-context-browser-internal:build_types", "//packages/core/execution-context/core-execution-context-browser-mocks:build_types", - "//packages/core/execution-context/core-execution-context-browser:build_types", "//packages/core/execution-context/core-execution-context-common:build_types", + "//packages/core/execution-context/core-execution-context-server:build_types", "//packages/core/execution-context/core-execution-context-server-internal:build_types", "//packages/core/execution-context/core-execution-context-server-mocks:build_types", - "//packages/core/execution-context/core-execution-context-server:build_types", + "//packages/core/fatal-errors/core-fatal-errors-browser:build_types", "//packages/core/fatal-errors/core-fatal-errors-browser-internal:build_types", "//packages/core/fatal-errors/core-fatal-errors-browser-mocks:build_types", - "//packages/core/fatal-errors/core-fatal-errors-browser:build_types", + "//packages/core/http/core-http-browser:build_types", "//packages/core/http/core-http-browser-internal:build_types", "//packages/core/http/core-http-browser-mocks:build_types", - "//packages/core/http/core-http-browser:build_types", "//packages/core/http/core-http-common:build_types", "//packages/core/http/core-http-context-server-internal:build_types", "//packages/core/http/core-http-context-server-mocks:build_types", "//packages/core/http/core-http-router-server-internal:build_types", "//packages/core/http/core-http-router-server-mocks:build_types", + "//packages/core/http/core-http-server:build_types", "//packages/core/http/core-http-server-internal:build_types", "//packages/core/http/core-http-server-mocks:build_types", - "//packages/core/http/core-http-server:build_types", + "//packages/core/i18n/core-i18n-browser:build_types", "//packages/core/i18n/core-i18n-browser-internal:build_types", "//packages/core/i18n/core-i18n-browser-mocks:build_types", - "//packages/core/i18n/core-i18n-browser:build_types", + "//packages/core/injected-metadata/core-injected-metadata-browser:build_types", "//packages/core/injected-metadata/core-injected-metadata-browser-internal:build_types", "//packages/core/injected-metadata/core-injected-metadata-browser-mocks:build_types", - "//packages/core/injected-metadata/core-injected-metadata-browser:build_types", "//packages/core/injected-metadata/core-injected-metadata-common-internal:build_types", "//packages/core/integrations/core-integrations-browser-internal:build_types", "//packages/core/integrations/core-integrations-browser-mocks:build_types", + "//packages/core/logging/core-logging-server:build_types", "//packages/core/logging/core-logging-server-internal:build_types", "//packages/core/logging/core-logging-server-mocks:build_types", - "//packages/core/logging/core-logging-server:build_types", "//packages/core/metrics/core-metrics-collectors-server-internal:build_types", "//packages/core/metrics/core-metrics-collectors-server-mocks:build_types", + "//packages/core/metrics/core-metrics-server:build_types", "//packages/core/metrics/core-metrics-server-internal:build_types", "//packages/core/metrics/core-metrics-server-mocks:build_types", - "//packages/core/metrics/core-metrics-server:build_types", - "//packages/core/mount-utils/core-mount-utils-browser-internal:build_types", "//packages/core/mount-utils/core-mount-utils-browser:build_types", + "//packages/core/mount-utils/core-mount-utils-browser-internal:build_types", + "//packages/core/node/core-node-server:build_types", "//packages/core/node/core-node-server-internal:build_types", "//packages/core/node/core-node-server-mocks:build_types", - "//packages/core/node/core-node-server:build_types", + "//packages/core/notifications/core-notifications-browser:build_types", "//packages/core/notifications/core-notifications-browser-internal:build_types", "//packages/core/notifications/core-notifications-browser-mocks:build_types", - "//packages/core/notifications/core-notifications-browser:build_types", + "//packages/core/overlays/core-overlays-browser:build_types", "//packages/core/overlays/core-overlays-browser-internal:build_types", "//packages/core/overlays/core-overlays-browser-mocks:build_types", - "//packages/core/overlays/core-overlays-browser:build_types", + "//packages/core/preboot/core-preboot-server:build_types", "//packages/core/preboot/core-preboot-server-internal:build_types", "//packages/core/preboot/core-preboot-server-mocks:build_types", - "//packages/core/preboot/core-preboot-server:build_types", "//packages/core/saved-objects/core-saved-objects-api-browser:build_types", "//packages/core/saved-objects/core-saved-objects-api-server:build_types", "//packages/core/saved-objects/core-saved-objects-base-server-internal:build_types", "//packages/core/saved-objects/core-saved-objects-base-server-mocks:build_types", + "//packages/core/saved-objects/core-saved-objects-browser:build_types", "//packages/core/saved-objects/core-saved-objects-browser-internal:build_types", "//packages/core/saved-objects/core-saved-objects-browser-mocks:build_types", - "//packages/core/saved-objects/core-saved-objects-browser:build_types", "//packages/core/saved-objects/core-saved-objects-common:build_types", "//packages/core/saved-objects/core-saved-objects-server:build_types", "//packages/core/saved-objects/core-saved-objects-utils-server:build_types", "//packages/core/test-helpers/core-test-helpers-deprecations-getters:build_types", "//packages/core/test-helpers/core-test-helpers-http-setup-browser:build_types", + "//packages/core/theme/core-theme-browser:build_types", "//packages/core/theme/core-theme-browser-internal:build_types", "//packages/core/theme/core-theme-browser-mocks:build_types", - "//packages/core/theme/core-theme-browser:build_types", + "//packages/core/ui-settings/core-ui-settings-browser:build_types", "//packages/core/ui-settings/core-ui-settings-browser-internal:build_types", "//packages/core/ui-settings/core-ui-settings-browser-mocks:build_types", - "//packages/core/ui-settings/core-ui-settings-browser:build_types", "//packages/core/ui-settings/core-ui-settings-common:build_types", "//packages/home/sample_data_card:build_types", "//packages/home/sample_data_tab:build_types", @@ -412,11 +412,11 @@ filegroup( "//packages/kbn-ci-stats-reporter:build_types", "//packages/kbn-cli-dev-mode:build_types", "//packages/kbn-coloring:build_types", + "//packages/kbn-config:build_types", "//packages/kbn-config-mocks:build_types", "//packages/kbn-config-schema:build_types", - "//packages/kbn-config:build_types", - "//packages/kbn-crypto-browser:build_types", "//packages/kbn-crypto:build_types", + "//packages/kbn-crypto-browser:build_types", "//packages/kbn-datemath:build_types", "//packages/kbn-dev-cli-errors:build_types", "//packages/kbn-dev-cli-runner:build_types", @@ -436,8 +436,8 @@ filegroup( "//packages/kbn-get-repo-files:build_types", "//packages/kbn-handlebars:build_types", "//packages/kbn-hapi-mocks:build_types", - "//packages/kbn-i18n-react:build_types", "//packages/kbn-i18n:build_types", + "//packages/kbn-i18n-react:build_types", "//packages/kbn-import-resolver:build_types", "//packages/kbn-interpreter:build_types", "//packages/kbn-io-ts-utils:build_types", @@ -445,21 +445,21 @@ filegroup( "//packages/kbn-jsonc:build_types", "//packages/kbn-kibana-manifest-parser:build_types", "//packages/kbn-kibana-manifest-schema:build_types", - "//packages/kbn-logging-mocks:build_types", "//packages/kbn-logging:build_types", - "//packages/kbn-managed-vscode-config-cli:build_types", + "//packages/kbn-logging-mocks:build_types", "//packages/kbn-managed-vscode-config:build_types", + "//packages/kbn-managed-vscode-config-cli:build_types", "//packages/kbn-mapbox-gl:build_types", "//packages/kbn-monaco:build_types", - "//packages/kbn-optimizer-webpack-helpers:build_types", "//packages/kbn-optimizer:build_types", + "//packages/kbn-optimizer-webpack-helpers:build_types", "//packages/kbn-performance-testing-dataset-extractor:build_types", "//packages/kbn-plugin-discovery:build_types", "//packages/kbn-plugin-generator:build_types", "//packages/kbn-plugin-helpers:build_types", "//packages/kbn-react-field:build_types", - "//packages/kbn-repo-source-classifier-cli:build_types", "//packages/kbn-repo-source-classifier:build_types", + "//packages/kbn-repo-source-classifier-cli:build_types", "//packages/kbn-rule-data-utils:build_types", "//packages/kbn-safer-lodash-set:build_types", "//packages/kbn-securitysolution-autocomplete:build_types", @@ -486,19 +486,19 @@ filegroup( "//packages/kbn-stdio-dev-helpers:build_types", "//packages/kbn-storybook:build_types", "//packages/kbn-telemetry-tools:build_types", - "//packages/kbn-test-jest-helpers:build_types", "//packages/kbn-test:build_types", + "//packages/kbn-test-jest-helpers:build_types", "//packages/kbn-tooling-log:build_types", + "//packages/kbn-type-summarizer:build_types", "//packages/kbn-type-summarizer-cli:build_types", "//packages/kbn-type-summarizer-core:build_types", - "//packages/kbn-type-summarizer:build_types", "//packages/kbn-typed-react-router-config:build_types", "//packages/kbn-ui-shared-deps-npm:build_types", "//packages/kbn-ui-shared-deps-src:build_types", "//packages/kbn-ui-theme:build_types", "//packages/kbn-user-profile-components:build_types", - "//packages/kbn-utility-types-jest:build_types", "//packages/kbn-utility-types:build_types", + "//packages/kbn-utility-types-jest:build_types", "//packages/kbn-utils:build_types", "//packages/kbn-yarn-lock-validator:build_types", "//packages/shared-ux/avatar/solution:build_types", diff --git a/packages/kbn-bazel-packages/BUILD.bazel b/packages/kbn-bazel-packages/BUILD.bazel index e3a3545be167d..9c7a793459623 100644 --- a/packages/kbn-bazel-packages/BUILD.bazel +++ b/packages/kbn-bazel-packages/BUILD.bazel @@ -7,6 +7,7 @@ PKG_REQUIRE_NAME = "@kbn/bazel-packages" SOURCE_FILES = glob( [ + "src/**/*.js", "src/**/*.ts", ], exclude = [ @@ -37,11 +38,6 @@ NPM_MODULE_EXTRA_FILES = [ # "@npm//name-of-package" # eg. "@npm//lodash" RUNTIME_DEPS = [ - "//packages/kbn-utils", - "//packages/kbn-std", - "//packages/kbn-synthetic-package-map", - "@npm//globby", - "@npm//normalize-path", ] # In this array place dependencies necessary to build the types, which will include the @@ -55,13 +51,8 @@ RUNTIME_DEPS = [ # References to NPM packages work the same as RUNTIME_DEPS: # eg. "@npm//@types/babel__core" TYPES_DEPS = [ - "//packages/kbn-utils:npm_module_types", - "//packages/kbn-std:npm_module_types", - "//packages/kbn-synthetic-package-map:npm_module_types", "@npm//@types/node", - "@npm//@types/normalize-path", - "@npm//globby", - "@npm//normalize-path", + "@npm//@types/jest", ] jsts_transpiler( @@ -87,6 +78,7 @@ ts_project( deps = TYPES_DEPS, declaration = True, declaration_map = True, + allow_js = True, emit_declaration_only = True, out_dir = "target_types", root_dir = "src", diff --git a/packages/kbn-bazel-packages/src/async.js b/packages/kbn-bazel-packages/src/async.js new file mode 100644 index 0000000000000..38b535447953f --- /dev/null +++ b/packages/kbn-bazel-packages/src/async.js @@ -0,0 +1,107 @@ +/* + * 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. + */ + +/** + * + * @template T + * @template T2 + * @param {(v: T) => Promise} fn + * @param {T} item + * @returns {Promise>} + */ +const settle = async (fn, item) => { + const [result] = await Promise.allSettled([(async () => fn(item))()]); + return result; +}; + +/** + * @template T + * @template T2 + * @param {Array} source + * @param {number} limit + * @param {(v: T) => Promise} mapFn + * @returns {Promise} + */ +function asyncMapWithLimit(source, limit, mapFn) { + return new Promise((resolve, reject) => { + if (limit < 1) { + reject(new Error('invalid limit, must be greater than 0')); + return; + } + + let failed = false; + let inProgress = 0; + const queue = [...source.entries()]; + + /** @type {T2[]} */ + const results = new Array(source.length); + + /** + * this is run for each item, manages the inProgress state, + * calls the mapFn with that item, writes the map result to + * the result array, and calls runMore() after each item + * completes to either start another item or resolve the + * returned promise. + * + * @param {number} index + * @param {T} item + */ + function run(index, item) { + inProgress += 1; + settle(mapFn, item).then((result) => { + inProgress -= 1; + + if (failed) { + return; + } + + if (result.status === 'fulfilled') { + results[index] = result.value; + runMore(); + return; + } + + // when an error occurs we update the state to prevent + // holding onto old results and ignore future results + // from in-progress promises + failed = true; + results.length = 0; + reject(result.reason); + }); + } + + /** + * If there is work in the queue, schedule it, if there isn't + * any work to be scheduled and there isn't anything in progress + * then we're done. This function is called every time a mapFn + * promise resolves and once after initialization + */ + function runMore() { + if (!queue.length) { + if (inProgress === 0) { + resolve(results); + } + + return; + } + + while (inProgress < limit) { + const entry = queue.shift(); + if (!entry) { + break; + } + + run(...entry); + } + } + + runMore(); + }); +} + +module.exports = { asyncMapWithLimit }; diff --git a/packages/kbn-bazel-packages/src/async.test.ts b/packages/kbn-bazel-packages/src/async.test.ts new file mode 100644 index 0000000000000..b238783f74fb8 --- /dev/null +++ b/packages/kbn-bazel-packages/src/async.test.ts @@ -0,0 +1,61 @@ +/* + * 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. + */ + +import { setTimeout } from 'timers/promises'; +import { asyncMapWithLimit } from './async'; + +const NUMS = [1, 2, 3, 4]; +const ident = jest.fn(async function ident(x: T) { + return await x; +}); +const double = jest.fn(async function double(x: number) { + return x * 2; +}); + +beforeEach(() => { + jest.clearAllMocks(); +}); + +it('resolves with an empty array', async () => { + const result = await asyncMapWithLimit([], 10, ident); + expect(ident).not.toHaveBeenCalled(); + expect(result).toEqual([]); +}); + +it('resolves with a mapped array', async () => { + const result = await asyncMapWithLimit(NUMS, 10, double); + expect(double).toHaveBeenCalledTimes(NUMS.length); + expect(result.join(',')).toMatchInlineSnapshot(`"2,4,6,8"`); +}); + +it('rejects when limit it not >= 1', async () => { + await expect(() => asyncMapWithLimit([], -1, ident)).rejects.toMatchInlineSnapshot( + `[Error: invalid limit, must be greater than 0]` + ); + await expect(() => asyncMapWithLimit([], 0, ident)).rejects.toMatchInlineSnapshot( + `[Error: invalid limit, must be greater than 0]` + ); + await expect(() => asyncMapWithLimit([], -Infinity, ident)).rejects.toMatchInlineSnapshot( + `[Error: invalid limit, must be greater than 0]` + ); +}); + +it('rejects with the first error produced and stops calling mapFn', async () => { + const map = jest.fn(async (num) => { + if (num % 2 === 0) { + throw new Error('even numbers are not supported'); + } + return num * 2; + }); + + await expect(() => asyncMapWithLimit(NUMS, 1, map)).rejects.toMatchInlineSnapshot( + `[Error: even numbers are not supported]` + ); + await setTimeout(10); + expect(map).toHaveBeenCalledTimes(2); +}); diff --git a/packages/kbn-bazel-packages/src/bazel_package.ts b/packages/kbn-bazel-packages/src/bazel_package.js similarity index 67% rename from packages/kbn-bazel-packages/src/bazel_package.ts rename to packages/kbn-bazel-packages/src/bazel_package.js index 91e411770175b..c6a31ccc0dbc1 100644 --- a/packages/kbn-bazel-packages/src/bazel_package.ts +++ b/packages/kbn-bazel-packages/src/bazel_package.js @@ -6,27 +6,31 @@ * Side Public License, v 1. */ -import { inspect } from 'util'; -import Path from 'path'; -import Fsp from 'fs/promises'; +const { inspect } = require('util'); +const Path = require('path'); +const Fsp = require('fs/promises'); -import normalizePath from 'normalize-path'; -import { REPO_ROOT } from '@kbn/utils'; - -import { readPackageJson, ParsedPackageJson } from './parse_package_json'; +/** @typedef {import('./types').ParsedPackageJson} ParsedPackageJson */ +const { readPackageJson } = require('./parse_package_json'); const BUILD_RULE_NAME = /(^|\s)name\s*=\s*"build"/; const BUILD_TYPES_RULE_NAME = /(^|\s)name\s*=\s*"build_types"/; /** * Representation of a Bazel Package in the Kibana repository + * @class + * @property {string} normalizedRepoRelativeDir + * @property {import('./types').ParsedPackageJson} pkg + * @property {string | undefined} buildBazelContent */ -export class BazelPackage { +class BazelPackage { /** * Create a BazelPackage object from a package directory. Reads some files from the package and returns * a Promise for a BazelPackage instance. + * @param {string} repoRoot + * @param {string} dir */ - static async fromDir(dir: string) { + static async fromDir(repoRoot, dir) { const pkg = readPackageJson(Path.resolve(dir, 'package.json')); let buildBazelContent; @@ -36,23 +40,30 @@ export class BazelPackage { throw new Error(`unable to read BUILD.bazel file in [${dir}]: ${error.message}`); } - return new BazelPackage(normalizePath(Path.relative(REPO_ROOT, dir)), pkg, buildBazelContent); + return new BazelPackage(Path.relative(repoRoot, dir), pkg, buildBazelContent); } constructor( /** * Relative path from the root of the repository to the package directory + * @type {string} */ - public readonly normalizedRepoRelativeDir: string, + normalizedRepoRelativeDir, /** * Parsed package.json file from the package + * @type {import('./types').ParsedPackageJson} */ - public readonly pkg: ParsedPackageJson, + pkg, /** * Content of the BUILD.bazel file + * @type {string | undefined} */ - private readonly buildBazelContent?: string - ) {} + buildBazelContent = undefined + ) { + this.normalizedRepoRelativeDir = normalizedRepoRelativeDir; + this.pkg = pkg; + this.buildBazelContent = buildBazelContent; + } /** * Returns true if the package includes a `:build` bazel rule @@ -83,3 +94,7 @@ export class BazelPackage { return `BazelPackage<${this.normalizedRepoRelativeDir}>`; } } + +module.exports = { + BazelPackage, +}; diff --git a/packages/kbn-bazel-packages/src/bazel_package_dirs.ts b/packages/kbn-bazel-packages/src/bazel_package_dirs.js similarity index 68% rename from packages/kbn-bazel-packages/src/bazel_package_dirs.ts rename to packages/kbn-bazel-packages/src/bazel_package_dirs.js index 755ef614c0456..7e3f728a21ca7 100644 --- a/packages/kbn-bazel-packages/src/bazel_package_dirs.ts +++ b/packages/kbn-bazel-packages/src/bazel_package_dirs.js @@ -6,10 +6,7 @@ * Side Public License, v 1. */ -import globby from 'globby'; -import Path from 'path'; - -import { REPO_ROOT } from '@kbn/utils'; +const { expandWildcards } = require('./find_files'); /** * This is a list of repo-relative paths to directories containing packages. Do not @@ -19,7 +16,7 @@ import { REPO_ROOT } from '@kbn/utils'; * 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 = [ +const BAZEL_PACKAGE_DIRS = [ 'packages', 'packages/shared-ux', 'packages/shared-ux/*', @@ -34,18 +31,13 @@ export const BAZEL_PACKAGE_DIRS = [ /** * Resolve all the BAZEL_PACKAGE_DIRS to absolute paths + * @param {string} repoRoot */ -export function getAllBazelPackageDirs() { - return globby.sync(BAZEL_PACKAGE_DIRS, { - cwd: REPO_ROOT, - onlyDirectories: true, - expandDirectories: false, - }); +function getAllBazelPackageDirs(repoRoot) { + return expandWildcards(repoRoot, BAZEL_PACKAGE_DIRS); } -/** - * Resolve all the BAZEL_PACKAGE_DIRS to repo-relative paths - */ -export function getAllRepoRelativeBazelPackageDirs() { - return getAllBazelPackageDirs().map((path) => Path.relative(REPO_ROOT, path)); -} +module.exports = { + BAZEL_PACKAGE_DIRS, + getAllBazelPackageDirs, +}; diff --git a/packages/kbn-bazel-packages/src/discover_packages.js b/packages/kbn-bazel-packages/src/discover_packages.js new file mode 100644 index 0000000000000..17678115c7452 --- /dev/null +++ b/packages/kbn-bazel-packages/src/discover_packages.js @@ -0,0 +1,44 @@ +/* + * 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. + */ + +const Path = require('path'); + +const { BazelPackage } = require('./bazel_package'); +const { getAllBazelPackageDirs } = require('./bazel_package_dirs'); +const { findPackages } = require('./find_files'); +const { asyncMapWithLimit } = require('./async'); + +/** + * @param {string} repoRoot + */ +function discoverBazelPackageLocations(repoRoot) { + const packagesWithBuildBazel = getAllBazelPackageDirs(repoRoot) + .flatMap((packageDir) => findPackages(packageDir, 'BUILD.bazel')) + .map((path) => Path.dirname(path)); + + // NOTE: only return as discovered packages with a package.json + BUILD.bazel file. + // In the future we should change this to only discover the ones with kibana.jsonc. + return getAllBazelPackageDirs(repoRoot) + .flatMap((packageDir) => findPackages(packageDir, 'package.json')) + .map((path) => Path.dirname(path)) + .filter((pkg) => packagesWithBuildBazel.includes(pkg)) + .sort((a, b) => a.localeCompare(b)); +} + +/** + * @param {string} repoRoot + */ +async function discoverBazelPackages(repoRoot) { + return await asyncMapWithLimit( + discoverBazelPackageLocations(repoRoot), + 100, + async (dir) => await BazelPackage.fromDir(repoRoot, dir) + ); +} + +module.exports = { discoverBazelPackageLocations, discoverBazelPackages }; diff --git a/packages/kbn-bazel-packages/src/discover_packages.ts b/packages/kbn-bazel-packages/src/discover_packages.ts deleted file mode 100644 index 8b78e4e293118..0000000000000 --- a/packages/kbn-bazel-packages/src/discover_packages.ts +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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. - */ - -import Path from 'path'; - -import globby from 'globby'; -import normalizePath from 'normalize-path'; -import { REPO_ROOT } from '@kbn/utils'; -import { asyncMapWithLimit } from '@kbn/std'; - -import { BazelPackage } from './bazel_package'; -import { BAZEL_PACKAGE_DIRS } from './bazel_package_dirs'; - -export function discoverBazelPackageLocations(repoRoot: string) { - const packagesWithPackageJson = globby - .sync( - BAZEL_PACKAGE_DIRS.map((dir) => `${dir}/*/package.json`), - { - cwd: repoRoot, - absolute: true, - } - ) - // NOTE: removing x-pack by default for now to prevent a situation where a BUILD.bazel file - // needs to be added at the root of the folder which will make x-pack to be wrongly recognized - // as a Bazel package in that case - .filter((path) => !normalizePath(path).includes('x-pack/package.json')) - .sort((a, b) => a.localeCompare(b)) - .map((path) => Path.dirname(path)); - - const packagesWithBuildBazel = globby - .sync( - BAZEL_PACKAGE_DIRS.map((dir) => `${dir}/*/BUILD.bazel`), - { - cwd: repoRoot, - absolute: true, - } - ) - .map((path) => Path.dirname(path)); - - // NOTE: only return as discovered packages the ones with a package.json + BUILD.bazel file. - // In the future we should change this to only discover the ones declaring kibana.json. - return packagesWithPackageJson.filter((pkg) => packagesWithBuildBazel.includes(pkg)); -} - -export async function discoverBazelPackages(repoRoot: string = REPO_ROOT) { - return await asyncMapWithLimit( - discoverBazelPackageLocations(repoRoot), - 100, - async (dir) => await BazelPackage.fromDir(dir) - ); -} diff --git a/packages/kbn-bazel-packages/src/find_files.js b/packages/kbn-bazel-packages/src/find_files.js new file mode 100644 index 0000000000000..9be6c2e256079 --- /dev/null +++ b/packages/kbn-bazel-packages/src/find_files.js @@ -0,0 +1,114 @@ +/* + * 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. + */ + +const Path = require('path'); +const Fs = require('fs'); + +/** + * @param {string} path + */ +function safeIsFile(path) { + try { + return Fs.statSync(path).isFile(); + } catch (error) { + if (error.code === 'ENOENT') { + return false; + } + + throw error; + } +} + +/** + * @param {string} path + */ +function safeReadDir(path) { + try { + return Fs.readdirSync(path, { + withFileTypes: true, + }); + } catch (error) { + if (error.code === 'ENOENT' || error.code === 'ENOTDIR') { + return []; + } + + throw error; + } +} + +/** + * Search for `name` files in directories within `packageDir` + * + * @param {string} packageDir + * @param {string} name + * @returns {string[]} + */ +function findPackages(packageDir, name) { + return ( + // get the directories within the "package dir" + safeReadDir(packageDir) + // if this directory has a file with the name, it's a match + .flatMap((e) => { + if (!e.isDirectory()) { + return []; + } + + const path = Path.resolve(packageDir, e.name, name); + return safeIsFile(path) ? path : []; + }) + ); +} + +/** + * Expand simple `*` wildcards in patterns, which are otherwise expected to be + * paths relative to `cwd`. + * + * @param {string} cwd + * @param {string[]} patterns + */ +function expandWildcards(cwd, patterns) { + /** @type {Set} */ + const results = new Set(); + + /** @type {Set} */ + const queue = new Set(patterns.map((p) => Path.resolve(cwd, p))); + + for (const pattern of queue) { + let length = 3; + let index = pattern.indexOf('/*/'); + if (index === -1 && pattern.endsWith('/*')) { + length = 2; + index = pattern.length - length; + } + + if (index === -1) { + results.add(pattern); + continue; + } + + const left = pattern.slice(0, index + 1); + const right = pattern.slice(index + length); + for (const ent of safeReadDir(left)) { + if (!ent.isDirectory()) { + continue; + } + + const path = Path.resolve(left, ent.name); + + if (right) { + queue.add(Path.resolve(path, right)); + } else { + results.add(path); + } + } + } + + return [...results]; +} + +module.exports = { findPackages, expandWildcards }; diff --git a/packages/kbn-bazel-packages/src/index.js b/packages/kbn-bazel-packages/src/index.js new file mode 100644 index 0000000000000..254b8bb9da44c --- /dev/null +++ b/packages/kbn-bazel-packages/src/index.js @@ -0,0 +1,19 @@ +/* + * 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. + */ + +/** @typedef {import('./bazel_package').BazelPackage} BazelPackage */ + +const { BAZEL_PACKAGE_DIRS, getAllBazelPackageDirs } = require('./bazel_package_dirs'); +const { discoverBazelPackageLocations, discoverBazelPackages } = require('./discover_packages'); + +module.exports = { + BAZEL_PACKAGE_DIRS, + getAllBazelPackageDirs, + discoverBazelPackageLocations, + discoverBazelPackages, +}; diff --git a/packages/kbn-bazel-packages/src/index.ts b/packages/kbn-bazel-packages/src/index.ts deleted file mode 100644 index 55d5db9bb3adb..0000000000000 --- a/packages/kbn-bazel-packages/src/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -/* - * 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. - */ - -export * from './bazel_package_dirs'; -export * from './discover_packages'; -export type { BazelPackage } from './bazel_package'; diff --git a/packages/kbn-bazel-packages/src/parse_package_json.ts b/packages/kbn-bazel-packages/src/parse_package_json.js similarity index 57% rename from packages/kbn-bazel-packages/src/parse_package_json.ts rename to packages/kbn-bazel-packages/src/parse_package_json.js index b060656c0c512..27a78f91a4466 100644 --- a/packages/kbn-bazel-packages/src/parse_package_json.ts +++ b/packages/kbn-bazel-packages/src/parse_package_json.js @@ -6,40 +6,22 @@ * Side Public License, v 1. */ -import Fs from 'fs'; +const Fs = require('fs'); /** - * Simple parsed representation of a package.json file, validated - * by `assertParsedPackageJson()` and extensible as needed in the future + * @param {unknown} v + * @returns {v is Record} */ -export interface ParsedPackageJson { - /** The name of the package, usually `@kbn/`+something */ - name: string; - /** "dependenices" property from package.json */ - dependencies?: Record; - /** "devDependenices" property from package.json */ - devDependencies?: Record; - /** Some kibana specific properties about this package */ - kibana?: { - /** Is this package only intended for dev? */ - devOnly?: boolean; - }; - /** Scripts defined in the package.json file */ - scripts?: { - [key: string]: string | undefined; - }; - /** All other fields in the package.json are typed as unknown as we don't care what they are */ - [key: string]: unknown; -} - -function isObj(v: unknown): v is Record { +function isObj(v) { return !!(typeof v === 'object' && v); } /** * Asserts that given value looks like a parsed package.json file + * @param {unknown} v + * @returns {asserts v is import('./types').ParsedPackageJson} */ -export function assertParsedPackageJson(v: unknown): asserts v is ParsedPackageJson { +function validateParsedPackageJson(v) { if (!isObj(v) || typeof v.name !== 'string') { throw new Error('Expected at least a "name" property'); } @@ -66,14 +48,18 @@ export function assertParsedPackageJson(v: unknown): asserts v is ParsedPackageJ /** * Reads a given package.json file from disk and parses it + * @param {string} path + * @returns {import('./types').ParsedPackageJson} */ -export function readPackageJson(path: string): ParsedPackageJson { +function readPackageJson(path) { let pkg; try { pkg = JSON.parse(Fs.readFileSync(path, 'utf8')); - assertParsedPackageJson(pkg); + validateParsedPackageJson(pkg); } catch (error) { throw new Error(`unable to parse package.json at [${path}]: ${error.message}`); } return pkg; } + +module.exports = { readPackageJson, validateParsedPackageJson }; diff --git a/packages/kbn-bazel-packages/src/types.ts b/packages/kbn-bazel-packages/src/types.ts new file mode 100644 index 0000000000000..dc77d35bc206a --- /dev/null +++ b/packages/kbn-bazel-packages/src/types.ts @@ -0,0 +1,31 @@ +/* + * 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. + */ + +/** + * Simple parsed representation of a package.json file, validated + * by `assertParsedPackageJson()` and extensible as needed in the future + */ +export interface ParsedPackageJson { + /** The name of the package, usually `@kbn/`+something */ + name: string; + /** "dependenices" property from package.json */ + dependencies?: Record; + /** "devDependenices" property from package.json */ + devDependencies?: Record; + /** Some kibana specific properties about this package */ + kibana?: { + /** Is this package only intended for dev? */ + devOnly?: boolean; + }; + /** Scripts defined in the package.json file */ + scripts?: { + [key: string]: string | undefined; + }; + /** All other fields in the package.json are typed as unknown as we don't care what they are */ + [key: string]: unknown; +} diff --git a/packages/kbn-bazel-packages/tsconfig.json b/packages/kbn-bazel-packages/tsconfig.json index 789c6b3111115..9f78bc243ac66 100644 --- a/packages/kbn-bazel-packages/tsconfig.json +++ b/packages/kbn-bazel-packages/tsconfig.json @@ -4,6 +4,8 @@ "declaration": true, "declarationMap": true, "emitDeclarationOnly": true, + "allowJs": true, + "checkJs": true, "outDir": "target_types", "rootDir": "src", "stripInternal": false, diff --git a/packages/kbn-generate/src/commands/package_command.ts b/packages/kbn-generate/src/commands/package_command.ts index 4f5d58184aeac..32d5ff008c79f 100644 --- a/packages/kbn-generate/src/commands/package_command.ts +++ b/packages/kbn-generate/src/commands/package_command.ts @@ -162,7 +162,7 @@ ${BAZEL_PACKAGE_DIRS.map((dir) => ` ./${dir}/*\n`).join Path.resolve(TEMPLATE_DIR, 'packages_BUILD.bazel.ejs'), Path.resolve(REPO_ROOT, 'packages/BUILD.bazel'), { - packages: await discoverBazelPackages(), + packages: await discoverBazelPackages(REPO_ROOT), } ); log.info('Updated packages/BUILD.bazel'); diff --git a/packages/kbn-generate/src/commands/packages_build_manifest_command.ts b/packages/kbn-generate/src/commands/packages_build_manifest_command.ts index 9103d56d42a1d..9b05c1aed332d 100644 --- a/packages/kbn-generate/src/commands/packages_build_manifest_command.ts +++ b/packages/kbn-generate/src/commands/packages_build_manifest_command.ts @@ -31,7 +31,7 @@ export const PackagesBuildManifestCommand: GenerateCommand = { async run({ log, render, flags }) { const validate = !!flags.validate; - const packages = await discoverBazelPackages(); + const packages = await discoverBazelPackages(REPO_ROOT); const dest = Path.resolve(REPO_ROOT, 'packages/BUILD.bazel'); const relDest = Path.relative(process.cwd(), dest); diff --git a/src/dev/build/tasks/build_packages_task.ts b/src/dev/build/tasks/build_packages_task.ts index 607e0ac0f08ba..521b1299f423d 100644 --- a/src/dev/build/tasks/build_packages_task.ts +++ b/src/dev/build/tasks/build_packages_task.ts @@ -8,6 +8,7 @@ import Path from 'path'; +import { REPO_ROOT } from '@kbn/utils'; import { discoverBazelPackages } from '@kbn/bazel-packages'; import { runBazel } from '@kbn/bazel-runner'; @@ -16,7 +17,7 @@ import { Task, scanCopy, write } from '../lib'; export const BuildBazelPackages: Task = { description: 'Building distributable versions of Bazel packages', async run(config, log, build) { - const packages = (await discoverBazelPackages()).filter((pkg) => !pkg.isDevOnly()); + const packages = (await discoverBazelPackages(REPO_ROOT)).filter((pkg) => !pkg.isDevOnly()); log.info(`Preparing Bazel projects production build non-devOnly packages`); await runBazel(['build', '//packages:build']); diff --git a/src/dev/build/tasks/clean_tasks.ts b/src/dev/build/tasks/clean_tasks.ts index 15b20e712334c..409f6a77b50bf 100644 --- a/src/dev/build/tasks/clean_tasks.ts +++ b/src/dev/build/tasks/clean_tasks.ts @@ -8,6 +8,7 @@ import minimatch from 'minimatch'; import { discoverBazelPackages } from '@kbn/bazel-packages'; +import { REPO_ROOT } from '@kbn/utils'; import { deleteAll, deleteEmptyFolders, scanDelete, Task, GlobalTask } from '../lib'; export const Clean: GlobalTask = { @@ -259,7 +260,7 @@ export const DeleteBazelPackagesFromBuildRoot: Task = { 'Deleting bazel packages outputs from build folder root as they are now installed as node_modules', async run(config, log, build) { - const bazelPackagesOnBuildRoot = (await discoverBazelPackages()).map((pkg) => + const bazelPackagesOnBuildRoot = (await discoverBazelPackages(REPO_ROOT)).map((pkg) => build.resolvePath(pkg.normalizedRepoRelativeDir) );