Skip to content

Commit

Permalink
chore(NA): creates pkg_npm_types bazel rule (#116465)
Browse files Browse the repository at this point in the history
* chore(NA): auto creation of the package.json for the new types pkg rule

* chore(NA): first alpha api extractor working version

* chore(NA): support kbn-analytics

* chore(NA): correctly read tsconfig files and deps from ts_config rule

* chore(NA): layed out pkg_npm_types tree artifact custom rule

* chore(NA): missing todos

* chore(NA): node modules link mapping

* chore(NA): fully working pkg_npm_types rule

* chore(NA): fix changes on new packages using elastic datemath pkgs

* docs(NA): remove todo

* docs(NA): last todo text correction

* chore(NA): removed commented lines

* fix(NA): include missing package version

* chore(NA): include license keys

* chore(NA): change mock types package into private

* chore(NA): disable validator on ts_project rule

* chore(NA): use the wrapper for ts_project

* commit using @elastic.co

* chore(NA): commit using @elastic.co

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
mistic and kibanamachine authored Nov 16, 2021
1 parent 67f73c5 commit 5fe8d74
Show file tree
Hide file tree
Showing 16 changed files with 438 additions and 16 deletions.
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}}}",
"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

0 comments on commit 5fe8d74

Please sign in to comment.