2023-08-22 20:12:59 +00:00
|
|
|
const { Buffer } = require('buffer');
|
2023-09-12 13:34:56 +00:00
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
2023-09-15 14:14:22 +00:00
|
|
|
const { once } = require('events');
|
2023-09-12 13:34:56 +00:00
|
|
|
|
2023-09-15 14:14:22 +00:00
|
|
|
const prettierContainerPath = process.argv[2];
|
2023-09-12 13:34:56 +00:00
|
|
|
if (prettierContainerPath == null || prettierContainerPath.length == 0) {
|
2023-09-21 20:49:22 +00:00
|
|
|
process.stderr.write(`Prettier path argument was not specified or empty.\nUsage: ${process.argv[0]} ${process.argv[1]} prettier/path\n`);
|
2023-09-12 13:34:56 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
fs.stat(prettierContainerPath, (err, stats) => {
|
|
|
|
if (err) {
|
2023-09-21 20:49:22 +00:00
|
|
|
process.stderr.write(`Path '${prettierContainerPath}' does not exist\n`);
|
2023-09-12 13:34:56 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stats.isDirectory()) {
|
2023-09-21 20:49:22 +00:00
|
|
|
process.stderr.write(`Path '${prettierContainerPath}' exists but is not a directory\n`);
|
2023-09-12 13:34:56 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
});
|
2023-09-15 14:14:22 +00:00
|
|
|
const prettierPath = path.join(prettierContainerPath, 'node_modules/prettier');
|
|
|
|
|
2023-09-22 12:52:34 +00:00
|
|
|
class Prettier {
|
|
|
|
constructor(path, prettier, config) {
|
|
|
|
this.path = path;
|
|
|
|
this.prettier = prettier;
|
|
|
|
this.config = config;
|
|
|
|
}
|
|
|
|
}
|
2023-09-12 13:34:56 +00:00
|
|
|
|
|
|
|
(async () => {
|
|
|
|
let prettier;
|
2023-09-22 12:52:34 +00:00
|
|
|
let config;
|
2023-09-12 13:34:56 +00:00
|
|
|
try {
|
|
|
|
prettier = await loadPrettier(prettierPath);
|
2023-09-22 12:52:34 +00:00
|
|
|
config = await prettier.resolveConfig(prettierPath) || {};
|
2023-09-18 11:56:40 +00:00
|
|
|
} catch (e) {
|
2023-09-21 20:49:22 +00:00
|
|
|
process.stderr.write(`Failed to load prettier: ${e}\n`);
|
2023-09-12 13:34:56 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2023-09-22 12:52:34 +00:00
|
|
|
process.stderr.write(`Prettier at path '${prettierPath}' loaded successfully, config: ${config}\n`);
|
2023-09-15 14:14:22 +00:00
|
|
|
process.stdin.resume();
|
2023-09-22 12:52:34 +00:00
|
|
|
handleBuffer(new Prettier(prettierPath, prettier, config));
|
2023-09-12 13:34:56 +00:00
|
|
|
})()
|
2023-08-22 20:12:59 +00:00
|
|
|
|
2023-09-15 14:14:22 +00:00
|
|
|
async function handleBuffer(prettier) {
|
|
|
|
for await (let messageText of readStdin()) {
|
2023-09-20 11:51:02 +00:00
|
|
|
let message;
|
|
|
|
try {
|
|
|
|
message = JSON.parse(messageText);
|
|
|
|
} catch (e) {
|
|
|
|
sendResponse(makeError(`Failed to parse message '${messageText}': ${e}`));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
handleMessage(message, prettier).catch(e => {
|
|
|
|
sendResponse({ id: message.id, ...makeError(`error during message handling: ${e}`) });
|
2023-09-15 14:14:22 +00:00
|
|
|
});
|
2023-08-22 20:12:59 +00:00
|
|
|
}
|
2023-09-15 14:14:22 +00:00
|
|
|
}
|
2023-08-22 20:12:59 +00:00
|
|
|
|
2023-09-18 14:16:16 +00:00
|
|
|
const headerSeparator = "\r\n";
|
2023-09-19 07:25:33 +00:00
|
|
|
const contentLengthHeaderName = 'Content-Length';
|
2023-09-18 14:16:16 +00:00
|
|
|
|
2023-09-15 14:14:22 +00:00
|
|
|
async function* readStdin() {
|
|
|
|
let buffer = Buffer.alloc(0);
|
|
|
|
let streamEnded = false;
|
|
|
|
process.stdin.on('end', () => {
|
|
|
|
streamEnded = true;
|
|
|
|
});
|
|
|
|
process.stdin.on('data', (data) => {
|
|
|
|
buffer = Buffer.concat([buffer, data]);
|
|
|
|
});
|
2023-08-22 20:12:59 +00:00
|
|
|
|
2023-09-15 20:10:42 +00:00
|
|
|
async function handleStreamEnded(errorMessage) {
|
|
|
|
sendResponse(makeError(errorMessage));
|
|
|
|
buffer = Buffer.alloc(0);
|
|
|
|
messageLength = null;
|
|
|
|
await once(process.stdin, 'readable');
|
|
|
|
streamEnded = false;
|
|
|
|
}
|
|
|
|
|
2023-09-15 14:14:22 +00:00
|
|
|
try {
|
2023-09-15 20:10:42 +00:00
|
|
|
let headersLength = null;
|
|
|
|
let messageLength = null;
|
2023-09-15 14:14:22 +00:00
|
|
|
main_loop: while (true) {
|
2023-09-15 20:10:42 +00:00
|
|
|
if (messageLength === null) {
|
2023-09-18 14:16:16 +00:00
|
|
|
while (buffer.indexOf(`${headerSeparator}${headerSeparator}`) === -1) {
|
2023-09-15 20:10:42 +00:00
|
|
|
if (streamEnded) {
|
|
|
|
await handleStreamEnded('Unexpected end of stream: headers not found');
|
|
|
|
continue main_loop;
|
|
|
|
} else if (buffer.length > contentLengthHeaderName.length * 10) {
|
|
|
|
await handleStreamEnded(`Unexpected stream of bytes: no headers end found after ${buffer.length} bytes of input`);
|
|
|
|
continue main_loop;
|
|
|
|
}
|
2023-09-15 14:14:22 +00:00
|
|
|
await once(process.stdin, 'readable');
|
2023-09-15 20:10:42 +00:00
|
|
|
}
|
2023-09-18 14:16:16 +00:00
|
|
|
const headers = buffer.subarray(0, buffer.indexOf(`${headerSeparator}${headerSeparator}`)).toString('ascii');
|
|
|
|
const contentLengthHeader = headers.split(headerSeparator).map(header => header.split(':'))
|
2023-09-15 20:10:42 +00:00
|
|
|
.filter(header => header[2] === undefined)
|
|
|
|
.filter(header => (header[1] || '').length > 0)
|
|
|
|
.find(header => header[0].trim() === contentLengthHeaderName);
|
|
|
|
if (contentLengthHeader === undefined) {
|
2023-09-18 14:16:16 +00:00
|
|
|
await handleStreamEnded(`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`);
|
2023-09-15 14:14:22 +00:00
|
|
|
continue main_loop;
|
|
|
|
}
|
2023-09-18 14:16:16 +00:00
|
|
|
headersLength = headers.length + headerSeparator.length * 2;
|
2023-09-15 20:10:42 +00:00
|
|
|
messageLength = parseInt(contentLengthHeader[1], 10);
|
2023-09-15 14:14:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 20:10:42 +00:00
|
|
|
while (buffer.length < (headersLength + messageLength)) {
|
2023-09-15 14:14:22 +00:00
|
|
|
if (streamEnded) {
|
2023-09-15 20:10:42 +00:00
|
|
|
await handleStreamEnded(
|
|
|
|
`Unexpected end of stream: buffer length ${buffer.length} does not match expected header length ${headersLength} + body length ${messageLength}`);
|
2023-09-15 14:14:22 +00:00
|
|
|
continue main_loop;
|
|
|
|
}
|
|
|
|
await once(process.stdin, 'readable');
|
|
|
|
}
|
|
|
|
|
2023-09-15 20:10:42 +00:00
|
|
|
const messageEnd = headersLength + messageLength;
|
|
|
|
const message = buffer.subarray(headersLength, messageEnd);
|
|
|
|
buffer = buffer.subarray(messageEnd);
|
|
|
|
messageLength = null;
|
2023-09-15 14:14:22 +00:00
|
|
|
yield message.toString('utf8');
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2023-09-18 11:56:40 +00:00
|
|
|
sendResponse(makeError(`Error reading stdin: ${e}`));
|
2023-09-15 14:14:22 +00:00
|
|
|
} finally {
|
2023-09-15 20:10:42 +00:00
|
|
|
process.stdin.off('data', () => { });
|
2023-09-15 14:14:22 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-22 20:12:59 +00:00
|
|
|
|
2023-09-22 12:52:34 +00:00
|
|
|
// TODO kb, more methods?
|
2023-08-22 20:12:59 +00:00
|
|
|
// shutdown
|
|
|
|
// error
|
2023-09-20 11:51:02 +00:00
|
|
|
async function handleMessage(message, prettier) {
|
2023-09-18 11:56:40 +00:00
|
|
|
const { method, id, params } = message;
|
|
|
|
if (method === undefined) {
|
2023-09-20 11:51:02 +00:00
|
|
|
throw new Error(`Message method is undefined: ${JSON.stringify(message)}`);
|
2023-09-18 11:56:40 +00:00
|
|
|
}
|
|
|
|
if (id === undefined) {
|
2023-09-20 11:51:02 +00:00
|
|
|
throw new Error(`Message id is undefined: ${JSON.stringify(message)}`);
|
2023-09-18 11:56:40 +00:00
|
|
|
}
|
2023-08-22 20:12:59 +00:00
|
|
|
|
2023-09-18 11:56:40 +00:00
|
|
|
if (method === 'prettier/format') {
|
|
|
|
if (params === undefined || params.text === undefined) {
|
2023-09-20 11:51:02 +00:00
|
|
|
throw new Error(`Message params.text is undefined: ${JSON.stringify(message)}`);
|
2023-09-18 11:56:40 +00:00
|
|
|
}
|
2023-09-18 15:08:55 +00:00
|
|
|
if (params.options === undefined) {
|
2023-09-20 11:51:02 +00:00
|
|
|
throw new Error(`Message params.options is undefined: ${JSON.stringify(message)}`);
|
2023-09-18 15:08:55 +00:00
|
|
|
}
|
2023-09-22 15:40:12 +00:00
|
|
|
|
|
|
|
const options = {
|
|
|
|
...(params.options.prettierOptions || prettier.config),
|
|
|
|
parser: params.options.parser,
|
|
|
|
path: params.options.path
|
|
|
|
};
|
2023-09-22 15:48:30 +00:00
|
|
|
// TODO kb always resolve prettier config for each file.
|
|
|
|
// need to understand if default prettier can be affected by other configs in the project
|
|
|
|
// (restart default prettiers on config changes too then)
|
2023-09-22 15:40:12 +00:00
|
|
|
const formattedText = await prettier.prettier.format(params.text, options);
|
2023-09-18 11:56:40 +00:00
|
|
|
sendResponse({ id, result: { text: formattedText } });
|
|
|
|
} else if (method === 'prettier/clear_cache') {
|
2023-09-22 12:52:34 +00:00
|
|
|
prettier.prettier.clearConfigCache();
|
|
|
|
prettier.config = await prettier.prettier.resolveConfig(prettier.path) || {};
|
2023-09-18 11:56:40 +00:00
|
|
|
sendResponse({ id, result: null });
|
|
|
|
} else if (method === 'initialize') {
|
|
|
|
sendResponse({
|
|
|
|
id,
|
|
|
|
result: {
|
|
|
|
"capabilities": {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unknown method: ${method}`);
|
|
|
|
}
|
2023-08-22 20:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeError(message) {
|
2023-09-18 11:56:40 +00:00
|
|
|
return {
|
|
|
|
error: {
|
|
|
|
"code": -32600, // invalid request code
|
|
|
|
message,
|
|
|
|
}
|
|
|
|
};
|
2023-08-22 20:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sendResponse(response) {
|
2023-09-18 11:56:40 +00:00
|
|
|
let responsePayloadString = JSON.stringify({
|
|
|
|
jsonrpc: "2.0",
|
|
|
|
...response
|
|
|
|
});
|
2023-09-19 07:25:33 +00:00
|
|
|
let headers = `${contentLengthHeaderName}: ${Buffer.byteLength(responsePayloadString)}${headerSeparator}${headerSeparator}`;
|
2023-09-18 11:56:40 +00:00
|
|
|
let dataToSend = headers + responsePayloadString;
|
|
|
|
process.stdout.write(dataToSend);
|
2023-08-22 20:12:59 +00:00
|
|
|
}
|
2023-09-12 13:34:56 +00:00
|
|
|
|
|
|
|
function loadPrettier(prettierPath) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.access(prettierPath, fs.constants.F_OK, (err) => {
|
|
|
|
if (err) {
|
|
|
|
reject(`Path '${prettierPath}' does not exist.Error: ${err}`);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
resolve(require(prettierPath));
|
|
|
|
} catch (err) {
|
|
|
|
reject(`Error requiring prettier module from path '${prettierPath}'.Error: ${err}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|