-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: update dependencies * feat: history file * test: fix * ci: update ci workflow
- Loading branch information
Showing
8 changed files
with
150 additions
and
27 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 |
---|---|---|
|
@@ -2,8 +2,6 @@ name: ci | |
|
||
on: | ||
push: | ||
pull_request: | ||
types: [opened, synchronize] | ||
|
||
jobs: | ||
test: | ||
|
@@ -31,7 +29,7 @@ jobs: | |
- uses: actions/checkout@v1 | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: 12 | ||
node-version: 14 | ||
- run: yarn install | ||
- uses: paambaati/[email protected] | ||
env: | ||
|
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,41 @@ | ||
import { command, program } from '../src' | ||
|
||
let url: string | null = null | ||
|
||
const app = program() | ||
.add( | ||
command('connect') | ||
.description('Connect to a server') | ||
.argument('host', { | ||
default: 'example.com', | ||
prompt: true, | ||
}) | ||
.argument('port', { | ||
default: '443', | ||
type: 'number', | ||
prompt: true, | ||
}) | ||
.argument('tls', { | ||
default: true, | ||
type: 'boolean', | ||
prompt: true, | ||
}) | ||
.action(async ({ host, port, tls }) => { | ||
url = `${tls ? 'https' : 'http'}://${host}:${port}` | ||
console.log(`Connecting to ${url}...`) | ||
}) | ||
) | ||
.add( | ||
command('disconnect') | ||
.description('Disconnect from a server') | ||
.action(async () => { | ||
if (!url) { | ||
throw new Error('Not connected') | ||
} | ||
|
||
console.log(`Disconnecting from ${url}...`) | ||
url = null | ||
}) | ||
) | ||
|
||
app.runOrRepl() |
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,53 @@ | ||
import fs from 'fs' | ||
import os from 'os' | ||
import { REPLServer } from 'repl' | ||
import { Program } from './program' | ||
|
||
/** | ||
* Create new history instance. | ||
*/ | ||
export function history(program: Program) { | ||
return new History(program) | ||
} | ||
|
||
export class History { | ||
private path: string | ||
|
||
constructor(private program: Program) { | ||
this.path = this.program.options.historyFile! | ||
|
||
this.program.on('run', (command) => this.push(command)) | ||
} | ||
|
||
/** | ||
* Add a new entry to the history file. | ||
*/ | ||
public push(entry: string | readonly string[]) { | ||
if (Array.isArray(entry)) { | ||
entry = entry.join(' ') | ||
} | ||
|
||
fs.appendFileSync(this.path, entry + os.EOL) | ||
} | ||
|
||
/** | ||
* Read the history file and hydrate the REPL server history. | ||
*/ | ||
public hydrateReplServer(server: REPLServer) { | ||
// @ts-ignore | ||
if (typeof server.history !== 'object') { | ||
return | ||
} | ||
|
||
try { | ||
fs.readFileSync(this.path, 'utf-8') | ||
.split(os.EOL) | ||
.reverse() | ||
.filter((line) => line.trim()) | ||
// @ts-ignore | ||
.map((line) => server.history.push(line)) | ||
} catch (err) { | ||
// Ignore history file read errors | ||
} | ||
} | ||
} |
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