Skip to content

Commit 415f04a

Browse files
committed
Prettify everything
1 parent e444ec0 commit 415f04a

File tree

3 files changed

+81
-89
lines changed

3 files changed

+81
-89
lines changed

bin/react-stdio

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env node
2-
require('../server')
2+
require('../server');

scripts/release.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
1-
const resolvePath = require('path').resolve
2-
const readFileSync = require('fs').readFileSync
3-
const execSync = require('child_process').execSync
4-
const prompt = require('readline-sync').question
1+
const resolvePath = require("path").resolve;
2+
const readFileSync = require("fs").readFileSync;
3+
const execSync = require("child_process").execSync;
4+
const prompt = require("readline-sync").question;
55

6-
const exec = (command) =>
7-
execSync(command, { stdio: 'inherit' })
6+
const exec = command => execSync(command, { stdio: "inherit" });
87

98
const getPackageVersion = () =>
10-
JSON.parse(readFileSync(resolvePath(__dirname, '../package.json'))).version
9+
JSON.parse(readFileSync(resolvePath(__dirname, "../package.json"))).version;
1110

12-
if (process.cwd() !== resolvePath(__dirname, '..')) {
13-
console.error('The release script must be run from the repo root')
14-
process.exit(1)
11+
if (process.cwd() !== resolvePath(__dirname, "..")) {
12+
console.error("The release script must be run from the repo root");
13+
process.exit(1);
1514
}
1615

1716
// Get the next version, which may be specified as a semver
1817
// version number or anything `npm version` recognizes. This
1918
// is a "pre-release" if nextVersion is premajor, preminor,
2019
// prepatch, or prerelease
21-
const nextVersion = prompt(`Next version (current version is ${getPackageVersion()})? `)
22-
const isPrerelease = nextVersion.substring(0, 3) === 'pre'
20+
const nextVersion = prompt(
21+
`Next version (current version is ${getPackageVersion()})? `
22+
);
23+
const isPrerelease = nextVersion.substring(0, 3) === "pre";
2324

2425
// 1) Increment the package version in package.json
2526
// 2) Create a new commit
2627
// 3) Create a v* tag that points to that commit
27-
exec(`npm version ${nextVersion} -m "Version %s"`)
28+
exec(`npm version ${nextVersion} -m "Version %s"`);
2829

2930
// 4) Push to GitHub master. Do this before we publish in
3031
// case anyone has pushed to GitHub since we last pulled
31-
exec('git push origin master')
32+
exec("git push origin master");
3233

3334
// 5) Publish to npm. Use the "next" tag for pre-releases,
3435
// "latest" for all others
35-
exec(`npm publish --tag ${isPrerelease ? 'next' : 'latest'}`)
36+
exec(`npm publish --tag ${isPrerelease ? "next" : "latest"}`);
3637

3738
// 6) Push the v* tag to GitHub
38-
exec(`git push -f origin v${getPackageVersion()}`)
39+
exec(`git push -f origin v${getPackageVersion()}`);
3940

4041
// 7) Push the "latest" tag to GitHub
4142
if (!isPrerelease) {
42-
exec('git tag -f latest')
43-
exec('git push -f origin latest')
43+
exec("git tag -f latest");
44+
exec("git push -f origin latest");
4445
}

server.js

Lines changed: 61 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,118 @@
1-
'use strict'
2-
3-
const path = require('path')
4-
const invariant = require('invariant')
5-
const EventStream = require('event-stream')
6-
const JSONStream = require('JSONStream')
7-
const ReactDOMServer = require('react-dom/server')
8-
const React = require('react')
9-
10-
function getDefaultExports(moduleID) {
1+
const path = require("path");
2+
const invariant = require("invariant");
3+
const EventStream = require("event-stream");
4+
const JSONStream = require("JSONStream");
5+
const ReactDOMServer = require("react-dom/server");
6+
const React = require("react");
7+
8+
function loadModule(moduleId) {
119
// Clear the require cache, in case the file was
1210
// changed since the server was started.
13-
const cacheKey = require.resolve(moduleID)
14-
delete require.cache[cacheKey]
11+
const cacheKey = require.resolve(moduleId);
12+
delete require.cache[cacheKey];
1513

16-
const moduleExports = require(moduleID)
14+
const moduleExports = require(moduleId);
1715

18-
// Return exports.default if using ES2015 modules.
19-
if (moduleExports && moduleExports.default)
20-
return moduleExports.default
16+
// Return exports.default if using ES modules.
17+
if (moduleExports && moduleExports.default) {
18+
return moduleExports.default;
19+
}
2120

22-
return moduleExports
21+
return moduleExports;
2322
}
2423

2524
function renderToStaticMarkup(element, callback) {
26-
callback(null, ReactDOMServer.renderToStaticMarkup(element))
25+
callback(null, ReactDOMServer.renderToStaticMarkup(element));
2726
}
2827

2928
function renderToString(element, callback) {
30-
callback(null, ReactDOMServer.renderToString(element))
29+
callback(null, ReactDOMServer.renderToString(element));
3130
}
3231

3332
function handleRequest(workingDir, request, callback) {
34-
const componentPath = request.component
35-
const renderMethod = request.render
36-
const props = request.props
33+
const componentPath = request.component;
34+
const renderMethod = request.render;
35+
const props = request.props;
3736

38-
invariant(
39-
componentPath != null,
40-
'Missing { component } in request'
41-
)
42-
43-
let render
44-
if (renderMethod == null || renderMethod === 'renderToString') {
45-
render = renderToString
46-
} else if (renderMethod === 'renderToStaticMarkup') {
47-
render = renderToStaticMarkup
37+
invariant(componentPath != null, "Missing { component } in request");
38+
39+
let render;
40+
if (renderMethod == null || renderMethod === "renderToString") {
41+
render = renderToString;
42+
} else if (renderMethod === "renderToStaticMarkup") {
43+
render = renderToStaticMarkup;
4844
} else {
49-
const methodFile = path.resolve(workingDir, renderMethod)
45+
const methodFile = path.resolve(workingDir, renderMethod);
5046

5147
try {
52-
render = getDefaultExports(methodFile)
48+
render = loadModule(methodFile);
5349
} catch (error) {
54-
if (error.code !== 'MODULE_NOT_FOUND')
55-
process.stderr.write(error.stack + '\n')
50+
if (error.code !== "MODULE_NOT_FOUND") {
51+
process.stderr.write(error.stack + "\n");
52+
}
5653
}
5754
}
5855

5956
invariant(
60-
typeof render === 'function',
61-
'Cannot load render method: %s',
57+
typeof render === "function",
58+
"Cannot load render method: %s",
6259
renderMethod
63-
)
60+
);
6461

65-
const componentFile = path.resolve(workingDir, componentPath)
62+
const componentFile = path.resolve(workingDir, componentPath);
6663

67-
let component
64+
let component;
6865
try {
69-
component = getDefaultExports(componentFile)
66+
component = loadModule(componentFile);
7067
} catch (error) {
71-
if (error.code !== 'MODULE_NOT_FOUND')
72-
process.stderr.write(error.stack + '\n')
68+
if (error.code !== "MODULE_NOT_FOUND") {
69+
process.stderr.write(error.stack + "\n");
70+
}
7371
}
7472

75-
invariant(
76-
component != null,
77-
'Cannot load component: %s',
78-
componentPath
79-
)
80-
81-
render(
82-
React.createElement(component, props),
83-
callback
84-
)
73+
invariant(component != null, "Cannot load component: %s", componentPath);
74+
75+
render(React.createElement(component, props), callback);
8576
}
8677

8778
function createRequestHandler(workingDir) {
88-
return function (request, callback) {
79+
return function(request, callback) {
8980
try {
90-
handleRequest(workingDir, request, function (error, html) {
81+
handleRequest(workingDir, request, function(error, html) {
9182
if (error) {
92-
callback(error)
93-
} else if (typeof html !== 'string') {
83+
callback(error);
84+
} else if (typeof html !== "string") {
9485
// Crash the server process.
95-
callback(new Error('Render method must return a string'))
86+
callback(new Error("Render method must return a string"));
9687
} else {
97-
callback(null, JSON.stringify({ html: html }))
88+
callback(null, JSON.stringify({ html: html }));
9889
}
99-
})
90+
});
10091
} catch (error) {
101-
callback(null, JSON.stringify({ error: error.message }))
92+
callback(null, JSON.stringify({ error: error.message }));
10293
}
103-
}
94+
};
10495
}
10596

10697
// Redirect stdout to stderr, but save a reference so we can
10798
// still write to stdout.
108-
const stdout = process.stdout
109-
Object.defineProperty(process, 'stdout', {
99+
const stdout = process.stdout;
100+
Object.defineProperty(process, "stdout", {
110101
configurable: true,
111102
enumerable: true,
112103
value: process.stderr
113-
})
104+
});
114105

115106
// Ensure console.log knows about the new stdout.
116-
const Console = require('console').Console
117-
Object.defineProperty(global, 'console', {
107+
const Console = require("console").Console;
108+
Object.defineProperty(global, "console", {
118109
configurable: true,
119110
enumerable: true,
120111
value: new Console(process.stdout, process.stderr)
121-
})
112+
});
122113

123114
// Read JSON blobs from stdin, pipe output to stdout.
124115
process.stdin
125116
.pipe(JSONStream.parse())
126117
.pipe(EventStream.map(createRequestHandler(process.cwd())))
127-
.pipe(stdout)
118+
.pipe(stdout);

0 commit comments

Comments
 (0)