Skip to content

Workspace introspection

Every script has access to a built-in @javasculpt/workspace module that lets you inspect the workspace it is running in — what files exist, what backends are registered, and what parameters another script defines.

const ws = require('@javasculpt/workspace')

The module is available in every backend (JSCAD, SVG, Canvas 2D, WAV, XM, PDF, and Plain Text).

ws.files() returns a snapshot of all files in the current workspace.

const ws = require('@javasculpt/workspace')
const files = ws.files()
// [
// { path: 'main.jscad.js', backendId: 'jscad' },
// { path: 'data.plaintext.js', backendId: 'plaintext' },
// { path: 'notes.plaintext.js' },
// ]

Each entry is an object with:

Field Type Notes
path string File name (or path if the workspace has subdirectories)
backendId string | undefined The backend explicitly assigned to this file; undefined if inferred from the extension
generated boolean | undefined true for files produced by a generator script

You can use this to make a script react differently depending on what other files are present, or to build a table of contents, index, or summary.

ws.self() returns the path and backend of the currently-executing script.

const ws = require('@javasculpt/workspace')
const self = ws.self()
// { path: 'report.plaintext.js', backendId: 'plaintext' }

This is useful when a script uses relative logic based on its own name, or when a helper module wants to know who loaded it.

ws.backends() returns the list of all backends registered in this build of JavaSculpt.

const ws = require('@javasculpt/workspace')
const backends = ws.backends()
// [
// { id: 'jscad', label: 'JSCAD 3D Modelling', category: 'modelling', fileExtension: '.jscad.js' },
// { id: 'plaintext', label: 'Plain Text', category: 'text', fileExtension: '.plaintext.js' },
// ...
// ]

Each entry has:

Field Type
id Backend identifier string
label Human-readable name
category Broad category (e.g. modelling, image, audio, text)
fileExtension The canonical file extension for this backend

Combining ws.files() and ws.backends() lets you group files by backend, filter for files that match a specific type, or generate summary output that varies by file extension.

ws.getParamDefs(filePath) calls getParameterDefinitions() from any other workspace file and returns a validated array of parameter definitions — without running main().

const ws = require('@javasculpt/workspace')
const params = await ws.getParamDefs('generator.jscad.js')
// [
// { name: 'width', type: 'slider', caption: 'Width (mm):', initial: 40, min: 10, max: 150 },
// { name: 'height', type: 'slider', caption: 'Height (mm):', initial: 20, min: 5, max: 100 },
// ]

This is always async — remember to await it (and mark your main function async too).

getParamDefs runs in a restricted sandbox with a 50 ms timeout. It never throws; if the file is missing, has no getParameterDefinitions, or exceeds the timeout, it silently returns [].

Use case: a script that documents another script’s interface

Section titled “Use case: a script that documents another script’s interface”
const ws = require('@javasculpt/workspace')
async function main() {
const defs = await ws.getParamDefs('shape.jscad.js')
if (!defs.length) return 'shape.jscad.js has no parameters.'
const lines = defs.map(d => {
const range = (d.min !== undefined)
? ` range: ${d.min}–${d.max}` : ''
return ` ${d.name} (${d.type})${range}`
})
return 'Parameters in shape.jscad.js:\n' + lines.join('\n')
}
module.exports = { main }

Use case: adopting defaults from another script

Section titled “Use case: adopting defaults from another script”

If you call a companion script with ws.run() (see the next tutorial) you often want to pass sensible default values. getParamDefs lets you derive them:

const ws = require('@javasculpt/workspace')
async function main() {
const defs = await ws.getParamDefs('generator.jscad.js')
const defaults = Object.fromEntries(
defs
.filter(d => d.initial !== undefined)
.map(d => [d.name, d.initial])
)
// { width: 40, height: 20, ... }
return JSON.stringify(defaults, null, 2)
}
module.exports = { main }

Here is a Plain Text script that produces a workspace summary — every file with its backend and parameter count:

const ws = require('@javasculpt/workspace')
async function main() {
const files = ws.files()
const backends = ws.backends()
const byId = Object.fromEntries(backends.map(b => [b.id, b]))
const lines = await Promise.all(files.map(async f => {
const bid = f.backendId ?? inferBackend(f.path, backends)
const label = byId[bid]?.label ?? bid
const defs = await ws.getParamDefs(f.path)
const pInfo = defs.length ? ` (${defs.length} param${defs.length > 1 ? 's' : ''})` : ''
return `${f.path} [${label}]${pInfo}`
}))
return lines.join('\n')
}
function inferBackend(path, backends) {
return backends.find(b => path.endsWith(b.fileExtension))?.id ?? 'unknown'
}
module.exports = { main }