Skip to content

Commit 78a5764

Browse files
author
Vincent Potucek
committed
[ECMAScript6BestPractices] prerequisite fix common-serve.js
Signed-off-by: Vincent Potucek <[email protected]>
1 parent 29cc16e commit 78a5764

File tree

5 files changed

+47
-80
lines changed

5 files changed

+47
-80
lines changed
Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,53 @@
1-
// this file will be glued to the top of the specific xy-serve.js file
2-
const debug_serve = false; // set to true for debug log output in node process
3-
const shutdownServer = require("http-graceful-shutdown");
41
const express = require("express");
52
const app = express();
6-
7-
app.use(express.json({limit: "50mb"}));
8-
93
const fs = require("fs");
4+
const shutdownServer = require("http-graceful-shutdown");
105

11-
function debugLog() {
12-
if (debug_serve) {
13-
console.log.apply(this, arguments)
14-
}
15-
}
6+
// this file will be glued to the top of the specific xy-serve.js file
167

17-
function getInstanceId() {
18-
const args = process.argv.slice(2);
8+
const app = express();
199

20-
// Look for the --node-server-instance-id option
21-
let instanceId;
10+
app.use(express.json({ limit: "50mb" }));
2211

23-
args.forEach(arg => {
24-
if (arg.startsWith('--node-server-instance-id=')) {
25-
instanceId = arg.split('=')[1];
26-
}
27-
});
12+
const server = app.listen(0, "127.0.0.1", () => listener(server));
2813

29-
// throw if instanceId is not set
30-
if (!instanceId) {
31-
throw new Error("Missing --node-server-instance-id argument");
32-
}
33-
return instanceId;
34-
}
14+
app.post("/shutdown", (req, res) => {
15+
res.status(200).send("Shutting down");
16+
setTimeout(async () => {
17+
try {
18+
await shutdownServer(server, {
19+
forceExit: false, // let the event loop clear
20+
finally: () => debugLog("graceful shutdown finished."),
21+
})();
22+
} catch (err) {
23+
console.error("Error during shutdown:", err);
24+
}
25+
}, 200);
26+
});
3527

36-
var listener = app.listen(0, "127.0.0.1", () => {
28+
function listener(server) {
3729
const instanceId = getInstanceId();
38-
debugLog("Server running on port " + listener.address().port + " for instance " + instanceId);
39-
fs.writeFile("server.port.tmp", "" + listener.address().port, function (err) {
30+
debugLog("Server running on port " + server.address().port + " for instance " + instanceId);
31+
fs.writeFile("server.port.tmp", "" + server.address().port, function (err) {
4032
if (err) {
4133
return console.log(err);
4234
} else {
4335
fs.rename("server.port.tmp", `server-${instanceId}.port`, function (err) {
4436
if (err) {
4537
return console.log(err);
4638
}
47-
}); // try to be as atomic as possible
39+
});
4840
}
4941
});
50-
});
51-
const shutdown = shutdownServer(listener, {
52-
forceExit: false, // let the event loop clear
53-
finally: () => debugLog("graceful shutdown finished."),
54-
});
42+
}
5543

56-
app.post("/shutdown", (req, res) => {
57-
res.status(200).send("Shutting down");
58-
setTimeout(async () => {
59-
try {
60-
await shutdown();
61-
} catch (err) {
62-
console.error("Error during shutdown:", err);
63-
}
64-
}, 200);
65-
});
44+
function getInstanceId() {
45+
return process.argv.slice(2).find(arg => arg.startsWith("--node-server-instance-id="))?.split("=")[1]
46+
|| (() => { throw new Error("Missing --node-server-instance-id argument"); })();
47+
}
48+
49+
function debugLog() {
50+
if (false) { // set to true for debug log output in node process
51+
console.log.apply(this, arguments);
52+
}
53+
}

