-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.ts
192 lines (171 loc) · 4.73 KB
/
converter.ts
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
189
190
191
192
import plist from 'plist';
import fs from 'fs';
import fse from 'fs-extra';
import chalk from 'chalk';
import _ from 'lodash';
import ActionNodeFinder from './actionNodeFinder';
import { removeRunNode } from './util';
import { modifierMap, supportedInputFormat } from './constant';
const getInputObjects = (nodeInfo: any) => {
let inputObjects: any[] = [];
for (const format of supportedInputFormat) {
inputObjects = [
...inputObjects,
..._.filter(nodeInfo, item => {
return item.type === format;
})
];
}
return inputObjects;
};
const convertHotkey = (hotmod: any, hotstring: string) => {
let hotkey = hotmod;
if (hotmod) {
const modifiers = (modifierMap as any)[hotmod];
if (hotstring) {
if (hotstring === 'double tap') {
hotkey = `Double ${modifiers}`;
} else {
hotkey = `${modifiers} + ${hotstring}`;
}
}
}
// if hotkey is not set
if (hotkey === 0) hotkey = undefined;
return hotkey;
};
const convertArgumentType = (argumenttype: any) => {
if (argumenttype === 0) {
return 'required';
}
if (argumenttype === 1) {
return 'optional';
}
if (argumenttype === 2) {
return 'no';
}
console.error('argumenttype in not proper!');
return 'optional';
}
const convert = async (plistPath: string, outputPath?: string) => {
if (fs.existsSync(plistPath)) {
const targetPlist: any = plist.parse(fs.readFileSync(plistPath, 'utf8'));
const {
bundleid,
category,
createdby: creator,
description,
name,
readme,
version,
webaddress: webAddress,
variables
} = targetPlist;
let bundleId;
if (!name || !creator) {
console.error(
chalk.redBright(
'There is missing "name" or "creator". Please make sure to fill this before creating workflow'
)
);
}
if (name && creator) {
bundleId = `@${creator}.${name}`;
} else if (name) {
bundleId = `@unknown.${name}`;
} else if (bundleid) {
bundleId = bundleid;
} else {
throw new Error(
'Required attributes are not set on info.plist, parsed plist: ' +
targetPlist
);
}
const result = {
$schema: 'https://raw.githubusercontent.com/jopemachine/arvis-extension-validator/master/workflow-schema.json',
defaultIcon: 'icon.png',
category,
creator,
description,
name,
readme,
version,
webAddress,
enabled: true,
variables,
commands: [] as any[]
};
const graph = targetPlist.connections;
const nodeInfo = targetPlist.objects;
const actionNodeFinder = new ActionNodeFinder(graph, nodeInfo);
const inputObjects = getInputObjects(nodeInfo);
for (const inputObject of inputObjects) {
const uid = inputObject.uid;
const {
// common
text,
title,
subtext: subtitle,
// hotkey
hotmod,
hotstring,
// scriptfilter
script,
withspace,
argumenttype,
runningsubtext: runningSubtext,
} = inputObject.config;
const hotkey = convertHotkey(hotmod, hotstring);
const argType = convertArgumentType(argumenttype);
const inputType = inputObject.type;
let type = inputType.split('.')[inputType.split('.').length - 1];
const keyword = inputObject.config.keyword;
let appendNode = graph[uid];
switch (inputType) {
case 'alfred.workflow.trigger.hotkey': {
appendNode = hotkey;
break;
}
case 'alfred.workflow.input.keyword': {
appendNode = keyword;
break;
}
case 'alfred.workflow.input.scriptfilter': {
type = 'scriptFilter';
appendNode = script && keyword && keyword !== '';
break;
}
}
if (appendNode) {
const actionNodes = actionNodeFinder.getActionNodes(inputObject);
result.commands.push({
type,
command: keyword,
title: title || text,
subtitle,
scriptFilter: removeRunNode(script),
runningSubtext,
withspace,
hotkey,
argType,
actions: actionNodes
});
} else {
console.log(
chalk.magentaBright(
`Node '${uid}' (Type: '${type}') doesn't have expected word.`
)
);
}
}
const out = outputPath ? outputPath : `arvis-workflow.json`;
await fse.writeJSON(out, result, {
encoding: 'utf-8',
spaces: 2
});
console.log(chalk.white(`${chalk.greenBright('✔')} '${bundleId}' info.plist converting is done..`));
} else {
throw new Error(`plist file not found! given plist path: ${plistPath}`);
}
};
export default convert;