-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[build_ts_refs] automatically update tsconfig.refs.json #107804
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
158f88a
[build_ts_refs] automatically update tsconfig.refs.json
spalger cf268b8
update config file, fix type error, provide useful errors in CI
spalger 1e5bc18
Merge branch 'master' of github.com:elastic/kibana into implement/roo…
spalger 23f0bbb
improve formatting of tsconfig.refs.json
spalger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,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. | ||
| */ | ||
|
|
||
| import Path from 'path'; | ||
| import Fs from 'fs/promises'; | ||
|
|
||
| import { REPO_ROOT, ToolingLog, createFailError } from '@kbn/dev-utils'; | ||
| import dedent from 'dedent'; | ||
|
|
||
| import { PROJECTS } from './projects'; | ||
| import { Project } from './project'; | ||
|
|
||
| export const ROOT_REFS_CONFIG_PATH = Path.resolve(REPO_ROOT, 'tsconfig.refs.json'); | ||
| export const REF_CONFIG_PATHS = [ROOT_REFS_CONFIG_PATH]; | ||
|
|
||
| const sort = (arr: string[]) => arr.slice().sort((a, b) => a.localeCompare(b)); | ||
|
|
||
| async function analyzeRootRefsConfig(log: ToolingLog) { | ||
| const compositeProjects = PROJECTS.filter((p) => p.isCompositeProject() && !p.disableTypeCheck); | ||
| const currentRefs = sort(new Project(ROOT_REFS_CONFIG_PATH).getRefdPaths()); | ||
| const newRefs = sort( | ||
| compositeProjects.map((p) => `./${Path.relative(REPO_ROOT, p.tsConfigPath)}`) | ||
| ); | ||
|
|
||
| log.verbose('updating root refs config file'); | ||
| log.verbose('existing composite projects', currentRefs); | ||
| log.verbose('found the following composite projects', newRefs); | ||
|
|
||
| const removedRefs: string[] = []; | ||
| const addedRefs: string[] = []; | ||
|
|
||
| for (let i = 0; i < currentRefs.length || i < newRefs.length; ) { | ||
| const currentRef = currentRefs[i]; | ||
| const newRef = newRefs[i]; | ||
| if (currentRef === newRef) { | ||
| // continue with next item | ||
| i++; | ||
| continue; | ||
| } | ||
|
|
||
| // see if this item is lower down in current refs | ||
| const newIndex = currentRefs.indexOf(newRef, i + 1); | ||
| if (newIndex === -1) { | ||
| addedRefs.push(newRef); | ||
| currentRefs.splice(i, 0, newRef); | ||
| // recheck this index | ||
| continue; | ||
| } | ||
|
|
||
| // this item was removed from currentRefs | ||
| removedRefs.push(currentRef); | ||
| currentRefs.splice(i, 1); | ||
| // recheck this index | ||
| continue; | ||
| } | ||
|
|
||
| return { | ||
| refs: newRefs, | ||
| addedRefs, | ||
| removedRefs, | ||
| }; | ||
| } | ||
|
|
||
| export async function validateRootRefsConfig(log: ToolingLog) { | ||
| const { addedRefs, removedRefs } = await analyzeRootRefsConfig(log); | ||
|
|
||
| for (const addedRef of addedRefs) { | ||
| log.warning('Missing reference to composite project', addedRef, 'in root refs config file'); | ||
| } | ||
| for (const removedRef of removedRefs) { | ||
| log.warning('Extra reference to non-composite project', removedRef, 'in root refs config file'); | ||
| } | ||
|
|
||
| if (addedRefs.length || removedRefs.length) { | ||
| throw createFailError( | ||
| 'tsconfig.refs.json at the root of the repo is not valid and needs to be updated. Please run `node scripts/build_ts_refs` locally and commit the changes' | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| function generateTsConfig(refs: string[]) { | ||
| return dedent` | ||
| // This file is automatically updated when you run \`node scripts/build_ts_refs\` and validated to be up-to-date by CI | ||
| { | ||
| "include": [], | ||
| "references": [ | ||
| ${refs.map((p) => ` { "path": ${JSON.stringify(p)} },`).join('\n')} | ||
| ] | ||
| } | ||
| `; | ||
| } | ||
|
|
||
| export async function updateRootRefsConfig(log: ToolingLog) { | ||
| const { refs, addedRefs, removedRefs } = await analyzeRootRefsConfig(log); | ||
|
|
||
| if (removedRefs.length === 0 && addedRefs.length === 0) { | ||
| log.verbose('root refs config file is up-to-date'); | ||
| return; | ||
| } | ||
|
|
||
| for (const addedRef of addedRefs) { | ||
| log.info('Adding ref to composite project', addedRef, 'to root refs config file'); | ||
| } | ||
| for (const removedRef of removedRefs) { | ||
| log.info('Removing ref to composite project', removedRef, 'to root refs config file'); | ||
| } | ||
|
|
||
| await Fs.writeFile(ROOT_REFS_CONFIG_PATH, generateTsConfig(refs) + '\n'); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes were necessary because this project wasn't being built in the composite build previously and private properties on class expressions aren't compatible. Functionality is the same but avoid the type errors