forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ts/build_ts_refs] add support for --clean flag (elastic#91060)
Co-authored-by: spalger <[email protected]>
- Loading branch information
1 parent
5950d26
commit 87bba9f
Showing
9 changed files
with
166 additions
and
51 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 was deleted.
Oops, something went wrong.
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,24 @@ | ||
/* | ||
* 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 execa from 'execa'; | ||
import { ToolingLog, REPO_ROOT } from '@kbn/dev-utils'; | ||
|
||
export const REF_CONFIG_PATHS = [Path.resolve(REPO_ROOT, 'tsconfig.refs.json')]; | ||
|
||
export async function buildAllTsRefs(log: ToolingLog) { | ||
for (const path of REF_CONFIG_PATHS) { | ||
const relative = Path.relative(REPO_ROOT, path); | ||
log.debug(`Building TypeScript projects refs for ${relative}...`); | ||
await execa(require.resolve('typescript/bin/tsc'), ['-b', relative, '--pretty'], { | ||
cwd: REPO_ROOT, | ||
}); | ||
} | ||
} |
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,37 @@ | ||
/* | ||
* 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 { run } from '@kbn/dev-utils'; | ||
import del from 'del'; | ||
|
||
import { buildAllTsRefs, REF_CONFIG_PATHS } from './build_ts_refs'; | ||
import { getOutputsDeep } from './ts_configfile'; | ||
import { concurrentMap } from './concurrent_map'; | ||
|
||
export async function runBuildRefsCli() { | ||
run( | ||
async ({ log, flags }) => { | ||
if (flags.clean) { | ||
const outDirs = getOutputsDeep(REF_CONFIG_PATHS); | ||
log.info('deleting', outDirs.length, 'ts output directories'); | ||
await concurrentMap(100, outDirs, (outDir) => del(outDir)); | ||
} | ||
|
||
await buildAllTsRefs(log); | ||
}, | ||
{ | ||
description: 'Build TypeScript projects', | ||
flags: { | ||
boolean: ['clean'], | ||
}, | ||
log: { | ||
defaultLevel: 'debug', | ||
}, | ||
} | ||
); | ||
} |
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 @@ | ||
/* | ||
* 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 * as Rx from 'rxjs'; | ||
import { mergeMap, toArray, map } from 'rxjs/operators'; | ||
import { lastValueFrom } from '@kbn/std'; | ||
|
||
export async function concurrentMap<T, T2>( | ||
concurrency: number, | ||
arr: T[], | ||
fn: (item: T, i: number) => Promise<T2> | ||
): Promise<T2[]> { | ||
return await lastValueFrom( | ||
Rx.from(arr).pipe( | ||
// execute items in parallel based on concurrency | ||
mergeMap(async (item, index) => ({ index, result: await fn(item, index) }), concurrency), | ||
// collect the results into an array | ||
toArray(), | ||
// sort items back into order and return array of just results | ||
map((list) => list.sort((a, b) => a.index - b.index).map(({ result }) => result)) | ||
) | ||
); | ||
} |
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,71 @@ | ||
/* | ||
* 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 Fs from 'fs'; | ||
import Path from 'path'; | ||
|
||
import { parseConfigFileTextToJson } from 'typescript'; | ||
|
||
// yes, this is just `any`, but I'm hoping that TypeScript will give us some help here eventually | ||
type TsConfigFile = ReturnType<typeof parseConfigFileTextToJson>['config']; | ||
|
||
export function parseTsConfig(tsConfigPath: string): TsConfigFile { | ||
const { error, config } = parseConfigFileTextToJson( | ||
tsConfigPath, | ||
Fs.readFileSync(tsConfigPath, 'utf8') | ||
); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
return config; | ||
} | ||
|
||
export function getOutputsDeep(tsConfigPaths: string[]) { | ||
const tsConfigs = new Map<string, TsConfigFile>(); | ||
|
||
const read = (path: string) => { | ||
const cached = tsConfigs.get(path); | ||
if (cached) { | ||
return cached; | ||
} | ||
|
||
const config = parseTsConfig(path); | ||
tsConfigs.set(path, config); | ||
return config; | ||
}; | ||
|
||
const outputDirs: string[] = []; | ||
const seen = new Set<TsConfigFile>(); | ||
|
||
const traverse = (path: string) => { | ||
const config = read(path); | ||
if (seen.has(config)) { | ||
return; | ||
} | ||
seen.add(config); | ||
|
||
const dirname = Path.dirname(path); | ||
const relativeOutDir: string | undefined = config.compilerOptions?.outDir; | ||
if (relativeOutDir) { | ||
outputDirs.push(Path.resolve(dirname, relativeOutDir)); | ||
} | ||
|
||
const refs: undefined | Array<{ path: string }> = config.references; | ||
for (const ref of refs ?? []) { | ||
traverse(Path.resolve(dirname, ref.path)); | ||
} | ||
}; | ||
|
||
for (const path of tsConfigPaths) { | ||
traverse(path); | ||
} | ||
|
||
return outputDirs; | ||
} |