Skip to content

Commit

Permalink
feat(sketch): error handling for missing IDL Sketch library
Browse files Browse the repository at this point in the history
  • Loading branch information
emyarod committed Apr 15, 2021
1 parent 64e53da commit 989e8a0
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions packages/sketch/src/commands/icons/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/* global MSSVGImporter, NSString, NSUTF8StringEncoding */

import sketch from 'sketch';
import { Artboard, Library, Rectangle, Shape } from 'sketch/dom';
import { groupByKey } from '../../tools/grouping';
import { syncSymbol } from '../../tools/symbols';
Expand Down Expand Up @@ -56,6 +57,35 @@ function removeDeprecatedSymbolArtboards({ icons, sizes, symbolsPage }) {
});
}

/**
* Fetch the IDL Sketch Library if it is installed
* @param {object} params - fetchIDLSketchLibrary parameters
* @param {Array<object>} params.icons - array of all icon object metadata
* @param {Array<number>} params.sizes - array of icon sizes
* @param {Page} params.symbolsPage - the symbols page as identified by Sketch
*/
function fetchIDLSketchLibrary({ IDL_SKETCH_LIBRARY_METADATA }) {
const {
id: idlSketchLibraryId,
name: idlSketchLibraryName,
} = IDL_SKETCH_LIBRARY_METADATA;

const installedLibraries = Library.getLibraries().filter(
({ id, name }) => id === idlSketchLibraryId && name === idlSketchLibraryName
);

if (!installedLibraries.length) {
sketch.UI.message(
'Please install the IDL V2 Sketch library before trying again'
);
throw new Error(
'Please install the IDL V2 Sketch library before trying again'
);
}

return installedLibraries[0];
}

/**
* Sync Carbon icon symbols into current Sketch Document
* @param {object} params - syncIconSymbols parameters
Expand All @@ -71,14 +101,9 @@ export function syncIconSymbols({
symbolsPage,
sizes = [32, 24, 20, 16],
}) {
const {
id: idlSketchLibraryId,
name: idlSketchLibraryName,
} = IDL_SKETCH_LIBRARY_METADATA;

const [idlSketchLibrary] = Library.getLibraries().filter(
({ id, name }) => id === idlSketchLibraryId && name === idlSketchLibraryName
);
const idlSketchLibrary = fetchIDLSketchLibrary({
IDL_SKETCH_LIBRARY_METADATA,
});

idlSketchLibrary
.getImportableLayerStyleReferencesForDocument(document)
Expand Down

0 comments on commit 989e8a0

Please sign in to comment.