Async-ify prettier wrapper

This commit is contained in:
Kirill Bulatov 2023-09-15 17:14:22 +03:00
parent 86618a64c6
commit bb2cc2d157

View File

@ -1,8 +1,9 @@
const { Buffer } = require('buffer'); const { Buffer } = require('buffer');
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const { once } = require('events');
let prettierContainerPath = process.argv[2]; const prettierContainerPath = process.argv[2];
if (prettierContainerPath == null || prettierContainerPath.length == 0) { if (prettierContainerPath == null || prettierContainerPath.length == 0) {
console.error(`Prettier path argument was not specified or empty.\nUsage: ${process.argv[0]} ${process.argv[1]} prettier/path`); console.error(`Prettier path argument was not specified or empty.\nUsage: ${process.argv[0]} ${process.argv[1]} prettier/path`);
process.exit(1); process.exit(1);
@ -18,48 +19,83 @@ fs.stat(prettierContainerPath, (err, stats) => {
process.exit(1); process.exit(1);
} }
}); });
let prettierPath = path.join(prettierContainerPath, 'node_modules/prettier'); const prettierPath = path.join(prettierContainerPath, 'node_modules/prettier');
(async () => { (async () => {
let prettier; let prettier;
try { try {
prettier = await loadPrettier(prettierPath); prettier = await loadPrettier(prettierPath);
} catch (error) { } catch (error) {
console.error(error); console.error("Failed to load prettier: ", error);
process.exit(1); process.exit(1);
} }
console.log("Prettier loadded successfully."); console.log("Prettier loadded successfully.");
// TODO kb do the rest here process.stdin.resume();
handleBuffer(prettier);
})() })()
let buffer = Buffer.alloc(0); async function handleBuffer(prettier) {
process.stdin.resume(); for await (let messageText of readStdin()) {
process.stdin.on('data', (data) => { handleData(messageText, prettier).catch(e => {
buffer = Buffer.concat([buffer, data]); console.error("Failed to handle formatter request", e);
handleData(); });
});
process.stdin.on('end', () => {
handleData();
});
function handleData() {
if (buffer.length < 4) {
return;
} }
}
const length = buffer.readUInt32LE(0); async function* readStdin() {
console.log(length); const bufferLengthOffset = 4;
console.log(buffer.toString()); let buffer = Buffer.alloc(0);
if (buffer.length < 4 + length) { let streamEnded = false;
return; process.stdin.on('end', () => {
} streamEnded = true;
});
const bytes = buffer.subarray(4, 4 + length); process.stdin.on('data', (data) => {
buffer = buffer.subarray(4 + length); buffer = Buffer.concat([buffer, data]);
});
try { try {
const message = JSON.parse(bytes); main_loop: while (true) {
handleMessage(message); while (buffer.length < bufferLengthOffset) {
if (streamEnded) {
sendResponse(makeError(`Unexpected end of stream: less than ${bufferLengthOffset} characters passed`));
buffer = Buffer.alloc(0);
streamEnded = false;
await once(process.stdin, 'readable');
continue main_loop;
}
await once(process.stdin, 'readable');
}
const length = buffer.readUInt32LE(0);
while (buffer.length < (bufferLengthOffset + length)) {
if (streamEnded) {
sendResponse(makeError(
`Unexpected end of stream: buffer length ${buffer.length} does not match expected length ${bufferLengthOffset} + ${length}`));
buffer = Buffer.alloc(0);
streamEnded = false;
await once(process.stdin, 'readable');
continue main_loop;
}
await once(process.stdin, 'readable');
}
const message = buffer.subarray(4, 4 + length);
buffer = buffer.subarray(4 + length);
yield message.toString('utf8');
}
} catch (e) {
console.error(`Error reading stdin: ${e}`);
} finally {
process.stdin.off('data');
}
}
async function handleData(messageText, prettier) {
try {
const message = JSON.parse(messageText);
await handleMessage(prettier, message);
} catch (e) { } catch (e) {
sendResponse(makeError(`Request JSON parse error: ${e}`)); sendResponse(makeError(`Request JSON parse error: ${e}`));
return; return;
@ -72,8 +108,8 @@ function handleData() {
// shutdown // shutdown
// error // error
function handleMessage(message) { async function handleMessage(prettier, message) {
console.log(message); console.log(`message: ${message}`);
sendResponse({ method: "hi", result: null }); sendResponse({ method: "hi", result: null });
} }
@ -82,8 +118,8 @@ function makeError(message) {
} }
function sendResponse(response) { function sendResponse(response) {
let message = Buffer.from(JSON.stringify(response)); const message = Buffer.from(JSON.stringify(response));
let length = Buffer.alloc(4); const length = Buffer.alloc(4);
length.writeUInt32LE(message.length); length.writeUInt32LE(message.length);
process.stdout.write(length); process.stdout.write(length);
process.stdout.write(message); process.stdout.write(message);