index.js

  1const { Buffer } = require('buffer');
  2const fs = require("fs");
  3const path = require("path");
  4
  5let prettierContainerPath = process.argv[2];
  6if (prettierContainerPath == null || prettierContainerPath.length == 0) {
  7    console.error(`Prettier path argument was not specified or empty.\nUsage: ${process.argv[0]} ${process.argv[1]} prettier/path`);
  8    process.exit(1);
  9}
 10fs.stat(prettierContainerPath, (err, stats) => {
 11    if (err) {
 12        console.error(`Path '${prettierContainerPath}' does not exist.`);
 13        process.exit(1);
 14    }
 15
 16    if (!stats.isDirectory()) {
 17        console.log(`Path '${prettierContainerPath}' exists but is not a directory.`);
 18        process.exit(1);
 19    }
 20});
 21let prettierPath = path.join(prettierContainerPath, 'node_modules/prettier');
 22
 23(async () => {
 24    let prettier;
 25    try {
 26        prettier = await loadPrettier(prettierPath);
 27    } catch (error) {
 28        console.error(error);
 29        process.exit(1);
 30    }
 31    console.log("Prettier loadded successfully.");
 32    // TODO kb do the rest here
 33})()
 34
 35let buffer = Buffer.alloc(0);
 36process.stdin.resume();
 37process.stdin.on('data', (data) => {
 38    buffer = Buffer.concat([buffer, data]);
 39    handleData();
 40});
 41process.stdin.on('end', () => {
 42    handleData();
 43});
 44
 45function handleData() {
 46    if (buffer.length < 4) {
 47        return;
 48    }
 49
 50    const length = buffer.readUInt32LE(0);
 51    console.log(length);
 52    console.log(buffer.toString());
 53    if (buffer.length < 4 + length) {
 54        return;
 55    }
 56
 57    const bytes = buffer.subarray(4, 4 + length);
 58    buffer = buffer.subarray(4 + length);
 59
 60    try {
 61        const message = JSON.parse(bytes);
 62        handleMessage(message);
 63    } catch (e) {
 64        sendResponse(makeError(`Request JSON parse error: ${e}`));
 65        return;
 66    }
 67}
 68
 69// format
 70// clear_cache
 71//
 72// shutdown
 73// error
 74
 75function handleMessage(message) {
 76    console.log(message);
 77    sendResponse({ method: "hi", result: null });
 78}
 79
 80function makeError(message) {
 81    return { method: "error", message };
 82}
 83
 84function sendResponse(response) {
 85    let message = Buffer.from(JSON.stringify(response));
 86    let length = Buffer.alloc(4);
 87    length.writeUInt32LE(message.length);
 88    process.stdout.write(length);
 89    process.stdout.write(message);
 90}
 91
 92function loadPrettier(prettierPath) {
 93    return new Promise((resolve, reject) => {
 94        fs.access(prettierPath, fs.constants.F_OK, (err) => {
 95            if (err) {
 96                reject(`Path '${prettierPath}' does not exist.Error: ${err}`);
 97            } else {
 98                try {
 99                    resolve(require(prettierPath));
100                } catch (err) {
101                    reject(`Error requiring prettier module from path '${prettierPath}'.Error: ${err}`);
102                }
103            }
104        });
105    });
106}