Skip to content

Commit 987c233

Browse files
author
Vincent Potucek
committed
[rewrite] apply ECMAScript6BestPractices
Signed-off-by: Vincent Potucek <[email protected]>
1 parent 29cc16e commit 987c233

File tree

7 files changed

+72
-73
lines changed

7 files changed

+72
-73
lines changed

gradle/rewrite.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ rewrite {
55
'**.dirty.java',
66
'**FormatterProperties.java',
77
'**_gradle_node_plugin_example_**',
8+
'**common-serve.js',
89
'**gradle/changelog.gradle',
910
'**gradle/java-publish.gradle',
1011
'**idea/full.clean.java',
@@ -15,14 +16,16 @@ rewrite {
1516
'**plugin-maven/build.gradle',
1617
'**settings.gradle',
1718
'**special-tests.gradle',
18-
'**testlib/src/main/resources**'
19+
'**testlib/src/main/resources**',
20+
'**tsfmt-serve.js',
1921
)
2022
exportDatatables = true
2123
failOnDryRunResults = true
2224
}
2325
dependencies {
24-
rewrite('org.openrewrite.recipe:rewrite-migrate-java:3.22.0')
26+
rewrite('org.openrewrite.recipe:rewrite-codemods:0.22.0')
2527
rewrite('org.openrewrite.recipe:rewrite-java-security:3.22.0')
28+
rewrite('org.openrewrite.recipe:rewrite-migrate-java:3.22.0')
2629
rewrite('org.openrewrite.recipe:rewrite-rewrite:0.16.0')
2730
rewrite('org.openrewrite.recipe:rewrite-static-analysis:2.22.0')
2831
rewrite('org.openrewrite.recipe:rewrite-third-party:0.31.2')
Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,58 @@
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+
// Try to be as atomic as possible
23+
fs.rename('server.port.tmp', `server-${instanceId}.port`, function (err) {
24+
if (err) {
25+
return console.log(err);
26+
}
27+
});
28+
}
29+
});
30+
}),
31+
{
32+
forceExit: false, // Let the event loop clear
33+
finally: () => debugLog('graceful shutdown finished.'),
34+
}
35+
)();
36+
} catch (err) {
37+
console.error('Error during shutdown:', err);
38+
}
39+
}, 200);
40+
});
1641

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

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-
});
54+
function debugLog() {
55+
if (false) { // Set to true for debug log output in node process
56+
console.log.apply(this, arguments);
57+
}
58+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {ESLint} = require("eslint");
1+
import { ESLint } from 'eslint';
22

33
app.post("/eslint/format", async (req, res) => {
44
try {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const prettier = require("prettier");
1+
import prettier from 'prettier';
22

33
app.post("/prettier/config-options", (req, res) => {
44
const config_data = req.body;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const tsfmt = require("typescript-formatter");
1+
import tsfmt from '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
}

rewrite.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
---
22
type: specs.openrewrite.org/v1beta/recipe
33
name: com.diffplug.spotless.openrewrite.SanityCheck
4-
displayName: Apply all Java & Gradle best practices
4+
displayName: Apply all best practices
55
description: Comprehensive code quality recipe combining modernization, security, and best practices.
66
tags:
77
- java
88
- gradle
99
- static-analysis
1010
- cleanup
1111
recipeList:
12+
- org.openrewrite.codemods.ESLint
13+
- org.openrewrite.codemods.UI5
14+
- org.openrewrite.codemods.cleanup.javascript.EmptyBraceSpaces
15+
- org.openrewrite.codemods.cleanup.javascript.PreferModule
16+
- org.openrewrite.codemods.ecmascript.5to6.ECMAScript6BestPractices
1217
- org.openrewrite.gradle.EnableGradleBuildCache
1318
- org.openrewrite.gradle.EnableGradleParallelExecution
1419
- org.openrewrite.gradle.GradleBestPractices

0 commit comments

Comments
 (0)