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); - } - }); }); }