Skip to content

Commit

Permalink
[type-summarizer] always use normalized paths, fix windows compat (el…
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer authored and lucasfcosta committed Mar 8, 2022
1 parent d12a0d2 commit 8399b23
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 64 deletions.
6 changes: 2 additions & 4 deletions packages/kbn-type-summarizer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ PKG_REQUIRE_NAME = "@kbn/type-summarizer"
SOURCE_FILES = glob(
[
"src/**/*.ts",
],
exclude = [
"**/*.test.*"
],
]
)

SRCS = SOURCE_FILES
Expand Down Expand Up @@ -49,6 +46,7 @@ TYPES_DEPS = [
"@npm//is-path-inside",
"@npm//normalize-path",
"@npm//source-map",
"@npm//strip-ansi",
"@npm//tslib",
]

Expand Down
85 changes: 60 additions & 25 deletions packages/kbn-type-summarizer/src/lib/bazel_cli_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* Side Public License, v 1.
*/

import Path from 'path';
import Fs from 'fs';

import { CliError } from './cli_error';
import { parseCliFlags } from './cli_flags';
import * as Path from './path';

const TYPE_SUMMARIZER_PACKAGES = ['@kbn/type-summarizer', '@kbn/crypto'];

Expand All @@ -25,6 +25,36 @@ interface BazelCliConfig {
use: 'api-extractor' | 'type-summarizer';
}

function isKibanaRepo(dir: string) {
try {
const json = Fs.readFileSync(Path.join(dir, 'package.json'), 'utf8');
const parsed = JSON.parse(json);
return parsed.name === 'kibana';
} catch {
return false;
}
}

function findRepoRoot() {
const start = Path.resolve(__dirname);
let dir = start;
while (true) {
if (isKibanaRepo(dir)) {
return dir;
}

// this is not the kibana directory, try moving up a directory
const parent = Path.join(dir, '..');
if (parent === dir) {
throw new Error(
`unable to find Kibana's package.json file when traversing up from [${start}]`
);
}

dir = parent;
}
}

