• Stars
    star
    633
  • Rank 68,311 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated about 1 year ago

Reviews

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

Repository Details

Quickly generate Sketch libraries from HTML documents and living style guides, powered by html-sketchapp

Build Status npm David David semantic-release Commitizen friendly

html-sketchapp-cli

Quickly generate Sketch libraries from HTML documents and living style guides, powered by html-sketchapp and Puppeteer.

Add some simple markup to your page, for example:

<div data-sketch-symbol="Button/Primary">...</div>
<div data-sketch-text="Heading">...</div>
<div data-sketch-color="#212121">...</div>

Then run the html-sketchapp command to generate JSON files in html-sketchapp's "Almost Sketch" format, ready to be imported into Sketch.

$ html-sketchapp --file sketch.html --out-dir dist/sketch

Install

Please note: html-sketchapp-cli requires Node.js and targets the latest "Active LTS" version. Older versions of Node are unsupported.

If you're in a hurry and just want to try it out, you can install html-sketchapp-cli globally, along with html-sketchapp's Sketch plugin:

$ npm install --global html-sketchapp-cli
$ html-sketchapp install

However, when using html-sketchapp-cli in the context of a project, you should install it locally instead:

$ npm install --save-dev html-sketchapp-cli

Then, add some scripts to your package.json:

{
  "scripts": {
    "html-sketchapp-install": "html-sketchapp install",
    "html-sketchapp": "html-sketchapp [...args]"
  }
}

Once these scripts have been added, the following commands can be run within your project:

$ npm run html-sketchapp-install
$ npm run html-sketchapp

Page Setup

Before using this tool, you'll need to add some hooks to your page so that everything can be selected, extracted and named correctly.

Annotate symbols with data-sketch-symbol attributes. Note that forward slashes will create nested menu items within Sketch.

<div data-sketch-symbol="Button/Primary">
  ...
</div>

Annotate nested symbols with data-sketch-symbol-instance attributes, where the attribute values reference existing symbols defined elsewhere in the document.

<div data-sketch-symbol="Icon/Reply">...</div>
<div data-sketch-symbol="Icon/Retweet">...</div>
<div data-sketch-symbol="Icon/Like">...</div>

<div data-sketch-symbol="IconRow">
  <div data-sketch-symbol-instance="Icon/Reply">...</div>
  <div data-sketch-symbol-instance="Icon/Retweet">...</div>
  <div data-sketch-symbol-instance="Icon/Like">...</div>
</div>

Annotate all text styles with data-sketch-text attributes.

<div data-sketch-text="Heading">
  ...
</div>

Annotate all colors with data-sketch-color attributes. Note that colors are unnamed in Sketch, so only the hex value needs to be provided.

<div data-sketch-color="#212121">
  ...
</div>

For a real world example, check out SEEK Style Guide's sketch exports page and the corresponding source code.

CLI Usage

Importing from a local file

If your page is self-contained, you can import from a local HTML file.

$ html-sketchapp --file sketch.html --out-dir dist

Importing from a local static web server

If your page needs to be hosted on a static web server, you can provide a local directory to serve and a root relative URL to import from.

$ html-sketchapp --serve docs --url /sketch --out-dir dist

Importing from existing web server

If your page is hosted on an existing web server, you can provide an absolute URL.

$ html-sketchapp --url http://localhost:3000 --out-dir dist

Viewport sizes and responsive design

If you provide a set of one or more named viewports, every symbol and text style will be rendered for each screen size.

$ html-sketchapp --viewports.Desktop 1024x768 --viewports.Mobile 320x568 --file sketch.html --out-dir dist

If multiple screen sizes are provided, the viewport name will be being appended to all symbol and text style names. For example, Button/Primary will be exported as Button/Primary/Desktop and Button/Primary/Mobile.

Optionally, you can set the pixel density for a given viewport by adding an @ followed by the desired scaling factor to the end of the viewport's resolution. For example, you can simulate a 1.5x and 2x display like so:

$ html-sketchapp --viewports.HigherDensity [email protected] --viewports.Retina 1024x768@2 --file sketch.html --out-dir dist

If no scaling factor is provided, a default of 1 will be used.

Config file

