Skip to content

Commit c1853df

Browse files
authored
Merge pull request microsoft#52 from Microsoft/dagaeta/external-api-helper
Adding custom external @types .d.ts files, ExternalApiHelper to create json files on external .d.ts
2 parents a78eb06 + c239253 commit c1853df

9 files changed

Lines changed: 333 additions & 13 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import * as ts from 'typescript';
2+
import * as os from 'os';
3+
import * as path from 'path';
4+
import * as fs from 'fs';
5+
import Analyzer from './Analyzer';
6+
import ApiJsonGenerator from './generators/ApiJsonGenerator';
7+
8+
/**
9+
* ExternalApiHelper has the specific use case of generating an API json file from third party definition files.
10+
* This class is invoked by the gulp-core-build-typescript gulpfile, where the external package names are
11+
* hard wired.
12+
* The job of this method is almost the same as the API Exactractor task that is executed on first party packages,
13+
* with the exception that all packages analyzed here are external packages with definition files.
14+
*/
15+
export default class ExternalApiHelper {
16+
17+
/**
18+
* @param rootDir - the absolute path containing a 'package.json' file and is also a parent of the
19+
* external package file. Ex: build.absolute_build_path.
20+
* @param libFolder - the path to the lib folder relative to the rootDir, this is where
21+
* 'external-api-json/external_package.api.json' file will be written. Ex: 'lib'.
22+
* @param externalPackageFilePath - the path to the '*.d.ts' file of the external package relative to the rootDir.
23+
* Ex: 'resources/external-api-json/es6-promise/index.t.ds'
24+
*/
25+
public static generateApiJson(rootDir: string, libFolder: string, externalPackageFilePath: string): void {
26+
const analyzer: Analyzer = new Analyzer(
27+
(message: string, fileName: string, lineNumber: number): void => {
28+
console.log(`TypeScript error: ${message}` + os.EOL
29+
+ ` ${fileName}#${lineNumber}`);
30+
}
31+
);
32+
33+
let outputPath: string = path.join(rootDir, libFolder);
34+
if (!fs.existsSync(outputPath)) {
35+
fs.mkdirSync(outputPath);
36+
}
37+
38+
outputPath = path.join(outputPath, 'external-api-json');
39+
if (!fs.existsSync(outputPath)) {
40+
fs.mkdirSync(outputPath);
41+
}
42+
43+
const externalPackageRootDir: string = path.dirname(externalPackageFilePath);
44+
const outputApiJsonFilePath: string = path.join(outputPath, `${path.basename(externalPackageRootDir)}.api.json`);
45+
const entryPointFile: string = path.join(rootDir, externalPackageFilePath);
46+
47+
analyzer.analyze({
48+
compilerOptions: {
49+
target: ts.ScriptTarget.ES5,
50+
module: ts.ModuleKind.CommonJS,
51+
moduleResolution: ts.ModuleResolutionKind.NodeJs,
52+
experimentalDecorators: true,
53+
jsx: ts.JsxEmit.React,
54+
rootDir: rootDir
55+
},
56+
entryPointFile: entryPointFile, // local/bundles/platform-exports.ts',
57+
otherFiles: []
58+
});
59+
60+
const apiJsonGenerator: ApiJsonGenerator = new ApiJsonGenerator();
61+
apiJsonGenerator.writeJsonFile(outputApiJsonFilePath, analyzer);
62+
63+
}
64+
}

‎api-extractor/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { default as Analyzer, IApiAnalyzerOptions, ApiErrorHandler } from './Analyzer';
22
export { default as ApiFileGenerator } from './generators/ApiFileGenerator';
33
export { default as ApiJsonGenerator } from './generators/ApiJsonGenerator';
4+
export { default as ExternalApiHelper } from './ExternalApiHelper';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/gulp-core-build-typescript",
5+
"comment": "Added external-api-json folder with external types definitions. Added gulp task to run ApiExtractor on external types defintions.",
6+
"type": "patch"
7+
},
8+
{
9+
"packageName": "@microsoft/api-extractor",
10+
"comment": "Added ExternalApiHelper class to be used in generating api documentation json files for external types.",
11+
"type": "patch"
12+
}
13+
],
14+
"email": "dgaeta@users.noreply.github.com"
15+
}

‎common/npm-shrinkwrap.json‎

Lines changed: 23 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎gulp-core-build-typescript/.npmignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ lib/**/*.test.d.ts
1010
lib/**/*.test.js
1111
node_modules
1212
npm-shrinkwrap.json
13-
src
13+
src
14+
lib/RunApiExtractorOnExternalApiTypes.*

‎gulp-core-build-typescript/gulpfile.js‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const tslint = require('tslint');
44
const build = require('@microsoft/node-library-build');
5+
const path = require('path');
6+
const fs = require('fs');
57

