Skip to content

Commit

Permalink
Add code snippet highlight to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentaTomas committed Nov 10, 2023
1 parent 8fd27f9 commit 36bac6e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-yaks-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@e2b/cli": patch
---

Add highlight to CLI code snippet examples
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"async-listen": "^3.0.1",
"boxen": "^7.1.1",
"chalk": "^5.3.0",
"cli-highlight": "^2.1.11",
"commander": "^11.1.0",
"common-tags": "^1.8.2",
"console-table-printer": "^2.11.2",
Expand Down
16 changes: 10 additions & 6 deletions packages/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
asLocal,
asLocalRelative,
asPrimary,
asPython,
asTypescript,
} from 'src/utils/format'
import { pathOption } from 'src/options'
import { createBlobFromFiles } from 'src/docker/archive'
Expand Down Expand Up @@ -122,7 +124,7 @@ export const buildCommand = new commander.Command('build')
const name = newName || config?.name

console.log(
`Preparing sandbox template building (${filePaths.length} files in Docker build context). ${!filePaths.length ? `If you are using ${asLocal('.dockerignore')} check if it is configured correctly.`:''}`,
`Preparing sandbox template building (${filePaths.length} files in Docker build context). ${!filePaths.length ? `If you are using ${asLocal('.dockerignore')} check if it is configured correctly.` : ''}`,
)

const { dockerfileContent, dockerfileRelativePath } = getDockerfile(
Expand Down Expand Up @@ -241,9 +243,10 @@ async function waitForBuildFinish(
)} finished.\n
Now you can start creating your sandboxes from this template. You can find more here:
${asPrimary('https://e2b.dev/docs/guide/custom-sandbox')}, section ${asBold('Spawn and control your sandbox.')}
| ${asBold('Python SDK')} |
==============
from e2b import Sandbox
${asPython(`from e2b import Sandbox
# Start sandbox
sandbox = Sandbox(id="${aliases?.length ? aliases[0] : template.data.envID}")
Expand All @@ -252,12 +255,12 @@ async function waitForBuildFinish(
# https://e2b.dev/docs/sandbox/overview
# Close sandbox once done
sandbox.close()
sandbox.close()`)
}
| ${asBold('JS SDK')} |
==========
import { Sandbox } from '@e2b/sdk'
${asTypescript(`import { Sandbox } from '@e2b/sdk'
// Start sandbox
const sandbox = await Sandbox.create({ id: '${aliases?.length ? aliases[0] : template.data.envID}' })
Expand All @@ -266,7 +269,8 @@ async function waitForBuildFinish(
// https://e2b.dev/docs/sandbox/overview
// Close sandbox once done
await sandbox.close()
await sandbox.close()`)
}
`,
)
break
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/commands/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const shellCommand = new commander.Command('shell')
const apiKey = ensureAPIKey()
let envID = id


const root = getRoot(opts.path)
const configPath = getConfigPath(root)

Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/utils/format.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as chalk from 'chalk'
import * as e2b from '@e2b/sdk'
import * as highlight from 'cli-highlight'

import { cwdRelative } from './filesystem'

Expand Down Expand Up @@ -53,7 +54,16 @@ export function asBuildLogs(content: string) {
export function asHeadline(content: string) {
return chalk.default.underline(asPrimary(asBold(content)))
}

export function listAliases(aliases: string[] | undefined) {
if (!aliases) return undefined
return aliases.join(' | ')
}

export function asTypescript(code: string) {
return highlight.default(code, { language: 'typescript', ignoreIllegals: true })
}

export function asPython(code: string) {
return highlight.default(code, { language: 'python', ignoreIllegals: true })
}
60 changes: 60 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 36bac6e

Please sign in to comment.