-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #490 from gigabo/cli-commands
Cli commands
- Loading branch information
Showing
29 changed files
with
589 additions
and
267 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Can't use `instanceof` with babel-ified subclasses of builtins. | ||
// | ||
// https://phabricator.babeljs.io/T3083 | ||
// | ||
// Gotta do this the old-fashioned way. :p | ||
// | ||
export default function ConfigurationError(message) { | ||
this.name = 'ConfigurationError'; | ||
this.message = message; | ||
this.stack = (new Error()).stack; | ||
} | ||
ConfigurationError.prototype = Object.create(Error.prototype); | ||
ConfigurationError.prototype.constructor = ConfigurationError; |
This file contains 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,5 +1,5 @@ | ||
require("babel-core/register"); | ||
|
||
const {start, parseCliArgs} = require("."); | ||
const {run, parseCliArgs} = require("."); | ||
|
||
start(parseCliArgs()); | ||
run(parseCliArgs()); |
This file contains 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import _ from "lodash"; | ||
import fs from "fs"; | ||
import {join} from "path"; | ||
import chalk from "chalk"; | ||
import mkdirp from "mkdirp"; | ||
import fileExists from "../fileExists"; | ||
import ConfigurationError from "../ConfigurationError"; | ||
|
||
const PAGE_SOURCE = _.template(` | ||
import React from "react"; | ||
export default class <%= className %> { | ||
handleRoute(next) { | ||
// Kick off data requests here. | ||
return next(); | ||
} | ||
getElements() { | ||
return <div>This is <%= className %>.</div> | ||
} | ||
} | ||
`); | ||
|
||
export default function addPage(options){ | ||
const {routesFile, routesPath, routes} = options; | ||
|
||
const path = options._[3]; | ||
const className = options._[4]; | ||
|
||
if (!path || !className) { | ||
throw new ConfigurationError("Usage: react-server add-page <urlPath> <ClassName>"); | ||
} | ||
|
||
const page = join("pages", className + ".js"); | ||
|
||
if (fileExists(page)) { | ||
throw new ConfigurationError(`Found a pre-existing ${page}. Aborting.`); | ||
} | ||
|
||
mkdirp("pages"); | ||
|
||
console.log(chalk.yellow("Generating " + page)); | ||
|
||
fs.writeFileSync(page, PAGE_SOURCE({className})); | ||
|
||
routes.routes[className] = { path, page }; | ||
|
||
console.log(chalk.yellow("Updating " + routesFile)); | ||
|
||
fs.writeFileSync(routesPath, JSON.stringify(routes, null, " ") + "\n"); | ||
|
||
console.log(chalk.green("All set!")); | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import compileClient from "../compileClient" | ||
import handleCompilationErrors from "../handleCompilationErrors"; | ||
import setupLogging from "../setupLogging"; | ||
import logProductionWarnings from "../logProductionWarnings"; | ||
import {logging} from "../react-server"; | ||
|
||
const logger = logging.getLogger(__LOGGER__); | ||
|
||
export default function compile(options){ | ||
setupLogging(options); | ||
logProductionWarnings(options); | ||
|
||
const {compiler} = compileClient(options); | ||
|
||
logger.notice("Starting compilation of client JavaScript..."); | ||
compiler.run((err, stats) => { | ||
const error = handleCompilationErrors(err, stats); | ||
if (!error) { | ||
logger.notice("Successfully compiled client JavaScript."); | ||
} else { | ||
logger.error(error); | ||
} | ||
}); | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import _ from "lodash"; | ||
import fs from "fs"; | ||
import {spawnSync} from "child_process"; | ||
import chalk from "chalk"; | ||
import fileExists from "../fileExists"; | ||
import ConfigurationError from "../ConfigurationError"; | ||
|
||
const DEPENDENCIES = [ | ||
"react-server", | ||
"babel-preset-react-server", | ||
|
||
// TODO: Modernize our peer deps and remove versions here. | ||
"[email protected]", | ||
"react@~0.14.2", | ||
"react-dom@~0.14.2", | ||
] | ||
|
||
const DEV_DEPENDENCIES = [ | ||
|
||
// TODO: These, too. | ||
"webpack-dev-server@~1.13.0", | ||
"webpack@^1.13.1", | ||
] | ||
|
||
const CONFIG = { | ||
"routes.json": { | ||
middleware: [], | ||
routes: {}, | ||
}, | ||
".reactserverrc": { | ||
routesFile: "routes.json", | ||
port: 3000, | ||
env: { | ||
production: { | ||
port: 80, | ||
}, | ||
}, | ||
}, | ||
".babelrc": { | ||
presets: ["react-server"], | ||
}, | ||
} | ||
|
||
export default function init(){ | ||
|
||
if (!fileExists("package.json")) { | ||
console.log(chalk.yellow("No package.json found. Running `npm init --yes`")); | ||
spawnSync("npm", ["init", "--yes"], {stdio: "inherit"}); | ||
} | ||
|
||
Object.keys(CONFIG).forEach(fn => { | ||
if (fileExists(fn)) { | ||
throw new ConfigurationError(`Found a pre-existing ${fn}. Aborting.`); | ||
} | ||
}); | ||
|
||
console.log(chalk.yellow("Installing dependencies")); | ||
|
||
spawnSync("npm", ["install", "--save", ...DEPENDENCIES], {stdio: "inherit"}); | ||
|
||
console.log(chalk.yellow("Installing devDependencies")); | ||
|
||
spawnSync("npm", ["install", "--save-dev", ...DEV_DEPENDENCIES], {stdio: "inherit"}); | ||
|
||
_.forEach(CONFIG, (config, fn) => { | ||
console.log(chalk.yellow("Generating " + fn)); | ||
|
||
fs.writeFileSync(fn, JSON.stringify(config, null, " ") + "\n"); | ||
}); | ||
|
||
console.log(chalk.green("All set!")); | ||
} |
Oops, something went wrong.