68
build.tslint.setConfig({
79
lintConfig: require('./src/defaultTslint.json'),
@@ -12,4 +14,22 @@ build.typescript.setConfig({
1214
typescript: require('typescript')
1315
});
1416

15-
build.initialize(require('gulp'));
17+
build.task('default', build.serial(build.parallel(build.tslint,
18+
build.typescript),
19+
build.parallel(build.serial(build.instrument,
20+
build.mocha),
21+
build.subTask('run-api-extractor',
22+
(gulp, buildConfig, callback) => {
23+
const externalApiHelper = require('@microsoft/api-extractor').ExternalApiHelper;
24+
const files = ['resources/external-api-types/es6-collections/index.d.ts',
25+
'resources/external-api-types/es6-promise/index.d.ts',
26+
'resources/external-api-types/whatwg-fetch/index.d.ts'];
27+
28+
for (const filePath of files) {
29+
externalApiHelper.generateApiJson(buildConfig.rootPath, buildConfig.libFolder, filePath);
30+
}
31+
32+
callback();
33+
}))));
34+
35+
build.initialize(require('gulp'));
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Typings based on DefinitelyTyped definitions from es6-collections@0.5.1
2+
3+
export interface IteratorResult<T> {
4+
done: boolean;
5+
value?: T;
6+
}
7+
8+
export interface Iterator<T> {
9+
next(value?: any): IteratorResult<T>;
10+
return?(value?: any): IteratorResult<T>;
11+
throw?(e?: any): IteratorResult<T>;
12+
}
13+
14+
export interface ForEachable<T> {
15+
forEach(callbackfn: (value: T) => void): void;
16+
}
17+
18+
export interface Map<K, V> {
19+
clear(): void;
20+
delete(key: K): boolean;
21+
forEach(callbackfn: (value: V, index: K, map: Map<K, V>) => void, thisArg?: any): void;
22+
get(key: K): V;
23+
has(key: K): boolean;
24+
set(key: K, value?: V): Map<K, V>;
25+
entries(): Iterator<[K, V]>;
26+
keys(): Iterator<K>;
27+
values(): Iterator<V>;
28+
size: number;
29+
}
30+
31+
export interface MapConstructor {
32+
new <K, V>(): Map<K, V>;
33+
new <K, V>(iterable: ForEachable<[K, V]>): Map<K, V>;
34+
prototype: Map<any, any>;
35+
}
36+
37+
export interface Set<T> {
38+
add(value: T): Set<T>;
39+
clear(): void;
40+
delete(value: T): boolean;
41+
forEach(callbackfn: (value: T, index: T, set: Set<T>) => void, thisArg?: any): void;
42+
has(value: T): boolean;
43+
entries(): Iterator<[T, T]>;
44+
keys(): Iterator<T>;
45+
values(): Iterator<T>;
46+
size: number;
47+
}
48+
49+
export interface SetConstructor {
50+
new <T>(): Set<T>;
51+
new <T>(iterable: ForEachable<T>): Set<T>;
52+
prototype: Set<any>;
53+
}
54+
55+
export interface WeakMap <K, V> {
56+
delete(key: K): boolean;
57+
clear(): void;
58+
get(key: K): V;
59+
has(key: K): boolean;
60+
set(key: K, value?: V): WeakMap<K, V>;
61+
}
62+
63+
export interface WeakMapConstructor {
64+
new <K, V>(): WeakMap<K, V>;
65+
new <K, V>(iterable: ForEachable<[K, V]>): WeakMap<K, V>;
66+
prototype: WeakMap<any, any>;
67+
}
68+
69+
export interface WeakSet <T> {
70+
delete(value: T): boolean;
71+
clear(): void;
72+
add(value: T): WeakSet<T>;
73+
has(value: T): boolean;
74+
}
75+
76+
export interface WeakSetConstructor {
77+
new <T>(): WeakSet<T>;
78+
new <T>(iterable: ForEachable<T>): WeakSet<T>;
79+
prototype: WeakSet<any>;
80+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Typings based on DefinitelyTyped definitions from es6-promise
2+
3+
export interface Thenable<T> {
4+
then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Thenable<U>;
5+
then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => void): Thenable<U>;
6+
}
7+
8+
export class Promise<T> implements Thenable<T> {
9+
10+
constructor(callback: (resolve: (value?: T | Thenable<T>) => void, reject: (error?: any) => void) => void);
11+
12+
then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Promise<U>;
13+
then<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => void): Promise<U>;
14+
15+
catch<U>(onRejected?: (error: any) => U | Thenable<U>): Promise<U>;
16+
17+
public static resolve<T>(value?: T | Thenable<T>): Promise<T>;
18+
19+
public static reject(error: any): Promise<any>;
20+
public static reject<T>(error: T): Promise<T>;
21+
22+
public static all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>, T9 | Thenable<T9>, T10 | Thenable<T10>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
23+
public static all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>, T9 | Thenable<T9>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;
24+
public static all<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>;
25+
public static all<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>]): Promise<[T1, T2, T3, T4, T5, T6, T7]>;
26+
public static all<T1, T2, T3, T4, T5, T6>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>]): Promise<[T1, T2, T3, T4, T5, T6]>;
27+
public static all<T1, T2, T3, T4, T5>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>]): Promise<[T1, T2, T3, T4, T5]>;
28+
public static all<T1, T2, T3, T4>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>]): Promise<[T1, T2, T3, T4]>;
29+
public static all<T1, T2, T3>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>]): Promise<[T1, T2, T3]>;
30+
public static all<T1, T2>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>]): Promise<[T1, T2]>;
31+
public static all<T>(values: (T | Thenable<T>)[]): Promise<T[]>;
32+
33+
public static race<T>(promises: (T | Thenable<T>)[]): Promise<T>;
34+
}

0 commit comments

Comments
 (0)