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
This commit is contained in:
committed by
Facebook Github Bot
parent
8d7774c409
commit
faf521aa87
@@ -7,64 +7,44 @@
|
|||||||
|
|
||||||
import child_process from 'child_process';
|
import child_process from 'child_process';
|
||||||
import {clipboard, shell} from 'electron';
|
import {clipboard, shell} from 'electron';
|
||||||
|
import JSONStream from 'JSONStream';
|
||||||
|
|
||||||
type PasteResponse =
|
type PasteResponse =
|
||||||
|
| string
|
||||||
| {
|
| {
|
||||||
id: number,
|
createdPaste: {
|
||||||
objectName: string,
|
id: number,
|
||||||
phid: string,
|
url: string,
|
||||||
authorPHID: string,
|
},
|
||||||
filePHID: string,
|
|
||||||
title: string,
|
|
||||||
dateCreated: number,
|
|
||||||
language: string,
|
|
||||||
uri: string,
|
|
||||||
parentPHID: ?number,
|
|
||||||
content: string,
|
|
||||||
}
|
|
||||||
| {
|
|
||||||
type: 'error',
|
|
||||||
message: string,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function createPaste(input: string): Promise<?string> {
|
export default function createPaste(input: string): Promise<?string> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const arc = '/opt/facebook/bin/arc';
|
const child = child_process.spawn('pastry', ['--json']);
|
||||||
const child = child_process.spawn(arc, [
|
|
||||||
'--conduit-uri=https://phabricator.intern.facebook.com/api/',
|
let lastMessage: ?PasteResponse;
|
||||||
'paste',
|
|
||||||
'--json',
|
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.write(input);
|
||||||
child.stdin.end();
|
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user