All options can be provided via an html-sketchapp.config.js file.

module.exports = {
  file: 'sketch.html',
  outDir: 'dist/sketch',
  viewports: {
    Desktop: '1024x768',
    Mobile: '320x568'
  },
  puppeteerArgs: '--no-sandbox --disable-setuid-sandbox',
  puppeteerExecutablePath: 'google-chrome-unstable'
};

You can provide an alternate config file path with the --config option.

$ html-sketchapp --config example.config.js

Importing into Sketch

Once this command has successfully run, the following files will be generated in the output directory.

  • document.asketch.json
  • page.asketch.json

These need to be imported into Sketch via html-sketchapp's corresponding Sketch plugin. To ease the install process, you can run the following command.

$ html-sketchapp install

Then, open a new Sketch document and, from the menu, select Plugins > From *Almost* Sketch to Sketch. In the file picker, select both document.asketch.json and page.asketch.json, and click Choose.

Congratulations! You should now have your symbols, text styles and document colors available within Sketch! ๐Ÿ’Ž๐ŸŽ‰

Advanced Usage

Debug mode

If you need to see what Puppeteer is doing, you can provide the --debug flag which will do the following things:

  • Turn off headless mode
  • Bring the browser window to the front
  • Forward console calls to the terminal
  • Stop the browser from closing until you exit the CLI tool

For example:

$ html-sketchapp --url http://localhost:3000 --out-dir dist --debug

Puppeteer args

If you need to provide command line arguments to the browser instance via Puppeteer, you can provide the puppeteer-args option.

Since Puppeteer uses Chromium internally, you can refer to the List of Chromium Command Line Switches for available options.

For example, if you'd like to disable the browser sandbox:

$ html-sketchapp --puppeteer-args="--no-sandbox --disable-setuid-sandbox" --file sketch.html --out-dir dist

Note: Because Puppeteer args are prefixed with hyphens, you must use an equals sign and quotes when providing this option via the command line (as seen above).

Puppeteer waitUntil

By default, Puppeteer is configured to consider the page loaded when there are no more than 2 network connections for at least 500ms (networkidle2). This is so that html-sketchapp-cli can handle development environments with long-lived connections.

If the page you're requesting has 2 or fewer resources that stall for longer than 500ms and doesn't complete loading, you can switch back to networkidle0 via the puppeteer-wait-until argument:

$ html-sketchapp --puppeteer-wait-until networkidle0 --file sketch.html --out-dir dist

For the full list of available options for waitUntil, view the Puppeteer page.goto() API documentation.

Chromium executable

If you'd like to override the Chromium used by Puppeteer, you can provide a path to the executable with the puppeteer-executable-path option.

$ html-sketchapp --puppeteer-executable-path google-chrome-unstable --file sketch.html --out-dir dist

Middleware

If you need to call out to lower-level html-sketchapp APIs, you can provide middleware functions that allow you to modify the underlying Sketch classes as they're generated.

It's recommended that you provide middleware via a config file as inline functions, for example:

module.exports = {
  symbolLayerMiddleware: (args) => { ... }
};

Alternatively, you can also provide middleware as standalone JavaScript files, configured via the command line:

$ html-sketchapp --symbol-layer-middleware symbol.layer.middleware.js

This assumes that your middleware is a JavaScript module that exports the function:

module.exports = (args) => { ... };

However, in order to keep the documentation streamlined, all examples will use the config file notation.

Symbol Layer Middleware

This middleware is executed for every layer within a symbol.

The typical use case for this is html-sketchapp's layer.setResizingConstraint API which allows you to configure how a layer should behave when a symbol is resized.

module.exports = {
  symbolLayerMiddleware: args => { ... }
};

The following arguments are passed into your middleware function:

  • layer: the html-sketchapp layer instance
  • SVG: The SVG class for type checking of layer
  • Text: The Text class for type checking of layer
  • Rectangle: The Rectangle class for type checking of layer
  • ShapeGroup: The ShapeGroup class for type checking of layer
  • RESIZING_CONSTRAINTS: Object containing constants for the setResizingConstraint API

For example, when handling SVGs differently from other layers:

