-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(NA): creates pkg_npm_types bazel rule (#116465)
* 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
1 parent
67f73c5
commit 5fe8d74
Showing
16 changed files
with
438 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
90
src/dev/bazel/pkg_npm_types/packager/create_api_extraction.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
src/dev/bazel/pkg_npm_types/packager/generate_package_json.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.