Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Add code snippet highlight to CLI

Tomas Valenta36bac6ee2b2f
diff --git a/.changeset/cold-yaks-dress.md b/.changeset/cold-yaks-dress.md
new file mode 100644
index 000000000..cd2fe5686
--- /dev/null
+++ b/.changeset/cold-yaks-dress.md
@@ -0,0 +1,5 @@
+---
+"@e2b/cli": patch
+---
+
+Add highlight to CLI code snippet examples
diff --git a/packages/cli/package.json b/packages/cli/package.json
index c0e01d070..09b40a181 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -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",
diff --git a/packages/cli/src/commands/build.ts b/packages/cli/src/commands/build.ts
index e4da30183..13b301ace 100644
--- a/packages/cli/src/commands/build.ts
+++ b/packages/cli/src/commands/build.ts
@@ -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'
@@ -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(
@@ -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}")
@@ -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}' })
@@ -266,7 +269,8 @@ async function waitForBuildFinish(
// https://e2b.dev/docs/sandbox/overview
// Close sandbox once done
- await sandbox.close()
+ await sandbox.close()`)
+ }
`,
)
break
diff --git a/packages/cli/src/commands/shell.ts b/packages/cli/src/commands/shell.ts
index a6a839b4a..727375491 100644
--- a/packages/cli/src/commands/shell.ts
+++ b/packages/cli/src/commands/shell.ts
@@ -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)
diff --git a/packages/cli/src/utils/format.ts b/packages/cli/src/utils/format.ts
index 1b398932f..83900231c 100644
--- a/packages/cli/src/utils/format.ts
+++ b/packages/cli/src/utils/format.ts
@@ -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'
@@ -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 })
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index b0a12efe5..7fc0cf22c 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -251,6 +251,9 @@ importers:
chalk:
specifier: ^5.3.0
version: 5.3.0
+ cli-highlight:
+ specifier: ^2.1.11
+ version: 2.1.11
commander:
specifier: ^11.1.0
version: 11.1.0
@@ -4196,6 +4199,19 @@ packages:
resolution: {integrity: sha512-/lzGpEWL/8PfI0BmBOPRwp0c/wFNX1RdUML3jK/RcSBA9T8mZDdQpqYBKtCFTOfQbwPqWEOpjqW+Fnayc0969g==}
engines: {node: '>=10'}
+ resolution: {integrity: sha512-9KDcoEVwyUXrjcJNvHD0NFc/hiwe/WPVYIleQh2O1N2Zro5gWJZ/K+3DGn8w8P/F6FxOgzyC5bxDyHIgCSPhGg==}
+ engines: {node: '>=8.0.0', npm: '>=5.0.0'}
+ hasBin: true
+ dependencies:
+ chalk: 4.1.2
+ highlight.js: 10.7.3
+ mz: 2.7.0
+ parse5: 5.1.1
+ parse5-htmlparser2-tree-adapter: 6.0.1
+ yargs: 16.2.0
+ dev: false
+
resolution: {integrity: sha512-w5Jac5SykAeZJKntOxJCrm63Eg5/4dhMWIcuTbo9rpE+brgaSZo0RuNJZeOyMgsUdhDeojvgyQLmjI+K50ZGyg==}
engines: {node: 10.* || >= 12.*}
@@ -4217,6 +4233,14 @@ packages:
wrap-ansi: 6.2.0
dev: false
+ resolution: {integrity: sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==}
+ dependencies:
+ string-width: 4.2.3
+ strip-ansi: 6.0.1
+ wrap-ansi: 7.0.0
+ dev: false
+
resolution: {integrity: sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==}
engines: {node: '>=12'}
@@ -6025,6 +6049,10 @@ packages:
resolution: {integrity: sha512-BXUKIkUuh6cmmxzi5OIbUJxrG8OAk2MqoL1DtO3Wo9D2faJg2ph5ntyuQeLqaHJmzER6H5tllCDA9ZnNe9BVGg==}
dev: false
+ resolution: {integrity: sha512-tzcUFauisWKNHaRkN4Wjl/ZA07gENAjFl3J/c480dprkGTg5EQstgaNFqBfUqCq54kZRIEcreTsAgF/m2quD7A==}
+ dev: false
+
resolution: {integrity: sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==}
dependencies:
@@ -8584,6 +8612,20 @@ packages:
semver: 6.3.1
dev: true
+ resolution: {integrity: sha512-qPuWvbLgvDGilKc5BoicRovlT4MtYT6JfJyBOMDsKoiT+GiuP5qyrPCnR9HcPECIJJmZh5jRndyNThnhhb/vlA==}
+ dependencies:
+ parse5: 6.0.1
+ dev: false
+
+ resolution: {integrity: sha512-ugq4DFI0Ptb+WWjAdOK16+u/nHfiIrcE+sh8kZMaM0WllQKLI9rOUq6c2b7cwPkXdzfQESqvoqK6ug7U/Yyzug==}
+ dev: false
+
+ resolution: {integrity: sha512-Ofn/CTFzRGTTxwpNEs9PP93gXShHcTq255nzRYSKe8AkVpZY7e1fpmTfOyoIvjP5HG7Z2ZM7VS9PPhQGW2pOpw==}
+ dev: false
+
resolution: {integrity: sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g==}
dev: false
@@ -11243,6 +11285,11 @@ packages:
decamelize: 1.2.0
dev: false
+ resolution: {integrity: sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==}
+ engines: {node: '>=10'}
+ dev: false
+
resolution: {integrity: sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==}
engines: {node: '>=12'}
@@ -11264,6 +11311,19 @@ packages:
yargs-parser: 18.1.3
dev: false
+ resolution: {integrity: sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==}
+ engines: {node: '>=10'}
+ dependencies:
+ cliui: 7.0.4
+ escalade: 3.1.1
+ get-caller-file: 2.0.5
+ require-directory: 2.1.1
+ string-width: 4.2.3
+ y18n: 5.0.8
+ yargs-parser: 20.2.9
+ dev: false
+
resolution: {integrity: sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==}
engines: {node: '>=12'}