forked from microsoft/rushstack
-
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.
New way to resolve & generate TSDoc metadata file
The resolver of the TSDoc metadata file now follows the following proposal: microsoft/tsdoc#7 (comment) The default location of the generated TSDoc metadata file is inferred as to match the resolver. This behaviour can be overriden in api-extractor.json. Generation of the TSDoc medatadata file can even be disabled.
- Loading branch information
1 parent
3a8ecb5
commit efd190b
Showing
13 changed files
with
288 additions
and
23 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 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,138 @@ | ||
|
||
import * as path from 'path'; | ||
import { PackageMetadataManager } from '../PackageMetadataManager'; | ||
import { FileSystem, PackageJsonLookup, IPackageJson } from '@microsoft/node-core-library'; | ||
|
||
/* tslint:disable:typedef */ | ||
|
||
describe('PackageMetadataManager', () => { | ||
describe('.writeTsdocMetadataFile()', () => { | ||
const originalWriteFile = FileSystem.writeFile; | ||
const mockWriteFile: jest.Mock = jest.fn(); | ||
beforeAll(() => { | ||
FileSystem.writeFile = mockWriteFile; | ||
}); | ||
afterEach(() => { | ||
mockWriteFile.mockClear(); | ||
}); | ||
afterAll(() => { | ||
FileSystem.writeFile = originalWriteFile; | ||
}); | ||
|
||
it('writes the tsdoc metadata file at the provided path', () => { | ||
PackageMetadataManager.writeTsdocMetadataFile('/foo/bar'); | ||
expect(firstArgument(mockWriteFile)).toBe('/foo/bar'); | ||
}); | ||
}); | ||
|
||
describe('.resolveTsdocMetadataPath()', () => { | ||
describe('when an empty tsdocMetadataPath is provided', () => { | ||
const tsdocMetadataPath: string = ''; | ||
describe('given a package.json where the field "tsdocMetadata" is defined', () => { | ||
it('outputs the tsdoc metadata path as given by "tsdocMetadata" relative to the folder of package.json', () => { | ||
const { | ||
packageFolder, | ||
packageJson | ||
} = getPackageMetadata('package-inferred-from-tsdoc-metadata'); | ||
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath)) | ||
.toBe(path.resolve(packageFolder, packageJson.tsdocMetadata)); | ||
}); | ||
}); | ||
describe('given a package.json where the field "typings" is defined and "tsdocMetadata" is not defined', () => { | ||
it('outputs the tsdoc metadata file "tsdoc-metadata.json" in the same folder as the path of "typings"', () => { | ||
const { | ||
packageFolder, | ||
packageJson | ||
} = getPackageMetadata('package-inferred-from-typings'); | ||
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath)) | ||
.toBe(path.resolve(packageFolder, path.dirname(packageJson.typings!), 'tsdoc-metadata.json')); | ||
}); | ||
}); | ||
describe('given a package.json where the field "main" is defined but not "typings" nor "tsdocMetadata"', () => { | ||
it('outputs the tsdoc metadata file "tsdoc-metadata.json" in the same folder as the path of "main"', () => { | ||
const { | ||
packageFolder, | ||
packageJson | ||
} = getPackageMetadata('package-inferred-from-main'); | ||
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath)) | ||
.toBe(path.resolve(packageFolder, path.dirname(packageJson.main!), 'tsdoc-metadata.json')); | ||
}); | ||
}); | ||
describe('given a package.json where the fields "main", "typings" and "tsdocMetadata" are not defined', () => { | ||
it('outputs the tsdoc metadata file "tsdoc-metadata.json" in the folder where package.json is located', () => { | ||
const { | ||
packageFolder, | ||
packageJson | ||
} = getPackageMetadata('package-default'); | ||
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath)) | ||
.toBe(path.resolve(packageFolder, 'tsdoc-metadata.json')); | ||
}); | ||
}); | ||
}); | ||
describe('when a non-empty tsdocMetadataPath is provided', () => { | ||
const tsdocMetadataPath: string = 'path/to/custom-tsdoc-metadata.json'; | ||
describe('given a package.json where the field "tsdocMetadata" is defined', () => { | ||
it('outputs the tsdoc metadata file at the provided path in the folder where package.json is located', () => { | ||
const { | ||
packageFolder, | ||
packageJson | ||
} = getPackageMetadata('package-inferred-from-tsdocMetadata'); | ||
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath)) | ||
.toBe(path.resolve(packageFolder, tsdocMetadataPath)); | ||
}); | ||
}); | ||
describe('given a package.json where the field "typings" is defined and "tsdocMetadata" is not defined', () => { | ||
it('outputs the tsdoc metadata file at the provided path in the folder where package.json is located', () => { | ||
const { | ||
packageFolder, | ||
packageJson | ||
} = getPackageMetadata('package-inferred-from-typings'); | ||
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath)) | ||
.toBe(path.resolve(packageFolder, tsdocMetadataPath)); | ||
}); | ||
}); | ||
describe('given a package.json where the field "main" is defined but not "typings" nor "tsdocMetadata"', () => { | ||
it('outputs the tsdoc metadata file at the provided path in the folder where package.json is located', () => { | ||
const { | ||
packageFolder, | ||
packageJson | ||
} = getPackageMetadata('package-inferred-from-main'); | ||
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath)) | ||
.toBe(path.resolve(packageFolder, tsdocMetadataPath)); | ||
}); | ||
}); | ||
describe('given a package.json where the fields "main", "typings" and "tsdocMetadata" are not defined', () => { | ||
it('outputs the tsdoc metadata file at the provided path in the folder where package.json is located', () => { | ||
const { | ||
packageFolder, | ||
packageJson | ||
} = getPackageMetadata('package-default'); | ||
expect(PackageMetadataManager.resolveTsdocMetadataPath(packageFolder, packageJson, tsdocMetadataPath)) | ||
.toBe(path.resolve(packageFolder, tsdocMetadataPath)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
/* tslint:enable:typedef */ | ||
|
||
const packageJsonLookup: PackageJsonLookup = new PackageJsonLookup(); | ||
|
||
function resolveInTestPackage(testPackageName: string, ...args: string[]): string { | ||
return path.resolve(__dirname, 'test-data/tsdoc-metadata-path-inference', testPackageName, ...args); | ||
} | ||
|
||
function getPackageMetadata(testPackageName: string): { packageFolder: string, packageJson: IPackageJson } { | ||
const packageFolder: string = resolveInTestPackage(testPackageName); | ||
const packageJson: IPackageJson | undefined = packageJsonLookup.tryLoadPackageJsonFor(packageFolder); | ||
if (!packageJson) { | ||
throw new Error('There should be a package.json file in the test package'); | ||
} | ||
return { packageFolder, packageJson }; | ||
} | ||
|
||
// tslint:disable-next-line:no-any | ||
function firstArgument(mockFn: jest.Mock): any { | ||
return mockFn.mock.calls[0][0]; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/analyzer/test/test-data/tsdoc-metadata-path-inference/package-default/package.json
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,4 @@ | ||
{ | ||
"name": "package-default", | ||
"version": "1.0.0" | ||
} |
5 changes: 5 additions & 0 deletions
5
...yzer/test/test-data/tsdoc-metadata-path-inference/package-inferred-from-main/package.json
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,5 @@ | ||
{ | ||
"name": "package-inferred-from-main", | ||
"version": "1.0.0", | ||
"main": "path/to/main.js" | ||
} |
7 changes: 7 additions & 0 deletions
7
...test-data/tsdoc-metadata-path-inference/package-inferred-from-tsdoc-metadata/package.json
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,7 @@ | ||
{ | ||
"name": "package-inferred-from-tsdoc-metadata", | ||
"version": "1.0.0", | ||
"main": "path/to/main.js", | ||
"typings": "path/to/typings.d.ts", | ||
"tsdocMetadata": "path/to/tsdoc-metadata.json" | ||
} |
6 changes: 6 additions & 0 deletions
6
...r/test/test-data/tsdoc-metadata-path-inference/package-inferred-from-typings/package.json
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,6 @@ | ||
{ | ||
"name": "package-inferred-from-typings", | ||
"version": "1.0.0", | ||
"main": "path/to/main.js", | ||
"typings": "path/to/typings.d.ts" | ||
} |
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
Oops, something went wrong.