diff --git a/src/Platform.ts b/src/Platform.ts index a5f529ec..f2824ca2 100644 --- a/src/Platform.ts +++ b/src/Platform.ts @@ -1,6 +1,8 @@ +import getSketchVersion from './utils/getSketchVersion'; + const Platform = { OS: 'sketch', - Version: 1, + Version: getSketchVersion(), select: (obj: { sketch: any }) => obj.sketch, }; diff --git a/src/jsonUtils/svgLayer.ts b/src/jsonUtils/svgLayer.ts index 489dc567..01289cf1 100644 --- a/src/jsonUtils/svgLayer.ts +++ b/src/jsonUtils/svgLayer.ts @@ -1,14 +1,11 @@ import { FileFormat1 as FileFormat } from '@sketch-hq/sketch-file-format-ts'; import { LayoutInfo } from '../types'; -import { getSketchVersion } from '../utils/getSketchVersion'; +import isRunningInSketch from '../utils/isRunningInSketch'; import sketchMethod from './sketchImpl/makeSvgLayer'; import nodeMethod from './nodeImpl/makeSvgLayer'; const makeSvgLayer = (layout: LayoutInfo, name: string, svg: string): FileFormat.Group => { - if (getSketchVersion() === 'NodeJS') { - return nodeMethod(layout, name, svg); - } - return sketchMethod(layout, name, svg); + return isRunningInSketch() ? sketchMethod(layout, name, svg) : nodeMethod(layout, name, svg); }; export default makeSvgLayer; diff --git a/src/sharedStyles/TextStyles.ts b/src/sharedStyles/TextStyles.ts index 2f8fcda8..211368fe 100644 --- a/src/sharedStyles/TextStyles.ts +++ b/src/sharedStyles/TextStyles.ts @@ -1,6 +1,7 @@ import { FileFormat1 as FileFormat } from '@sketch-hq/sketch-file-format-ts'; import { SketchDocumentData, SketchDocument, WrappedSketchDocument, TextStyle } from '../types'; -import { getSketchVersion } from '../utils/getSketchVersion'; +import getSketchVersion from '../utils/getSketchVersion'; +import isRunningInSketch from '../utils/isRunningInSketch'; import hashStyle from '../utils/hashStyle'; import { getDocument } from '../utils/getDocument'; import sharedTextStyles from '../utils/sharedTextStyles'; @@ -22,8 +23,6 @@ type StyleHash = { [key: string]: RegisteredStyle }; let _styles: StyleHash = {}; const _byName: { [key: string]: MurmurHash } = {}; -const sketchVersion = getSketchVersion(); - const registerStyle = (name: string, style: TextStyle): void => { const safeStyle = pick(style, INHERITABLE_FONT_STYLES); const hash = hashStyle(safeStyle); @@ -51,7 +50,7 @@ const create = (styles: { [key: string]: TextStyle }, options: Options = {}): St const doc = getDocument(document); - if (sketchVersion !== 'NodeJS' && sketchVersion < 50) { + if (isRunningInSketch() && parseInt(getSketchVersion()) < 50) { doc.showMessage('💎 Requires Sketch 50+ 💎'); return {}; } diff --git a/src/utils/getSketchVersion.ts b/src/utils/getSketchVersion.ts index e754f282..d38095d5 100644 --- a/src/utils/getSketchVersion.ts +++ b/src/utils/getSketchVersion.ts @@ -1,6 +1,5 @@ -export function getSketchVersion(): number | 'NodeJS' { - if (typeof NSBundle !== 'undefined') { - return parseFloat(NSBundle.mainBundle().infoDictionary().CFBundleShortVersionString); - } - return 'NodeJS'; +export default function getSketchVersion(): string { + return typeof NSBundle !== 'undefined' + ? NSBundle.mainBundle().infoDictionary().CFBundleShortVersionString + : ''; } diff --git a/src/utils/isRunningInSketch.ts b/src/utils/isRunningInSketch.ts new file mode 100644 index 00000000..6703c9da --- /dev/null +++ b/src/utils/isRunningInSketch.ts @@ -0,0 +1,5 @@ +import getSketchVersion from './getSketchVersion'; + +export default function isRunningInSketch() { + return getSketchVersion() !== ''; +} diff --git a/src/utils/sharedTextStyles.ts b/src/utils/sharedTextStyles.ts index 117bee6e..92c4b20b 100644 --- a/src/utils/sharedTextStyles.ts +++ b/src/utils/sharedTextStyles.ts @@ -1,5 +1,5 @@ -import { getSketchVersion } from './getSketchVersion'; import SketchStyles from '../jsonUtils/sketchImpl/sharedTextStyles'; import NodeStyles from '../jsonUtils/nodeImpl/sharedTextStyles'; +import isRunningInSketch from './isRunningInSketch'; -export default getSketchVersion() === 'NodeJS' ? new NodeStyles() : new SketchStyles(); +export default isRunningInSketch() ? new NodeStyles() : new SketchStyles();