-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathcommon.js
More file actions
188 lines (161 loc) · 5.51 KB
/
common.js
File metadata and controls
188 lines (161 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import * as WebViewUtils from 'utils/webview'
export function parseNameAndQuery(inputName, getDefaultValues = () => ({
})) {
const defaultValues = getDefaultValues()
const result = { name: '', query: defaultValues, hash: {} }
const name = String(inputName)
if (name[0] !== '[') return result
// name/query/hash
let state = 'none'
let stack = ''
function clearLastStack() {
if (state === 'name') {
result.name = stack
} else if (state === 'query') {
stack.split('&').forEach((subStack) => {
const [queryName, queryValue] = subStack.split('=')
const defaultValue = defaultValues[queryName]
result.query[queryName] = typeof defaultValue === 'boolean' ?
Boolean(queryValue) :
typeof defaultValue === 'number' ?
Number(queryValue) :
queryValue
})
} else {
// hash
stack.split('&').forEach((subStack) => {
const [queryName, queryValue] = subStack.split('=')
const defaultValue = defaultValues[queryName]
result.hash[queryName] = typeof defaultValue === 'boolean' ?
Boolean(queryValue) :
typeof defaultValue === 'number' ?
Number(queryValue) :
queryValue
})
}
stack = ''
}
for (let i = 0; i < name.length; i++) {
const c = name[i]
if (c === '[' && state === 'none') {
state = 'name'
} else if (c === '?' && (state === 'name' || state === 'hash')) {
clearLastStack()
state = 'query'
} else if (c === '#' && (state === 'name' || state === 'query')) {
clearLastStack()
state = 'hash'
} else if (c === ']' && (state === 'name' || state === 'query' || state === 'hash')) {
clearLastStack()
state = 'end'
return result
} else {
stack += c
}
}
throw new Error(`tag not closed: ${inputName}`)
}
export function sendCommandToPanel(path, command, argv) {
WebViewUtils.sendPanelAction(path, command, argv)
}
export function sendCommandToWindow(path, command, argv) {
WebViewUtils.sendWindowAction(path, command, argv)
}
export function showWindow(path) {
// CAUTION use path as identifier
WebViewUtils.openWindow(path, path)
}
export function showPanel(path) {
WebViewUtils.showPanel(path, path)
}
export function hidePanel(path) {
WebViewUtils.hidePanel(path, path)
}
export function recursiveParse(entry, parsers, context = {}) {
const { name, query = {}, hash } = parseNameAndQuery(entry.name)
let resolvedName = name
if (resolvedName === '') {
if (entry.isArtboard) {
resolvedName = 'App'
} else if (entry.isGroup) {
resolvedName = 'Group'
} else if (entry.isText) {
resolvedName = 'Text'
} else {
// resolvedName = 'Unknown'
resolvedName = 'Img'
}
}
if (parsers[resolvedName] === undefined) {
log(`unknown parser ${resolvedName}, entry: ${entry.name}, parsers: ${Object.keys(parsers).join(',')}`)
throw new Error(`unknown parser ${resolvedName}`)
}
const [result, next] = parsers[resolvedName](entry, context)
if (next && next.length !== 0) {
result.children = next.map(child => recursiveParse(child, parsers, context))
}
return Object.assign(result, {
state: Object.assign(result.state || {}, query),
props: Object.assign(result.props || {}, hash),
})
}
export function isWindowOpened(path) {
return Boolean(WebViewUtils.findWebView(path))
}
export function createFolder(path) {
const manager = NSFileManager.defaultManager()
manager.createDirectoryAtPath_withIntermediateDirectories_attributes_error(path, true, null, null)
// [file_manager createDirectoryAtPath:[folders objectAtIndex:i] withIntermediateDirectories:true attributes:nil error:nil];
}
export function getPluginFolderPath (context) {
// Get absolute folder path of plugin
let split = context.scriptPath.split('/');
split.splice(-3, 3);
return split.join('/');
}
export function getCurrentFilePath(context) {
return context.document.fileURL().path().replace(/\.sketch$/, '')
}
export function isFileExist(source) {
const manager = NSFileManager.defaultManager()
return manager.fileExistsAtPath(source)
}
export function copyFile(source, target) {
const manager = NSFileManager.defaultManager()
if( !manager.fileExistsAtPath(source)) throw new Error(`file not exist ${source}`)
// [file_manager copyItemAtPath:org toPath:tar error:nil];
manager.copyItemAtPath_toPath_error(source, target, null)
}
export function writeToFile(path, content) {
const resultStr = NSString.stringWithFormat('%@', content)
resultStr.writeToFile_atomically(path, true)
}
export function removeFile(path) {
const manager = NSFileManager.defaultManager()
// [file_manager removeItemAtPath:folder error:nil]
manager.removeItemAtPath_error(path, null)
}
export function exportLayer(layer, targetFolder, name, options = {}) {
const finalOptions = {
...options,
'use-id-for-name': true,
scales: '3',
formats: 'png',
output: targetFolder,
}
const tmpFullPath = `${targetFolder}/${layer.id}@${finalOptions.scales}x.${finalOptions.formats}`
layer.export(finalOptions)
log(`generating image: ${tmpFullPath}`)
const manager = NSFileManager.defaultManager()
const targetPath = `${targetFolder}/${name}.${finalOptions.formats}`
log(`renaming image to ${targetPath}` )
manager.moveItemAtURL_toURL_error(
NSURL.fileURLWithPath(tmpFullPath),
NSURL.fileURLWithPath(targetPath),
null
)
}
export function parseRawName(inputName) {
const name = String(inputName)
return name.replace(/^\[.+\]/, '')
}