scratch-project-ts
v0.1.0
Published
Read Scratch projects and retrieve their scripts with TypeScript
Maintainers
Readme
scratch-project-ts
A small TypeScript wrapper for reading Scratch projects and retrieving their scripts as scratchblocks code.
Installation
npm install scratch-project-tsUsage
import { Opcode, readSB3 } from "scratch-project-ts";
const data = await file.arrayBuffer();
const project = await readSB3(data);
// retrieve scripts from the sprite `Crab`
const crab = project.getTarget("Crab");
// retrieve all `when green flag clicked` scripts
const greenFlagScripts = crab?.getScripts().filter(
script => script.opcode === Opcode.GreenFlag
);
// convert to scratchblocks sources calling `parse-sb3-blocks`
const sources = greenFlagScripts?.map(
script => script.toScratchblocks()
);A target can have more than one script starting with the same opcode. Use contains() to check for a block (e.g. if, repeat until etc.) anywhere inside a script:
const scriptsWithLoops = crab?.getScripts().filter(
script => script.contains("control_repeat_until")
);Opcode contains a few commonly used blocks such as event_whenflagclicked. Other opcode strings, including extension opcodes, can be used directly. Converting a script to scratchblocks code depends on support in parse-sb3-blocks library.
Development
npm install
npm test
npm run build