Skip to content

Commit

Permalink
Implement the generation of image files via www.plantuml.com/plantuml (
Browse files Browse the repository at this point in the history
…#38)

* * Implement the generation of image files via www.plantuml.com/plantuml
* Fix Namespace bug for an empty body

* * Use fs.createWriteStream to download the image
  • Loading branch information
Marcos Vinícius Rubido authored May 23, 2019
1 parent 96751cd commit b8ddd11
Show file tree
Hide file tree
Showing 8 changed files with 537 additions and 484 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
# tplant
Typescript to PlantUML
Typescript to UML (PlantUML)

Will convert a Typescript file to a PlantUML file. Following all inheritances.
Will convert a Typescript file to UML diagram. Following all inheritances.

## Usage

```
### Install
```shell
npm install --global tplant
```

### Generate image file (svg or png)
```shell
tplant --input test/Playground/**/*.ts --output test/Playground/Playground.svg
tplant --input test/Playground/Classes/Greeter.ts --output test/Playground/Classes/Greeter.png
```
> Internet connection is required
### Generate puml file
```shell
tplant --input test/Playground/**/*.ts --output test/Playground/Playground.puml
tplant --input test/Playground/Classes/Greeter.ts --output test/Playground/Classes/Greeter.puml
```

Expand Down Expand Up @@ -45,7 +58,7 @@ Car *-- Wheel
@enduml
```
### -I, --only-interfaces
Only output interfaces
Only convert interfaces

# References
https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API
21 changes: 20 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"name": "tplant",
"version": "2.2.1",
"version": "2.3.0",
"description": "Typescript to PlantUML",
"keywords": [
"Class Diagram",
"class diagram",
"uml diagram",
"plantuml",
"typescript",
"uml"
"uml",
"cli"
],
"homepage": "https://github.com/bafolts/tplant#readme",
"bugs": {
Expand Down Expand Up @@ -41,6 +43,7 @@
"dependencies": {
"commander": "^2.20.0",
"glob": "^7.1.3",
"plantuml-encoder": "^1.2.5",
"typescript": "^3.4.5"
},
"devDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions src/@types/plantuml-encoder/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="node" />

declare module 'plantuml-encoder' {
export function encode(text: string): string;
}
11 changes: 2 additions & 9 deletions src/Components/Namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,14 @@ export class Namespace implements IComponentComposite {

public toPUML(): string {
const result: string[] = [];
const declaration: string[] = [];
declaration.push(`namespace ${this.name}`);
if (this.parts.length > 0) {
declaration.push(' {');
}
result.push(declaration.join(''));
result.push(`namespace ${this.name} {`);
this.parts.forEach((part: IComponentComposite): void => {
result.push(
part.toPUML()
.replace(/^(?!\s*$)/gm, ' ')
);
});
if (this.parts.length > 0) {
result.push('}');
}
result.push('}');

return result.join(os.EOL);
}
Expand Down
46 changes: 32 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import commander from 'commander';
import fs from 'fs';
import G from 'glob';
import http from 'http';
import os from 'os';
import path from 'path';
import { encode } from 'plantuml-encoder';
import ts from 'typescript';
import { tplant } from './tplant';

const AVAILABLE_PLANTUML_EXTENSIONS: string[] = ['svg', 'png', 'txt'];

commander
.version('2.2.1')
.version('2.3.0')
.option('-i, --input <path>', 'Define the path of the Typescript file')
.option('-o, --output <path>', 'Define the path of the output file. If not defined, it\'ll output on the STDOUT')
.option(
Expand All @@ -31,14 +35,12 @@ if (!commander.input) {

G(<string>commander.input, {}, (err: Error | null, matches: string[]): void => {
if (err !== null) {
console.error(err);

return;
throw err;
}

const tsConfigFile: string | undefined = findTsConfigFile(<string>commander.input, <string | undefined>commander.tsconfig);

const output: string = tplant.convertToPlant(
const plantUMLDocument: string = tplant.convertToPlant(
tplant.generateDocumentation(matches, getCompilerOptions(tsConfigFile)),
{
compositions: <boolean>commander.compositions,
Expand All @@ -47,21 +49,22 @@ G(<string>commander.input, {}, (err: Error | null, matches: string[]): void => {
);

if (commander.output === undefined) {
console.log(output);
console.log(plantUMLDocument);

return;
}

// tslint:disable-next-line non-literal-fs-path
fs.writeFile(<string>commander.output, output, (errNoException: NodeJS.ErrnoException | null): void => {
if (errNoException !== null) {
console.error(errNoException);
const extension: string = path.extname(<string>commander.output)
.replace(/^\./gm, '');

return;
}
if (AVAILABLE_PLANTUML_EXTENSIONS.includes(extension)) {
requestImageFile(<string>commander.output, plantUMLDocument, extension);

console.log('The file was saved!');
});
return;
}

// tslint:disable-next-line non-literal-fs-path
fs.writeFileSync(<string>commander.output, plantUMLDocument, 'binary');
});

function findTsConfigFile(inputPath: string, tsConfigPath?: string): string | undefined {
Expand Down Expand Up @@ -129,3 +132,18 @@ function getCompilerOptions(tsConfigFilePath?: string): ts.CompilerOptions {

return convertedCompilerOptions.options;
}

function requestImageFile(output: string, input: string, extension: string): void {
http.get({
host: 'www.plantuml.com',
path: `/plantuml/${extension}/${encode(input)}`
}, (res: http.IncomingMessage): void => {
// tslint:disable-next-line non-literal-fs-path
const fileStream: fs.WriteStream = fs.createWriteStream(output);
res.setEncoding('binary');
res.pipe(fileStream);
res.on('error', (err: Error): void => {
throw err;
});
});
}
Loading

0 comments on commit b8ddd11

Please sign in to comment.