Skip to content

Commit

Permalink
try typescript in kibana `plugin (#2)
Browse files Browse the repository at this point in the history
* try typescript in kibana `plugin

* compile js and ts from protobuf message
  • Loading branch information
spacedragon authored and mw-ding committed Jun 6, 2018
1 parent b02d208 commit ff85675
Show file tree
Hide file tree
Showing 9 changed files with 569 additions and 154 deletions.
14 changes: 12 additions & 2 deletions kibana-extra/castro/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@ node {
download = true
}

task generateProtoJs(type: NodeTask) {
script = file('node_modules/.bin/pbjs')
args = ['-t', 'static-module', '-w', 'commonjs', '--no-delimited', '--no-encode', '--no-decode', '--es6', '-o', 'common/proto.js', 'proto/*.proto']
}

task generateProto(type: NodeTask, dependsOn: [generateProtoJs]) {
script = file('node_modules/.bin/pbts')
args = ['-o', 'common/proto.d.ts', 'common/proto.js']
}

task bootstrap(type: YarnTask) {
args = ['kbn', 'bootstrap']
}

task startKibana(type: YarnTask) {
task startKibana(type: YarnTask, dependsOn: [generateProto]) {
args = ['start', '--elasticsearch.url', 'http://localhost:9201']
}

Expand All @@ -31,7 +41,7 @@ task setupKibana(type: Sync, dependsOn: downloadKibana) {
eachFile { FileCopyDetails fcp ->
if (fcp.relativePath.pathString.startsWith("kibana-${kibanaVersion}/")) {
def segments = fcp.relativePath.segments
def pathsegments =segments[1..-1] as String[]
def pathsegments = segments[1..-1] as String[]
fcp.relativePath = new RelativePath(!fcp.file.isDirectory(), pathsegments)
} else {
fcp.exclude()
Expand Down
8 changes: 7 additions & 1 deletion kibana-extra/castro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
"devDependencies": {
"@elastic/eslint-config-kibana": "link:../../kibana/packages/eslint-config-kibana",
"@elastic/eslint-import-resolver-kibana": "^0.9.0",
"@elastic/eui": "^0.0.51",
"@kbn/plugin-helpers": "link:../../kibana/packages/kbn-plugin-helpers",
"@types/hapi": "^17.0.12",
"@types/nodegit": "^0.18.8",
"@types/react": "^16.3.16",
"babel-eslint": "^8.0.2",
"eslint": "^4.11.0",
"eslint-plugin-babel": "^4.1.1",
Expand All @@ -29,8 +33,10 @@
"eslint-plugin-prefer-object-spread": "^1.2.1",
"eslint-plugin-react": "^7.0.1",
"expect.js": "^0.3.1"

},
"dependencies": {
"nodegit": "^0.22.1"
"nodegit": "^0.22.1",
"protobufjs": "^6.8.6"
}
}
18 changes: 18 additions & 0 deletions kibana-extra/castro/proto/Example.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";

option java_package = "castro";
option java_multiple_files = true;

message Commit {
string commit = 1;
string committer = 2;
string date = 3;
string message= 4;
repeated Entry entries = 5;
}

message Entry {
string path = 1;
string blob = 2;
bool isBinary = 3;
}
104 changes: 0 additions & 104 deletions kibana-extra/castro/public/components/main/main.js

This file was deleted.

116 changes: 116 additions & 0 deletions kibana-extra/castro/public/components/main/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import React from "react";
import {
EuiPage,
EuiPageHeader,
EuiTitle,
EuiPageBody,
EuiPageContent,
EuiPageContentHeader,
EuiPageContentBody,
EuiText,
EuiDescriptionList,
EuiAccordion,
EuiCodeBlock,
EuiSpacer
} from "@elastic/eui";
import {ICommit, IEntry} from '../../../common/proto'

interface MainProps {
title: String,
httpClient: any
}

interface MainState {
entries: IEntry[],
commitInfo: Array<{title: string, description?: string | null }>
}

export class Main extends React.Component<MainProps, MainState> {
constructor(props: MainProps) {
super(props);
this.state = {
commitInfo: [],
entries: []
};
}

componentDidMount() {
/*
FOR EXAMPLE PURPOSES ONLY. There are much better ways to
manage state and update your UI than this.
*/
const {httpClient} = this.props;
httpClient.get("../api/castro/example").then((resp) => {
const data: ICommit = resp.data;
const commitInfo = [
{
title: "Commit",
description: data.commit
},
{
title: "Date",
description: data.date
},
{
title: "Committer",
description: data.committer
},
{
title: "Message",
description: data.message
}
];

this.setState({commitInfo, entries: data.entries || []});
});
}

render() {
const {title} = this.props;
return (
<EuiPage>
<EuiPageHeader>
<EuiTitle size="l">
<h1>Hello {title}!</h1>
</EuiTitle>
</EuiPageHeader>
<EuiPageBody>
<EuiPageContent>
<EuiPageContentHeader>
<EuiTitle>
<h2>Current Commit</h2>
</EuiTitle>
</EuiPageContentHeader>
<EuiPageContentBody>
<EuiDescriptionList
type="column"
listItems={this.state.commitInfo}
style={{maxWidth: '800px'}}
/>
<EuiSpacer size="xl"/>
<EuiTitle>
<h2>Changed files</h2>
</EuiTitle>
<EuiSpacer size="xs"/>
{
this.state.entries.map((entry, idx) =>
<EuiAccordion
id={"fid" + idx}
key={"fid" + idx}
buttonContent={entry.path}
>
<EuiCodeBlock language="javascript">
{entry.blob}
</EuiCodeBlock>

</EuiAccordion>
)
}
</EuiPageContentBody>
</EuiPageContent>
</EuiPageBody>
</EuiPage>
);
}

};
43 changes: 0 additions & 43 deletions kibana-extra/castro/server/routes/example.js

This file was deleted.

48 changes: 48 additions & 0 deletions kibana-extra/castro/server/routes/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {TreeEntry} from "nodegit";
import * as Hapi from 'hapi';
import {Commit} from '../../common/proto'

const Git = require("nodegit");
const Path = require("path");


async function getHeadCommit(): Promise<Commit> {
const repodir = Path.join(__dirname, "../../../../");

const repo = await Git.Repository.open(repodir);
const commit = await repo.getMasterCommit();

const result = Commit.create({
commit: commit.id().tostrS(),
committer: commit.committer().toString(),
message: commit.message(),
date: commit.date().toLocaleDateString()
});
const tree = await commit.getTree();
const walker = tree.walk(true);
walker.on("entry", async (entry: TreeEntry) => {
const blob = await entry.getBlob();
result.entries.push({
path: entry.path(),
isBinary: blob.isBinary() === 1,
blob: blob.isBinary() === 1 ? "binary" : blob.toString()
})
});
walker.start();
return await (new Promise<Commit>(function (resolve, reject) {
walker.on("end", () => {
resolve(result)
})
}));
}

export default function (server: Hapi.Server) {
server.route({
path: '/api/castro/example',
method: 'GET',
handler(req: Hapi.Request, reply: any) {
getHeadCommit().then((result: Commit) => reply(result))
}
})

}
Loading

0 comments on commit ff85675

Please sign in to comment.