Skip to content

Commit c4470a0

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

File tree

4 files changed

+30
-49
lines changed

4 files changed

+30
-49
lines changed
Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,12 @@
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");
5+
app.use(express.json({limit: "50mb"}));
106

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

36-
var listener = app.listen(0, "127.0.0.1", () => {
9+
let listener = app.listen(0, "127.0.0.1", () => {
3710
const instanceId = getInstanceId();
3811
debugLog("Server running on port " + listener.address().port + " for instance " + instanceId);
3912
fs.writeFile("server.port.tmp", "" + listener.address().port, function (err) {
@@ -44,22 +17,32 @@ var listener = app.listen(0, "127.0.0.1", () => {
4417
if (err) {
4518
return console.log(err);
4619
}
47-
}); // try to be as atomic as possible
20+
});
4821
}
4922
});
5023
});
51-
const shutdown = shutdownServer(listener, {
52-
forceExit: false, // let the event loop clear
53-
finally: () => debugLog("graceful shutdown finished."),
54-
});
5524

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

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+
let 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+
let 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+
let str2=str.charAt(3)+str[0];
1414

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

0 commit comments

Comments
 (0)