-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ink ui template with commands (#74)
- Loading branch information
1 parent
3f7549e
commit 1e9314b
Showing
13 changed files
with
3,970 additions
and
1,980 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,22 @@ | ||
{ | ||
"extends": [ | ||
"plugin:react/recommended", | ||
"./node_modules/ericlint" | ||
], | ||
"rules": { | ||
"react/jsx-uses-react": "error", | ||
"react/jsx-uses-vars": "error", | ||
"react/react-in-jsx-scope": "error" | ||
}, | ||
"parserOptions": { | ||
"project": "tsconfig.eslint.json" | ||
}, | ||
"plugins": [ | ||
"react" | ||
], | ||
"settings": { | ||
"react": { | ||
"version": "detect" | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react' | ||
import { Box } from 'ink' | ||
|
||
export default function Table () { | ||
return ( | ||
<Box flexDirection='column'> | ||
<Box> | ||
<Box width='25%'>props</Box> | ||
</Box> | ||
</Box> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { execSync } from 'child_process' | ||
|
||
export default class commandContext { | ||
public getCommandContext (command: string): (string|undefined)[] { | ||
const text = execSync(`${command} --help`).toString() | ||
|
||
const regex = /(?::|:|คอมมาน)\n((?:\s+.*\n)+)/ | ||
const match = regex.exec(text) | ||
|
||
const commandsRegex = this.makeRegex(command) | ||
if (match) { | ||
const commandsText = match[1] | ||
const commands = commandsText.match(commandsRegex) | ||
?.map((match) => `${command} ${match.trim().split(/\s+/).pop()}`) ?? [] | ||
console.log(commands) | ||
return commands | ||
} | ||
return [] | ||
} | ||
|
||
public getCommandOutput (command: string): string { | ||
let output | ||
if (this.checkInteractive(command)) { | ||
output = execSync(`${command} --interactive`).toString() | ||
} | ||
output = execSync(command).toString() | ||
return output | ||
} | ||
|
||
private checkInteractive (command: string): boolean { | ||
const text = execSync(`${command} --help`).toString() | ||
const regex = /interactive/ | ||
const match = regex.exec(text) | ||
if (match) return true | ||
return false | ||
} | ||
|
||
private makeRegex (command: string): RegExp { | ||
return new RegExp(`\\s+${command}\\s+([A-Za-z0-9]+)`, 'g') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react' | ||
import { Argv } from 'yargs' | ||
import { render } from 'ink' | ||
import App from './views/app.js' | ||
|
||
export const command = 'ui' | ||
|
||
export const desc = '使用 ui 模式' | ||
export const builder = (yargs: Argv<any>) => { | ||
return yargs | ||
} | ||
|
||
export const handler = () => { | ||
render(<App />) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react' | ||
import { Box, Text, useApp, useInput } from 'ink' | ||
import Logo from './logo' | ||
import Command from './command' | ||
import DockerLogs from './dockerLogs' | ||
import Terminal from './terminal' | ||
import SelectInput from 'ink-select-input' | ||
|
||
export default function App () { | ||
const { exit } = useApp() | ||
useInput((input, key) => { | ||
if (input === 'q' || (key.ctrl && input === 'c') || key.escape) { | ||
exit() | ||
} | ||
}) | ||
const items = [ | ||
{ | ||
label: 'Fabric', | ||
value: 'bdk fabric', | ||
}, | ||
{ | ||
label: 'Quorum', | ||
value: 'bdk quorum', | ||
}, | ||
] | ||
return ( | ||
<Box borderStyle='single' flexDirection='column' height={50}> | ||
<Box flexDirection='row' height={90}> | ||
<Box flexDirection='column' flexGrow={1}> | ||
<Logo /> | ||
<Command /> | ||
<SelectInput items={items} isFocused={false} /> | ||
<DockerLogs /> | ||
</Box> | ||
<Box flexGrow={1}> | ||
<Terminal /> | ||
</Box> | ||
</Box> | ||
<Box flexDirection='column'> | ||
</Box> | ||
<Text bold={true} color='yellow'>-- Press q to exit --</Text> | ||
</Box> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from 'react' | ||
import { Text, Box, Newline } from 'ink' | ||
import QuorumCommands from '../services/commandContext' | ||
|
||
export default function Command () { | ||
const quorum = new QuorumCommands() | ||
const [output, setOutput] = React.useState('') | ||
React.useEffect(() => { | ||
setOutput(quorum.getCommandContext('bdk quorum').toString()) | ||
}, [setOutput]) | ||
|
||
return ( | ||
<Box> | ||
<Text>Choose a command:</Text> | ||
<Newline /> | ||
<Box flexDirection="column"> | ||
<Text> Quorum: {output}</Text> | ||
</Box> | ||
</Box> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react' | ||
import { Text, Box } from 'ink' | ||
|
||
const items = [ | ||
{ | ||
label: 'Fabric', | ||
value: 'bdk fabric', | ||
}, | ||
{ | ||
label: 'Quorum', | ||
value: 'bdk quorum', | ||
}, | ||
] | ||
export default function DockerLogs () { | ||
return ( | ||
<Box borderStyle='single' flexDirection='column'> | ||
<Text>Blockchain Platform:</Text> | ||
<Box flexDirection='column'> | ||
{items.map((item) => ( | ||
<Box key={item.value}> | ||
<Text>{item.label}</Text> | ||
</Box> | ||
))} | ||
</Box> | ||
</Box> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react' | ||
import { Text, Box } from 'ink' | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
import Gradient from 'ink-gradient' | ||
|
||
export default function Logo () { | ||
return ( | ||
<Box borderStyle='single' flexDirection='column'> | ||
<Gradient name="rainbow"> | ||
<Text> | ||
Blockchain Deploy Kit | ||
</Text> | ||
</Gradient> | ||
</Box> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react' | ||
import { Text, Box } from 'ink' | ||
import { execSync } from 'child_process' | ||
|
||
export default function Terminal () { | ||
const [output, setOutput] = React.useState('') | ||
React.useEffect(() => { | ||
setOutput(execSync('bdk fabric --help').toString()) | ||
}, [setOutput]) | ||
|
||
return ( | ||
<Box borderStyle="single" flexDirection="column" flexGrow={1}> | ||
<Text>Command output:</Text> | ||
<Box marginTop={1}> | ||
<Text>{output}</Text> | ||
</Box> | ||
</Box> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters