From faf521aa87cc7255cd56d2308a3338104d3e780d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=BCchele?= Date: Mon, 15 Oct 2018 03:07:27 -0700 Subject: [PATCH] use pastry for createPaste Summary: Moving from `arc` to `pastry` to create Pastes. arc paste is deprecated and throws errors sometimes. Pastry seems to be the tool of choice now and has a nicer API anyways. Reviewed By: passy Differential Revision: D10302075 fbshipit-source-id: a846adf3768a2adf5c7ff73dc89b18c1e9169ac0 --- src/utils/createPaste.js | 78 +++++++++++++++------------------------- 1 file changed, 29 insertions(+), 49 deletions(-) diff --git a/src/utils/createPaste.js b/src/utils/createPaste.js index 2028cc146..f53ba9a07 100644 --- a/src/utils/createPaste.js +++ b/src/utils/createPaste.js @@ -7,64 +7,44 @@ import child_process from 'child_process'; import {clipboard, shell} from 'electron'; +import JSONStream from 'JSONStream'; type PasteResponse = + | string | { - id: number, - objectName: string, - phid: string, - authorPHID: string, - filePHID: string, - title: string, - dateCreated: number, - language: string, - uri: string, - parentPHID: ?number, - content: string, - } - | { - type: 'error', - message: string, + createdPaste: { + id: number, + url: string, + }, }; export default function createPaste(input: string): Promise { return new Promise((resolve, reject) => { - const arc = '/opt/facebook/bin/arc'; - const child = child_process.spawn(arc, [ - '--conduit-uri=https://phabricator.intern.facebook.com/api/', - 'paste', - '--json', - ]); + const child = child_process.spawn('pastry', ['--json']); + + let lastMessage: ?PasteResponse; + + child.stdout + .pipe(JSONStream.parse([true])) + .on('data', (data: PasteResponse) => { + if (typeof data === 'string' && lastMessage === 'error') { + new window.Notification('Failed to create paste', { + body: data, + }); + reject(data); + } else if (typeof data === 'object' && data.createdPaste) { + const {url, id} = data.createdPaste; + clipboard.writeText(url); + const notification = new window.Notification(`Paste P${id} created`, { + body: 'URL copied to clipboard', + }); + notification.onclick = () => shell.openExternal(url); + resolve(url); + } + lastMessage = data; + }); child.stdin.write(input); child.stdin.end(); - let response = ''; - child.stdout.on('data', (data: Buffer) => { - response += data.toString(); - }); - child.stdout.on('end', (data: Buffer) => { - const result: PasteResponse = JSON.parse(response || 'null'); - - if (!result) { - new window.Notification('Failed to create paste', { - body: `Does ${arc} exist and is executable?`, - }); - } else if (result.type === 'error') { - new window.Notification('Failed to create paste', { - body: result.message != null ? result.message : '', - }); - reject(result); - } else { - clipboard.writeText(result.uri); - const notification = new window.Notification( - `Paste ${result.objectName} created`, - { - body: 'URL copied to clipboard', - }, - ); - notification.onclick = () => shell.openExternal(result.uri); - resolve(result.uri); - } - }); }); }