diff --git a/gradle/rewrite.gradle b/gradle/rewrite.gradle index 62f5170c46..1d389dfe99 100644 --- a/gradle/rewrite.gradle +++ b/gradle/rewrite.gradle @@ -5,6 +5,7 @@ rewrite { '**.dirty.java', '**FormatterProperties.java', '**_gradle_node_plugin_example_**', + '**common-serve.js', '**gradle/changelog.gradle', '**gradle/java-publish.gradle', '**idea/full.clean.java', @@ -15,14 +16,16 @@ rewrite { '**plugin-maven/build.gradle', '**settings.gradle', '**special-tests.gradle', - '**testlib/src/main/resources**' + '**testlib/src/main/resources**', + '**tsfmt-serve.js', ) exportDatatables = true failOnDryRunResults = true } dependencies { - rewrite('org.openrewrite.recipe:rewrite-migrate-java:3.22.0') + rewrite('org.openrewrite.recipe:rewrite-codemods:0.22.0') rewrite('org.openrewrite.recipe:rewrite-java-security:3.22.0') + rewrite('org.openrewrite.recipe:rewrite-migrate-java:3.22.0') rewrite('org.openrewrite.recipe:rewrite-rewrite:0.16.0') rewrite('org.openrewrite.recipe:rewrite-static-analysis:2.22.0') rewrite('org.openrewrite.recipe:rewrite-third-party:0.31.2') diff --git a/lib/src/main/resources/com/diffplug/spotless/npm/common-serve.js b/lib/src/main/resources/com/diffplug/spotless/npm/common-serve.js index edce61465a..af9f05780c 100644 --- a/lib/src/main/resources/com/diffplug/spotless/npm/common-serve.js +++ b/lib/src/main/resources/com/diffplug/spotless/npm/common-serve.js @@ -1,65 +1,58 @@ -// this file will be glued to the top of the specific xy-serve.js file -const debug_serve = false; // set to true for debug log output in node process -const shutdownServer = require("http-graceful-shutdown"); -const express = require("express"); const app = express(); +const express = require("express"); +const fs = require("fs"); +const shutdownServer = require("http-graceful-shutdown"); app.use(express.json({limit: "50mb"})); -const fs = require("fs"); - -function debugLog() { - if (debug_serve) { - console.log.apply(this, arguments) - } -} +// This file will be glued to the top of the specific xy-serve.js file + +app.post('/shutdown', (req, res) => { + res.status(200).send('Shutting down'); + setTimeout(async () => { + try { + await shutdownServer( + app.listen(0, '127.0.0.1', () => { + const instanceId = getInstanceId(); + debugLog('Server running on port ' + listener.address().port + ' for instance ' + instanceId); + fs.writeFile('server.port.tmp', '' + listener.address().port, function (err) { + if (err) { + return console.log(err); + } else { + // Try to be as atomic as possible + fs.rename('server.port.tmp', `server-${instanceId}.port`, function (err) { + if (err) { + return console.log(err); + } + }); + } + }); + }), + { + forceExit: false, // Let the event loop clear + finally: () => debugLog('graceful shutdown finished.'), + } + )(); + } catch (err) { + console.error('Error during shutdown:', err); + } + }, 200); +}); function getInstanceId() { - const args = process.argv.slice(2); - - // Look for the --node-server-instance-id option - let instanceId; - - args.forEach(arg => { - if (arg.startsWith('--node-server-instance-id=')) { - instanceId = arg.split('=')[1]; - } - }); - - // throw if instanceId is not set - if (!instanceId) { - throw new Error("Missing --node-server-instance-id argument"); - } - return instanceId; + return ( + process.argv + .slice(2) + .find(arg => arg.startsWith('--node-server-instance-id=')) + ?.split('=')[1] + || (() => { + throw new Error('Missing --node-server-instance-id argument'); + })() + ); } -var listener = app.listen(0, "127.0.0.1", () => { - const instanceId = getInstanceId(); - debugLog("Server running on port " + listener.address().port + " for instance " + instanceId); - fs.writeFile("server.port.tmp", "" + listener.address().port, function (err) { - if (err) { - return console.log(err); - } else { - fs.rename("server.port.tmp", `server-${instanceId}.port`, function (err) { - if (err) { - return console.log(err); - } - }); // try to be as atomic as possible - } - }); -}); -const shutdown = shutdownServer(listener, { - forceExit: false, // let the event loop clear - finally: () => debugLog("graceful shutdown finished."), -}); - -app.post("/shutdown", (req, res) => { - res.status(200).send("Shutting down"); - setTimeout(async () => { - try { - await shutdown(); - } catch (err) { - console.error("Error during shutdown:", err); - } - }, 200); -}); +function debugLog() { + if (false) { // Set to true for debug log output in node process + console.log.apply(this, arguments); + } +} diff --git a/lib/src/main/resources/com/diffplug/spotless/npm/eslint-serve.js b/lib/src/main/resources/com/diffplug/spotless/npm/eslint-serve.js index 8b60e56dc8..532c481ddd 100644 --- a/lib/src/main/resources/com/diffplug/spotless/npm/eslint-serve.js +++ b/lib/src/main/resources/com/diffplug/spotless/npm/eslint-serve.js @@ -1,4 +1,4 @@ -const {ESLint} = require("eslint"); +import { ESLint } from 'eslint'; app.post("/eslint/format", async (req, res) => { try { diff --git a/lib/src/main/resources/com/diffplug/spotless/npm/prettier-serve.js b/lib/src/main/resources/com/diffplug/spotless/npm/prettier-serve.js index ac1f26790d..0a5c0bb6e5 100644 --- a/lib/src/main/resources/com/diffplug/spotless/npm/prettier-serve.js +++ b/lib/src/main/resources/com/diffplug/spotless/npm/prettier-serve.js @@ -1,4 +1,4 @@ -const prettier = require("prettier"); +import prettier from 'prettier'; app.post("/prettier/config-options", (req, res) => { const config_data = req.body; diff --git a/lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-serve.js b/lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-serve.js index ffbb36c8a4..f536afad5d 100644 --- a/lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-serve.js +++ b/lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-serve.js @@ -1,7 +1,7 @@ -const tsfmt = require("typescript-formatter"); +import tsfmt from 'typescript-formatter'; app.post("/tsfmt/format", (req, res) => { - var format_data = req.body; + let format_data = req.body; tsfmt.processString("spotless-format-string.ts", format_data.file_content, format_data.config_options).then(resultMap => { /* export interface ResultMap { diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/npm/NpmTestsWithDynamicallyInstalledNpmInstallationTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/npm/NpmTestsWithDynamicallyInstalledNpmInstallationTest.java index 78830bf09d..eeb6929f3b 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/npm/NpmTestsWithDynamicallyInstalledNpmInstallationTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/npm/NpmTestsWithDynamicallyInstalledNpmInstallationTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 DiffPlug + * Copyright 2023-2025 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,8 @@ public class NpmTestsWithDynamicallyInstalledNpmInstallationTest extends MavenIntegrationHarness { + public static final String TS = "typescript"; + @Test void useDownloadedNpmInstallation() throws Exception { writePomWithPrettierSteps( @@ -34,15 +36,11 @@ void useDownloadedNpmInstallation() throws Exception { " " + installedNpmPath() + "", ""); - String kind = "typescript"; - String suffix = "ts"; - String configPath = ".prettierrc.yml"; - setFile(configPath).toResource("npm/prettier/filetypes/" + kind + "/" + ".prettierrc.yml"); - String path = "src/main/" + kind + "/test." + suffix; - setFile(path).toResource("npm/prettier/filetypes/" + kind + "/" + kind + ".dirty"); - + String path = "src/main/" + TS + "/test.ts"; + setFile(path).toResource("npm/prettier/filetypes/" + TS + "/" + TS + ".dirty"); + setFile(".prettierrc.yml").toResource("npm/prettier/filetypes/" + TS + "/" + ".prettierrc.yml"); mavenRunner().withArguments(installNpmMavenGoal(), "spotless:apply").runNoError(); - assertFile(path).sameAsResource("npm/prettier/filetypes/" + kind + "/" + kind + ".clean"); + assertFile(path).sameAsResource("npm/prettier/filetypes/" + TS + "/" + TS + ".clean"); } } diff --git a/rewrite.yml b/rewrite.yml index 00f05a6fa4..6c188d0bf4 100644 --- a/rewrite.yml +++ b/rewrite.yml @@ -1,7 +1,7 @@ --- type: specs.openrewrite.org/v1beta/recipe name: com.diffplug.spotless.openrewrite.SanityCheck -displayName: Apply all Java & Gradle best practices +displayName: Apply all best practices description: Comprehensive code quality recipe combining modernization, security, and best practices. tags: - java @@ -9,6 +9,11 @@ tags: - static-analysis - cleanup recipeList: + - org.openrewrite.codemods.ESLint + - org.openrewrite.codemods.UI5 + - org.openrewrite.codemods.cleanup.javascript.EmptyBraceSpaces + - org.openrewrite.codemods.cleanup.javascript.PreferModule + - org.openrewrite.codemods.ecmascript.5to6.ECMAScript6BestPractices - org.openrewrite.gradle.EnableGradleBuildCache - org.openrewrite.gradle.EnableGradleParallelExecution - org.openrewrite.gradle.GradleBestPractices