Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(NA): creates pkg_npm_types bazel rule #116465

Merged
merged 52 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
82961a1
chore(NA): auto creation of the package.json for the new types pkg rule
mistic Sep 30, 2021
2edbb63
chore(NA): first alpha api extractor working version
mistic Sep 30, 2021
4516010
chore(NA): support kbn-analytics
mistic Oct 1, 2021
28fa3d9
Merge remote-tracking branch 'upstream/master' into create-types-pkg-…
mistic Oct 5, 2021
2822a60
Merge remote-tracking branch 'upstream/master' into create-types-pkg-…
mistic Oct 5, 2021
d510f31
chore(NA): correctly read tsconfig files and deps from ts_config rule
mistic Oct 5, 2021
99c677c
Merge remote-tracking branch 'upstream/master' into create-types-pkg-…
mistic Oct 6, 2021
afee280
Merge remote-tracking branch 'upstream/master' into create-types-pkg-…
mistic Oct 6, 2021
78e9719
chore(NA): layed out pkg_npm_types tree artifact custom rule
mistic Oct 6, 2021
b1d9b7f
chore(NA): missing todos
mistic Oct 6, 2021
bdead3e
chore(NA): node modules link mapping
mistic Oct 6, 2021
ae054bb
chore(NA): fully working pkg_npm_types rule
mistic Oct 7, 2021
13f7801
Merge remote-tracking branch 'upstream/master' into create-types-pkg-…
mistic Oct 7, 2021
385454a
chore(NA): merge and solve conflicts with master
mistic Oct 7, 2021
1286d9b
chore(NA): merge and solve conflicts with master
mistic Oct 25, 2021
199300a
Merge remote-tracking branch 'upstream/master' into create-types-pkg-…
mistic Oct 26, 2021
5f5ed46
chore(NA): fix changes on new packages using elastic datemath pkgs
mistic Oct 26, 2021
e1b2ec0
docs(NA): remove todo
mistic Oct 26, 2021
2fa62c7
Merge remote-tracking branch 'upstream/master' into create-types-pkg-…
mistic Oct 26, 2021
ac72784
docs(NA): last todo text correction
mistic Oct 27, 2021
2df52f5
Merge branch 'master' into create-types-pkg-bazel-rule
kibanamachine Oct 27, 2021
89888b4
Merge branch 'master' into create-types-pkg-bazel-rule
kibanamachine Oct 27, 2021
15cc77a
Merge branch 'master' into create-types-pkg-bazel-rule
kibanamachine Oct 27, 2021
488407b
Merge branch 'master' into create-types-pkg-bazel-rule
kibanamachine Oct 28, 2021
8aa3705
Merge branch 'master' into create-types-pkg-bazel-rule
kibanamachine Oct 28, 2021
a036832
chore(NA): removed commented lines
mistic Oct 28, 2021
0a437b6
Merge remote-tracking branch 'upstream/master' into create-types-pkg-…
mistic Oct 28, 2021
c3d6e43
fix(NA): include missing package version
mistic Oct 28, 2021
8872d5a
Merge branch 'master' into create-types-pkg-bazel-rule
kibanamachine Oct 28, 2021
c9de76f
chore(NA): include license keys
mistic Oct 28, 2021
bdc396b
Merge branch 'create-types-pkg-bazel-rule' of github.com:mistic/kiban…
mistic Oct 28, 2021
051d25f
chore(NA): change mock types package into private
mistic Oct 29, 2021
b0ef9d1
Merge branch 'main' into create-types-pkg-bazel-rule
kibanamachine Oct 29, 2021
ffae6f4
Merge branch 'main' into create-types-pkg-bazel-rule
kibanamachine Oct 29, 2021
f94089c
Merge branch 'main' into create-types-pkg-bazel-rule
kibanamachine Nov 2, 2021
5bbdd13
Merge remote-tracking branch 'upstream/main' into create-types-pkg-ba…
mistic Nov 3, 2021
e2938df
Merge branch 'main' into create-types-pkg-bazel-rule
kibanamachine Nov 3, 2021
51e17a3
chore(NA): disable validator on ts_project rule
mistic Nov 3, 2021
c1e37a0
Merge remote-tracking branch 'upstream/main' into create-types-pkg-ba…
mistic Nov 3, 2021
b07b499
chore(NA): merge and solve conflicts with main
mistic Nov 4, 2021
5a69198
chore(NA): use the wrapper for ts_project
mistic Nov 4, 2021
2a78de7
Merge branch 'main' into create-types-pkg-bazel-rule
kibanamachine Nov 5, 2021
72942cd
commit using @elastic.co
mistic Nov 5, 2021
fd9cd5d
chore(NA): commit using @elastic.co
mistic Nov 5, 2021
c114e5b
chore(NA): merge and solve conflicts with master
mistic Nov 5, 2021
51f5b9d
Merge branch 'main' into create-types-pkg-bazel-rule
kibanamachine Nov 6, 2021
4ff37c7
Merge branch 'main' into create-types-pkg-bazel-rule
kibanamachine Nov 8, 2021
4d02d60
Merge branch 'main' into create-types-pkg-bazel-rule
kibanamachine Nov 9, 2021
8170b72
Merge branch 'main' into create-types-pkg-bazel-rule
kibanamachine Nov 9, 2021
8024695
Merge branch 'main' into create-types-pkg-bazel-rule
kibanamachine Nov 11, 2021
2cfff27
chore(NA): merge and solve conflicts with main
mistic Nov 12, 2021
0c14fa4
Merge branch 'main' into create-types-pkg-bazel-rule
kibanamachine Nov 15, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@
"@types/delete-empty": "^2.0.0",
"@types/ejs": "^3.0.6",
"@types/elasticsearch": "^5.0.33",
"@types/elastic__datemath": "link:bazel-bin/packages/elastic-datemath/npm_module_types",
"@types/enzyme": "^3.10.8",
"@types/eslint": "^7.28.0",
"@types/extract-zip": "^1.6.2",
Expand Down
26 changes: 23 additions & 3 deletions packages/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Grouping target to call all underlying packages build
# targets so we can build them all at once
# It will build all declared code packages
filegroup(
name = "build",
name = "build_pkg_code",
srcs = [
"//packages/elastic-apm-synthtrace:build",
"//packages/elastic-datemath:build",
Expand Down Expand Up @@ -70,3 +69,24 @@ filegroup(
"//packages/kbn-utils:build",
],
)

# It will build all declared package types
filegroup(
name = "build_pkg_types",
srcs = [
"//packages/elastic-datemath:build_types",
],
)



# Grouping target to call all underlying packages build
# targets so we can build them all at once
# It will auto build all declared code packages and types packages
filegroup(
name = "build",
srcs = [
":build_pkg_code",
":build_pkg_types"
],
)
2 changes: 1 addition & 1 deletion packages/elastic-apm-synthtrace/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ RUNTIME_DEPS = [
]

TYPES_DEPS = [
"//packages/elastic-datemath",
"//packages/elastic-datemath:npm_module_types",
"@npm//@elastic/elasticsearch",
"@npm//moment",
"@npm//p-limit",
Expand Down
32 changes: 25 additions & 7 deletions packages/elastic-datemath/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_project")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm")
load("//src/dev/bazel:index.bzl", "jsts_transpiler")
load("@npm//@bazel/typescript:index.bzl", "ts_config")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
load("//src/dev/bazel:index.bzl", "jsts_transpiler", "ts_project", "pkg_npm", "pkg_npm_types")

PKG_BASE_NAME = "elastic-datemath"
PKG_REQUIRE_NAME = "@elastic/datemath"
TYPES_PKG_REQUIRE_NAME = "@types/elastic__datemath"

SOURCE_FILES = glob([
"src/index.ts",
Expand All @@ -26,7 +27,7 @@ TYPES_DEPS = [
"@npm//@types/node",
]

DEPS = TYPES_DEPS
RUNTIME_DEPS = TYPES_DEPS

jsts_transpiler(
name = "target_node",
Expand All @@ -47,20 +48,20 @@ ts_project(
name = "tsc_types",
args = ['--pretty'],
srcs = SRCS,
deps = DEPS,
deps = TYPES_DEPS,
declaration = True,
declaration_map = True,
emit_declaration_only = True,
out_dir = "target_types",
source_map = True,
root_dir = "src",
tsconfig = ":tsconfig",
tsconfig = ":tsconfig"
)

js_library(
name = PKG_BASE_NAME,
srcs = NPM_MODULE_EXTRA_FILES,
deps = DEPS + [":target_node", ":tsc_types"],
deps = RUNTIME_DEPS + [":target_node"],
package_name = PKG_REQUIRE_NAME,
visibility = ["//visibility:public"],
)
Expand All @@ -79,3 +80,20 @@ filegroup(
],
visibility = ["//visibility:public"],
)

pkg_npm_types(
name = "npm_module_types",
srcs = SRCS,
deps = [":tsc_types"],
package_name = TYPES_PKG_REQUIRE_NAME,
tsconfig = ":tsconfig",
visibility = ["//visibility:public"],
)

filegroup(
name = "build_types",
srcs = [
":npm_module_types",
],
visibility = ["//visibility:public"],
)
1 change: 0 additions & 1 deletion packages/elastic-datemath/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"description": "elasticsearch datemath parser, used in kibana",
"license": "Apache-2.0",
"main": "./target_node/index.js",
"types": "./target_types/index.d.ts",
"peerDependencies": {
"moment": "^2.24.0"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-securitysolution-io-ts-utils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ RUNTIME_DEPS = [
]

TYPES_DEPS = [
"//packages/elastic-datemath",
"//packages/elastic-datemath:npm_module_types",
"@npm//fp-ts",
"@npm//io-ts",
"@npm//moment",
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-ui-shared-deps-src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ RUNTIME_DEPS = [
]

TYPES_DEPS = [
"//packages/elastic-datemath",
"//packages/elastic-datemath:npm_module_types",
"//packages/elastic-safer-lodash-set",
"//packages/kbn-analytics",
"//packages/kbn-babel-preset",
Expand Down
6 changes: 4 additions & 2 deletions src/dev/bazel/index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
# Side Public License, v 1.
#

"""Public API interface for Bazel custom rules.
"""Public API interface for all Kibana Bazel custom rules.
Please do not import from any other files when looking to use a custom rule
"""

load("//src/dev/bazel:jsts_transpiler.bzl", _jsts_transpiler = "jsts_transpiler")
load("//src/dev/bazel:ts_project.bzl", _ts_project = "ts_project")
load("//src/dev/bazel:pkg_npm.bzl", _pkg_npm = "pkg_npm")
load("//src/dev/bazel/pkg_npm_types:index.bzl", _pkg_npm_types = "pkg_npm_types")
load("//src/dev/bazel:ts_project.bzl", _ts_project = "ts_project")

jsts_transpiler = _jsts_transpiler
pkg_npm = _pkg_npm
pkg_npm_types = _pkg_npm_types
ts_project = _ts_project
28 changes: 28 additions & 0 deletions src/dev/bazel/pkg_npm_types/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package(default_visibility = ["//visibility:public"])

load("@build_bazel_rules_nodejs//internal/node:node.bzl", "nodejs_binary")

filegroup(
name = "packager_all_files",
srcs = glob([
"packager/*",
]),
)

exports_files(
[
"package_json.mustache",
],
visibility = ["//visibility:public"]
)

nodejs_binary(
name = "_packager",
data = [
"@npm//@bazel/typescript",
"@npm//@microsoft/api-extractor",
"@npm//mustache",
":packager_all_files"
],
entry_point = ":packager/index.js",
)
15 changes: 15 additions & 0 deletions src/dev/bazel/pkg_npm_types/index.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# 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.
#

"""Public API interface for pkg_npm_types rule.
Please do not import from any other files when looking to this rule
"""

load(":pkg_npm_types.bzl", _pkg_npm_types = "pkg_npm_types")

pkg_npm_types = _pkg_npm_types
8 changes: 8 additions & 0 deletions src/dev/bazel/pkg_npm_types/package_json.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "{{{NAME}}}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

~/dev/kibana/node_modules/@types/elastic__datemath(pr/116465*) » cat package.json                                                                                                                          jon@xps
{
  "name": "@types/elastic__datemath",
  "description": "Generated by Bazel",
  "types": "./index.d.ts",
  "private": true,
  "license": "MIT",
  "version": "1.1.0"
}

Should license/version match the package's settings?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a placeholder and those packages won't be published, I believe we can ignore those 😃

"description": "Generated by Bazel",
"types": "./index.d.ts",
"private": true,
"license": "MIT",
"version": "1.1.0"
}
90 changes: 90 additions & 0 deletions src/dev/bazel/pkg_npm_types/packager/create_api_extraction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.
*/

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

const { format, parseTsconfig } = require('@bazel/typescript');
const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor');
const fs = require('fs');
const path = require('path');

function createApiExtraction(
tsConfig,
entryPoint,
dtsBundleOut,
apiReviewFolder,
acceptApiUpdates = false
) {
const [parsedConfig, errors] = parseTsconfig(tsConfig);
if (errors && errors.length) {
console.error(format('', errors));
return 1;
}
const pkgJson = path.resolve(path.dirname(entryPoint), 'package.json');
if (!fs.existsSync(pkgJson)) {
fs.writeFileSync(
pkgJson,
JSON.stringify({
name: 'GENERATED-BY-BAZEL',
description: 'This is a dummy package.json as API Extractor always requires one.',
types: './index.d.ts',
private: true,
license: 'SSPL-1.0 OR Elastic License 2.0',
version: '1.0.0',
})
);
}
// API extractor doesn't always support the version of TypeScript used in the repo
// example: at the moment it is not compatable with 3.2
// to use the internal TypeScript we shall not create a program but rather pass a parsed tsConfig.
const parsedTsConfig = parsedConfig.config;
const extractorOptions = {
localBuild: acceptApiUpdates,
};
const configObject = {
compiler: {
overrideTsconfig: parsedTsConfig,
},
projectFolder: path.resolve(path.dirname(tsConfig)),
mainEntryPointFilePath: path.resolve(entryPoint),
apiReport: {
enabled: !!apiReviewFolder,
// TODO(alan-agius4): remove this folder name when the below issue is solved upstream
// See: https://github.com/microsoft/web-build-tools/issues/1470
reportFileName: (apiReviewFolder && path.resolve(apiReviewFolder)) || 'invalid',
},
docModel: {
enabled: false,
},
dtsRollup: {
enabled: !!dtsBundleOut,
untrimmedFilePath: dtsBundleOut && path.resolve(dtsBundleOut),
},
tsdocMetadata: {
enabled: false,
},
};
const options = {
configObject,
packageJson: undefined,
packageJsonFullPath: pkgJson,
configObjectFullPath: undefined,
};
const extractorConfig = ExtractorConfig.prepare(options);
const { succeeded } = Extractor.invoke(extractorConfig, extractorOptions);
// API extractor errors are emitted by it's logger.
return succeeded ? 0 : 1;
}

module.exports.createApiExtraction = createApiExtraction;
43 changes: 43 additions & 0 deletions src/dev/bazel/pkg_npm_types/packager/generate_package_json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 fs = require('fs');
const Mustache = require('mustache');
const path = require('path');

function generatePackageJson(outputBasePath, packageJsonTemplatePath, rawPackageJsonTemplateArgs) {
const packageJsonTemplateArgsInTuples = rawPackageJsonTemplateArgs.reduce(
(a, v) => {
const lastTupleIdx = a.length - 1;
const lastTupleSize = a[lastTupleIdx].length;

if (lastTupleSize < 2) {
a[lastTupleIdx].push(v);

return a;
}

return a.push([v]);
},
[[]]
);
const packageJsonTemplateArgs = Object.fromEntries(new Map(packageJsonTemplateArgsInTuples));

try {
const template = fs.readFileSync(packageJsonTemplatePath);
const renderedTemplate = Mustache.render(template.toString(), packageJsonTemplateArgs);
fs.writeFileSync(path.resolve(outputBasePath, 'package.json'), renderedTemplate);
} catch (e) {
console.error(e);
return 1;
}

return 0;
}

module.exports.generatePackageJson = generatePackageJson;
Loading