Skip to content

Commit

Permalink
eslint: reduce log-noise, cleanup error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
simschla committed Jan 5, 2023
1 parent 4598319 commit 3540242
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
6 changes: 5 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/npm/NpmProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ class NpmProcess {
}

void install() {
npmAwait("install", "--no-audit", "--no-package-lock", "--prefer-offline");
npmAwait("install",
"--no-audit",
"--no-package-lock",
"--no-fund",
"--prefer-offline");
}

Process start() {
Expand Down
13 changes: 10 additions & 3 deletions lib/src/main/resources/com/diffplug/spotless/npm/common-serve.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// this file will be glued to the top of the specific xy-server.js file
// this file will be glued to the top of the specific xy-serve.js file
const debug_serve = false; // set to true for debug log output in node process
const GracefulShutdownManager = require("@moebius/http-graceful-shutdown").GracefulShutdownManager;
const express = require("express");
const app = express();
Expand All @@ -7,8 +8,14 @@ app.use(express.json({ limit: "50mb" }));

const fs = require("fs");

function debugLog() {
if (debug_serve) {
console.log.apply(this, arguments)
}
}

var listener = app.listen(0, "127.0.0.1", () => {
console.log("Server running on port " + listener.address().port);
debugLog("Server running on port " + listener.address().port);
fs.writeFile("server.port.tmp", "" + listener.address().port, function(err) {
if (err) {
return console.log(err);
Expand All @@ -26,7 +33,7 @@ const shutdownManager = new GracefulShutdownManager(listener);
app.post("/shutdown", (req, res) => {
res.status(200).send("Shutting down");
setTimeout(function() {
shutdownManager.terminate(() => console.log("graceful shutdown finished."));
shutdownManager.terminate(() => debugLog("graceful shutdown finished."));
}, 200);
});

18 changes: 9 additions & 9 deletions lib/src/main/resources/com/diffplug/spotless/npm/eslint-serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ app.post("/eslint/format", async (req, res) => {
const ESLintOverrideConfigFile = format_data.eslint_override_config_file;

if (!ESLintOverrideConfig && !ESLintOverrideConfigFile) {
res.status(501).send("Error while formatting: No config provided");
res.status(400).send("Error while formatting: No config provided");
return;
}

const filePath = format_data.file_path;

if (!filePath) {
res.status(501).send("Error while formatting: No file path provided");
res.status(400).send("Error while formatting: No file path provided");
return;
}

Expand All @@ -41,34 +41,34 @@ app.post("/eslint/format", async (req, res) => {
eval("ESLintOptions.overrideConfig = " + ESLintOverrideConfig);
}

console.log("using options: " + JSON.stringify(ESLintOptions));
console.log("format input: ", format_data.file_content);
debugLog("using options: " + JSON.stringify(ESLintOptions));
debugLog("format input: ", format_data.file_content);

const eslint = new ESLint(ESLintOptions);


const lintTextOptions = {
filePath: filePath,
}
console.log("lintTextOptions", lintTextOptions);
debugLog("lintTextOptions", lintTextOptions);

// LintResult[] // https://eslint.org/docs/latest/developer-guide/nodejs-api#-lintresult-type
const results = await eslint.lintText(format_data.file_content, lintTextOptions);
if (results.length !== 1) {
res.status(501).send("Error while formatting: Unexpected number of results: " + JSON.stringify(results));
res.status(500).send("Error while formatting: Unexpected number of results: " + JSON.stringify(results));
return;
}
const result = results[0];
console.log("result: " + JSON.stringify(result));
debugLog("result: " + JSON.stringify(result));
if (result.fatalErrorCount && result.fatalErrorCount > 0) {
res.status(501).send("Fatal error while formatting: " + JSON.stringify(result.messages));
res.status(500).send("Fatal error while formatting: " + JSON.stringify(result.messages));
return;
}
const formatted = result.output || result.source || format_data.file_content;
res.set("Content-Type", "text/plain");
res.send(formatted);
} catch (err) {
console.log("error", err);
res.status(501).send("Error while formatting: " + err);
res.status(500).send("Error while formatting: " + err);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ app.post("/prettier/format", (req, res) => {
try {
formatted_file_content = prettier.format(format_data.file_content, format_data.config_options);
} catch(err) {
res.status(501).send("Error while formatting: " + err);
res.status(500).send("Error while formatting: " + err);
return;
}
res.set("Content-Type", "text/plain");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ app.post("/tsfmt/format", (req, res) => {
res.set("Content-Type", "text/plain");
res.send(resultMap.dest);
}).catch(reason => {
res.status(501).send(reason);
res.status(500).send(reason);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void verifyPrettierErrorMessageIsRelayed() throws Exception {
new PrettierConfig(null, ImmutableMap.of("parser", "postcss")));
try (StepHarnessWithFile stepHarness = StepHarnessWithFile.forStep(this, formatterStep)) {
stepHarness.testResourceExceptionMsg("npm/prettier/filetypes/scss/scss.dirty").isEqualTo(
"Unexpected response status code at /prettier/format [HTTP 501] -- (Error while formatting: Error: Couldn't resolve parser \"postcss\")");
"Unexpected response status code at /prettier/format [HTTP 500] -- (Error while formatting: Error: Couldn't resolve parser \"postcss\")");
}
}
}
Expand Down

0 comments on commit 3540242

Please sign in to comment.