Skip to content

Commit b5775ec

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

File tree

2 files changed

+56
-66
lines changed

2 files changed

+56
-66
lines changed
Lines changed: 49 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,57 @@
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");
4-
const express = require("express");
51
const app = express();
2+
const express = require("express");
3+
const fs = require("fs");
4+
const shutdownServer = require("http-graceful-shutdown");
65

76
app.use(express.json({limit: "50mb"}));
87

9-
const fs = require("fs");
10-
11-
function debugLog() {
12-
if (debug_serve) {
13-
console.log.apply(this, arguments)
14-
}
15-
}
8+
// This file will be glued to the top of the specific xy-serve.js file
9+
10+
app.post('/shutdown', (req, res) => {
11+
res.status(200).send('Shutting down');
12+
setTimeout(async () => {
13+
try {
14+
await shutdownServer(
15+
app.listen(0, '127.0.0.1', () => {
16+
const instanceId = getInstanceId();
17+
debugLog('Server running on port ' + listener.address().port + ' for instance ' + instanceId);
18+
fs.writeFile('server.port.tmp', '' + listener.address().port, function (err) {
19+
if (err) {
20+
return console.log(err);
21+
} else {
22+
fs.rename('server.port.tmp', `server-${instanceId}.port`, function (err) {
23+
if (err) {
24+
return console.log(err);
25+
}
26+
});
27+
}
28+
});
29+
}),
30+
{
31+
forceExit: false, // Let the event loop clear
32+
finally: () => debugLog('graceful shutdown finished.'),
33+
}
34+
)();
35+
} catch (err) {
36+
console.error('Error during shutdown:', err);
37+
}
38+
}, 200);
39+
});
1640

1741
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;
42+
return (
43+
process.argv
44+
.slice(2)
45+
.find(arg => arg.startsWith('--node-server-instance-id='))
46+
?.split('=')[1]
47+
|| (() => {
48+
throw new Error('Missing --node-server-instance-id argument');
49+
})()
50+
);
3451
}
3552

36-
var listener = app.listen(0, "127.0.0.1", () => {
37-
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) {
40-
if (err) {
41-
return console.log(err);
42-
} else {
43-
fs.rename("server.port.tmp", `server-${instanceId}.port`, function (err) {
44-
if (err) {
45-
return console.log(err);
46-
}
47-
}); // try to be as atomic as possible
48-
}
49-
});
50-
});
51-
const shutdown = shutdownServer(listener, {
52-
forceExit: false, // let the event loop clear
53-
finally: () => debugLog("graceful shutdown finished."),
54-
});
55-
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-
});
53+
function debugLog() {
54+
if (false) { // Set to true for debug log output in node process
55+
console.log.apply(this, arguments);
56+
}
57+
}

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
}

0 commit comments

Comments
 (0)