Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

Commit

Permalink
Add fs get and write functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentaTomas committed Jan 27, 2022
1 parent 836eda7 commit 285197f
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 69 deletions.
26 changes: 17 additions & 9 deletions examples/react-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@codemirror/lang-javascript": "^0.19.6",
"@codemirror/language": "^0.19.7",
"@codemirror/matchbrackets": "^0.19.3",
"@devbookhq/sdk": "^1.0.2",
"@devbookhq/sdk": "file:../..",
"@devbookhq/splitter": "^1.3.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
29 changes: 26 additions & 3 deletions examples/react-app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
useState,
useCallback,
useEffect,
} from 'react';

import {
Expand All @@ -19,7 +20,7 @@ console.log('Hostname:', os.hostname());
console.log(process.env)`

const initialCmd =
`ls -l
`ls -l
`

function App() {
Expand All @@ -28,8 +29,30 @@ function App() {
const [cmd, setCmd] = useState(initialCmd);
const [execType, setExecType] = useState('code');

const { stderr, stdout, runCode, runCmd, status } = useDevbook({ debug: true, env: Env.NodeJS });
console.log({ stdout, stderr })
const {
stderr,
stdout,
runCode,
runCmd,
status,
fs,
} = useDevbook({ debug: true, env: Env.NodeJS });
console.log({ stdout, stderr });

useEffect(() => {
async function init() {
if (!fs) return
if (status !== DevbookStatus.Connected) return

// setInterval(async () => {
const random = Math.random()
await fs.write('/src/indexues.js', random.toString())
const content = await fs.get('/src/indexues.js')
console.log('content', content)
// }, 2000)
}
init()
}, [fs, status])

const handleEditorChange = useCallback((content: string) => {
if (execType === 'code') {
Expand Down
46 changes: 23 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devbookhq/sdk",
"version": "1.0.3",
"version": "1.0.4",
"description": "Devbook allows visitors of your docs to interact with and execute any code snippet or shell command in a private VM",
"homepage": "https://usedevbook.com",
"license": "SEE LICENSE IN LICENSE",
Expand All @@ -24,15 +24,15 @@
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^13.1.3",
"@types/node": "^17.0.10",
"@types/node": "^17.0.12",
"@types/react": "^17.0.38",
"@types/react-dom": "^17.0.11",
"rollup": "^2.64.0",
"rollup": "^2.66.1",
"rollup-plugin-auto-external": "^2.0.0",
"rollup-plugin-polyfill-node": "^0.8.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.31.1",
"typescript": "^4.5.4"
"typescript": "^4.5.5"
},
"files": [
"lib",
Expand Down
36 changes: 34 additions & 2 deletions src/core/devbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { SessionStatus } from './session/sessionManager'

const generateExecutionID = makeIDGenerator(6)

export interface FS {
get: (path: string) => Promise<string | undefined>
write: (path: string, content: string) => Promise<void>
}

/**
* States of a {@link Devbook} connection to a VM.
*/
Expand Down Expand Up @@ -36,6 +41,13 @@ class Devbook {
private executionID: string
private readonly contextID: string

get fs(): FS {
return {
get: this.getFile.bind(this),
write: this.writeFile.bind(this),
}
}

private _isDestroyed = false
private get isDestroyed() {
return this._isDestroyed
Expand Down Expand Up @@ -137,6 +149,26 @@ class Devbook {
})
}

async getFile(path: string) {
if (this.status !== DevbookStatus.Connected) throw new Error('Not connected to the VM yet.')

return this.context.getFile({
templateID: this.opts.env,
path,
})

}

async writeFile(path: string, content: string) {
if (this.status !== DevbookStatus.Connected) throw new Error('Not connected to the VM yet.')

return this.context.updateFile({
templateID: this.opts.env,
path,
content,
})
}

/**
* Run `command` in the VM.
*
Expand All @@ -145,7 +177,7 @@ class Devbook {
* @param command Command to run
*/
runCmd(command: string) {
if (this.status === DevbookStatus.Disconnected) return
if (this.status !== DevbookStatus.Connected) throw new Error('Not connected to the VM yet.')

this.executionID = generateExecutionID()

Expand All @@ -164,7 +196,7 @@ class Devbook {
* @param code Code to run
*/
runCode(code: string) {
if (this.status === DevbookStatus.Disconnected) return
if (this.status !== DevbookStatus.Connected) throw new Error('Not connected to the VM yet.')

this.executionID = generateExecutionID()

Expand Down
Loading

0 comments on commit 285197f

Please sign in to comment.