Merge pull request #638 from ethereum/addgitService
Add git service in remixd & use it from the terminalwebsite_link
commit
25b83afd05
@ -0,0 +1,18 @@ |
|||||||
|
import { WebsocketPlugin } from '@remixproject/engine-web' |
||||||
|
import * as packageJson from '../../../../../package.json' |
||||||
|
|
||||||
|
const profile = { |
||||||
|
name: 'git', |
||||||
|
displayName: 'Git', |
||||||
|
url: 'ws://127.0.0.1:65521', |
||||||
|
methods: ['execute'], |
||||||
|
description: 'Using Remixd daemon, allow to access git API', |
||||||
|
kind: 'other', |
||||||
|
version: packageJson.version |
||||||
|
} |
||||||
|
|
||||||
|
export class GitHandle extends WebsocketPlugin { |
||||||
|
constructor () { |
||||||
|
super(profile) |
||||||
|
} |
||||||
|
} |
@ -1 +1,2 @@ |
|||||||
export { RemixdClient as Sharedfolder } from './services/remixdClient' |
export { RemixdClient as Sharedfolder } from './services/remixdClient' |
||||||
|
export { GitClient } from './services/gitClient' |
||||||
|
@ -0,0 +1,50 @@ |
|||||||
|
import * as WS from 'ws' // eslint-disable-line
|
||||||
|
import { PluginClient } from '@remixproject/plugin' |
||||||
|
const { spawn } = require('child_process') |
||||||
|
|
||||||
|
export class GitClient extends PluginClient { |
||||||
|
methods: ['execute'] |
||||||
|
websocket: WS |
||||||
|
currentSharedFolder: string |
||||||
|
readOnly: boolean |
||||||
|
|
||||||
|
setWebSocket (websocket: WS): void { |
||||||
|
this.websocket = websocket |
||||||
|
} |
||||||
|
|
||||||
|
sharedFolder (currentSharedFolder: string, readOnly: boolean): void { |
||||||
|
this.currentSharedFolder = currentSharedFolder |
||||||
|
this.readOnly = readOnly |
||||||
|
} |
||||||
|
|
||||||
|
execute (cmd: string) { |
||||||
|
assertCommand(cmd) |
||||||
|
const options = { cwd: this.currentSharedFolder, shell: true } |
||||||
|
const child = spawn(cmd, options) |
||||||
|
let result = '' |
||||||
|
let error = '' |
||||||
|
return new Promise((resolve, reject) => { |
||||||
|
child.stdout.on('data', (data) => { |
||||||
|
result += data.toString() |
||||||
|
}) |
||||||
|
child.stderr.on('data', (err) => { |
||||||
|
error += err.toString() |
||||||
|
}) |
||||||
|
child.on('close', () => { |
||||||
|
if (error) reject(error) |
||||||
|
else resolve(result) |
||||||
|
}) |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Validate that command can be run by service |
||||||
|
* @param cmd |
||||||
|
*/ |
||||||
|
function assertCommand (cmd) { |
||||||
|
const regex = '^git\\s[^&|;]*$' |
||||||
|
if (!RegExp(regex).test(cmd)) { // git then space and then everything else
|
||||||
|
throw new Error('Invalid command for service!') |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue