-
Notifications
You must be signed in to change notification settings - Fork 607
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
[api-extractor] Replace the old "tsdocFlavor" field with a dist/tsdoc-metadata.json output #960
Changes from all commits
924dc88
58a4300
6700652
1efb25c
51fd48b
181a8e8
c28c261
e97a5f0
47db611
9c32be6
0fe4bf3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
"run", | ||
"-l" | ||
], | ||
"sourceMaps": false | ||
"sourceMaps": true | ||
}, | ||
{ | ||
"type": "node", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
// See LICENSE in the project root for license information. | ||
|
||
import * as path from 'path'; | ||
|
||
import { | ||
PackageJsonLookup, | ||
IPackageJson | ||
IPackageJson, | ||
FileSystem, | ||
JsonFile, | ||
NewlineKind | ||
} from '@microsoft/node-core-library'; | ||
import { Extractor } from '../../extractor/Extractor'; | ||
import { ILogger } from '../../extractor/ILogger'; | ||
|
||
/** | ||
* Represents analyzed information for a package.json file. | ||
|
@@ -26,37 +33,64 @@ export class PackageMetadata { | |
*/ | ||
public readonly aedocSupported: boolean; | ||
|
||
private readonly _packageJsonLookup: PackageJsonLookup; | ||
|
||
public constructor(packageJsonPath: string, packageJsonLookup: PackageJsonLookup) { | ||
this._packageJsonLookup = packageJsonLookup; | ||
public constructor(packageJsonPath: string, packageJson: IPackageJson, aedocSupported: boolean) { | ||
this.packageJsonPath = packageJsonPath; | ||
|
||
this.packageJson = this._packageJsonLookup.loadPackageJson(packageJsonPath); | ||
|
||
this.aedocSupported = false; | ||
|
||
if (this.packageJson.tsdoc) { | ||
if (this.packageJson.tsdoc.tsdocFlavor) { | ||
if (this.packageJson.tsdoc.tsdocFlavor.toUpperCase() === 'AEDOC') { | ||
this.aedocSupported = true; | ||
} | ||
} | ||
} | ||
this.packageJson = packageJson; | ||
this.aedocSupported = aedocSupported; | ||
} | ||
} | ||
|
||
/** | ||
* This class maintains a cache of analyzed information obtained from package.json | ||
* files. It is built on top of the PackageJsonLookup class. | ||
* | ||
* @remarks | ||
* | ||
* IMPORTANT: Don't use PackageMetadataManager to analyze source files from the current project: | ||
* 1. Files such as tsdoc-metadata.json may not have been built yet, and thus may contain incorrect information. | ||
* 2. The current project is not guaranteed to have a package.json file at all. For example, API Extractor can | ||
* be invoked on a bare .d.ts file. | ||
* | ||
* Use ts.program.isSourceFileFromExternalLibrary() to test source files before passing the to PackageMetadataManager. | ||
*/ | ||
export class PackageMetadataManager { | ||
public static tsdocMetadataFilename: string = 'tsdoc-metadata.json'; | ||
|
||
private readonly _packageJsonLookup: PackageJsonLookup; | ||
private readonly _logger: ILogger; | ||
private readonly _packageMetadataByPackageJsonPath: Map<string, PackageMetadata> | ||
= new Map<string, PackageMetadata>(); | ||
|
||
public constructor(packageJsonLookup: PackageJsonLookup) { | ||
public static writeTsdocMetadataFile(packageJsonFolder: string): void { | ||
// This feature is still being standardized: https://github.com/Microsoft/tsdoc/issues/7 | ||
// In the future we will use the @microsoft/tsdoc library to read this file. | ||
const tsdocMetadataPath: string = path.join(packageJsonFolder, | ||
'dist', PackageMetadataManager.tsdocMetadataFilename); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rollup file may not exist. But we could put it in the same directory as the default entry point for the NPM package. In reply to: 236576554 [](ancestors = 236576554) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
const fileObject: Object = { | ||
tsdocVersion: '0.12', | ||
toolPackages: [ | ||
{ | ||
packageName: '@microsoft/api-extractor', | ||
packageVersion: Extractor.version | ||
} | ||
] | ||
}; | ||
|
||
const fileContent: string = | ||
'// This file is read by tools that parse documentation comments conforming to the TSDoc standard.\n' | ||
+ '// It should be published with your NPM package. It should not be tracked by Git.\n' | ||
+ JsonFile.stringify(fileObject); | ||
|
||
FileSystem.writeFile(tsdocMetadataPath, fileContent, { | ||
convertLineEndings: NewlineKind.CrLf, | ||
ensureFolderExists: true | ||
}); | ||
} | ||
|
||
public constructor(packageJsonLookup: PackageJsonLookup, logger: ILogger) { | ||
this._packageJsonLookup = packageJsonLookup; | ||
this._logger = logger; | ||
} | ||
|
||
/** | ||
|
@@ -72,16 +106,34 @@ export class PackageMetadataManager { | |
} | ||
let packageMetadata: PackageMetadata | undefined | ||
= this._packageMetadataByPackageJsonPath.get(packageJsonFilePath); | ||
|
||
if (!packageMetadata) { | ||
packageMetadata = new PackageMetadata(packageJsonFilePath, this._packageJsonLookup); | ||
const packageJson: IPackageJson = this._packageJsonLookup.loadPackageJson(packageJsonFilePath); | ||
|
||
const packageJsonFolder: string = path.dirname(packageJsonFilePath); | ||
|
||
// This feature is still being standardized: https://github.com/Microsoft/tsdoc/issues/7 | ||
// In the future we will use the @microsoft/tsdoc library to read this file. | ||
let aedocSupported: boolean = false; | ||
|
||
const tsdocMetadataPath: string = path.join(packageJsonFolder, | ||
'dist', PackageMetadataManager.tsdocMetadataFilename); | ||
|
||
if (FileSystem.exists(tsdocMetadataPath)) { | ||
this._logger.logVerbose('Found metadata in ' + tsdocMetadataPath); | ||
// If the file exists at all, assume it was written by API Extractor | ||
aedocSupported = true; | ||
} | ||
|
||
packageMetadata = new PackageMetadata(packageJsonFilePath, packageJson, aedocSupported); | ||
this._packageMetadataByPackageJsonPath.set(packageJsonFilePath, packageMetadata); | ||
} | ||
|
||
return packageMetadata; | ||
} | ||
|
||
/** | ||
* Returns true if the source file has an associated PackageMetadata object | ||
* with aedocSupported=true. | ||
* Returns true if the source file is part of a package whose .d.ts files support AEDoc annotations. | ||
*/ | ||
public isAedocSupportedFor(sourceFilePath: string): boolean { | ||
const packageMetadata: PackageMetadata | undefined = this.tryFetchPackageMetadata(sourceFilePath); | ||
|
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.
What would the perf impact be if we tested for this in
PackageMetadataManager
? #WontFixThere 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.
Generally if you're inside
PackageMetadataManager
for a local file, it's already too late.I could add an assertion here to catch that mistake however.
PackageMetadataManager
is a fairly half-baked file right now, though. I want to make tsdoc-metadata.json a feature of the TSDoc library, and then we'll redesignPackageMetadataManager
when we update it to use that.In reply to: 236576224 [](ancestors = 236576224)