Skip to content

Commit

Permalink
Fixed a few issues
Browse files Browse the repository at this point in the history
  • Loading branch information
remojansen committed Mar 28, 2018
1 parent cffe0f0 commit 4091451
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 44 deletions.
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
src
tsconfig.json
assets
.vscodeignore
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tsuml",
"version": "0.0.1-alpha.5",
"version": "0.0.1-alpha.6",
"description": "UML diagrams for TypeScript",
"main": "dist/lib/index.js",
"bin": {
Expand All @@ -10,13 +10,15 @@
"@types/glob": "5.0.35",
"@types/lodash": "4.14.106",
"@types/node": "9.6.0",
"@types/opn": "5.1.0",
"@types/request": "2.47.0",
"@types/yargs": "11.0.0"
},
"dependencies": {
"chalk": "2.3.2",
"glob": "7.1.2",
"lodash": "4.17.5",
"opn": "5.3.0",
"request": "2.85.0",
"ts-simple-ast": "9.5.0",
"typescript": "2.7.2",
Expand Down
9 changes: 4 additions & 5 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import chalk from "chalk";
import * as yargs from "yargs";
import { yUML } from "../core";
import { render } from "../core/renderer";
import { getUrl } from "../core";

(async () => {

Expand All @@ -18,9 +17,9 @@ import { render } from "../core/renderer";
if (!pattern) {
console.log(chalk.redBright("Missing --glob"));
} else {
const yuml = await yUML("./tsconfig.json", pattern);
const path = await render(yuml, process.cwd());
console.log(chalk.greenBright(`Saved UML diagram available at ${path}`));
const url = await getUrl("./tsconfig.json", pattern);
const opn = require("opn");
opn(url);
}

} catch(e) {
Expand Down
10 changes: 7 additions & 3 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import * as fs from "fs";
import chalk from "chalk";
import { flatten, join } from "lodash";
import { findFilesByGlob } from "./io";
import { findFilesByGlob, download } from "./io";
import { getAst, parseClasses, parseInterfaces, parseHeritageClauses } from "./parser";
import { emitSingleClass, emitSingleInterface, emitHeritageClauses } from "./emitter";
import { render } from "./renderer";

export async function yUML(tsConfigPath: string, pattern: string) {
async function getDsl(tsConfigPath: string, pattern: string) {

const sourceFilesPaths = await findFilesByGlob(pattern);

Expand Down Expand Up @@ -43,3 +42,8 @@ export async function yUML(tsConfigPath: string, pattern: string) {
return join(flatten(entities), ",");

}

export async function getUrl(tsConfigPath: string, pattern: string) {
const dsl = await getDsl(tsConfigPath, pattern);
return await download(dsl);
}
21 changes: 16 additions & 5 deletions src/core/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@ export async function findFilesByGlob(pattern: string) {
});
}

export function download(uri: string, filename: string, callback: () => void) {
request.head(uri, (err, res, body) => {
request(uri)
.pipe(fs.createWriteStream(filename))
.on("close", callback);
export async function download(dsl: string) {
return new Promise<string>((resolve, reject) => {
const url = "https://yuml.me/diagram/scruffy/class/";
const options = {
form: {
dsl_text: dsl
}
};
request.post(url, options, (err, res, body) => {
if (err) {
reject(err);
}
const svgFileName = body.replace(".png", ".svg");
const diagramUrl = `${url}${svgFileName}`;
resolve(diagramUrl);
});
});
};
46 changes: 33 additions & 13 deletions src/core/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@ export function parseClasses(classDeclaration: SimpleAST.ClassDeclaration) {
const propertyDeclarations = classDeclaration.getProperties();
const methodDeclarations = classDeclaration.getMethods();

const properties = propertyDeclarations.map(property => ({
name: property.getChildrenOfKind(ts.SyntaxKind.Identifier)[0].getFullText().trim()
} as PropertyDetails));
const properties = propertyDeclarations.map(property => {
const sym = property.getSymbol();
if (sym) {
return {
name: sym.getName()
};
}
}).filter((p) => p !== undefined) as PropertyDetails[];

const methods = methodDeclarations.map(method => ({
name: method.getChildrenOfKind(ts.SyntaxKind.Identifier)[0].getFullText().trim()
} as MethodDetails));
const methods = methodDeclarations.map(method => {
const sym = method.getSymbol();
if (sym) {
return {
name: sym.getName()
}
}
}).filter((p) => p !== undefined) as MethodDetails[];

return { className, properties, methods };
}
Expand All @@ -37,13 +47,23 @@ export function parseInterfaces(interfaceDeclaration: SimpleAST.InterfaceDeclara
const propertyDeclarations = interfaceDeclaration.getProperties();
const methodDeclarations = interfaceDeclaration.getMethods();

const properties = propertyDeclarations.map(property => ({
name: property.getChildrenOfKind(ts.SyntaxKind.Identifier)[0].getFullText().trim()
} as PropertyDetails));
const properties = propertyDeclarations.map(property => {
const sym = property.getSymbol();
if (sym) {
return {
name: sym.getName()
}
}
}).filter((p) => p !== undefined) as PropertyDetails[];

const methods = methodDeclarations.map(method => ({
name: method.getChildrenOfKind(ts.SyntaxKind.Identifier)[0].getFullText().trim()
} as MethodDetails));
const methods = methodDeclarations.map(method => {
const sym = method.getSymbol();
if (sym) {
return {
name: sym.getName()
}
}
}).filter((p) => p !== undefined) as MethodDetails[];

return { interfaceName, properties, methods };
}
Expand All @@ -57,7 +77,7 @@ export function parseHeritageClauses(classDeclaration: SimpleAST.ClassDeclaratio
return flatten(
heritageClause
.getChildren()
.map(ff => ff.getChildren().map(c => c.getFullText().trim()))
.map(ff => ff.getChildren().map(c => c.getText()))
);
});

Expand Down
12 changes: 0 additions & 12 deletions src/core/renderer.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/core/templates.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { PropertyDetails, MethodDetails} from "./interfaces";

export const templates = {
url: (dsl: string) => `http://yuml.me/diagram/scruffy/class/${dsl}`,
composition: "+->",
implementsOrExtends: (abstraction: string, implementation: string) => {
return (
Expand All @@ -17,7 +16,7 @@ export const templates = {
const mTemplate = (method: MethodDetails) => `${method.name}();`;
return (
`${templates.colorClass(name)}` +
`[${name}|${props.map(pTemplate)}|${methods.map(mTemplate)}]`
`[${name}|${props.map(pTemplate).join("")}|${methods.map(mTemplate).join("")}]`
);
},
interface: (
Expand All @@ -29,7 +28,7 @@ export const templates = {
const mTemplate = (method: MethodDetails) => `${method.name}();`;
return (
`${templates.colorInterface(name)}` +
`[${name}|${props.map(pTemplate)}|${methods.map(mTemplate)}]`
`[${name}|${props.map(pTemplate).join("")}|${methods.map(mTemplate).join("")}]`
);
}
};
3 changes: 1 addition & 2 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { yUML } from "../core";
export { render } from "../core/renderer";
export { getUrl } from "../core";

0 comments on commit 4091451

Please sign in to comment.