-
Notifications
You must be signed in to change notification settings - Fork 499
[ECMAScript6BestPractices] fix common-serve.js
#2773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Pankraz76
wants to merge
9
commits into
diffplug:main
Choose a base branch
from
Pankraz76:fix-ECMAScript6BestPractices-fix-common-serve
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cd3f993
[ECMAScript6BestPractices] prerequisite fix `common-serve.js`
7d1eb76
[rewrite] apply `ECMAScript6BestPractices`
6cc4dcc
[rewrite] apply `ECMAScript6BestPractices`
b48205f
[rewrite] apply `ECMAScript6BestPractices`
eb97996
[rewrite] apply `ECMAScript6BestPractices`
15fd403
[rewrite] apply `ECMAScript6BestPractices`
e2dfdc4
[rewrite] apply `ECMAScript6BestPractices`
e5d81a3
[rewrite] apply `ECMAScript6BestPractices`
f558260
Merge remote-tracking branch 'origin/fix-ECMAScript6BestPractices-fix…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 39 additions & 53 deletions
92
lib/src/main/resources/com/diffplug/spotless/npm/common-serve.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,65 +1,51 @@ | ||
| // 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(); | ||
|
|
||
| app.use(express.json({limit: "50mb"})); | ||
|
|
||
| const fs = require("fs"); | ||
| const shutdownServer = require("http-graceful-shutdown"); | ||
|
|
||
| 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 | ||
|
|
||
| function getInstanceId() { | ||
| const args = process.argv.slice(2); | ||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Look for the --node-server-instance-id option | ||
| let 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"); })(); | ||
| } | ||
|
|
||
| args.forEach(arg => { | ||
| if (arg.startsWith('--node-server-instance-id=')) { | ||
| instanceId = arg.split('=')[1]; | ||
| } | ||
| }); | ||
| function streamOutput(port, instanceId) { | ||
| debugLog("Server running on port " + port + " for instance " + instanceId); | ||
| fs.writeFile("server.port.tmp", "" + 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); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // throw if instanceId is not set | ||
| if (!instanceId) { | ||
| throw new Error("Missing --node-server-instance-id argument"); | ||
| } | ||
| return instanceId; | ||
| function debugLog() { | ||
| if (false) { // set to true for debug log output in node process | ||
| console.log.apply(this, arguments); | ||
| } | ||
| } | ||
|
|
||
| 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 | ||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }); | ||
| }); | ||
| const shutdown = shutdownServer(listener, { | ||
Pankraz76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| forceExit: false, // let the event loop clear | ||
| finally: () => debugLog("graceful shutdown finished."), | ||
| }); | ||
| const app = express(); | ||
|
|
||
| app.use(express.json({ limit: "50mb" })); | ||
|
|
||
| const server = app.listen(0, "127.0.0.1", () => streamOutput(server.address().port, getInstanceId())); | ||
|
|
||
| app.post("/shutdown", (req, res) => { | ||
| res.status(200).send("Shutting down"); | ||
| setTimeout(async () => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assuming if we fail here we still would have send out 200 before. |
||
| try { | ||
| await shutdown(); | ||
| } catch (err) { | ||
| console.error("Error during shutdown:", err); | ||
| } | ||
| }, 200); | ||
| setTimeout(async () => { | ||
| try { | ||
| await shutdownServer(server, { | ||
| forceExit: false, // let the event loop clear | ||
| finally: () => debugLog("graceful shutdown finished."), | ||
| })(); | ||
| } catch (err) { | ||
| console.error("Error during shutdown:", err); | ||
| } | ||
| }, 200); | ||
| res.ok().send("Graceful shutdown."); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 1 addition & 17 deletions
18
lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-serve.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,7 @@ | ||
| const tsfmt = require("typescript-formatter"); | ||
|
|
||
| app.post("/tsfmt/format", (req, res) => { | ||
| var format_data = req.body; | ||
| tsfmt.processString("spotless-format-string.ts", format_data.file_content, format_data.config_options).then(resultMap => { | ||
| /* | ||
| export interface ResultMap { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we still need this? |
||
| [fileName: string]: Result; | ||
| } | ||
| export interface Result { | ||
| fileName: string; | ||
| settings: ts.FormatCodeSettings | null; | ||
| message: string; | ||
| error: boolean; | ||
| src: string; | ||
| dest: string; | ||
| } | ||
| */ | ||
| // result contains 'message' (String), 'error' (boolean), 'dest' (String) => formatted | ||
| tsfmt.processString("spotless-format-string.ts", req.body.file_content, req.body.config_options).then(resultMap => { | ||
| if (resultMap.error !== undefined && resultMap.error) { | ||
| res.status(400).send(resultMap.message); | ||
| return; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leftover.