From b4afd8f5d7e765bbd001d3e40187c216773a4cd1 Mon Sep 17 00:00:00 2001 From: chriscohoat Date: Sun, 26 Apr 2020 12:12:48 -0500 Subject: [PATCH 1/3] Updated plugin file writing to use NSString.writeToFile_atomically --- README.md | 7 +++++- .../Contents/Sketch/attribute-export.js | 22 ++++++++++++++----- .../Contents/Sketch/manifest.json | 2 +- package.json | 2 +- src/attribute-export.js | 16 +++++++++++++- src/manifest.json | 2 +- 6 files changed, 41 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 7ddba98..cc94c9f 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,12 @@ Additionally, if you wish to run the plugin every time it is built: npm run start ``` +You may need to update your Sketch defaults to always reload plugins: + +```bash +defaults write com.bohemiancoding.sketch3 AlwaysReloadScript -bool YES +``` + ### Custom Configuration #### Babel @@ -169,4 +175,3 @@ skpm log ``` The `-f` option causes `skpm log` to not stop when the end of logs is reached, but rather to wait for additional data to be appended to the input - diff --git a/interactive-export.sketchplugin/Contents/Sketch/attribute-export.js b/interactive-export.sketchplugin/Contents/Sketch/attribute-export.js index 7269d66..a8e0adc 100644 --- a/interactive-export.sketchplugin/Contents/Sketch/attribute-export.js +++ b/interactive-export.sketchplugin/Contents/Sketch/attribute-export.js @@ -70,10 +70,7 @@ var exports = /************************************************************************/ /******/ ([ /* 0 */ -/***/ (function(module, exports, __webpack_require__) { - -"use strict"; - +/***/ (function(module, exports) { Object.defineProperty(exports, "__esModule", { value: true @@ -81,6 +78,8 @@ Object.defineProperty(exports, "__esModule", { exports['default'] = function (context) {}; +var writeNonConflictingFile = false; // TODO: Use plugin settings here + function saveJSON(obj, filePath) { obj = JSON.parse(JSON.stringify(obj)); var data = NSJSONSerialization.dataWithJSONObject_options_error(obj, NSJSONWritingPrettyPrinted, nil), @@ -97,6 +96,14 @@ function getClassFromName(name) { return name.split('.').slice(1).join(' '); } +function buildNonConflictingPath(path) { + var splitParts = path.split('\\').pop().split('/'); + var pathToFile = splitParts.slice(0, splitParts.length - 1).join('/'); + var filenameParts = splitParts.pop().split('.'); + var newFileName = filenameParts.slice(0, -2).concat([filenameParts[filenameParts.length - 2] + "_attribute_export", filenameParts[filenameParts.length - 1]]).join('.'); + return pathToFile + '/' + newFileName; +} + function onExportSlices(context) { var exp = context.actionContext.exports; var document = context.actionContext.document; @@ -139,7 +146,12 @@ function onExportSlices(context) { }); svgString = svgString.replace(/svg width\=\"\d+px" height\=\"\d+px"/, 'svg '); - NSString.stringWithString(svgString).writeToFile_atomically_encoding_error(path, true, NSUTF8StringEncoding, nil); + + var svgAsNsString = NSString.stringWithFormat("%@", svgString); + if (writeNonConflictingFile) { + svgAsNsString.writeToFile_atomically(buildNonConflictingPath(path), true); + } + svgAsNsString.writeToFile_atomically(path, true); } } } diff --git a/interactive-export.sketchplugin/Contents/Sketch/manifest.json b/interactive-export.sketchplugin/Contents/Sketch/manifest.json index 25609a6..df2d50c 100644 --- a/interactive-export.sketchplugin/Contents/Sketch/manifest.json +++ b/interactive-export.sketchplugin/Contents/Sketch/manifest.json @@ -1,5 +1,5 @@ { - "version": "1.0.1", + "version": "1.0.2", "icon": "icon.png", "commands": [ { diff --git a/package.json b/package.json index fe525fa..40b2dfd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "interactive-export", - "version": "0.1.1", + "version": "0.1.2", "engines": { "sketch": ">=3.0" }, diff --git a/src/attribute-export.js b/src/attribute-export.js index 9519025..0f42840 100644 --- a/src/attribute-export.js +++ b/src/attribute-export.js @@ -1,3 +1,4 @@ +var writeNonConflictingFile = false; // TODO: Use plugin settings here function saveJSON(obj, filePath) { obj = JSON.parse(JSON.stringify(obj)); @@ -15,6 +16,14 @@ function getClassFromName(name) { return name.split('.').slice(1).join(' '); } +function buildNonConflictingPath(path) { + const splitParts = path.split('\\').pop().split('/') + const pathToFile = splitParts.slice(0, splitParts.length - 1).join('/') + const filenameParts = splitParts.pop().split('.'); + const newFileName = filenameParts.slice(0, -2).concat([filenameParts[filenameParts.length - 2] + "_attribute_export", filenameParts[filenameParts.length - 1]]).join('.') + return pathToFile + '/' + newFileName; +} + function onExportSlices(context) { const exp = context.actionContext.exports; const document = context.actionContext.document; @@ -55,7 +64,12 @@ function onExportSlices(context) { }) svgString = svgString.replace(/svg width\=\"\d+px" height\=\"\d+px"/, 'svg '); - NSString.stringWithString(svgString).writeToFile_atomically_encoding_error(path, true, NSUTF8StringEncoding, nil); + + const svgAsNsString = NSString.stringWithFormat("%@", svgString); + if (writeNonConflictingFile) { + svgAsNsString.writeToFile_atomically(buildNonConflictingPath(path), true); + } + svgAsNsString.writeToFile_atomically(path, true); } } diff --git a/src/manifest.json b/src/manifest.json index f01c62e..d7a7100 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,7 +1,7 @@ { - "version": "1.0.1", + "version": "1.0.2", "icon": "icon.png", "commands" : [ { From c2780f4ec99f865ce594be301bc0e55ae8038384 Mon Sep 17 00:00:00 2001 From: chriscohoat Date: Sun, 26 Apr 2020 12:17:08 -0500 Subject: [PATCH 2/3] Updated writing to keep the original if non-conflicting mode is enabled --- src/attribute-export.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/attribute-export.js b/src/attribute-export.js index 0f42840..3317dbc 100644 --- a/src/attribute-export.js +++ b/src/attribute-export.js @@ -68,8 +68,9 @@ function onExportSlices(context) { const svgAsNsString = NSString.stringWithFormat("%@", svgString); if (writeNonConflictingFile) { svgAsNsString.writeToFile_atomically(buildNonConflictingPath(path), true); + } else { + svgAsNsString.writeToFile_atomically(path, true); } - svgAsNsString.writeToFile_atomically(path, true); } } From 85d85e0edadfda2cb69cae58fc1e45fd0967577b Mon Sep 17 00:00:00 2001 From: chriscohoat Date: Sun, 26 Apr 2020 12:20:02 -0500 Subject: [PATCH 3/3] Built latest to update the output Sketch plugin --- .../Contents/Sketch/attribute-export.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/interactive-export.sketchplugin/Contents/Sketch/attribute-export.js b/interactive-export.sketchplugin/Contents/Sketch/attribute-export.js index a8e0adc..32b544d 100644 --- a/interactive-export.sketchplugin/Contents/Sketch/attribute-export.js +++ b/interactive-export.sketchplugin/Contents/Sketch/attribute-export.js @@ -150,8 +150,9 @@ function onExportSlices(context) { var svgAsNsString = NSString.stringWithFormat("%@", svgString); if (writeNonConflictingFile) { svgAsNsString.writeToFile_atomically(buildNonConflictingPath(path), true); + } else { + svgAsNsString.writeToFile_atomically(path, true); } - svgAsNsString.writeToFile_atomically(path, true); } } }