Skip to content

Commit

Permalink
fix(schema): allow overriding context and tsserver path when extracti…
Browse files Browse the repository at this point in the history
…ng (teambit#7906)
  • Loading branch information
luvkapur authored Sep 13, 2023
1 parent 5359a03 commit a899063
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions scopes/semantics/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SchemaAspect } from './schema.aspect';

export { Parser } from './parser';
export { SchemaExtractor } from './schema-extractor';
export type { SchemaExtractorOptions } from './schema-extractor';
export {
SchemaTask,
SCHEMA_ARTIFACT_NAME,
Expand Down
8 changes: 7 additions & 1 deletion scopes/semantics/schema/schema-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface SchemaExtractor {
/**
* extract a semantic schema from a component.
*/
extract(component: Component, formatter?: Formatter): Promise<APISchema>;
extract(component: Component, options?: SchemaExtractorOptions): Promise<APISchema>;
/**
* release resources if no schemas are needed for this process.
* for typescript, this will kill the tsserver process.
Expand All @@ -15,3 +15,9 @@ export interface SchemaExtractor {
*/
dispose(): void;
}

export type SchemaExtractorOptions = {
formatter?: Formatter;
tsserverPath?: string;
contextPath?: string;
};
2 changes: 1 addition & 1 deletion scopes/semantics/schema/schema.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class SchemaMain {
}
const schemaExtractor: SchemaExtractor = env.getSchemaExtractor(undefined, tsserverPath, contextPath);

const result = await schemaExtractor.extract(component, formatter);
const result = await schemaExtractor.extract(component, { formatter, tsserverPath, contextPath });
if (shouldDisposeResourcesOnceDone) schemaExtractor.dispose();

return result;
Expand Down
13 changes: 10 additions & 3 deletions scopes/typescript/typescript/typescript.extractor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ts, { Node, SourceFile, SyntaxKind } from 'typescript';
import { getTsconfig } from 'get-tsconfig';
import { SchemaExtractor } from '@teambit/schema';
import { SchemaExtractor, SchemaExtractorOptions } from '@teambit/schema';
import { TsserverClient } from '@teambit/ts-server';
import type { Workspace } from '@teambit/workspace';
import { ComponentDependency, DependencyResolverMain } from '@teambit/dependency-resolver';
Expand Down Expand Up @@ -52,7 +52,14 @@ export class TypeScriptExtractor implements SchemaExtractor {
/**
* extract a component schema.
*/
async extract(component: Component, formatter?: Formatter): Promise<APISchema> {
async extract(component: Component, options: SchemaExtractorOptions = {}): Promise<APISchema> {
// override the rootTsserverPath and rootContextPath if passed via options
if (options.tsserverPath) {
this.rootTsserverPath = options.tsserverPath;
}
if (options.contextPath) {
this.rootContextPath = options.contextPath;
}
const tsserver = await this.getTsServer();
const mainFile = component.mainFile;
const compatibleExts = ['.tsx', '.ts'];
Expand All @@ -61,7 +68,7 @@ export class TypeScriptExtractor implements SchemaExtractor {
);
const allFiles = [mainFile, ...internalFiles];

const context = await this.createContext(tsserver, component, formatter);
const context = await this.createContext(tsserver, component, options.formatter);

await pMapSeries(allFiles, async (file) => {
const ast = this.parseSourceFile(file);
Expand Down

0 comments on commit a899063

Please sign in to comment.