lib/src/main/resources/com/diffplug/spotless/npm/prettier-serve.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,13 @@ app.post("/prettier/config-options", (req, res) => {
2222

2323
app.post("/prettier/format", async (req, res) => {
2424
const format_data = req.body;
25-
26-
let formatted_file_content = "";
2725
try {
28-
formatted_file_content = await prettierFormat(format_data.file_content, format_data.config_options);
26+
res.set("Content-Type", "text/plain");
27+
res.send(await prettierFormat(format_data.file_content, format_data.config_options));
2928
} catch(err) {
3029
res.status(500).send("Error while formatting: " + err);
3130
return;
3231
}
33-
res.set("Content-Type", "text/plain");
34-
res.send(formatted_file_content);
3532
});
3633

3734
const prettierFormat = async function(file_content, config_options) {
@@ -62,7 +59,7 @@ const mergeConfigOptions = function(resolved_config_options, config_options) {
6259
const extend = function() {
6360
// Variables
6461
const extended = {};
65-
let i = 0;
62+
const i = 0;
6663
const length = arguments.length;
6764

6865
// Merge the object into the extended object

lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-serve.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
const tsfmt = require("typescript-formatter");
22

33
app.post("/tsfmt/format", (req, res) => {
4-
var format_data = req.body;
5-
tsfmt.processString("spotless-format-string.ts", format_data.file_content, format_data.config_options).then(resultMap => {
6-
/*
7-
export interface ResultMap {
8-
[fileName: string]: Result;
9-
}
10-
11-
export interface Result {
12-
fileName: string;
13-
settings: ts.FormatCodeSettings | null;
14-
message: string;
15-
error: boolean;
16-
src: string;
17-
dest: string;
18-
}
19-
*/
20-
// result contains 'message' (String), 'error' (boolean), 'dest' (String) => formatted
4+
tsfmt.processString("spotless-format-string.ts", req.body.file_content, req.body.config_options).then(resultMap => {
215
if (resultMap.error !== undefined && resultMap.error) {
226
res.status(400).send(resultMap.message);
237
return;

plugin-maven/src/test/java/com/diffplug/spotless/maven/npm/NpmTestsWithDynamicallyInstalledNpmInstallationTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,8 @@
2525

2626
public class NpmTestsWithDynamicallyInstalledNpmInstallationTest extends MavenIntegrationHarness {
2727

28+
public static final String TS = "typescript";
29+
2830
@Test
2931
void useDownloadedNpmInstallation() throws Exception {
3032
writePomWithPrettierSteps(
@@ -34,15 +36,11 @@ void useDownloadedNpmInstallation() throws Exception {
3436
" <npmExecutable>" + installedNpmPath() + "</npmExecutable>",
3537
"</prettier>");
3638

37-
String kind = "typescript";
38-
String suffix = "ts";
39-
String configPath = ".prettierrc.yml";
40-
setFile(configPath).toResource("npm/prettier/filetypes/" + kind + "/" + ".prettierrc.yml");
41-
String path = "src/main/" + kind + "/test." + suffix;
42-
setFile(path).toResource("npm/prettier/filetypes/" + kind + "/" + kind + ".dirty");
43-
39+
String path = "src/main/" + TS + "/test.ts";
40+
setFile(path).toResource("npm/prettier/filetypes/" + TS + "/" + TS + ".dirty");
41+
setFile(".prettierrc.yml").toResource("npm/prettier/filetypes/" + TS + "/" + ".prettierrc.yml");
4442
mavenRunner().withArguments(installNpmMavenGoal(), "spotless:apply").runNoError();
45-
assertFile(path).sameAsResource("npm/prettier/filetypes/" + kind + "/" + kind + ".clean");
43+
assertFile(path).sameAsResource("npm/prettier/filetypes/" + TS + "/" + TS + ".clean");
4644
}
4745

4846
}

testlib/src/main/resources/clang/example.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var numbers=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
1+
const numbers=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
22
];
33

44
const p = {
@@ -10,9 +10,9 @@ const p = {
1010
const str = "Hello, world!"
1111
;
1212

13-
var str2=str.charAt(3)+str[0];
13+
const str2=str.charAt(3)+str[0];
1414

15-
var multilinestr = "Hello \
15+
const multilinestr = "Hello \
1616
World"
1717
;
1818

0 commit comments

Comments
 (0)