Skip to content

Commit

Permalink
Change the interface of getSketchVersion()
Browse files Browse the repository at this point in the history
  • Loading branch information
lordofthelake committed Jan 21, 2020
1 parent 0071286 commit 3aab5da
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/Platform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import getSketchVersion from './utils/getSketchVersion';

const Platform = {
OS: 'sketch',
Version: 1,
Version: getSketchVersion(),
select: (obj: { sketch: any }) => obj.sketch,
};

Expand Down
7 changes: 2 additions & 5 deletions src/jsonUtils/svgLayer.ts
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 3 additions & 4 deletions src/sharedStyles/TextStyles.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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 {};
}
Expand Down
9 changes: 4 additions & 5 deletions src/utils/getSketchVersion.ts
Original file line number Diff line number Diff line change
@@ -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
: '';
}
5 changes: 5 additions & 0 deletions src/utils/isRunningInSketch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import getSketchVersion from './getSketchVersion';

export default function isRunningInSketch() {
return getSketchVersion() !== '';
}
4 changes: 2 additions & 2 deletions src/utils/sharedTextStyles.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 3aab5da

Please sign in to comment.