Require prettier argument and library in the wrapper

This commit is contained in:
Kirill Bulatov 2023-09-12 16:34:56 +03:00
parent 1ff17bd15d
commit 86618a64c6
3 changed files with 61 additions and 29 deletions

View file

@ -1,29 +1,12 @@
{
"name": "prettier_server",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "prettier_server",
"version": "1.0.0",
"dependencies": {
"prettier": "^3.0.3"
}
},
"node_modules/prettier": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.3.tgz",
"integrity": "sha512-L/4pUDMxcNa8R/EthV08Zt42WBO4h1rarVtK0K+QJG0X187OLo7l699jWw0GKuwzkPQ//jMFA/8Xm6Fh3J/DAg==",
"bin": {
"prettier": "bin/prettier.cjs"
},
"engines": {
"node": ">=14"
},
"funding": {
"url": "https://github.com/prettier/prettier?sponsor=1"
}
"name": "prettier_server",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "prettier_server",
"version": "1.0.0"
}
}
}
}

View file

@ -8,7 +8,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Zed Industries",
"dependencies": {
"dev-dependencies": {
"prettier": "^3.0"
}
}

View file

@ -1,4 +1,36 @@
const { Buffer } = require('buffer');
const fs = require("fs");
const path = require("path");
let prettierContainerPath = process.argv[2];
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`);
process.exit(1);
}
fs.stat(prettierContainerPath, (err, stats) => {
if (err) {
console.error(`Path '${prettierContainerPath}' does not exist.`);
process.exit(1);
}
if (!stats.isDirectory()) {
console.log(`Path '${prettierContainerPath}' exists but is not a directory.`);
process.exit(1);
}
});
let prettierPath = path.join(prettierContainerPath, 'node_modules/prettier');
(async () => {
let prettier;
try {
prettier = await loadPrettier(prettierPath);
} catch (error) {
console.error(error);
process.exit(1);
}
console.log("Prettier loadded successfully.");
// TODO kb do the rest here
})()
let buffer = Buffer.alloc(0);
process.stdin.resume();
@ -28,14 +60,15 @@ function handleData() {
try {
const message = JSON.parse(bytes);
handleMessage(message);
} catch (_) {
sendResponse(makeError("Request JSON parse error"));
} catch (e) {
sendResponse(makeError(`Request JSON parse error: ${e}`));
return;
}
}
// format
// clear_cache
//
// shutdown
// error
@ -55,3 +88,19 @@ function sendResponse(response) {
process.stdout.write(length);
process.stdout.write(message);
}
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}`);
}
}
});
});
}