module.exports = {
  symbolLayerMiddleware: (args) => {
    const { layer, SVG, RESIZING_CONSTRAINTS } = args;

    layer.setResizingConstraint(RESIZING_CONSTRAINTS.LEFT, RESIZING_CONSTRAINTS.TOP);

    if(layer instanceof SVG) {
      layer.setResizingConstraint(RESIZING_CONSTRAINTS.TOP, RESIZING_CONSTRAINTS.LEFT, RESIZING_CONSTRAINTS.WIDTH, RESIZING_CONSTRAINTS.HEIGHT);
    }
  }
};

Symbol Middleware

This middleware is executed for every symbol within a document.

module.exports = {
  symbolMiddleware: args => { ... }
};

The following arguments are passed into your middleware function:

  • symbol: The current symbol master
  • node: The source HTML node
  • suffix: The symbol name suffix (e.g. /Desktop)
  • RESIZING_CONSTRAINTS: Object containing constants for the setResizingConstraint API

Symbol Instance Middleware

This middleware is executed for every symbol instance within a document.

module.exports = {
  symbolInstanceMiddleware: args => { ... }
};

The following arguments are passed into your middleware function:

  • symbolInstance: The current symbol instance
  • symbolMaster: The symbol master that the symbol instance is referencing
  • node: The source HTML node
  • RESIZING_CONSTRAINTS: Object containing constants for the setResizingConstraint API

Contributing

Refer to CONTRIBUTING.md.

License

MIT.

More Repositories

1

vanilla-extract

Zero-runtime Stylesheets-in-TypeScript
TypeScript
5,738
star
2

playroom

Design with JSX, powered by your own component library.
TypeScript
4,321
star
3

braid-design-system

Themeable design system for the SEEK Group
TypeScript
1,465
star
4

capsize

Flipping how we define typography in CSS.
TypeScript
1,356
star
5

treat

๐Ÿฌ Themeable, statically extracted CSSโ€‘inโ€‘JS with nearโ€‘zero runtime.
TypeScript
1,152
star
6

sku

Front-end development toolkit
JavaScript
472
star
7

seek-style-guide

Living style guide for SEEK, powered by React, webpack, CSS Modules and Less.
JavaScript
309
star
8

serverless-haskell

Deploying Haskell applications to AWS Lambda with Serverless
Haskell
213
star
9

css-modules-typescript-loader

Webpack loader to create TypeScript declarations for CSS Modules
JavaScript
193
star
10

vocab

Vocab is a strongly typed internationalization framework for React
TypeScript
122
star
11

csp-server

CSP (Content Security Policy) reports server which forwards reports to Elasticsearch.
JavaScript
56
star
12

docker-ecr-cache-buildkite-plugin

Zero config plugin for caching Docker images in Amazon ECR or Google Container Registry
Shell
51
star
13

skuba

๐Ÿคฟ SEEK development toolkit for backend applications and packages
TypeScript
48
star
14

aws-sm-buildkite-plugin

Buildkite plugin for working with AWS Secrets Manager
Shell
42
star
15

listo

Listo. Use questionnaires and checklists to make it easy to do the right thing, regarding the software you build.
TypeScript
26
star
16

github-merged-pr-buildkite-plugin

BuildKite plugin to work with GitHub PRs
Shell
25
star
17

crackle

A build tool for apps and packages, static and server-rendered sites
TypeScript
20
star
18

changesets-snapshot

A GitHub Action for publishing snapshot releases when using changesets
TypeScript
18
star
19

fsharp-workshop

Exercises for an F# workshop.
F#
15
star
20

lightgbm4j

A JVM interface ๐ŸŒฏ for LightGBM, written in Scala, for inference in production.
Scala
14
star
21

is-pwned

is-pwned โ€”ย Utility to safely check the HaveIBeenPwned "Pwned Passwords" database in the browser.
TypeScript
12
star
22

docker-ecr-publish-buildkite-plugin

Build, tag, and push Docker images to Amazon ECR
Shell
11
star
23

seek-style-guide-webpack

Webpack decorators for integrating with the SEEK Style Guide.
JavaScript
11
star
24

react-scrollmonitor

React wrapper for scrollMonitor
JavaScript
11
star
25

gradle-aws-plugin

