Détail du package

@sketch-hq/sketch-file

sketch-hq3.9kMIT1.1.5

Utilities to read and write Sketch documents

sketch, sketch files

readme

Sketch File

Read and write Sketch documents from TypeScript.

Usage

fromFile function

Read a Sketch Library file containing Color Variables, and dump them to JSON:

import { fromFile } from '@sketch-hq/sketch-file'
import { resolve } from 'path'

const sketchDocumentPath = './sketch-color-library.sketch'

// Custom type color swatches should be converted to
type ColorToken = {
  name: string
  value: {
    r: number
    g: number
    b: number
    a: number
  }
}

// Transform swatches into list of `ColorToken` after parsing the file successfully
fromFile(resolve(__dirname, sketchDocumentPath)).then((parsedFile) => {
  const doc = parsedFile.contents.document
  if (!doc.sharedSwatches) return

  var colors: Array<ColorToken> = []
  doc.sharedSwatches.objects.forEach((color) => {
    colors.push({
      name: color.name,
      value: {
        r: color.value.red,
        g: color.value.green,
        b: color.value.blue,
        a: color.value.alpha,
      },
    })
  })

  console.log(JSON.stringify(colors, null, 2))
})

toFile function

Set the'Background color › Include in Export' option to true for all Artboards in a Sketch document:

import { fromFile, SketchFile, toFile } from '@sketch-hq/sketch-file'
import { Artboard } from '@sketch-hq/sketch-file-format-ts/dist/cjs/types'
import { resolve } from 'path'

const sketchDocumentPath = './artboards-with-background.sketch'

fromFile(resolve(__dirname, sketchDocumentPath)).then((parsedFile) => {
  parsedFile.contents.document.pages.forEach((page) => {
    page.layers
      .filter((layer): layer is Artboard => true)
      .forEach((artboard) => {
        artboard.includeBackgroundColorInExport = true
      })
  })
  var exportableFile: SketchFile = {
    contents: parsedFile.contents,
    filepath: sketchDocumentPath,
  }
  toFile(exportableFile)
})

changelog

@sketch-hq/sketch-file

1.1.5

Patch Changes

  • Updated dependencies [147fb8a]
    • @sketch-hq/sketch-file-format-ts@6.5.0

1.1.4

Patch Changes

  • Updated dependencies [0cf07f5]
  • Updated dependencies [2d515aa]
    • @sketch-hq/sketch-file-format-ts@6.4.0

1.1.3

Patch Changes

  • Updated dependencies [a1323c4]
    • @sketch-hq/sketch-file-format-ts@6.3.1

1.1.2

Patch Changes

  • 6a66fb9: Added preservesSpaceWhenHidden to SymbolInstance and updated dependencies
  • Updated dependencies [6a66fb9]
  • Updated dependencies [582e216]
    • @sketch-hq/sketch-file-format-ts@6.3.0

1.1.1

Patch Changes

  • Updated dependencies [52af1d1]
    • @sketch-hq/sketch-file-format-ts@6.2.1

1.1.0

Minor Changes

  • 452a19c: Fix issue with using String.length to calculate bytesize for unicode strings

Patch Changes

  • 28270c7: Update dependencies
  • Updated dependencies [bc328f6]
  • Updated dependencies [e22c7f3]
  • Updated dependencies [28270c7]
  • Updated dependencies [4ca5853]
  • Updated dependencies [91a0f47]
    • @sketch-hq/sketch-file-format-ts@6.2.0

1.1.0-next.0

Minor Changes

  • 452a19c: Fix issue with using String.length to calculate bytesize for unicode strings

Patch Changes

  • 28270c7: Update dependencies
  • Updated dependencies [bc328f6]
  • Updated dependencies [e22c7f3]
  • Updated dependencies [28270c7]
  • Updated dependencies [4ca5853]
  • Updated dependencies [91a0f47]
    • @sketch-hq/sketch-file-format-ts@6.2.0-next.0

1.0.5

Patch Changes

  • Updated dependencies [9853403]
    • @sketch-hq/sketch-file-format-ts@6.1.1

1.0.4

Patch Changes

  • d5af64e: Updated dependencies
  • Updated dependencies [dd2e77d]
  • Updated dependencies [d5af64e]
    • @sketch-hq/sketch-file-format-ts@6.1.0

1.0.3

Patch Changes

  • Updated dependencies [fd65032]
    • @sketch-hq/sketch-file-format-ts@6.0.3

1.0.2

Patch Changes

  • 6ae901d: Include missing dependencies

1.0.1

Patch Changes

  • 9226ec6: Fixed exports to allow import { fromFile, toFile } from '@sketch-hq/sketch-file'

1.0.0

Major Changes

  • 9014f7b: Add sketch-file package