export function parseBazelCliFlags(argv: string[]): BazelCliConfig {
const { rawFlags, unknownFlags } = parseCliFlags(argv, {
string: ['use'],
Expand All @@ -39,19 +69,7 @@ export function parseBazelCliFlags(argv: string[]): BazelCliConfig {
});
}

let REPO_ROOT;
try {
const name = 'utils';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const utils = require('@kbn/' + name);
REPO_ROOT = utils.REPO_ROOT as string;
} catch (error) {
if (error && error.code === 'MODULE_NOT_FOUND') {
throw new CliError('type-summarizer bazel cli only works after bootstrap');
}

throw error;
}
const repoRoot = findRepoRoot();

const [relativePackagePath, ...extraPositional] = rawFlags._;
if (typeof relativePackagePath !== 'string') {
Expand All @@ -70,26 +88,45 @@ export function parseBazelCliFlags(argv: string[]): BazelCliConfig {
const packageName: string = JSON.parse(
Fs.readFileSync(Path.join(packageDir, 'package.json'), 'utf8')
).name;
const repoRelativePackageDir = Path.relative(REPO_ROOT, packageDir);
const repoRelativePackageDir = Path.relative(repoRoot, packageDir);

return {
use,
packageName,
tsconfigPath: Path.join(REPO_ROOT, repoRelativePackageDir, 'tsconfig.json'),
inputPath: Path.resolve(REPO_ROOT, 'node_modules', packageName, 'target_types/index.d.ts'),
tsconfigPath: Path.join(repoRoot, repoRelativePackageDir, 'tsconfig.json'),
inputPath: Path.join(repoRoot, 'node_modules', packageName, 'target_types/index.d.ts'),
repoRelativePackageDir,
outputDir: Path.resolve(REPO_ROOT, 'data/type-summarizer-output', use),
outputDir: Path.join(repoRoot, 'data/type-summarizer-output', use),
};
}

export function parseBazelCliJson(json: string): BazelCliConfig {
let config;
function parseJsonFromCli(json: string) {
try {
config = JSON.parse(json);
return JSON.parse(json);
} catch (error) {
throw new CliError('unable to parse first positional argument as JSON');
// TODO: This is to handle a bug in Bazel which escapes `"` in .bat arguments incorrectly, replacing them with `\`
if (
error.message === 'Unexpected token \\ in JSON at position 1' &&
process.platform === 'win32'
) {
const unescapedJson = json.replaceAll('\\', '"');
try {
return JSON.parse(unescapedJson);
} catch (e) {
throw new CliError(
`unable to parse first positional argument as JSON: "${e.message}"\n unescaped value: ${unescapedJson}\n raw value: ${json}`
);
}
}

throw new CliError(
`unable to parse first positional argument as JSON: "${error.message}"\n value: ${json}`
);
}
}

export function parseBazelCliJson(json: string): BazelCliConfig {
const config = parseJsonFromCli(json);
if (typeof config !== 'object' || config === null) {
throw new CliError('config JSON must be an object');
}
Expand Down Expand Up @@ -131,14 +168,12 @@ export function parseBazelCliJson(json: string): BazelCliConfig {
throw new CliError(`buildFilePath [${buildFilePath}] must be a relative path`);
}

const repoRelativePackageDir = Path.dirname(buildFilePath);

return {
packageName,
outputDir: Path.resolve(outputDir),
tsconfigPath: Path.resolve(tsconfigPath),
inputPath: Path.resolve(inputPath),
repoRelativePackageDir,
repoRelativePackageDir: Path.dirname(buildFilePath),
use: TYPE_SUMMARIZER_PACKAGES.includes(packageName) ? 'type-summarizer' : 'api-extractor',
};
}
Expand Down
6 changes: 3 additions & 3 deletions packages/kbn-type-summarizer/src/lib/is_node_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* Side Public License, v 1.
*/

import Path from 'path';

import isPathInside from 'is-path-inside';

import * as Path from './path';

export function isNodeModule(dtsDir: string, path: string) {
return (isPathInside(path, dtsDir) ? Path.relative(dtsDir, path) : path)
.split(Path.sep)
.split('/')
.includes('node_modules');
}
36 changes: 36 additions & 0 deletions packages/kbn-type-summarizer/src/lib/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 normalizePath from 'normalize-path';
const cwd = normalizePath(process.cwd());

export function cwdRelative(path: string) {
return relative(cwd, path);
}

export function relative(from: string, to: string) {
return normalizePath(Path.relative(from, to));
}

export function join(...segments: string[]) {
return Path.join(...segments);
}

export function dirname(path: string) {
return Path.dirname(path);
}

export function resolve(path: string) {
return Path.isAbsolute(path) ? normalizePath(path) : join(cwd, path);
}

export function isAbsolute(path: string) {
return Path.isAbsolute(path);
}
3 changes: 1 addition & 2 deletions packages/kbn-type-summarizer/src/lib/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
* Side Public License, v 1.
*/

import Path from 'path';

import * as ts from 'typescript';
import { SourceNode, CodeWithSourceMap } from 'source-map';

import * as Path from './path';
import { findKind } from './ts_nodes';
import { SourceMapper } from './source_mapper';
import { CollectorResult } from './export_collector';
Expand Down
12 changes: 5 additions & 7 deletions packages/kbn-type-summarizer/src/lib/source_mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
* Side Public License, v 1.
*/

import Path from 'path';

import * as ts from 'typescript';
import { SourceNode, SourceMapConsumer, BasicSourceMapConsumer } from 'source-map';
import normalizePath from 'normalize-path';

import { Logger } from './log';
import { tryReadFile } from './helpers/fs';
import { parseJson } from './helpers/json';
import { isNodeModule } from './is_node_module';
import * as Path from './path';

type SourceMapConsumerEntry = [ts.SourceFile, BasicSourceMapConsumer | undefined];

Expand All @@ -38,9 +36,9 @@ export class SourceMapper {
return [sourceFile, undefined];
}

const relSourceFile = Path.relative(process.cwd(), sourceFile.fileName);
const sourceMapPath = Path.resolve(Path.dirname(sourceFile.fileName), match[1]);
const relSourceMapPath = Path.relative(process.cwd(), sourceMapPath);
const relSourceFile = Path.cwdRelative(sourceFile.fileName);
const sourceMapPath = Path.join(Path.dirname(sourceFile.fileName), match[1]);
const relSourceMapPath = Path.cwdRelative(sourceMapPath);
const sourceJson = await tryReadFile(sourceMapPath, 'utf8');
if (!sourceJson) {
throw new Error(
Expand Down Expand Up @@ -81,7 +79,7 @@ export class SourceMapper {
* us the path to the source, relative to the `repoRelativePackageDir`.
*/
fixSourcePath(source: string) {
return normalizePath(Path.relative(this.sourceFixDir, Path.join('/', source)));
return Path.relative(this.sourceFixDir, Path.join('/', source));
}

getSourceNode(generatedNode: ts.Node, code: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-type-summarizer/src/lib/tsconfig_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/

import * as ts from 'typescript';
import Path from 'path';

import * as Path from './path';
import { CliError } from './cli_error';

export function readTsConfigFile(path: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import Fsp from 'fs/promises';

import * as ts from 'typescript';
import stripAnsi from 'strip-ansi';
import normalizePath from 'normalize-path';

import { loadTsConfigFile } from '../src/lib/tsconfig_file';
import { createTsProject } from '../src/lib/ts_project';
import { TestLog } from '../src/lib/log';
import { summarizePackage } from '../src/summarize_package';
import { loadTsConfigFile } from '../lib/tsconfig_file';
import { createTsProject } from '../lib/ts_project';
import { TestLog } from '../lib/log';
import { summarizePackage } from '../summarize_package';

const TMP_DIR = Path.resolve(__dirname, '__tmp__');
const TMP_DIR = Path.resolve(__dirname, '../../__tmp__');

const DIAGNOSTIC_HOST = {
getCanonicalFileName: (p: string) => p,
Expand Down Expand Up @@ -153,11 +154,11 @@ class MockCli {

// summarize the .d.ts files into the output dir
await summarizePackage(log, {
dtsDir: this.dtsOutputDir,
inputPaths: [this.inputPath],
outputDir: this.outputDir,
dtsDir: normalizePath(this.dtsOutputDir),
inputPaths: [normalizePath(this.inputPath)],
outputDir: normalizePath(this.outputDir),
repoRelativePackageDir: 'src',
tsconfigPath: this.tsconfigPath,
tsconfigPath: normalizePath(this.tsconfigPath),
strictPrinting: false,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ it('prints basic class correctly', async () => {
}
`);
expect(output.logs).toMatchInlineSnapshot(`
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts' ]
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/__tmp__/dist_dts/index.d.ts' ]
debug Ignoring 1 global declarations for \\"Record\\"
debug Ignoring 5 global declarations for \\"Promise\\"
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ it('prints the function declaration, including comments', async () => {
`);
expect(result.logs).toMatchInlineSnapshot(`
"debug loaded sourcemaps for [
'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/bar.d.ts',
'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts'
'packages/kbn-type-summarizer/__tmp__/dist_dts/bar.d.ts',
'packages/kbn-type-summarizer/__tmp__/dist_dts/index.d.ts'
]
"
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ it('output type links to named import from node modules', async () => {
}
`);
expect(output.logs).toMatchInlineSnapshot(`
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts' ]
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/__tmp__/dist_dts/index.d.ts' ]
"
`);
});
Expand Down Expand Up @@ -84,7 +84,7 @@ it('output type links to default import from node modules', async () => {
}
`);
expect(output.logs).toMatchInlineSnapshot(`
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts' ]
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/__tmp__/dist_dts/index.d.ts' ]
"
`);
});
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ it('prints the whole interface, including comments', async () => {
}
`);
expect(result.logs).toMatchInlineSnapshot(`
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts' ]
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/__tmp__/dist_dts/index.d.ts' ]
debug Ignoring 5 global declarations for \\"Promise\\"
"
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ it('collects references from source files which contribute to result', async ()
`);
expect(result.logs).toMatchInlineSnapshot(`
"debug loaded sourcemaps for [
'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/files/foo.d.ts',
'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/files/index.d.ts',
'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts'
'packages/kbn-type-summarizer/__tmp__/dist_dts/files/foo.d.ts',
'packages/kbn-type-summarizer/__tmp__/dist_dts/files/index.d.ts',
'packages/kbn-type-summarizer/__tmp__/dist_dts/index.d.ts'
]
debug Ignoring 5 global declarations for \\"Promise\\"
debug Ignoring 4 global declarations for \\"Symbol\\"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ it('prints basic type alias', async () => {
}
`);
expect(output.logs).toMatchInlineSnapshot(`
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts' ]
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/__tmp__/dist_dts/index.d.ts' ]
"
`);
});
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ it('prints basic variable exports with sourcemaps', async () => {
}
`);
expect(output.logs).toMatchInlineSnapshot(`
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/tests/__tmp__/dist_dts/index.d.ts' ]
"debug loaded sourcemaps for [ 'packages/kbn-type-summarizer/__tmp__/dist_dts/index.d.ts' ]
"
`);
});
3 changes: 1 addition & 2 deletions packages/kbn-type-summarizer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
]
},
"include": [
"src/**/*",
"tests/**/*"
"src/**/*"
]
}

0 comments on commit 8399b23

Please sign in to comment.