AWS Plugin for Gradle
Scala
10
star
26

evaporate

Evaporate is a convention-based, simple CloudFormation Stack deployment tool.
Haskell
9
star
27

browserslist-config-seek

Shareable Browserslist config for SEEK
JavaScript
9
star
28

seek.automation.phantom

A Pact based service simulator.
C#
9
star
29

toolbox

Standard build tools
Shell
8
star
30

aec

AWS EC2 CLI - easily work with ec2 instances via the cli ๐Ÿถ
Python
8
star
31

create-ecr-buildkite-plugin

Create and manage an Amazon ECR repository
Shell
7
star
32

seek.automation.stub

A Pact based stubbing library for .NET.
C#
7
star
33

dynamotools

Tools to manage DynamoDB tables.
Go
7
star
34

snyk-buildkite-plugin

Buildkite plugin for running Snyk scans
Python
7
star
35

renovate-config-seek

Shareable Renovate config for SEEK
JavaScript
7
star
36

eslint-config-seek

Shareable ESLint configuration used by SEEK
JavaScript
7
star
37

private-npm-buildkite-plugin

A Buildkite plugin to simplify the use of an NPM token to install private modules
Shell
6
star
38

datadog-event-buildkite-plugin

BuildKite plugin to send a deployment event to datadog
Shell
5
star
39

seek-stackable

iOS framework for laying out nested views vertically and horizontally.
Swift
5
star
40

ensure-gitignore

Ensure the presence of patterns within a project's gitignore
JavaScript
5
star
41

aws-lambda-scala

Example of an AWS Lambda function written in Scala with deployment tools
Scala
4
star
42

https-proxy-server

HTTP proxy server in Node.js allowing modification of user responses.
JavaScript
4
star
43

style-guide-resources-for-sketch

A template containing SEEK UI components, for designers using Sketch.
4
star
44

ssm-buildkite-plugin

Secure Store Parameters for Buildkite
Shell
4
star
45

scoobie

๐Ÿงถ Component library for SEEK documentation sites
TypeScript
4
star
46

wingman

๐Ÿ›ฉ Reference implementation of a SEEK-integrated recruitment system
TypeScript
4
star
47

koala

๐Ÿจ Koa add-ons for SEEK-standard tracing, logging and metrics
TypeScript
4
star
48

nodejs-consumer-pact-interceptor

A Node.js consumer Pact tool.
JavaScript
3
star
49

rynovate

๐Ÿฆ dangerouslySetRenovatePreset
3
star
50

commitlint-config-seek

Shareable commitlint config for SEEK
JavaScript
3
star
51

kpt-functions

A library of Kpt functions for extending Kpt's functionality
Go
3
star
52

ad-posting-api-client

SEEK's Job Ad Posting API .NET client and sample consumer code.
C#
2
star
53

serverless-plugin-inspect

Serverless plugin - Get AWS stack info in JSON.
JavaScript
2
star
54

IdentityServer4.Contrib.DynamoDB

A persistence layer using DynamoDB for operational data for Identity Server 4
C#
2
star
55

datadog-custom-metrics

๐Ÿถ Common interface for sending Datadog custom metrics from Node.js runtime environments
TypeScript
1
star
56

text1

A NonEmpty version of Data.Text
Haskell
1
star
57

skuba-dive

๐ŸŒŠ Minimal runtime for skuba
TypeScript
1
star
58

action-project-manager

A github action which labels and adds isses and PRs to github projects
JavaScript
1
star
59

fake-hr

Node.js package of HR data sets for mocking and testing
TypeScript
1
star
60

logger

TypeScript
1
star
61

tslint-config-seek

TSLint configuration used by SEEK
JavaScript
1
star
62

babel-plugin-seek-style-guide

Optimise your bundle by automatically rewriting import statements from seek-style-guide
JavaScript
1
star
63

splunk-logger

Generates Splunk consumable logs in Node JS 4.3.2 AWS Lambda functions.
JavaScript
1
star
64

redux-batched-subscribe

FORK: https://github.com/tappleby/redux-batched-subscribe
JavaScript
1
star
65

spark-dist

Spark with hadoop-aws 3.1 for modern S3 access
Makefile
1
star