parent
09ce1a6bbd
commit
c76772137b
@ -0,0 +1,27 @@ |
||||
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: ['command'], |
||||
events: [], |
||||
description: 'Using Remixd daemon, allow to access git API', |
||||
kind: 'other', |
||||
version: packageJson.version |
||||
} |
||||
|
||||
export class GitHandle extends WebsocketPlugin { |
||||
constructor () { |
||||
super(profile) |
||||
} |
||||
|
||||
deactivate () { |
||||
super.deactivate() |
||||
} |
||||
|
||||
activate () { |
||||
super.activate() |
||||
} |
||||
} |
@ -1 +1,2 @@ |
||||
export { RemixdClient as Sharedfolder } from './services/remixdClient' |
||||
export { GitClient } from './services/gitClient' |
||||
|
@ -0,0 +1,62 @@ |
||||
import * as WS from 'ws' // eslint-disable-line
|
||||
import { PluginClient } from '@remixproject/plugin' |
||||
const { spawn } = require('child_process') |
||||
const gitRegex = '^git\\s[^&|;]*$' |
||||
|
||||
export class GitClient extends PluginClient { |
||||
methods: ['command'] |
||||
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 |
||||
} |
||||
|
||||
command (cmd: string) { |
||||
return new Promise((resolve, reject) => { |
||||
try { |
||||
try { |
||||
validateCommand(cmd, gitRegex) |
||||
} catch (e) { |
||||
return reject(e) |
||||
} |
||||
const options = { cwd: this.currentSharedFolder, shell: true } |
||||
const child = spawn(cmd, options) |
||||
let result = '' |
||||
let error = '' |
||||
child.stdout.on('data', (data) => { |
||||
result += data.toString() |
||||
}) |
||||
child.stderr.on('data', (err) => { |
||||
error += err.toString() |
||||
}) |
||||
child.on('close', (exitCode) => { |
||||
if (exitCode !== 0) { |
||||
reject(error) |
||||
} else { |
||||
resolve(result + error) |
||||
} |
||||
}) |
||||
} catch (e) { |
||||
reject(e) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Validate that command can be run by service |
||||
* @param cmd |
||||
* @param regex |
||||
*/ |
||||
function validateCommand (cmd, regex) { |
||||
if (!RegExp(regex).test(cmd)) { // git then space and then everything else
|
||||
throw new Error('Invalid command for service!') |
||||
} |
||||
} |
Loading…
Reference in new issue