• Stars
    star
    542
  • Rank 81,982 (Top 2 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Read/write/manipulate Sketch files in Node without Sketch plugins!

Sketch Constructor

This library provides helpers and classes that make it easy to read/write/manipulate Sketch files in Javascript, without Sketch installed!

⚠️ Warning ⚠️

This library is a work in progress, use at your own risk. But feel free to help out where you see bugs or incomplete things! Also, because this library is not using any Sketch APIs/libraries and manipulating the underlying sketch files, the internal file API might change in the future. We will do our best to keep up with any Sketch changes and communicate any breaking API changes.

Why?

If you want to produce Sketch assets for your design team that are generated from your production codebase in a reliable and consistent way, like part of a build process or CI/CD pipeline. Or maybe you want to have your source of truth for your design tokens or components in Sketch, you can use this to extract that data out into your codebase.

What can you do with this?

  • Generate Sketch files programmatically without actually running Sketch (no plugins!)
  • Use Sketch as input to create resources/documentation for a design system
  • Build Sketch files in a CI/CD pipeline
  • Read a Sketch file as a template, hydrate it with data, output a new Sketch file

How is this different from html-sketchapp or react-sketchapp?

Those tools are great and very powerful, but rely on creating sketch plugins on the fly and manipulating a Sketch document that is open on your computer. They are also focused on rendering sketch files and not using a Sketch file as input or data. This tool however helps you directly manipulate and generate Sketch files without a sketch plugin or even having Sketch open or installed.

Installation

This library is published as an npm module, you can install it via npm or yarn:

npm install --save-dev sketch-constructor
yarn add sketch-constructor

Usage

Creating a Sketch file

Creating a completely new Sketch file from scratch, programmatically in node.

const {Sketch, Page, Artboard} = require('sketch-constructor');

// Without a path it creates an empty sketch class to work with
const newSketch = new Sketch();

// Create a new Page instance
const page = new Page({ });

// Add an artboard to the page
const artboard = new Artboard({});
page.addArtboard(artboard);

// Add the page to the Sketch instance
newSketch.addPage( page );

// You can also add a page by giving it an object, the arguments
// are the same as that of the Page constructor
newSketch.addPage({
  name: 'My Page'
});

// Add preview image
Sketch.addPreview('/path/to/preview.png');

// Creates the sketch file
newSketch.build('newSketch.sketch');

Temporary files

This library by defaults uses .sketch-constructor directory for placing temporary files like Bitmap images or preview. We provide APIs to change default temporary directory by providing STORAGE_DIR environment variable.

Read/manipulate an existing Sketch file

Getting data from or manipulating an existing Sketch file.

const {Sketch, Page} = require('sketch-constructor');

// static method fromFile returns a promise
Sketch.fromFile(`${process.cwd()}/__tests__/__sketch-files/test.sketch`)
  .then((sketch) => {
    // You can now get data from the sketch instance
    // For example, get all the Groups in a particular artboard
    // and page that has a name that includes 'test'
    sketch.getPage('Page 1')
      .getArtboard('Artboard 1')
      .getGroups((group) => group.name.includes('test'));

    // Or manipulate the sketch file the same way you would
    // with a new sketch instance
    var page = new Page({});
    sketch.addPage( page );
  });

Models

These are all the classes Sketch uses in it's JSON schema. They should all map 1:1 with any object in the JSON that has _class attribute. All the models are ES6 classes which have instance properties that map 1:1 with the attributes of JSON schema of that class. This makes it easy automatically create class instances from a sketch document as well as write the JSON of a sketch document because calling JSON.stringify() on a class will only output instance properties.

All classes have the same constructor method signature:

class Layer {
  constructor(args = {}, json) {}
}

If you are creating new classes from scratch, you will supply the constructor with a single object like this:

let text = new Text({
  // options
});

This options object does NOT map 1:1 with the underlying JSON schema of the class. It simplifies the API so that you don't have to understand the underlying structure and to hopefully future-proof the classes. A simple example is the Color class:

let color = new Color("#fff")
console.log(JSON.stringify(color, null, 2));
/*
{
  "_class": "color",
  "alpha": 1,
  "blue": 1,
  "green": 1,
  "red": 1
}
*/

Rather than having to pass in the rgba channels (out of 1 not 255 like CSS), you can pass in anything that Tiny Color can understand.

For most classes there are helper methods to make interacting with them easier. For example, the Color class has all of the Tiny Color methods so you can modify the color or get different representations of it.

If you start with an existing Sketch file by using Sketch.fromFile(filePath), the library will recursively create all the underlying classes so you don't have to worry about manually instantiating the classes. For example:

const { Sketch, Page, Artboard } = require('sketch-constructor');

Sketch.fromFile('myFile.sketch')
  .then((sketch) => {
    // create an array of Group instances
    const groups = sketch.getPage('Page 1')
      .getArtboard('Artboard 1')
      .getGroups((group) => group.name.includes('test'));

    // Now you can use any Group instance methods like adding layers
    groups.forEach((group) => {
      group.addLayer(
      new Text({
        string: "Hello",
        name: "Hello",
        fontSize: 24,
        color: "#ccc",
        frame: {
          width: 200,
          height: 50,
          x: 0,
          y: 120,
        }
      })
      )
    })
  })

Contributing

We love contributors! This project is still WIP so any help would be amazing. Please take a look at our contributing docs

License

This library is licensed under the Apache 2.0 License.

More Repositories

1

style-dictionary

A build system for creating cross-platform styles.
JavaScript
3,880
star
2

computer-vision-basics-in-microsoft-excel

Computer Vision Basics in Microsoft Excel (using just formulas)
2,394
star
3

selling-partner-api-docs

This repository contains documentation for developers to use to call Selling Partner APIs.
1,543
star
4

smoke-framework

A light-weight server-side service framework written in the Swift programming language.
Swift
1,443
star
5

alexa-skills-kit-js

SDK and example code for building voice-enabled skills for the Amazon Echo.
1,134
star
6

ion-java

Java streaming parser/serializer for Ion.
Java
840
star
7

selling-partner-api-models

This repository contains OpenAPI models for developers to use when developing software to call Selling Partner APIs.
Mustache
590
star
8

pecos

PECOS - Prediction for Enormous and Correlated Spaces
Python
509
star
9

amzn-drivers

Official AWS drivers repository for Elastic Network Adapter (ENA) and Elastic Fabric Adapter (EFA)
C
455
star
10

ion-js

A JavaScript implementation of Amazon Ion.
TypeScript
323
star
11

convolutional-handwriting-gan

ScrabbleGAN: Semi-Supervised Varying Length Handwritten Text Generation (CVPR20)
Python
265
star
12

xfer

Transfer Learning library for Deep Neural Networks.
Python
253
star
13

awsssmchaosrunner

Amazon's light-weight library for chaos engineering on AWS. It can be used for EC2 and ECS (with EC2 launch type).
Kotlin
249
star
14

ion-python

A Python implementation of Amazon Ion.
Python
210
star
15

amazon-pay-sdk-php

Amazon Pay PHP SDK
PHP
209
star
16

kotlin-inject-anvil

Extensions for the kotlin-inject dependency injection framework
Kotlin
191
star
17

fire-app-builder

Fire App Builder is a framework for building java media apps for Fire TV, allowing you to add your feed of media content to a configuration file and build an app to browse and play it quickly.
Java
182
star
18

exoplayer-amazon-port

Official port of ExoPlayer for Amazon devices
Java
173
star
19

oss-dashboard

A dashboard for viewing many GitHub organizations at once.
Ruby
159
star
20

ion-c

A C implementation of Amazon Ion.
C
149
star
21

metalearn-leap

Original PyTorch implementation of the Leap meta-learner (https://arxiv.org/abs/1812.01054) along with code for running the Omniglot experiment presented in the paper.
Python
148
star
22

ion-go

A Go implementation of Amazon Ion.
Go
146
star
23

auction-gym

AuctionGym is a simulation environment that enables reproducible evaluation of bandit and reinforcement learning methods for online advertising auctions.
Jupyter Notebook
144
star
24

distance-assistant

Pedestrian monitor that provides visual feedback to help ensure proper social distancing guidelines are being observed
Python
135
star
25

hawktracer

HawkTracer is a highly portable, low-overhead, configurable profiling tool built in Amazon Video for getting performance metrics from low-end devices.
C++
133
star
26

trans-encoder

Trans-Encoder: Unsupervised sentence-pair modelling through self- and mutual-distillations
Python
133
star
27

smoke-aws

AWS services integration for the Smoke Framework
Swift
111
star
28

amazon-payments-magento-2-plugin

Extension to enable Amazon Pay on Magento 2
PHP
108
star
29

MXFusion

Modular Probabilistic Programming on MXNet
Python
103
star
30

amazon-weak-ner-needle

Named Entity Recognition with Small Strongly Labeled and Large Weakly Labeled Data
Python
100
star
31

amazon-advertising-api-php-sdk

⛔️ DEPRECATED - Amazon Advertising API PHP Client Library
PHP
93
star
32

ads-advanced-tools-docs

Code samples and supplements for the Amazon Ads advanced tools center
Jupyter Notebook
91
star
33

ion-rust

Rust implementation of Amazon Ion
Rust
86
star
34

image-to-recipe-transformers

Code for CVPR 2021 paper: Revamping Cross-Modal Recipe Retrieval with Hierarchical Transformers and Self-supervised Learning
Python
81
star
35

oss-attribution-builder

The OSS Attribution Builder is a website that helps teams create attribution documents (notices, "open source screens", credits, etc) commonly found in software products.
TypeScript
80
star
36

smoke-http

Specialised HTTP Client for service operations abstracted from the HTTP protocol.
Swift
70
star
37

amazon-ray

Staging area for ongoing enhancements to Ray focused on improving integration with AWS and other Amazon technologies.
Python
66
star
38

alexa-coho

Sample code for building skill adapters for Alexa Connected Home using the Lighting API
JavaScript
62
star
39

amazon-pay-sdk-ruby

Amazon Pay Ruby SDK
Ruby
58
star
40

selling-partner-api-samples

Sample code for Amazon Selling Partner API use cases
Java
56
star
41

amazon-pay-sdk-python

Amazon Pay Python SDK
Python
53
star
42

amazon-pay-sdk-java

Amazon Pay Java SDK
Java
53
star
43

zero-shot-rlhr

Python
51
star
44

supply-chain-simulation-environment

Python
50
star
45

amazon-pay-api-sdk-php

Amazon Pay API SDK (PHP)
PHP
48
star
46

amazon-pay-sdk-csharp

Amazon Pay C# SDK
C#
47
star
47

ion-dotnet

A .NET implementation of Amazon Ion.
C#
47
star
48

multiconer-baseline

Python
47
star
49

zeek-plugin-enip

Zeek network security monitor plugin that enables parsing of the Ethernet/IP and Common Industrial Protocol standards
Zeek
44
star
50

amazon-pay-sdk-samples

Amazon Pay SDK Sample Code
PHP
43
star
51

oss-contribution-tracker

Track contributions made to external projects and manage CLAs
TypeScript
40
star
52

amazon-s3-gst-plugin

A collection of Amazon S3 GStreamer elements.
C
40
star
53

fashion-attribute-disentanglement

Python
39
star
54

zeek-plugin-s7comm

Zeek network security monitor plugin that enables parsing of the S7 protocol
Zeek
39
star
55

milan

Milan is a Scala API and runtime infrastructure for building data-oriented systems, built on top of Apache Flink.
Scala
39
star
56

orthogonal-additive-gaussian-processes

Light-weighted code for Orthogonal Additive Gaussian Processes
Python
38
star
57

jekyll-doc-project

This repository contains an open-source Jekyll theme for authoring and publishing technical documentation. This theme is used by Appstore/Alexa tech writers and other community members. Most of the theme's files are stored in a Ruby Gem (called jekyll-doc-project).
HTML
37
star
58

amazon-pay-api-sdk-nodejs

Amazon Pay API SDK (Node.js)
JavaScript
36
star
59

smoke-dynamodb

SmokeDynamoDB is a library to make it easy to use DynamoDB from Swift-based applications, with a particular focus on usage with polymorphic database tables (tables that do not have a single schema for all rows).
Swift
34
star
60

chalet-charging-location-for-electric-trucks

Optimization tool to identify charging locations for electric trucks
Python
34
star
61

sparse-vqvae

Experimental implementation for a sparse-dictionary based version of the VQ-VAE2 paper
Python
31
star
62

ss-aga-kgc

Python
31
star
63

amazon-pay-api-sdk-java

Amazon Pay API SDK (Java)
Java
30
star
64

zeek-plugin-bacnet

Zeek network security monitor plugin that enables parsing of the BACnet standard building controls protocol
Zeek
29
star
65

credence-to-causal-estimation

A framework for generating complex and realistic datasets for use in evaluating causal inference methods.
Python
29
star
66

basis-point-sets

Python
28
star
67

buy-with-prime-cdk-constructs

This package extends common CDK constructs with opinionated defaults to help create an organization strategy around infrastructure as code.
TypeScript
28
star
68

zeek-plugin-profinet

Zeek network security monitor plugin that enables parsing of the Profinet protocol
Zeek
27
star
69

differential-privacy-bayesian-optimization

This repo contains the underlying code for all the experiments from the paper: "Automatic Discovery of Privacy-Utility Pareto Fronts"
Python
26
star
70

ion-tests

Test vectors for testing compliant Ion implementations.
25
star
71

ion-hive-serde

A Apache Hive SerDe (short for serializer/deserializer) for the Ion file format.
Java
24
star
72

zeek-plugin-tds

Zeek network security monitor plugin that enables parsing of the Tabular Data Stream (TDS) protocol
Zeek
24
star
73

smoke-framework-application-generate

Code generator to generate SmokeFramework-based applications from service models.
Swift
24
star
74

ion-intellij-plugin

Support for Ion in Intellij IDEA.
Kotlin
23
star
75

ion-schema-kotlin

A Kotlin reference implementation of the Ion Schema Specification.
Kotlin
23
star
76

ftv-livetv-sample-tv-app

Java
23
star
77

emukit-playground

A web page explaining concepts of statistical emulation and making decisions under uncertainty in an interactive way.
JavaScript
22
star
78

ion-hash-go

A Go implementation of Amazon Ion Hash.
Go
22
star
79

pretraining-or-self-training

Codebase for the paper "Rethinking Semi-supervised Learning with Language Models"
Python
22
star
80

smoke-framework-examples

Sample applications showing the usage of the SmokeFramework and related libraries.
Swift
21
star
81

confident-sinkhorn-allocation

Pseudo-labeling for tabular data
Jupyter Notebook
21
star
82

tiny-attribution-generator

A small tool and library to create attribution notices from various formats
TypeScript
20
star
83

smoke-aws-generate

Code generator to generate the SmokeAWS library from service models.
Swift
19
star
84

ion-docs

Source for the GitHub Pages for Ion.
Java
19
star
85

autotrail

AutoTrail is a highly modular, partial automation workflow engine providing run time execution control
Python
19
star
86

smoke-aws-credentials

A library to obtain and assume automatically rotating AWS IAM roles written in the Swift programming language.
Swift
19
star
87

amazon-codeguru-profiler-for-spark

A Spark plugin for CPU and memory profiling
Java
18
star
88

git-commit-template

Set commit templates for git
JavaScript
18
star
89

service-model-swift-code-generate

Modular code generator to generate Swift applications from service models.
Swift
18
star
90

amazon-pay-api-sdk-dotnet

Amazon Pay API SDK (.NET)
C#
18
star
91

sample-fire-tv-app-video-skill

This sample Fire TV app shows how to integrate an Alexa video skill in a simple, basic way.
Java
16
star
92

amazon-template-library

A collection of general purpose C++ utilities that play well with the Standard Library and Boost.
C++
16
star
93

rheoceros

Cloud-based AI / ML workflow and data application development framework
Python
16
star
94

ion-cli

Rust
15
star
95

refuel-open-domain-qa

Python
15
star
96

amazon-instant-access-sdk-php

PHP SDK to aid in 3p integration with Instant Access
PHP
14
star
97

amazon-mcf-plugin-for-magento-1

Plugin code to enable Amazon MCF in Magento 1.
PHP
14
star
98

login-with-amazon-wordpress

A pre-integrated plugin that can be installed into a Wordpress powered website to integrate with Login with Amazon.
PHP
14
star
99

firetv-sample-touch-app

This sample Android project demonstrates how to build the main UI of a Fire TV application in order to support both Touch interactions and Remote D-Pad controls.
Java
14
star
100

amzn-ec2-ena-utilities

Python
14
star