Skip to content

Updated plugin file writing to use NSString.writeToFile_atomically #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,16 @@ var exports =
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";

/***/ (function(module, exports) {

Object.defineProperty(exports, "__esModule", {
value: true
});

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),
Expand All @@ -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;
Expand Down Expand Up @@ -139,7 +146,13 @@ 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);
} else {
svgAsNsString.writeToFile_atomically(path, true);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.1",
"version": "1.0.2",
"icon": "icon.png",
"commands": [
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "interactive-export",
"version": "0.1.1",
"version": "0.1.2",
"engines": {
"sketch": ">=3.0"
},
Expand Down
17 changes: 16 additions & 1 deletion src/attribute-export.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var writeNonConflictingFile = false; // TODO: Use plugin settings here

function saveJSON(obj, filePath) {
obj = JSON.parse(JSON.stringify(obj));
Expand All @@ -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;
Expand Down Expand Up @@ -55,7 +64,13 @@ 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);
} else {
svgAsNsString.writeToFile_atomically(path, true);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


{
"version": "1.0.1",
"version": "1.0.2",
"icon": "icon.png",
"commands" : [
{
Expand Down