Skip to content

Commit

Permalink
Add API for converting from pre-parsed thrift files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Giancarlo Anemone authored Feb 13, 2020
1 parent eb35838 commit e2d789c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 249 deletions.
21 changes: 9 additions & 12 deletions src/__tests__/services.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,16 @@ test('Extending a service', () => {
const fixturePath = 'src/__tests__/fixtures/extending-service.thrift';
const converter = new ThriftFileConverter(fixturePath, false);
expect(converter.generateFlowFile()).toMatchInlineSnapshot(`
"// @flow
"// @flow
import * as service from \\"./service\\";
import * as service from \\"./service\\";
export type ExtendingService = service.RealService;
export type ExtendingService = service.RealService;
export type ExtendingServiceWithMethods = {
getNumberTwo: ({|
a: string,
what: boolean
|}) => number,
...service.RealService
};
"
`);
export type ExtendingServiceWithMethods = {
getNumberTwo: ({| a: string, what: boolean |}) => number,
...service.RealService
};
"
`);
});
27 changes: 15 additions & 12 deletions src/main/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,23 @@ const i64Mappings = {
Double: 'number',
};

type Parsed = {|
asts: {[filename: string]: Ast},
filename: string,
idls: {[filename: string]: {||}},
|};

export class ThriftFileConverter {
thriftPath: string;
thrift: {|
asts: {[filename: string]: Ast},
filename: string,
idls: {[filename: string]: {||}},
|};
thrift: Parsed;
withsource: boolean;
ast: Ast;
identifiersTable: {[key: string]: AstNode};

constructor(thriftPath: string, withsource: boolean) {
constructor(thriftPath: string, withsource: boolean, parsed?: Parsed) {
this.thriftPath = path.resolve(thriftPath);
this.thrift = thrift({...thriftOptions, entryPoint: this.thriftPath});
this.thrift =
parsed || thrift({...thriftOptions, entryPoint: this.thriftPath});
this.ast = this.thrift.asts[this.thrift.filename];
this.initIdentifiersTable();
this.withsource = withsource;
Expand Down Expand Up @@ -136,9 +139,7 @@ export class ThriftFileConverter {
if (definition.type === 'Enum') {
definition.definitions.forEach(enumDefinition => {
this.identifiersTable[
`${includePrefix}${definition.id.name}.${
enumDefinition.id.name
}`
`${includePrefix}${definition.id.name}.${enumDefinition.id.name}`
] = enumDefinition;
});
}
Expand All @@ -147,7 +148,7 @@ export class ThriftFileConverter {
);
}

generateFlowFile: () => string = () => {
generateFlowFile: (?boolean) => string = skipFormat => {
const result = [
'// @flow',
this.withsource && `// Source: ${this.thriftPath}`,
Expand All @@ -156,7 +157,9 @@ export class ThriftFileConverter {
]
.filter(Boolean)
.join('\n\n');
return prettier.format(result, {parser: 'flow'});
return skipFormat === true
? result
: prettier.format(result, {parser: 'flow'});
};

convertDefinitionToCode = (def: Definition) => {
Expand Down
38 changes: 37 additions & 1 deletion src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

import {ThriftFileConverter as Converter} from './convert';
import path from 'path';
import type {Ast} from './ast-types';

type OptionsType = {|
withSource?: boolean,
commonPath: string,
outputDir?: string,
skipFormat?: boolean,
|};

type Parsed = {|
asts: {[filename: string]: Ast},
filename: string,
idls: {[filename: string]: {||}},
|};

export const ThriftFileConverter = Converter;
Expand All @@ -32,7 +40,35 @@ export default function convert(
relativeThriftPath,
`${path.basename(thriftPath, '.thrift')}.js`
);
allOutput[jsFilename] = converter.generateFlowFile();
allOutput[jsFilename] = converter.generateFlowFile(options.skipFormat);
}
return allOutput;
}

export function convertParsed(thriftParsed: Parsed, options: OptionsType) {
const {withSource = false, outputDir = 'flow-output', commonPath} = options;
const allOutput = {};
const thriftPaths = [thriftParsed.filename];
for (const thriftPath of thriftPaths) {
const parsed = {
...thriftParsed,
filename: thriftPath,
};
const converter = new ThriftFileConverter(thriftPath, withSource, parsed);
converter
.getImportAbsPaths()
.filter(p => thriftPaths.indexOf(p) === -1)
.forEach(p => thriftPaths.push(p));
const root = commonPath;
const relativeThriftPath = path.dirname(
path.relative(root, converter.thriftPath)
);
const jsFilename = path.resolve(
outputDir,
relativeThriftPath,
`${path.basename(thriftPath, '.thrift')}.js`
);
allOutput[jsFilename] = converter.generateFlowFile(options.skipFormat);
}
return allOutput;
}
Loading

0 comments on commit e2d789c

Please sign in to comment.