Skip to content
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

Merged
merged 11 commits into from
Nov 28, 2018

Conversation

octogonz
Copy link
Collaborator

@octogonz octogonz commented Nov 27, 2018

This is based on the discussion in microsoft/tsdoc#7 .

Instead of the tsdocFlavor field in package.json:

{
  "name": "widget-lib",
  "version": "1.0.0",
  "main": "lib/index.d.ts",
  "typings": "lib/index.js",
  "tsdoc": {
    "tsdocFlavor": "AEDoc"
  }
}

...we now recognize that an external package is TSDoc-compatible by looking for a file dist/tsdoc-metadata.json with contents like this:

{
  "tsdocVersion": "0.12",
  "toolPackages": [
    {
       "packageName": "@microsoft/api-extractor",
       "packageVersion": "7.0.0"
    }
  ]
}

This is a prototype until an official implementation is provided by the @microsoft/tsdoc library.

__dirname, '..', FileConstants.PackageJson)
);
const myPackageJson: { version: string } = require(myPackageJsonFilename);
const myPackageVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname, '..').version;
Copy link
Member

@iclanton iclanton Nov 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you actually need the ..? #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, package.json is above the src folder.

We could walk upwards to probe for package.json, but that's inefficient and error-prone for a scenario that is supposed to be basically hardcoded.


In reply to: 236574942 [](ancestors = 236574942)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think you persuaded me


In reply to: 236587957 [](ancestors = 236587957,236574942)

* The NPM package version for the currently executing instance of the "\@microsoft/api-extractor" library.
*/
public static get version(): string {
return PackageJsonLookup.loadOwnPackageJson(__dirname, '../..').version;
Copy link
Member

@iclanton iclanton Nov 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. #ByDesign

} else {
throw new Error(`The specified entry point does not appear to have an associated package.json file:\n`
+ sourceFile.fileName);
if (this._program.isSourceFileFromExternalLibrary(sourceFile)) {
Copy link
Member

@iclanton iclanton Nov 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right, or is this condition backwards? #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's right. If the source file is NOT from an external library, then it's part of the project we're analyzing, which obviously supports API Extractor.


In reply to: 236575683 [](ancestors = 236575683)

* 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.
Copy link
Member

@iclanton iclanton Nov 27, 2018

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? #WontFix

Copy link
Collaborator Author

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 redesign PackageMetadataManager when we update it to use that.


In reply to: 236576224 [](ancestors = 236576224)

// 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);
Copy link
Member

@iclanton iclanton Nov 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dist is an arbitrary assumption to make. This should probably be placed in the same directory as the rollup file. #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated TSDoc #7 to reflect this idea.

However I'm not going to implement it here, since this is a temporary workaround.


In reply to: 236597139 [](ancestors = 236597139,236576554)

__dirname, '..', FileConstants.PackageJson)
);
const myPackageJson: { version: string } = require(myPackageJsonFilename);
const myPackageVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname, '..').version;
Copy link
Member

@iclanton iclanton Nov 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just use Extractor.version #Resolved

}

return Rush._version;
return PackageJsonLookup.loadOwnPackageJson(__dirname, '../..').version;
Copy link
Member

@iclanton iclanton Nov 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here with the ../.. #Resolved

@@ -51,7 +48,8 @@ import { MinimalRushConfiguration } from './MinimalRushConfiguration';

// Load the configuration
const configuration: MinimalRushConfiguration | undefined = MinimalRushConfiguration.loadFromDefaultLocation();
const currentPackageJson: IPackageJson = JsonFile.load(path.join(__dirname, '..', FileConstants.PackageJson));

const currentPackageVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname, '..').version;
Copy link
Member

@iclanton iclanton Nov 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here #Resolved

* loading, an exception will be thrown instead.
*/
public static loadOwnPackageJson(dirnameOfCaller: string, pathToPackageJsonFolder: string): IPackageJson {
const packageJsonPath: string = path.join(dirnameOfCaller, pathToPackageJsonFolder, FileConstants.PackageJson);
Copy link
Member

@iclanton iclanton Nov 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this would be simpler if it just used the normal package discovery algorithm. We could include an optimization where we check if the specified path is under a cached path, but without a node_modules folder between the provided path and the cached path. #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be a static member. That means we assume the files we look at will never change on disk, and the cache can accumulate entries without bound. We need to be careful about what we store in this cache.


In reply to: 236578450 [](ancestors = 236578450)

.tryGetPackageJsonFilePathFor(sourceFile.fileName);

if (packageJsonPath) {
throw new Error(`Please add a field such as "tsdoc": { "tsdocFlavor": "AEDoc" } to this file:\n`
Copy link
Member

@iclanton iclanton Nov 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't we say we wanted to just use the metadata file if we're generating a rollup? #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch -- I meant to delete this code


In reply to: 236579005 [](ancestors = 236579005)

Copy link
Member

@iclanton iclanton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@octogonz
Copy link
Collaborator Author

Fixes #959

@octogonz octogonz merged commit b31c2e7 into master Nov 28, 2018
@octogonz octogonz changed the title [api-extractor] Replace the old "tsdocFlavor" field with a dist/api-metadata.json output [api-extractor] Replace the old "tsdocFlavor" field with a dist/tsdoc-metadata.json output Nov 28, 2018
@octogonz octogonz deleted the pgonzal/tsdoc-metadata branch February 3, 2019 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants