Skip to content

Commit ca950be

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

File tree

5 files changed

+33
-54
lines changed

5 files changed

+33
-54
lines changed
Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,13 @@
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
31
const shutdownServer = require("http-graceful-shutdown");
42
const express = require("express");
53
const app = express();
6-
7-
app.use(express.json({limit: "50mb"}));
8-
94
const fs = require("fs");
105

11-
function debugLog() {
12-
if (debug_serve) {
13-
console.log.apply(this, arguments)
14-
}
15-
}
16-
17-
function getInstanceId() {
18-
const args = process.argv.slice(2);
19-
20-
// Look for the --node-server-instance-id option
21-
let instanceId;
22-
23-
args.forEach(arg => {
24-
if (arg.startsWith('--node-server-instance-id=')) {
25-
instanceId = arg.split('=')[1];
26-
}
27-
});
6+
app.use(express.json({ limit: "50mb" }));
287

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-
}
8+
// this file will be glued to the top of the specific xy-serve.js file
359

36-
var listener = app.listen(0, "127.0.0.1", () => {
10+
const listener = app.listen(0, "127.0.0.1", () => {
3711
const instanceId = getInstanceId();
3812
debugLog("Server running on port " + listener.address().port + " for instance " + instanceId);
3913
fs.writeFile("server.port.tmp", "" + listener.address().port, function (err) {
@@ -44,22 +18,32 @@ var listener = app.listen(0, "127.0.0.1", () => {
4418
if (err) {
4519
return console.log(err);
4620
}
47-
}); // try to be as atomic as possible
21+
});
4822
}
4923
});
5024
});
51-
const shutdown = shutdownServer(listener, {
52-
forceExit: false, // let the event loop clear
53-
finally: () => debugLog("graceful shutdown finished."),
54-
});
5525

5626
app.post("/shutdown", (req, res) => {
5727
res.status(200).send("Shutting down");
5828
setTimeout(async () => {
5929
try {
60-
await shutdown();
30+
await shutdownServer(listener, {
31+
forceExit: false, // let the event loop clear
32+
finally: () => debugLog("graceful shutdown finished."),
33+
})();
6134
} catch (err) {
6235
console.error("Error during shutdown:", err);
6336
}
6437
}, 200);
6538
});
39+
40+
function getInstanceId() {
41+
return process.argv.slice(2).find(arg => arg.startsWith("--node-server-instance-id="))?.split("=")[1] ||
42+
(() => { throw new Error("Missing --node-server-instance-id argument"); })();
43+
}
44+
45+
function debugLog() {
46+
if (false) { // set to true for debug log output in node process
47+
console.log.apply(this, arguments);
48+
}
49+
}

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const tsfmt = require("typescript-formatter");
22

33
app.post("/tsfmt/format", (req, res) => {
4-
var format_data = req.body;
4+
const format_data = req.body;
55
tsfmt.processString("spotless-format-string.ts", format_data.file_content, format_data.config_options).then(resultMap => {
66
/*
77
export interface ResultMap {

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)