Skip to content

Commit

Permalink
feat: allow for source from strings
Browse files Browse the repository at this point in the history
  • Loading branch information
hbofolts1 committed Aug 30, 2023
1 parent 6289f83 commit 4988dd8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
33 changes: 31 additions & 2 deletions src/tplant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,25 @@ import * as FileFactory from './Factories/FileFactory';
import { MermaidFormat } from './Formatter/MermaidFormat';
import { ICommandOptions } from './Models/ICommandOptions';

const DEFAULT_FILE_NAME = 'source.ts';

export function generateDocumentation(
fileNames: ReadonlyArray<string>,
fileNames: ReadonlyArray<string> | string,
options: ts.CompilerOptions = ts.getDefaultCompilerOptions()
): IComponentComposite[] {

// Build a program using the set of root file names in fileNames
const program: ts.Program = ts.createProgram(fileNames, options);
let program: ts.Program;

if (Array.isArray(fileNames)) {
program = ts.createProgram(fileNames, options);
} else {
program = ts.createProgram({
rootNames: [DEFAULT_FILE_NAME],
options,
host: getCompilerHostForSource(fileNames as string)
});
}

// Get the checker, we will use it to find more about classes
const checker: ts.TypeChecker = program.getTypeChecker();
Expand All @@ -37,6 +49,23 @@ export function generateDocumentation(
return result;
}

function getCompilerHostForSource(source: string): ts.CompilerHost {
const sourceFile = ts.createSourceFile(DEFAULT_FILE_NAME, source, ts.ScriptTarget.ES2016);
return {
getSourceFile: () => sourceFile,
getDefaultLibFileName: () => "",
writeFile: () => undefined,
getCurrentDirectory: () => "",
getCanonicalFileName: () => DEFAULT_FILE_NAME,
useCaseSensitiveFileNames: () => false,
getNewLine: () => "\n",
fileExists: () => true,
readFile: () => {
throw new Error("NOT IMPLEMENTED");
}
}
}

export function convertToPlant(files: IComponentComposite[], options: ICommandOptions = {
associations: false,
onlyInterfaces: false,
Expand Down
22 changes: 13 additions & 9 deletions test/playground.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import * as os from 'os';
import * as fs from 'fs';
import * as tplant from '../src/tplant';

describe('Parse Playground codes', () => {

it('generate PlantUML for Abstract/AbstractClass.ts', () => {
expect(tplant.convertToPlant(tplant.generateDocumentation(['test/Playground/Abstract/AbstractClass.ts'])))
.toEqual(
['@startuml',
'abstract class AbstractClass {',
' +{abstract} ToTest(): any',
' +{abstract} PropTest: number',
'}',
'@enduml'].join(os.EOL)
);
const fileName = 'test/Playground/Abstract/AbstractClass.ts';
const source = fs.readFileSync(fileName).toString();
const expectedForAbstractClass = ['@startuml',
'abstract class AbstractClass {',
' +{abstract} ToTest(): any',
' +{abstract} PropTest: number',
'}',
'@enduml'].join(os.EOL);
expect(tplant.convertToPlant(tplant.generateDocumentation(source)))
.toEqual(expectedForAbstractClass);
expect(tplant.convertToPlant(tplant.generateDocumentation([fileName])))
.toEqual(expectedForAbstractClass);
});

it('generate PlantUML for Classes/Greeter.ts', () => {
Expand Down

0 comments on commit 4988dd8

Please sign in to comment.