-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·423 lines (382 loc) · 9.88 KB
/
cli.js
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import util from 'node:util';
import meow from 'meow';
import ora from 'ora';
import consola from 'consola';
import chalk from 'chalk';
import SSHConfig from 'ssh-config';
import slugify from '@sindresorhus/slugify';
import deepEqual from 'deep-equal';
import { runAppleScript } from 'run-applescript';
import { execString } from 'applescript';
const cli = meow(
`
Usage
$ t2sc
`,
{
importMeta: import.meta,
}
);
const transmitFavoritesScript = `
tell application "Transmit"
set favoriteItems to {}
set listSize to count of favorites
repeat with counter from 1 to listSize
set fav to {}
set end of fav to name of item counter of favorites
set end of fav to address of item counter of favorites
set end of fav to user name of item counter of favorites
set end of fav to port of item counter of favorites
set end of fav to protocol of item counter of favorites as string
set end of fav to remote path of item counter of favorites
set end of fav to identifier of item counter of favorites
set end of favoriteItems to fav
end repeat
favoriteItems
end tell`;
const transmitFavoritesFoldersScript = `
tell application "System Events"
set menuItems to every menu item of menu 1 of menu bar item 8 of menu bar 1 of process "Transmit"
set favoritesFolders to {}
set separators to {}
repeat with menuItem in menuItems
set favFolder to {}
set menuTilte to title of menuItem
if menuTilte = "" then
set end of separators to menuTilte
end if
-- Only get favorites, after the second separator
if count of separators > 2 then exit repeat
-- Get sub menus
set subMenuItemsCount to count of menu items of menu of menuItem
if subMenuItemsCount >= 1 then
set subMenuItems to every menu item of menu 1 of menuItem
set end of favFolder to menuTilte
set menuChildren to {}
repeat with subMenuItem in subMenuItems
set subMenuTilte to title of subMenuItem
-- Exit repeat before "Open in tabs"
if subMenuTilte is equal to "" then exit repeat
set end of menuChildren to subMenuTilte
end repeat
set end of favFolder to menuChildren
set end of favoritesFolders to favFolder
end if
end repeat
favoritesFolders
end tell`;
const execStringPromise = util.promisify(execString);
// Get SSH config path
const sshConfigFile = path.join(process.env['HOME'], '.ssh', 'config');
/**
* Get favorite ID
* @param {Object} favorite
*/
const getFavoriteId = (favorite) => {
if (favorite.param !== 'Host') {
return false;
}
if (!favorite.hasOwnProperty('config') && favorite.config.length === 0) {
return false;
}
const transmitIdPattern =
/^#[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$/;
let id = false;
favorite.config.forEach((line) => {
if (line.type !== SSHConfig.COMMENT) {
return;
}
if (!transmitIdPattern.test(line.content)) {
return;
}
id = line.content;
});
return id;
};
/**
* Get Host
* @param {Object} favorite
*/
const getHost = (favorite) => {
if (favorite.param !== 'Host') {
return false;
}
return favorite.value;
};
/**
* Get favorite param
* @param {Object} favorite
* @param {String} param
*/
const getParam = (favorite, param) => {
if (!favorite.hasOwnProperty('config') && favorite.config.length === 0) {
return false;
}
let value = false;
favorite.config.forEach((line) => {
if (line.type !== SSHConfig.DIRECTIVE) {
return;
}
if (line.param !== param) {
return;
}
value = line.value;
});
return value;
};
/**
* Get favorite param
* @param {Object} favorite
* @param {String} param
*/
const sanitizeFavorite = (favorite) => {
const f = JSON.parse(JSON.stringify(favorite));
delete f.before;
delete f.after;
delete f.separator;
f.config.forEach((line) => {
delete line.before;
delete line.after;
delete line.separator;
});
return f;
};
/**
* Get the SSH config items
*
* @returns {Array}
*/
const getSSHConfig = async (configFile) => {
// Check if SSH config is readable/writable
try {
await fs.promises.access(configFile, fs.constants.R_OK | fs.constants.W_OK);
} catch (error) {
console.error(chalk.red(`Could not read/write ${configFile}`));
process.exit(1);
}
try {
const SSHConfigContents = await fs.promises.readFile(configFile, 'utf8');
return SSHConfig.parse(SSHConfigContents);
} catch (error) {
console.error(chalk.red(`${error.message}`));
process.exit(1);
}
};
/**
* Get host log
* @param {Object} favorite
*/
const getHostLog = (favorite) => {
return `${getParam(favorite, 'User')}@${getParam(
favorite,
'HostName'
)} [${getHost(favorite)}]`;
};
/**
* Get favorite folder name
* @param {String} favorite
* @param {Array} folder
*/
const getFavoriteFolderName = (favoriteName, folders) => {
for(let i = 0; i < folders.length; i++) {
/**
* Folder name
* @type {String}
*/
const folderName = folders[i][0];
/**
* Folder favorites
* @type {Array}
*/
const folderFavorites = folders[i][1];
if(folderFavorites.includes(favoriteName)) {
return folderName;
}
}
return null;
}
/**
* Get Transmit favorites
*
* @returns {Array}
*/
const getTransmitFavorites = async () => {
const favoritesRaw = await execStringPromise(transmitFavoritesScript);
if (!favoritesRaw) {
return [];
}
const folders = await execStringPromise(transmitFavoritesFoldersScript);
const favorites = favoritesRaw
.map((f) => {
const folderName = Array.isArray(folders) ? getFavoriteFolderName(f[0], folders) : null;
const name = folderName ? `${folderName}/${f[0]}` : f[0];
return {
name: `${name
.split('/')
.map((p) => slugify(p))
.join('/')}`,
address: f[1],
username: f[2],
port: f[3],
protocol: f[4],
remote_path: f[5] === 'missing value' ? null : f[5],
id: f[6],
};
})
.filter((f) => f.protocol === 'SFTP');
return favorites;
};
(async () => {
const spinner = ora('Fetching Transmit favorites...').start();
const appStatus = await execStringPromise(
'application "Transmit" is running'
);
let favorites = [];
try {
favorites = await getTransmitFavorites();
} catch (error) {
spinner.stop();
console.error(chalk.red(`\n${error}\n`));
process.exit(1);
}
spinner.stop();
// SFTP Favorites ?
if (!favorites.length) {
consola.warn(`No Transmit SFTP favorites were found`);
process.exit(1);
}
let config;
try {
await fs.promises.access(sshConfigFile, fs.constants.R_OK);
config = await getSSHConfig(sshConfigFile);
} catch (error) {
consola.info(
`No SSH config file was found, it will be created at: ${sshConfigFile}`
);
config = new SSHConfig();
}
// Convert favorites from Transmit into SSH config
const favoritesFromTransmit = favorites.map((f, i) => {
let favorite = `Host ${f.name}
#${f.id}
HostName ${f.address}
User ${f.username}
`;
if (f.port) {
favorite += ` Port ${f.port}\n`;
}
favorite += '\n';
const favoriteParsed = SSHConfig.parse(favorite);
return favoriteParsed[0];
});
const counts = {
deleted: 0,
updated: 0,
added: 0,
};
// Delete
config.forEach((fc, i) => {
const fcId = getFavoriteId(fc);
if (!fcId) {
return false;
}
const removeFavorite = favoritesFromTransmit.every((ft) => {
const ftId = getFavoriteId(ft);
if (fcId !== ftId) {
return true;
}
return false;
});
if (removeFavorite) {
counts['deleted'] += 1;
consola.warn(`Removed ${getHostLog(fc)}`);
config.splice(i, 1);
}
});
// Update
favoritesFromTransmit.forEach((ft, i) => {
const ftId = getFavoriteId(ft);
const updateFavorite = config.some((fc) => {
const fcId = getFavoriteId(fc);
if (!fcId) {
return false;
}
if (
fcId === ftId &&
!deepEqual(sanitizeFavorite(ft), sanitizeFavorite(fc))
) {
return true;
}
});
if (updateFavorite) {
consola.info(`Updated ${getHostLog(ft)}`);
counts['updated'] += 1;
config.forEach((fc, k) => {
const fcId = getFavoriteId(fc);
if (ftId === fcId) {
config[k] = ft;
}
});
}
});
// Add
favoritesFromTransmit.forEach((ft, i) => {
const ftId = getFavoriteId(ft);
const addFavorite = config.every((fc) => {
const fcId = getFavoriteId(fc);
if (!fcId) {
return true;
}
return fcId !== ftId;
});
if (addFavorite) {
counts['added'] += 1;
consola.success(`Added ${getHostLog(ft)}`);
config.push(ft);
}
});
// Nothing happened
const nothing = Object.keys(counts).every((key) => counts[key] === 0);
if (nothing) {
consola.info(`No Transmit favorites to add, update or delete.`);
} else {
// Sort alphabetically
config.sort((a, b) => {
let comparison = 0;
if (a.value > b.value) {
comparison = 1;
} else if (a.value < b.value) {
comparison = -1;
}
return comparison;
});
// Write file
await fs.promises.writeFile(sshConfigFile, SSHConfig.stringify(config));
// Fix permissions
await fs.promises.chmod(sshConfigFile, 0o644);
// Summary
Object.keys(counts).forEach((key) => {
if (counts[key] === 0) {
return;
}
consola.success(
`${counts[key]} Transmit favorite${counts[key] > 1 ? 's' : ''} ${
counts[key] > 1 ? 'have' : 'has'
} been successfully ${key} in your SSH config file.`
);
});
}
// Quit Transmit if it was not running at launch
if (appStatus === 'false' || !appStatus) {
try {
await runAppleScript(`quit app "Transmit"`);
} catch (error) {
consola.error(error);
process.exit(1);
}
}
process.exit(0);
})();