包详细信息

@sketch-hq/sketch-file-format-ts

sketch-hq224.7kMIT6.5.0

TypeScript types for the Sketch File Format

sketch, sketch files, file format, file spec

自述文件

Sketch File Format TS

Contains TypeScript types automatically generated from the Sketch File Format JSON Schemas.

Overview

Types are maintained and exported for each Sketch File Format major version. See usage instructions below for more information.

Usage

Add the npm module using npm or yarn

npm install @sketch-hq/sketch-file-format-ts

Types for the latest file format are on the default export

import FileFormat from '@sketch-hq/sketch-file-format-ts'

Read about how file format versions map to Sketch document versions here

Examples

Create a typed layer blur object

import FileFormat from '@sketch-hq/sketch-file-format-ts'

const blur: FileFormat.Blur = {
  _class: 'blur',
  isEnabled: false,
  center: '{0.5, 0.5}',
  motionAngle: 0,
  radius: 10,
  saturation: 1,
  type: FileFormat.BlurType.Gaussian,
}

Layer types can be narrowed using discriminate properties on the helper union types like AnyLayer

import FileFormat from '@sketch-hq/sketch-file-format-ts'

const mapLayers = (layers: FileFormat.AnyLayer[]) => {
  return layers.map((layer) => {
    switch (layer._class) {
      case 'bitmap':
      // type narrowed to Bitmap layers
      case 'star':
      // type narrowed to Star layers
    }
  })
}

Development

Following is further information for making changes to how the types are generated.

Approach

The scripts/generate.ts ingests the file format JSON Schema, and generates type definitions using the TypeScript compiler API.

We depend on multiple major versions of the schemas in package.json using yarn aliases, and generate types for each one. This means that users that have to implement multiple versions of the file format don't need to manually manage multiple versions of this package.

Please note that the latest version of the schemas is referenced directly within the monorepo and not using aliases.

Scripts

Script Description
yarn build Builds the project into the dist folder
yarn test Build script unit tests
yarn format-check Checks the repo with Prettier

Workflows

Conventional commits

Try and use the conventional commits convention when writing commit messages.

Changing how the types are generated

  1. Update scripts/generate.ts
  2. Unit test your changes
  3. Determine the semver bump type and call yarn changeset to create an intent to release your changes (read more about changesets here).
  4. Open a PR to main

Adding or updating a file format version

  1. Use the yarn aliases syntax to add new schema version
  2. Use exact semvers, for example to update or add v3 of the schemas as 3.4.3 run,
    yarn add @sketch-hq/sketch-file-format-3@npm:@sketch-hq/sketch-file-format@3.4.3
  3. If the schema version is new to the repo you'll also need to update the index.ts to export the types, and scripts/generate.ts to generate the new types
  4. Open a PR to main

更新日志

@sketch-hq/sketch-file-format-ts

6.5.0

Minor Changes

  • 147fb8a: Add support for per-document libraries

6.4.0

Minor Changes

  • 0cf07f5: Bump document version to reflect per-document library changes
  • 2d515aa: Add per-document libraries info

6.3.1

Patch Changes

  • a1323c4: Added missing document version number

6.3.0

Minor Changes

  • 6a66fb9: Added preservesSpaceWhenHidden to SymbolInstance and updated dependencies
  • 582e216: - Add isTemplate attribute to Layer to mark it as template for insertion of copies elsewhere.
    • Artboards now contain a prototypeViewport attribute with a PrototypeViewport object to describe the associated viewport for prototypes, replacing the untyped presetDictionary attribute.
    • FlowConnection now contains an automatic destination option.

6.2.1

Patch Changes

  • 52af1d1: Bump document version reflecting changes in Sketch.

6.2.0

Minor Changes

  • e22c7f3: Add a new export prefix naming scheme
  • 4ca5853: Add strikethroughStyle property to text style.
  • 91a0f47: Add cornerStyle to curve point

Patch Changes

  • bc328f6: Bump Sketch document version.
  • 28270c7: Update dependencies

6.2.0-next.0

Minor Changes

  • e22c7f3: Add a new export prefix naming scheme
  • 4ca5853: Add strikethroughStyle property to text style.
  • 91a0f47: Add cornerStyle to curve point

Patch Changes

  • bc328f6: Bump Sketch document version.
  • 28270c7: Update dependencies

6.1.1

Patch Changes

  • 9853403: Revert adding didManuallyDisableResizingContent

6.1.0

Minor Changes

  • dd2e77d: Added didManuallyDisableResizingContent property to stop resizesContent from being enabled automatically when setting a constraint on a child layer.

Patch Changes

  • d5af64e: Updated dependencies

6.0.3

Patch Changes

  • fd65032: Removed fonts property from meta.json

6.0.2

Patch Changes

  • 1ceb662: Remove includeInCloudUpload

6.0.1

Patch Changes

  • 7ba2909: Updated dependencies

6.0.0

Major Changes

  • 4704c97: Drop types for older schema versions. This is a breaking change. Instead of FileFormat1, FileFormat2 and FileFormat3 in addition to the latest file format version as default, types are now only exported as default for the latest schemas.

    To import specific versions of the file format, use specific versions of @sketch-hq/sketch-file-format-ts instead.

Patch Changes

  • fa8a43b: Reorganise source code

5.2.4

Patch Changes

  • 6ba4724: Re-include type declarations

5.2.3

Patch Changes

  • Updated dependencies [34bb488]
  • Updated dependencies [0ec06d1]
    • @sketch-hq/sketch-file-format@5.2.3

5.2.2

Patch Changes

  • 84c9fc2: Update sketch-file-format to 3.7.1

5.2.1

Patch Changes

  • 3a2a6ea: Update sketch-file-format to 3.7.1

5.2.0

Minor Changes

  • 8b269e9: Update sketch-file-format to 3.7.0

Patch Changes

  • 14b05c3: Update to sketch-file-format@3.6.6.

5.1.1

Patch Changes

  • 5b298d9: Update to file format 3.6.2

5.1.0

Minor Changes

  • 1aff125: Include ESM entrypoint.

5.0.0

Major Changes

  • 8c997ce: Update to Sketch File Format 3.6.1

Minor Changes

  • 8c997ce: Add ClassMap a type that maps _class strings to their object type.

4.0.5

Patch Changes

  • c0292ca: Fix problem where package was being built as ESM.

4.0.4

Patch Changes

  • 208e91f: Update dependencies.
  • 208e91f: Add a ClassValue enum containing values for all possible _class values.

4.0.3

Patch Changes

  • 4d07d47: Update to file format 3.5.3