💎 node-sketch
Javascript library to manipulate sketch files
Install
npm install node-sketch
Example:
const ns = ; { const sketch = await ns; //Search the symbol named 'button' const buttonSymbol = sketchsymbolsPage; //Search all instances of a symbol named 'old-button' and replace it with 'button' sketch ; //Save the result sketch ;} ;
API
Two classes are used to manage sketch files:
Sketch
Represents the sketch file and contains all data (pages, symbols, styles, shapes, etc). Contains the method .save()
to create a sketch file with the result.
const ns = ; ns;
Node
It's the base class used by all other elements. Any page, symbol, color, etc is an instance of this class.
const symbolsPage = sketchsymbolsPage; console; //true //It include useful methods to search an inner node by class:const button = symbolsPage; //by class and nameconst button = symbolsPage; //by class and callbackconst button = symbolsPage; //Just a callbackconst button = symbolsPage; //And the same than above but returning all inner nodes instead just the first:const allSymbols = symbolsPage;
There are other classes extending Node
to provide special funcionalities in some nodes, like Style
or SymbolInstance
.
JSON Scheme
Technically, the sketch format consist in a zip with some json files. To manipulate a sketch file with this library, you need to know the scheme of json. You can use this code to read and extract a sketch file into a directory, in order to inspect the json scheme:
const ns = ; ns;
Here you can see an example of extracted file
CLI
Starting from v0.14.0, the command node-sketch
was included to use the library from CLI. You only need a file named node-sketch.js
exporting the function to manipulate a sketch file. For example:
module { //Convert the text style names to uppercase sketchtextStyles}
To execute this script with the sketch file my-styles.sketch
, run node-sketch my-styles.sketch
.
By default, the file is readed, but not saved. If you want to override the file with the modifications, run node-sketch my-styles.sketch --save
.
And to execute a script file with a different name, use the --script
argument: node-sketch my-styles.sketch --script=my-script.js --save
.
Or build it locally with npm run docs