Skip to content

@javasculpt/workspace

type WorkspaceFileInfo = { path: string; id?: string; backendId?: string; kind?: string; header?: ProjectStubOptions; }

Metadata for one file in the workspace.

type BackendInfo = { id: string; label: string; category: string; fileExtension: string; }

A backend registered in this build of JavaSculpt.

type BackendOutput = { readonly kind: string; [key: string]: unknown; }

Output returned by ws.run(). The shape depends on the target backend: JSCAD: kind=‘jscad’ SVG: kind=‘svg’, svg: string Plaintext: kind=‘plaintext’, text: string Canvas 2D: kind=‘canvas2d’, canvas: OffscreenCanvas

type ProjectStubOptions = { name?: string; category?: string; description?: string; tags?: string[]; author?: string; licence?: string; notes?: string; extraTags?: Record<string, string>; }

Parsed JSDoc header metadata from a workspace file’s leading comment block.

type ParamDef = { name: string; type: 'number' | 'text' | 'date' | 'color' | 'boolean' | 'select' | 'group'; caption?: string; default?: string | number | boolean | string[]; [key: string]: unknown; }

A parameter definition returned by ws.getParamDefs().

function files(): WorkspaceFileInfo[]

Return all files currently in the workspace.

Paths are normalised to absolute form with a leading slash (e.g. ‘/lib/helpers.js’) and can be passed directly to require() or to ws.run().

Returns: Array of file descriptors — each has path, id, backendId, kind, and header.

const ws = require('@javasculpt/workspace')
const fonts = ws.files().filter(f => f.kind === 'font')
const jscadFiles = ws.files().filter(f => f.backendId === 'jscad')
function backends(): BackendInfo[]

Return all backends registered in this build of JavaSculpt.

Returns: Array of backend descriptors — each has id, label, category, and fileExtension.

const ws = require('@javasculpt/workspace')
const ids = ws.backends().map(b => b.id)
// e.g. ['jscad', 'svg', 'pdf', 'canvas2d', 'plaintext']
function self(): { path: string; backendId: string }

Return the workspace path and backendId of the currently-running script.

Returns: Object with path and backendId of the executing file.

const ws = require('@javasculpt/workspace')
const { path, backendId } = ws.self()
// path: '/my-project.jscad.js', backendId: 'jscad'
function getParamDefs(filePathOrId: string): Promise<ParamDef[]>

Extract parameter definitions from another workspace script without running its main().

The target script’s getParameterDefinitions() is called inside a restricted sandbox: no DOM, no network access, no @javasculpt/workspace, and a 50 ms timeout. Returns [] if the file has no getParameterDefinitions(), cannot be found, or times out. Never rejects — safe to await without a try/catch.

Parameters:

Name Type Description
filePathOrId — Workspace path (e.g. ‘/generator.jscad.js’) or server UUID.

Returns: The validated parameter definition array from the target script.

const ws = require('@javasculpt/workspace')
async function main(params) {
const defs = await ws.getParamDefs('/shared/config-generator.jscad.js')
const hasResolution = defs.some(d => d.name === 'resolution')
}
function run( filePathOrId: string, params?: Record<string, unknown> ): Promise<BackendOutput>

Execute any workspace script via its backend and return its output.

The output shape depends on the target backend (see BackendOutput). Circular dependencies are detected and cause rejection. Rejects if called inside a ws.getParamDefs() sandbox — use only in main().

Parameters:

Name Type Description
filePathOrId — Workspace path (e.g. ‘/labels.svg.js’) or server UUID.
params — Parameters passed to the target script’s main(). Defaults to {}.

Returns: The backend output of the executed script.

const ws = require('@javasculpt/workspace')
async function main({ count }) {
const out = await ws.run('/data/report.plaintext.js', { rows: count })
return { kind: 'plaintext', text: 'Report:\n' + out.text }
}
function header(filePathOrId: string): ProjectStubOptions

Return the parsed JSDoc header for the given workspace file.

Accepts a workspace path or server UUID. Returns {} if the file has no header block or is not found. Useful for building dynamic parameter pickers (e.g. a font selector that shows each font’s display name).

Parameters:

Name Type Description
filePathOrId — Workspace path or server UUID.

Returns: Parsed header fields, or {} if none.

const ws = require('@javasculpt/workspace')
function getParameterDefinitions() {
const fonts = ws.files()
.filter(f => f.kind === 'font')
.map(f => ({
value: f.path,
label: ws.header(f.path).name ?? f.path.split('/').pop()
}))
return [{ name: 'font', type: 'select', options: fonts, default: fonts[0]?.value ?? '' }]
}