From 5987f4c6ad3f8171acc98d74afd4812ac8645d44 Mon Sep 17 00:00:00 2001 From: Christine Brass Date: Sat, 30 Apr 2016 16:36:16 -0700 Subject: [PATCH] Linting and minor refactoring --- docker/Dockerfile | 2 +- ...create_base_image => create-base-image.js} | 0 ...rate_dockerfile => generate-dockerfile.js} | 2 +- package.json | 8 ++- src/{auto-upgrade => autoUpgrade}/index.js | 4 +- .../previous/.babelrc | 0 .../updateBabelConfig.js | 4 +- .../updateConfig.js | 4 +- .../updatePackage.js | 16 ++--- src/cli-harmony.js | 5 +- src/cli.js | 14 ++-- src/commands/destroy.js | 28 ++++---- src/commands/dockerize.js | 3 +- src/commands/generate.js | 42 ++++++------ src/commands/new.js | 6 +- src/commands/start-client.js | 68 +++++++++---------- src/commands/start-server.js | 15 ++-- src/commands/test.js | 8 +-- .../karmaConfig.js => config/karma-config.js} | 18 ++--- .../webpack-isomorphic-tools-config.js} | 10 +-- .../webpack-shared-config.js} | 17 ++--- src/entrypoints/client.js | 5 +- src/entrypoints/server.js | 10 +++ src/lib/detectEnvironmentVariables.js | 16 +++-- ...ck-additions.js => getWebpackAdditions.js} | 13 ++-- src/lib/help-text.js | 28 -------- src/lib/helpText.js | 28 ++++++++ src/lib/logger.js | 19 +++--- src/lib/logsColorScheme.js | 2 +- ...npm-dependencies.js => npmDependencies.js} | 4 +- .../runThroughBabel.js} | 10 +-- src/lib/server-side-rendering.js | 39 ----------- src/{shared/components => lib/server}/Body.js | 9 ++- .../errorHandler.js} | 6 +- .../components => lib/server}/getHead.js | 9 +-- src/lib/server/index.js | 29 ++++++++ .../requestHandler.js} | 28 ++++---- src/lib/test-helper.js | 15 ---- src/lib/testHelper.js | 15 ++++ {generate => templates/generate}/component.js | 0 .../generate}/component.test.js | 3 + {generate => templates/generate}/container.js | 0 .../generate}/container.test.js | 3 + {generate => templates/generate}/reducer.js | 0 .../generate}/reducer.test.js | 2 + {new => templates/new}/.babelrc | 0 {new => templates/new}/.dockerignore | 0 {new => templates/new}/.eslintrc | 0 {new => templates/new}/.gluestick | 0 {new => templates/new}/.npmrc | 0 {new => templates/new}/505.hbs | 0 {new => templates/new}/Index.js | 0 {new => templates/new}/_gitignore | 0 {new => templates/new}/assets/.empty | 0 .../new}/assets/css/normalize.css | 0 {new => templates/new}/package.json | 0 {new => templates/new}/src/actions/.empty | 0 {new => templates/new}/src/components/Home.js | 0 .../new}/src/components/MasterLayout.js | 0 templates/new/src/config/.Dockerfile | 9 +++ {new => templates/new}/src/config/.entry.js | 0 {new => templates/new}/src/config/.store.js | 0 .../new}/src/config/application.js | 0 .../new}/src/config/redux-middleware.js | 0 {new => templates/new}/src/config/routes.js | 0 .../new}/src/config/webpack-additions.js | 0 .../new}/src/containers/HomeApp.js | 0 .../new}/src/containers/NoMatchApp.js | 0 {new => templates/new}/src/reducers/index.js | 0 .../new}/test/components/Home.test.js | 0 .../new}/test/components/MasterLayout.test.js | 0 {new => templates/new}/test/containers/.empty | 0 .../new}/test/containers/HomeApp.test.js | 0 .../new}/test/containers/NoMatchApp.test.js | 0 {new => templates/new}/test/reducers/.empty | 0 test/commands/destroy.test.js | 13 ++-- test/commands/generate.test.js | 3 +- test/commands/new.test.js | 19 +++--- test/lib/updateVersion.test.js | 1 + test/lib/utils.test.js | 1 + 80 files changed, 328 insertions(+), 285 deletions(-) rename docker/{create_base_image => create-base-image.js} (100%) rename docker/{generate_dockerfile => generate-dockerfile.js} (89%) rename src/{auto-upgrade => autoUpgrade}/index.js (95%) rename src/{auto-upgrade => autoUpgrade}/previous/.babelrc (100%) rename src/{auto-upgrade => autoUpgrade}/updateBabelConfig.js (93%) rename src/{auto-upgrade => autoUpgrade}/updateConfig.js (92%) rename src/{auto-upgrade => autoUpgrade}/updatePackage.js (94%) rename src/{lib/karmaConfig.js => config/karma-config.js} (79%) rename src/{lib/webpack-isomorphic-tools-configuration.js => config/webpack-isomorphic-tools-config.js} (87%) rename src/{lib/shared.js => config/webpack-shared-config.js} (73%) create mode 100644 src/entrypoints/server.js rename src/lib/{get-webpack-additions.js => getWebpackAdditions.js} (87%) delete mode 100644 src/lib/help-text.js create mode 100644 src/lib/helpText.js rename src/lib/{npm-dependencies.js => npmDependencies.js} (82%) rename src/{run-through-babel.js => lib/runThroughBabel.js} (68%) delete mode 100644 src/lib/server-side-rendering.js rename src/{shared/components => lib/server}/Body.js (69%) rename src/lib/{server-error-handler.js => server/errorHandler.js} (92%) rename src/{shared/components => lib/server}/getHead.js (86%) create mode 100644 src/lib/server/index.js rename src/lib/{server-request-handler.js => server/requestHandler.js} (85%) delete mode 100644 src/lib/test-helper.js create mode 100644 src/lib/testHelper.js rename {generate => templates/generate}/component.js (100%) rename {generate => templates/generate}/component.test.js (82%) rename {generate => templates/generate}/container.js (100%) rename {generate => templates/generate}/container.test.js (82%) rename {generate => templates/generate}/reducer.js (100%) rename {generate => templates/generate}/reducer.test.js (84%) rename {new => templates/new}/.babelrc (100%) rename {new => templates/new}/.dockerignore (100%) rename {new => templates/new}/.eslintrc (100%) rename {new => templates/new}/.gluestick (100%) rename {new => templates/new}/.npmrc (100%) rename {new => templates/new}/505.hbs (100%) rename {new => templates/new}/Index.js (100%) rename {new => templates/new}/_gitignore (100%) rename {new => templates/new}/assets/.empty (100%) rename {new => templates/new}/assets/css/normalize.css (100%) rename {new => templates/new}/package.json (100%) rename {new => templates/new}/src/actions/.empty (100%) rename {new => templates/new}/src/components/Home.js (100%) rename {new => templates/new}/src/components/MasterLayout.js (100%) create mode 100644 templates/new/src/config/.Dockerfile rename {new => templates/new}/src/config/.entry.js (100%) rename {new => templates/new}/src/config/.store.js (100%) rename {new => templates/new}/src/config/application.js (100%) rename {new => templates/new}/src/config/redux-middleware.js (100%) rename {new => templates/new}/src/config/routes.js (100%) rename {new => templates/new}/src/config/webpack-additions.js (100%) rename {new => templates/new}/src/containers/HomeApp.js (100%) rename {new => templates/new}/src/containers/NoMatchApp.js (100%) rename {new => templates/new}/src/reducers/index.js (100%) rename {new => templates/new}/test/components/Home.test.js (100%) rename {new => templates/new}/test/components/MasterLayout.test.js (100%) rename {new => templates/new}/test/containers/.empty (100%) rename {new => templates/new}/test/containers/HomeApp.test.js (100%) rename {new => templates/new}/test/containers/NoMatchApp.test.js (100%) rename {new => templates/new}/test/reducers/.empty (100%) diff --git a/docker/Dockerfile b/docker/Dockerfile index 5d8f7329d..823f69938 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -10,7 +10,7 @@ RUN npm install -g gluestick@$GLUESTICK_VERSION RUN mkdir /app WORKDIR /app -ADD ./new/package.json /app +ADD ./templates/new/package.json /app RUN npm install diff --git a/docker/create_base_image b/docker/create-base-image.js similarity index 100% rename from docker/create_base_image rename to docker/create-base-image.js diff --git a/docker/generate_dockerfile b/docker/generate-dockerfile.js similarity index 89% rename from docker/generate_dockerfile rename to docker/generate-dockerfile.js index 43b6c9cea..cb8a9ea66 100755 --- a/docker/generate_dockerfile +++ b/docker/generate-dockerfile.js @@ -18,4 +18,4 @@ var dockerfile = [ "" ]; -fs.writeFile("./new/src/config/.Dockerfile", dockerfile.join("\n")); +fs.writeFile("./templates/new/src/config/.Dockerfile", dockerfile.join("\n")); diff --git a/package.json b/package.json index a6745d35c..b7ac510fb 100644 --- a/package.json +++ b/package.json @@ -8,10 +8,11 @@ "bugs": "https://github.com/TrueCar/gluestick/issues", "license": "MIT", "scripts": { + "lint": "eslint src test templates", "test": "mocha --compilers js:babel-core/register --require babel-polyfill --recursive", "test-coverage": "./node_modules/.bin/babel-node ./node_modules/babel-istanbul/lib/cli.js cover --include-all-sources --report html --dir coverage/html ./node_modules/.bin/_mocha -- --recursive", - "prepublish": "./docker/generate_dockerfile", - "postpublish": "./docker/create_base_image" + "prepublish": "./docker/generate-dockerfile.js", + "postpublish": "./docker/create-base-image.js" }, "keywords": [ "react", @@ -97,6 +98,9 @@ }, "peerDependencies": {}, "devDependencies": { + "babel-eslint": "6.0.4", + "eslint": "2.9.0", + "eslint-plugin-react": "5.0.1", "temp": "0.8.3" } } diff --git a/src/auto-upgrade/index.js b/src/autoUpgrade/index.js similarity index 95% rename from src/auto-upgrade/index.js rename to src/autoUpgrade/index.js index c84c1dc52..20c5a758b 100644 --- a/src/auto-upgrade/index.js +++ b/src/autoUpgrade/index.js @@ -50,7 +50,7 @@ module.exports = async function () { } catch (e) { const fileName = path.parse(filePath).base; - const newFile = fs.readFileSync(path.join(__dirname, "..", "..", "new", filePath), "utf8"); + const newFile = fs.readFileSync(path.join(__dirname, "..", "..", "templates", "new", filePath), "utf8"); replaceFile(fileName, newFile); } }); @@ -65,7 +65,7 @@ module.exports = async function () { const currentFile = fs.readFileSync(getCurrentFilePath(fileName)); const currentSha = sha1(currentFile); - const newFile = fs.readFileSync(path.join(__dirname, "..", "..", "new", "src", "config", fileName), "utf8"); + const newFile = fs.readFileSync(path.join(__dirname, "..", "..", "templates", "new", "src", "config", fileName), "utf8"); const newSha = sha1(newFile); if (currentSha !== newSha) { diff --git a/src/auto-upgrade/previous/.babelrc b/src/autoUpgrade/previous/.babelrc similarity index 100% rename from src/auto-upgrade/previous/.babelrc rename to src/autoUpgrade/previous/.babelrc diff --git a/src/auto-upgrade/updateBabelConfig.js b/src/autoUpgrade/updateBabelConfig.js similarity index 93% rename from src/auto-upgrade/updateBabelConfig.js rename to src/autoUpgrade/updateBabelConfig.js index 07f20c276..e2d101899 100644 --- a/src/auto-upgrade/updateBabelConfig.js +++ b/src/autoUpgrade/updateBabelConfig.js @@ -16,14 +16,14 @@ export default function updateBabelConfig() { return new Promise(resolve => { const projectConfigPath = path.join(process.cwd(), ".babelrc"); - const latestConfigPath = path.join(__dirname, "../..", "new", ".babelrc"); + const latestConfigPath = path.join(__dirname, "../..", "templates", "new", ".babelrc"); const projectSha = sha1(readFileSyncStrip(projectConfigPath)); const previousSha = sha1(readFileSyncStrip(path.join(__dirname, "previous", ".babelrc"))); const latestSha = sha1(readFileSyncStrip(latestConfigPath)); if (projectSha === latestSha) { resolve(); - } + } else if (projectSha === previousSha) { doUpdate(latestConfigPath, projectConfigPath); resolve(); diff --git a/src/auto-upgrade/updateConfig.js b/src/autoUpgrade/updateConfig.js similarity index 92% rename from src/auto-upgrade/updateConfig.js rename to src/autoUpgrade/updateConfig.js index 72dde40dc..1549908fe 100644 --- a/src/auto-upgrade/updateConfig.js +++ b/src/autoUpgrade/updateConfig.js @@ -19,7 +19,7 @@ export default function updateConfig () { const appConfigPath = path.join(CWD, "src", "config", "application.js"); const appConfig = readFileSyncStrip(appConfigPath); const lastAppConfigLine = appConfig.split("\n").pop(); - const expectedLastLine = 'export default (config[process.env.NODE_ENV] || config["development"]);'; + const expectedLastLine = "export default (config[process.env.NODE_ENV] || config[\"development\"]);"; const hasExportUpgrade = lastAppConfigLine === expectedLastLine; const hasHeadUpgrade = appConfig.indexOf("const headContent = {") !== -1; @@ -28,7 +28,7 @@ export default function updateConfig () { return; } - const exampleContents = fs.readFileSync(path.join(__dirname, "..", "..", "new", "src", "config", "application.js"), "utf8"); + const exampleContents = fs.readFileSync(path.join(__dirname, "..", "..", "templates", "new", "src", "config", "application.js"), "utf8"); logger.warn("The format of src/config/application.js is out of date."); if (hasExportUpgrade) { logger.warn("You should *manually* update it to include the new document head content as seen in the example below."); diff --git a/src/auto-upgrade/updatePackage.js b/src/autoUpgrade/updatePackage.js similarity index 94% rename from src/auto-upgrade/updatePackage.js rename to src/autoUpgrade/updatePackage.js index e1f493efc..b94cb7274 100644 --- a/src/auto-upgrade/updatePackage.js +++ b/src/autoUpgrade/updatePackage.js @@ -46,7 +46,7 @@ export default function fixVersionMismatch () { // Compare the new project dependencies, mark any module that is missing in // the generated project's dependencies - for(let key in newProjectDependencies) { + for(const key in newProjectDependencies) { if (newProjectDependencies[key] !== projectDependencies[key]) { mismatchedModules[key] = { required: newProjectDependencies[key], project: projectDependencies[key] || "missing", type: "dependencies" }; } @@ -54,7 +54,7 @@ export default function fixVersionMismatch () { // Compare the new project development dependencies, mark any module that // is missing in the generated project's development dependencies - for(let key in newProjectDevDependencies) { + for(const key in newProjectDevDependencies) { if (newProjectDevDependencies[key] !== projectDevDependencies[key]) { mismatchedModules[key] = { required: newProjectDevDependencies[key], project: projectDevDependencies[key] || "missing", type: "devDependencies" }; } @@ -62,7 +62,7 @@ export default function fixVersionMismatch () { // Compare the CLI dependencies, only mark a module as mismatched if it is // included in both and the version do not match - for(let key in cliDependencies) { + for(const key in cliDependencies) { if (projectDependencies[key] && cliDependencies[key] !== projectDependencies[key]) { mismatchedModules[key] = { required: cliDependencies[key], project: projectDependencies[key], type: "dependencies" }; } @@ -111,7 +111,7 @@ function loadCLIPackage () { * @return {Object} */ function loadNewProjectPackage () { - return loadPackage(path.join(__dirname, "../../new/package.json")); + return loadPackage(path.join(__dirname, "../../templates/new/package.json")); } /** @@ -148,7 +148,7 @@ ${chalk.yellow(mismatchedModuleOutput)} Would you like to automatically update your project's dependencies to match the CLI?` }; inquirer.prompt([question]).then(function (answers) { - if (!answers.confirm) return done(); + if (!answers.confirm) { return done(); } performModulesUpdate(mismatchedModules, done); }); } @@ -164,9 +164,9 @@ Would you like to automatically update your project's dependencies to match the * update completes */ function performModulesUpdate (mismatchedModules, done) { - let projectPackageData = loadProjectPackage(); + const projectPackageData = loadProjectPackage(); let module; - for (let moduleName in mismatchedModules) { + for (const moduleName in mismatchedModules) { module = mismatchedModules[moduleName]; projectPackageData[module.type][moduleName] = module.required; } @@ -184,7 +184,7 @@ function performModulesUpdate (mismatchedModules, done) { const npmInstall = spawn("npm" + postFix, ["install"], {stdio: "inherit"}); npmInstall.on("close", () => { - logger.success(`node_modules have been updated.`); + logger.success("node_modules have been updated."); done(); }); } diff --git a/src/cli-harmony.js b/src/cli-harmony.js index 90d8d95f9..2624c45e3 100755 --- a/src/cli-harmony.js +++ b/src/cli-harmony.js @@ -1,7 +1,6 @@ #!/usr/bin/env node -'use strict'; - -require("./run-through-babel"); +"use strict"; +require("./lib/runThroughBabel"); require("./cli"); diff --git a/src/cli.js b/src/cli.js index 18bafad21..ade0741d6 100755 --- a/src/cli.js +++ b/src/cli.js @@ -22,7 +22,7 @@ const { highlight } = logsColorScheme; const utils = require("./lib/utils"); const { quitUnlessGluestickProject } = utils; -const autoUpgrade = require("./auto-upgrade"); +const autoUpgrade = require("./autoUpgrade"); const isProduction = process.env.NODE_ENV === "production"; @@ -167,7 +167,7 @@ Upgrade Notice: Newer versions of Index.js now include react-helmet for allowing dynamic changes to document header data. You will need to manually update your Index.js file to receive this change. For a simple example see: -https://github.com/TrueCar/gluestick/blob/develop/new/Index.js +https://github.com/TrueCar/gluestick/blob/develop/templates/new/Index.js ########################################################################## `); } @@ -186,8 +186,10 @@ function spawnProcess (type, args=[]) { case "test": childProcess = spawn("gluestick" + postFix, ["start-test", ...args], {stdio: "inherit", env: Object.assign({}, process.env, {NODE_ENV: isProduction ? "production": "development-test"})}); break; + default: + break; } - childProcess.on("error", function (data) { logger.error(JSON.stringify(arguments)); }); + childProcess.on("error", function () { logger.error(JSON.stringify(arguments)); }); return childProcess; } @@ -200,12 +202,12 @@ async function startAll(withoutTests=false, debug=false) { process.exit(); } - const client = spawnProcess("client"); - const server = spawnProcess("server", (debug ? ["--debug"] : [])); + spawnProcess("client"); + spawnProcess("server", (debug ? ["--debug"] : [])); // Start tests unless they asked us not to or we are in production mode if (!isProduction && !withoutTests) { - const testProcess = spawnProcess("test"); + spawnProcess("test"); } } diff --git a/src/commands/destroy.js b/src/commands/destroy.js index 07e8cd77f..f68f5b340 100644 --- a/src/commands/destroy.js +++ b/src/commands/destroy.js @@ -1,11 +1,11 @@ -var fs = require("fs"); -var path = require("path"); -var inquirer = require("inquirer"); +const fs = require("fs"); +const path = require("path"); +const inquirer = require("inquirer"); const logger = require("../lib/logger"); const logsColorScheme = require("../lib/logsColorScheme"); const { highlight, filename } = logsColorScheme; -var availableCommands = { +const availableCommands = { "container": "containers", "component": "components", "reducer": "reducers" @@ -22,7 +22,7 @@ module.exports = async function (command, name) { // Validate the name by stripping out unwanted characters if (!name || name.length === 0) { - logger.error(`invalid arguments. You must specify a name.`); + logger.error("invalid arguments. You must specify a name."); return; } @@ -32,7 +32,7 @@ module.exports = async function (command, name) { } // Possibly mutate the name by converting it to Pascal Case (only for container and component for now) - var originalName = name; // store original name for later + const originalName = name; // store original name for later if (["container", "component"].indexOf(command) !== -1) { name = name.substr(0, 1).toUpperCase() + name.substr(1); } @@ -41,8 +41,8 @@ module.exports = async function (command, name) { } // Remove the file - var destinationPath = path.join(process.cwd(), "/src/", availableCommands[command] + "/" + name + ".js"); - var fileExists = true; + const destinationPath = path.join(process.cwd(), "/src/", availableCommands[command] + "/" + name + ".js"); + let fileExists = true; try { fs.statSync(destinationPath); } @@ -54,7 +54,7 @@ module.exports = async function (command, name) { // If they typed a name that would normally be mutated, check with the user first before deleting the files if (originalName !== name) { // @NOTE: We are using await so that we can wait for the result of the promise before moving on - const continueDestroying = await new Promise((resolve, reject) => { + const continueDestroying = await new Promise((resolve) => { const question = { type: "confirm", name: "confirm", @@ -66,7 +66,7 @@ module.exports = async function (command, name) { }); // Since we used await, this code will not be executed until the promise above resolves - if (!continueDestroying) process.exit(); + if (!continueDestroying) { process.exit(); } } fs.unlinkSync(destinationPath); @@ -79,7 +79,7 @@ module.exports = async function (command, name) { // If we destroyed a reducer, remove it from the reducers index if (command === "reducer") { - var reducerIndexPath = path.resolve(process.cwd(), "src/reducers/index.js"); + const reducerIndexPath = path.resolve(process.cwd(), "src/reducers/index.js"); try { const indexLines = fs.readFileSync(reducerIndexPath, {encoding: "utf8"}).split("\n"); const reducerLine = `export { default as ${name} } from "./${name}"`; @@ -92,13 +92,13 @@ module.exports = async function (command, name) { logger.success(`${highlight(name)} removed from reducer index ${filename(reducerIndexPath)}`); } catch (e) { - logger.error(`Unable to modify reducers index. Reducer not removed from index`); + logger.error("Unable to modify reducers index. Reducer not removed from index"); } } // Remove the test file - var testPath = path.join(process.cwd(), "/test/", availableCommands[command], `/${name}.test.js`); - var testFileExists = true; + const testPath = path.join(process.cwd(), "/test/", availableCommands[command], `/${name}.test.js`); + let testFileExists = true; try { fs.statSync(testPath); } diff --git a/src/commands/dockerize.js b/src/commands/dockerize.js index f3ed525a5..ad67b74a5 100644 --- a/src/commands/dockerize.js +++ b/src/commands/dockerize.js @@ -1,10 +1,9 @@ import { spawn } from "cross-spawn"; import path from "path"; import process from "process"; -import fs from "fs"; module.exports = function (name) { - const { version: gluestickVersion } = JSON.parse(fs.readFileSync(path.join(__dirname, "../../package.json"), "utf-8")); + // @TODO: check if docker is installed first (https://github.com/TrueCar/gluestick/issues/128) spawn("docker", ["build", "-t", name, "-f", path.join(process.cwd(), "src", "config", ".Dockerfile"), process.cwd()], {stdio: "inherit"}); }; diff --git a/src/commands/generate.js b/src/commands/generate.js index d3f85e758..2a1657d52 100644 --- a/src/commands/generate.js +++ b/src/commands/generate.js @@ -1,11 +1,11 @@ -var fs = require("fs"); -var path = require("path"); -var mkdirp = require("mkdirp"); +const fs = require("fs"); +const path = require("path"); +const mkdirp = require("mkdirp"); const logger = require("../lib/logger"); const logsColorScheme = require("../lib/logsColorScheme"); const { highlight, filename } = logsColorScheme; -var availableCommands = { +const availableCommands = { "container": "containers", "component": "components", "reducer": "reducers" @@ -16,7 +16,7 @@ function replaceName (input, name) { } module.exports = function (command, name, cb) { - var CWD = process.cwd(); + const CWD = process.cwd(); // Validate the command type by verifying that it exists in `availableCommands` if (!availableCommands[command]) { @@ -26,7 +26,7 @@ module.exports = function (command, name, cb) { // Validate the name by stripping out unwanted characters if (!name || name.length === 0) { - return cb(`invalid arguments. You must specify a name.`); + return cb("invalid arguments. You must specify a name."); } if (/\W/.test(name)) { @@ -42,9 +42,9 @@ module.exports = function (command, name, cb) { } // Get the output string and destination filename - var template; + let template; try { - template = fs.readFileSync(path.resolve(__dirname, `../../generate/${command}.js`), {encoding: "utf8"}) + template = fs.readFileSync(path.resolve(__dirname, `../../templates/generate/${command}.js`), {encoding: "utf8"}); } catch(e) { return cb("Couldn't read generator file"); @@ -53,8 +53,8 @@ module.exports = function (command, name, cb) { template = replaceName(template, name); // Check if the file already exists before we write to it - var destinationPath = path.join(CWD, "/src/", availableCommands[command] + "/" + name + ".js"); - var fileExists = true; + const destinationPath = path.join(CWD, "/src/", availableCommands[command] + "/" + name + ".js"); + let fileExists = true; try { fs.statSync(destinationPath); } @@ -72,31 +72,31 @@ module.exports = function (command, name, cb) { // If we just generated a reducer, add it to the reducers index if (command === "reducer") { - var reducerIndexPath = path.resolve(CWD, "src/reducers/index.js"); + const reducerIndexPath = path.resolve(CWD, "src/reducers/index.js"); try { // Get the file contents, but strip off any trailing whitespace. This sets us up // to place the new export on the last line, followed by a blank whitespace line at the end - var indexFileContents = fs.readFileSync(reducerIndexPath, {encoding: "utf8"}).replace(/\s*$/, ""); - var newLine = `export { default as ${name} } from "./${name}";`; + const indexFileContents = fs.readFileSync(reducerIndexPath, {encoding: "utf8"}).replace(/\s*$/, ""); + const newLine = `export { default as ${name} } from "./${name}";`; // Write back to the index file with the previous contents in addition to our new line and a blank line for git fs.writeFileSync(reducerIndexPath, `${indexFileContents}\n${newLine}\n\n`); logger.success(`${highlight(name)} added to reducer index ${filename(reducerIndexPath)}`); } catch (e) { - return cb(`Unable to modify reducers index. Reducer not added to index`); + return cb("Unable to modify reducers index. Reducer not added to index"); } } // Write test file - var testFolder = path.join(CWD, "/test/", availableCommands[command]); + const testFolder = path.join(CWD, "/test/", availableCommands[command]); // Older generated projects don't have test/reducers or test/containers mkdirp.sync(testFolder); - var testPath = path.join(testFolder, `/${name}.test.js`); - var testTemplate; - var testFileExists = true; + const testPath = path.join(testFolder, `/${name}.test.js`); + let testTemplate; + let testFileExists = true; try { fs.statSync(testPath); @@ -110,17 +110,17 @@ module.exports = function (command, name, cb) { } try { - testTemplate = fs.readFileSync(path.resolve(__dirname, `../../generate/${command}.test.js`), {encoding: "utf8"}) + testTemplate = fs.readFileSync(path.resolve(__dirname, `../../templates/generate/${command}.test.js`), {encoding: "utf8"}); } catch (e) { - return cb(`Couldn't read generator file`); + return cb("Couldn't read generator file"); } try { fs.writeFileSync(testPath, replaceName(testTemplate, name)); } catch (e) { - return cb(`Couldn't create test file`); + return cb("Couldn't create test file"); } logger.success(`New file created: ${filename(testPath)}`); diff --git a/src/commands/new.js b/src/commands/new.js index 097c098b1..14b00e15f 100644 --- a/src/commands/new.js +++ b/src/commands/new.js @@ -2,7 +2,7 @@ const process = require("process"); const inquirer = require("inquirer"); const fs = require("fs-extra"); const path = require("path"); -const npmDependencies = require("../lib/npm-dependencies"); +const npmDependencies = require("../lib/npmDependencies"); const utils = require("../lib/utils"); const { isGluestickProject } = utils; const logger = require("../lib/logger"); @@ -10,7 +10,7 @@ const logsColorScheme = require("../lib/logsColorScheme"); const { highlight, filename } = logsColorScheme; function copyTo (destination) { - fs.copySync(path.join(__dirname, "../../new"), destination); + fs.copySync(path.join(__dirname, "../../templates/new"), destination); process.chdir(destination); npmDependencies.install(); @@ -38,7 +38,7 @@ module.exports = function (projectName) { message: "Do you wish to continue?" }; inquirer.prompt([question]).then(function (answers) { - if (!answers.confirm) return; + if (!answers.confirm) { return; } copyTo(process.cwd()); return false; }); diff --git a/src/commands/start-client.js b/src/commands/start-client.js index 91d9265d8..8121a5d6e 100644 --- a/src/commands/start-client.js +++ b/src/commands/start-client.js @@ -1,37 +1,37 @@ -var path = require("path"); -var webpack = require("webpack"); -var process = require("process"); -var express = require("express"); -var proxy = require("express-http-proxy"); -var WebpackIsomorphicToolsPlugin = require("webpack-isomorphic-tools/plugin"); -var shared = require("../lib/shared"); -var detectEnvironmentVariables = require("../lib/detectEnvironmentVariables"); -var getWebpackAdditions = require("../lib/get-webpack-additions").default; -var { additionalLoaders, additionalPreLoaders } = getWebpackAdditions(); -var assetPath = require(path.join(process.cwd(), "src", "config", "application")).default.assetPath; +const path = require("path"); +const webpack = require("webpack"); +const process = require("process"); +const express = require("express"); +const proxy = require("express-http-proxy"); +const WebpackIsomorphicToolsPlugin = require("webpack-isomorphic-tools/plugin"); +const webpackSharedConfig = require("../config/webpack-shared-config"); +const detectEnvironmentVariables = require("../lib/detectEnvironmentVariables"); +const getWebpackAdditions = require("../lib/getWebpackAdditions").default; +const { additionalLoaders, additionalPreLoaders } = getWebpackAdditions(); const logger = require("../lib/logger"); const logsColorScheme = require("../lib/logsColorScheme"); +let assetPath = require(path.join(process.cwd(), "src", "config", "application")).default.assetPath; if (assetPath.substr(-1) !== "/") { assetPath = assetPath + "/"; } -var PORT = 8888; -var OUTPUT_PATH = path.join(process.cwd(), "build"); -var OUTPUT_FILE = "main-bundle.js"; -var PUBLIC_PATH = assetPath; +const PORT = 8888; +const OUTPUT_PATH = path.join(process.cwd(), "build"); +const OUTPUT_FILE = "main-bundle.js"; +const PUBLIC_PATH = assetPath; -var webpackIsomorphicToolsPlugin = new WebpackIsomorphicToolsPlugin(require('../lib/webpack-isomorphic-tools-configuration')) +const webpackIsomorphicToolsPlugin = new WebpackIsomorphicToolsPlugin(require("../config/webpack-isomorphic-tools-config")) .development(process.env.NODE_ENV !== "production"); process.env.NODE_PATH = path.join(__dirname, "../.."); const isProduction = process.env.NODE_ENV === "production"; -var entry = [ +const entry = [ path.join(__dirname, "../entrypoints/client.js") ]; -var environmentPlugins = []; +let environmentPlugins = []; if (isProduction) { environmentPlugins = environmentPlugins.concat([ @@ -58,29 +58,29 @@ if (!isProduction) { // The config/application.js file is consumed on both the server side and the // client side. However, we want developers to have access to environment -// variables in there so they can override defaults with an environment -// variable. For that reason we are going to perform static analysis on that -// file to determine all of the environment variables that are used in that +// constiables in there so they can override defaults with an environment +// constiable. For that reason we are going to perform static analysis on that +// file to determine all of the environment constiables that are used in that // file and make sure that webpack makes those available in the application. -var configEnvVariables = detectEnvironmentVariables(path.join(process.cwd(), "src", "config", "application.js")); +const configEnvVariables = detectEnvironmentVariables(path.join(process.cwd(), "src", "config", "application.js")); configEnvVariables.push("NODE_ENV"); -var exposedEnvironmentVariables = {}; +const exposedEnvironmentVariables = {}; configEnvVariables.forEach((v) => { exposedEnvironmentVariables[v] = JSON.stringify(process.env[v]); }); -var compiler = webpack({ +const compiler = webpack({ context: process.cwd(), - devtool: isProduction ? undefined : "cheap-module-eval-source-map", + devtool: isProduction ? null : "cheap-module-eval-source-map", entry: { "main": entry }, module: { loaders: [ - ].concat(shared.loaders, additionalLoaders), + ].concat(webpackSharedConfig.loaders, additionalLoaders), preLoaders: [ // only place client specific preLoaders here - ].concat(shared.preLoaders, additionalPreLoaders), + ].concat(webpackSharedConfig.preLoaders, additionalPreLoaders), }, output: { path: OUTPUT_PATH, @@ -97,28 +97,28 @@ var compiler = webpack({ "process.env": exposedEnvironmentVariables }), new webpack.IgnorePlugin(/\.server(\.js)?$/) - ].concat(environmentPlugins, shared.plugins), + ].concat(environmentPlugins, webpackSharedConfig.plugins), resolve: { - ...shared.resolve + ...webpackSharedConfig.resolve } }); module.exports = function (buildOnly) { if (!buildOnly && !isProduction) { - var app = express(); + const app = express(); app.use(require("webpack-dev-middleware")(compiler, { noInfo: true, publicPath: PUBLIC_PATH })); app.use(require("webpack-hot-middleware")(compiler)); app.use(proxy("localhost:8880", { - forwardPath: function (req, res) { + forwardPath: function (req) { return require("url").parse(req.url).path; }, preserveHostHdr: true })); app.use(function(err, req, res, next) { - if (err && err.code == "ECONNREFUSED") { + if (err && err.code === "ECONNREFUSED") { // render a friendly loading page during server restarts res.status(200).sendFile("poll.html", {root: path.join(__dirname, "../lib")}); } @@ -128,7 +128,7 @@ module.exports = function (buildOnly) { }); app.listen(PORT, "localhost", function (error) { if (error) { - console.log(error); + logger.error(error); return; } @@ -146,7 +146,7 @@ module.exports = function (buildOnly) { return; } - logger.success(`Assets have been prepared for production.`); + logger.success("Assets have been prepared for production."); logger.success(`Assets can be served from the ${logsColorScheme.filename("/assets")} route but it is recommended that you serve the generated ${logsColorScheme.filename("build")} folder from a Content Delivery Network`); }); } diff --git a/src/commands/start-server.js b/src/commands/start-server.js index 0d560251a..f590f76a8 100644 --- a/src/commands/start-server.js +++ b/src/commands/start-server.js @@ -3,7 +3,6 @@ import process from "process"; import { spawn } from "cross-spawn"; import pm2 from "pm2"; import sha1 from "sha1"; -import fs from "fs"; import logger from "../lib/logger"; import { highlight } from "../lib/logsColorScheme"; @@ -22,13 +21,13 @@ const CWD = process.cwd(); * @param {Boolean} debug whether or not to use node-inspector for debugging */ module.exports = function startServer (debug=false) { - const scriptPath = path.join(__dirname, "../lib/", "server-side-rendering.js"); + const scriptPath = path.join(__dirname, "../entrypoints/", "server.js"); // If debug mode is enabled, we do not use PM2, instead we spawn `node-debug` for the server side rendering if (debug) { const debugSpawn = spawn(path.join(CWD, "node_modules", ".bin", "node-debug"), [scriptPath], {stdio: "inherit", env: Object.assign({}, process.env, {NODE_ENV: "development-server"})}); debugSpawn.on("error", (e) => { - console.log("Error: ", e); + logger.error(e); }); return; } @@ -40,7 +39,7 @@ module.exports = function startServer (debug=false) { logger.info("Server rendering server started with PM2"); pm2.connect((error) => { if (error) { - console.error(error); + logger.error(error); pm2.disconnect(); process.exit(2); } @@ -56,9 +55,9 @@ module.exports = function startServer (debug=false) { no_autorestart: false, merge_logs: true, watch: process.env.NODE_ENV !== "production" - }, (error, a) => { + }, (error) => { if (error) { - console.error(error); + logger.error(error); pm2.disconnect(); } @@ -71,8 +70,6 @@ module.exports = function startServer (debug=false) { * started up because of PM2 and we terminate them. */ process.on("SIGINT", () => { - const app_cwd = CWD; - logger.info(`Stopping pm2 instance: ${highlight(name)}…`); pm2.delete(name, () => { pm2.disconnect(() => { @@ -81,5 +78,5 @@ module.exports = function startServer (debug=false) { }); }); }); -} +}; diff --git a/src/commands/test.js b/src/commands/test.js index 90711ef5f..0be361aab 100644 --- a/src/commands/test.js +++ b/src/commands/test.js @@ -1,7 +1,7 @@ -var karma = require("karma"); -var Server = karma.Server; -var runner = karma.runner; -var karmaConfig = require("../lib/karmaConfig").default; +const karma = require("karma"); +const Server = karma.Server; +const runner = karma.runner; +const karmaConfig = require("../config/karma-config").default; const config = karmaConfig; diff --git a/src/lib/karmaConfig.js b/src/config/karma-config.js similarity index 79% rename from src/lib/karmaConfig.js rename to src/config/karma-config.js index 3dff9e35c..c76b0a7d6 100644 --- a/src/lib/karmaConfig.js +++ b/src/config/karma-config.js @@ -1,18 +1,18 @@ const path = require("path"); const process = require("process"); -const shared = require("./shared"); const webpack = require("webpack"); const WebpackIsomorphicToolsPlugin = require("webpack-isomorphic-tools/plugin"); -const getWebpackAdditions = require("../lib/get-webpack-additions").default; +const webpackSharedConfig = require("./webpack-shared-config"); +const getWebpackAdditions = require("../lib/getWebpackAdditions").default; const { additionalLoaders, additionalPreLoaders } = getWebpackAdditions(); const PORT = 9876; const CWD = process.cwd(); -const webpackIsomorphicToolsPlugin = new WebpackIsomorphicToolsPlugin(require("../lib/webpack-isomorphic-tools-configuration")).development(true); +const webpackIsomorphicToolsPlugin = new WebpackIsomorphicToolsPlugin(require("./webpack-isomorphic-tools-config")).development(true); const preprocessors = {}; -const helperPath = path.resolve(__dirname, "./test-helper.js"); +const helperPath = path.resolve(__dirname, "../lib/testHelper.js"); preprocessors[helperPath] = ["webpack", "sourcemap"]; export default { @@ -33,7 +33,7 @@ export default { test: webpackIsomorphicToolsPlugin.regular_expression("styles"), loader: "file-loader" }, - ].concat(shared.loaders).concat( + ].concat(webpackSharedConfig.loaders).concat( // babel-istanbul must be defined after the babel loader { test: /\.js$/, @@ -44,7 +44,7 @@ export default { ).concat(additionalLoaders), preLoaders: [ // only place test specific preLoaders here - ].concat(shared.preLoaders, additionalPreLoaders), + ].concat(webpackSharedConfig.preLoaders, additionalPreLoaders), }, node: { fs: "empty" @@ -54,11 +54,11 @@ export default { "TEST_PATH": JSON.stringify(path.join(CWD, "test")), "SRC_PATH": JSON.stringify(path.join(CWD, "src")) }) - ].concat(shared.plugins), + ].concat(webpackSharedConfig.plugins), resolve: { - ...shared.resolve, + ...webpackSharedConfig.resolve, alias: { - ...shared.resolve.alias, + ...webpackSharedConfig.resolve.alias, colors: path.resolve(CWD, "src/config/colors") }, root: [ diff --git a/src/lib/webpack-isomorphic-tools-configuration.js b/src/config/webpack-isomorphic-tools-config.js similarity index 87% rename from src/lib/webpack-isomorphic-tools-configuration.js rename to src/config/webpack-isomorphic-tools-config.js index f9d0d68e0..afdac4dca 100644 --- a/src/lib/webpack-isomorphic-tools-configuration.js +++ b/src/config/webpack-isomorphic-tools-config.js @@ -1,20 +1,20 @@ -import logger from "./logger"; -import { highlight } from "./logsColorScheme"; +import logger from "../lib/logger"; +import { highlight } from "../lib/logsColorScheme"; const path = require("path"); const process = require("process"); const WebpackIsomorphicToolsPlugin = require("webpack-isomorphic-tools/plugin"); -// We use `get-webpack-additions` to get the data back because it will first +// We use `getWebpackAdditions` to get the data back because it will first // check if the file exists before trying to include it, falling back to empty -// arrays for defaults. Requiring get-webpack-additions will return a function +// arrays for defaults. Requiring getWebpackAdditions will return a function // instead of the result so we can execute the check asynchronously vs making // it part of the initial build. If it were a simple require it would throw an // error when babel or webpack try to resolve missing dependencies. We then // call this method with `true` as the first and only argument because that is // how we tell the method we are requiring for the purpose of the isomorphic // tools, not the webpack config file. -const { additionalLoaders, additionalPreLoaders } = require("./get-webpack-additions").default(true); +const { additionalLoaders, additionalPreLoaders } = require("../lib/getWebpackAdditions").default(true); const userExtensions = []; [...additionalLoaders, ...additionalPreLoaders].forEach((loader) => { diff --git a/src/lib/shared.js b/src/config/webpack-shared-config.js similarity index 73% rename from src/lib/shared.js rename to src/config/webpack-shared-config.js index 5ca1dc1e9..18895111f 100644 --- a/src/lib/shared.js +++ b/src/config/webpack-shared-config.js @@ -1,10 +1,12 @@ -var path = require("path"); -var process = require("process"); -var WebpackIsomorphicToolsPlugin = require("webpack-isomorphic-tools/plugin"); -var ExtractTextPlugin = require("extract-text-webpack-plugin"); +// Webpack configuration that is shared between the client and tests +import path from "path"; +import process from "process"; + +const WebpackIsomorphicToolsPlugin = require("webpack-isomorphic-tools/plugin"); +const ExtractTextPlugin = require("extract-text-webpack-plugin"); const isProduction = process.env.NODE_ENV === "production"; -var webpackIsomorphicToolsPlugin = new WebpackIsomorphicToolsPlugin(require('../lib/webpack-isomorphic-tools-configuration')) +const webpackIsomorphicToolsPlugin = new WebpackIsomorphicToolsPlugin(require("../config/webpack-isomorphic-tools-config")) .development(process.env.NODE_ENV !== "production"); module.exports = { @@ -24,15 +26,14 @@ module.exports = { ], presets: [ "react", - "es2015", + "es2015", "stage-0" ] }, include: [ path.join(process.cwd(), "Index.js"), path.join(process.cwd(), "src"), - path.join(process.cwd(), "test"), - path.join(__dirname, "../shared") + path.join(process.cwd(), "test") ] }, { diff --git a/src/entrypoints/client.js b/src/entrypoints/client.js index 6de9f480d..dd0226d5b 100644 --- a/src/entrypoints/client.js +++ b/src/entrypoints/client.js @@ -1,7 +1,10 @@ +/*global __PATH_TO_ENTRY__*/ +/*global __webpack_public_path__*/ +/*exported __webpack_public_path__*/ require("match-media/matchMedia.js"); require("match-media/matchMedia.addListener.js"); require("babel-polyfill"); __webpack_public_path__ = window.__GS_PUBLIC_PATH__; -var Entry = require(__PATH_TO_ENTRY__).default; +const Entry = require(__PATH_TO_ENTRY__).default; Entry.start(); diff --git a/src/entrypoints/server.js b/src/entrypoints/server.js new file mode 100644 index 000000000..e9b9e3d44 --- /dev/null +++ b/src/entrypoints/server.js @@ -0,0 +1,10 @@ +require("../lib/runThroughBabel"); +const WebpackIsomorphicTools = require("webpack-isomorphic-tools"); + +(function () { + global.webpackIsomorphicTools = new WebpackIsomorphicTools(require("../config/webpack-isomorphic-tools-config")) + .development(process.NODE_ENV !== "production") + .server(process.cwd(), function () { + require("../lib/server/index.js"); + }); +})(); diff --git a/src/lib/detectEnvironmentVariables.js b/src/lib/detectEnvironmentVariables.js index e602901c6..d48d951bd 100644 --- a/src/lib/detectEnvironmentVariables.js +++ b/src/lib/detectEnvironmentVariables.js @@ -1,5 +1,6 @@ -var babel = require("babel-core"); -var traverse = require("babel-traverse").default; +const babel = require("babel-core"); +const traverse = require("babel-traverse").default; +const logger = require("./logger"); /** * Read the contents of a file, convert it to an abstract syntax tree and @@ -9,18 +10,21 @@ var traverse = require("babel-traverse").default; * @param {String} pathToFile absolute path to the file to analyze */ module.exports = function detectEnvironmentVariables (pathToFile) { - var ast = babel.transformFileSync(pathToFile).ast; - var environmentVariables = []; + const ast = babel.transformFileSync(pathToFile).ast; + const environmentVariables = []; traverse(ast.program, { enter: function (path) { if (path.type === "MemberExpression") { const node = path.node; try { - if (node.object.object.name === "process" && node.object.property.name === "env") { + const target = node.object; + if (!!target.object && target.object.name === "process" && target.property.name === "env") { environmentVariables.push(node.property.name); } } - catch (e) {} + catch (e) { + logger.warn("Error detecting environment variables:", e); + } } } }); diff --git a/src/lib/get-webpack-additions.js b/src/lib/getWebpackAdditions.js similarity index 87% rename from src/lib/get-webpack-additions.js rename to src/lib/getWebpackAdditions.js index 5c637d3c2..79612f8bf 100644 --- a/src/lib/get-webpack-additions.js +++ b/src/lib/getWebpackAdditions.js @@ -2,6 +2,7 @@ import fs from "fs"; import process from "process"; import path from "path"; import WebpackIsomorphicToolsPlugin from "webpack-isomorphic-tools/plugin"; +import logger from "./logger"; /** * GlueStick uses webpack-isomorphic-tools to support server side rendering. @@ -21,19 +22,19 @@ import WebpackIsomorphicToolsPlugin from "webpack-isomorphic-tools/plugin"; */ function prepareUserAdditionsForWebpack (additions) { return additions.map((addition) => { - let test = addition.test && toString.call(addition.test) === "[object RegExp]" ? + const test = addition.test && toString.call(addition.test) === "[object RegExp]" ? addition.test : WebpackIsomorphicToolsPlugin.regular_expression(addition.extensions); - let webpackAddition = { + const webpackAddition = { loader: addition.loader, test: test }; - ['include', 'exclude', 'query'].forEach((optionalAddition) => { + ["include", "exclude", "query"].forEach((optionalAddition) => { if (addition[optionalAddition]) { webpackAddition[optionalAddition] = addition[optionalAddition]; } }); - return webpackAddition + return webpackAddition; }); } @@ -55,7 +56,9 @@ export default function (isomorphic=false) { additionalPreLoaders: isomorphic ? additionalPreLoaders : prepareUserAdditionsForWebpack(additionalPreLoaders) }; } - catch (e) {} + catch (e) { + logger.warn("Error getting webpack additions:", e); + } return userAdditions; } diff --git a/src/lib/help-text.js b/src/lib/help-text.js deleted file mode 100644 index 83b80fdcd..000000000 --- a/src/lib/help-text.js +++ /dev/null @@ -1,28 +0,0 @@ -import chalk from "chalk"; -import logger from "./logger" - -export const MISSING_404_TEXT = "MISSING_404_TEXT"; - -const helpTextHandlers = { - [MISSING_404_TEXT]: showMissing404Text -}; - -export default function showHelpText (key) { - const bar = "#########################################################################"; - logger.info("\n" + bar); - helpTextHandlers[key](); - logger.info(bar + "\n"); -} - -function showMissing404Text () { - logger.info("Rendered a 404 Error but no custom 404 route detected."); - console.log("You can create a custom 404 handler with the following steps:"); - console.log("1. Create a new container that you would like to use when rendering 404 errors."); - console.log(` ${chalk.cyan('gluestick generate container RouteNotFound')}`); - console.log("2. Import this new container in the routes file along with the \`ROUTE_NAME_404_NOT_FOUND\` constant."); - console.log(` ${chalk.cyan('import RouteNotFound from "../containers/RouteNotFound";')}`); - console.log(` ${chalk.cyan('import { ROUTE_NAME_404_NOT_FOUND } from "gluestick-shared";')}`); - console.log("3. Add a new route that uses this constant and container as your very last route."); - console.log(" " + chalk.cyan(``)); -} - diff --git a/src/lib/helpText.js b/src/lib/helpText.js new file mode 100644 index 000000000..03646b754 --- /dev/null +++ b/src/lib/helpText.js @@ -0,0 +1,28 @@ +import logger from "./logger"; +import { filename, info } from "./logsColorScheme"; + +export const MISSING_404_TEXT = "MISSING_404_TEXT"; + +const helpTextHandlers = { + [MISSING_404_TEXT]: showMissing404Text +}; + +export default function showHelpText (key) { + const bar = "#########################################################################"; + logger.info("\n" + bar); + helpTextHandlers[key](); + logger.info(bar + "\n"); +} + +function showMissing404Text () { + logger.info("Rendered a 404 Error but no custom 404 route detected."); + logger.info("You can create a custom 404 handler with the following steps:"); + logger.info("1. Create a new container that you would like to use when rendering 404 errors."); + logger.info(` ${filename("gluestick generate container RouteNotFound")}`); + logger.info("2. Import this new container in the routes file along with the \`ROUTE_NAME_404_NOT_FOUND\` constant."); + logger.info(` ${filename("import RouteNotFound from \"../containers/RouteNotFound\";")}`); + logger.info(` ${filename("import { ROUTE_NAME_404_NOT_FOUND } from \"gluestick-shared\";")}`); + logger.info("3. Add a new route that uses this constant and container as your very last route."); + logger.info(" " + filename(``)); +} + diff --git a/src/lib/logger.js b/src/lib/logger.js index 4e0ce53a2..509029fdb 100644 --- a/src/lib/logger.js +++ b/src/lib/logger.js @@ -1,19 +1,20 @@ -var logsColorScheme = require("./logsColorScheme"); +/*eslint-disable no-console*/ +const logsColorScheme = require("./logsColorScheme"); -function success(text){ - console.log(logsColorScheme.success(text)); +function success(...args){ + console.log(...args.map(arg => logsColorScheme.success(arg))); } -function info(text){ - console.log(logsColorScheme.info(text)); +function info(...args){ + console.log(...args.map(arg => logsColorScheme.info(arg))); } -function warn(text){ - console.log(logsColorScheme.warn(text)); +function warn(...args) { + console.log(...args.map(arg => logsColorScheme.warn(arg))); } -function error(text){ - warn("ERROR: " + text); +function error(...args){ + warn("ERROR:", ...args); } module.exports = { diff --git a/src/lib/logsColorScheme.js b/src/lib/logsColorScheme.js index 5c5a3f452..c864ad5c4 100644 --- a/src/lib/logsColorScheme.js +++ b/src/lib/logsColorScheme.js @@ -1,4 +1,4 @@ -var chalk = require("chalk"); +import chalk from "chalk"; const success = chalk.green; const info = chalk.yellow; diff --git a/src/lib/npm-dependencies.js b/src/lib/npmDependencies.js similarity index 82% rename from src/lib/npm-dependencies.js rename to src/lib/npmDependencies.js index 3cec2be18..3d6ba6f6a 100644 --- a/src/lib/npm-dependencies.js +++ b/src/lib/npmDependencies.js @@ -3,10 +3,10 @@ const spawn = require("cross-spawn").spawn; const IS_WINDOWS = process.platform === "win32"; function install () { - var postFix = IS_WINDOWS ? ".cmd" : ""; + const postFix = IS_WINDOWS ? ".cmd" : ""; spawn("npm" + postFix, ["install"], {stdio: "inherit"}); } module.exports = { install: install -} +}; diff --git a/src/run-through-babel.js b/src/lib/runThroughBabel.js similarity index 68% rename from src/run-through-babel.js rename to src/lib/runThroughBabel.js index 17b03fe8b..0b2234e41 100644 --- a/src/run-through-babel.js +++ b/src/lib/runThroughBabel.js @@ -3,21 +3,21 @@ require("babel-core/register")({ presets: [ "react", "es2015", - "stage-0" + "stage-0" ], plugins: [ "transform-decorators-legacy" ], ignore: function(filename) { - var node_modules = "node_modules"; + const node_modules = "node_modules"; // We don't use path.join because the filename slashes use the Unix style // forward slashes on unix and on windows but path.join will give us the // filenames based on the system. This way we force forward slashes and it // works on Windows and Unix - var gluestick_folder = [node_modules, "gluestick", "src"].join("/"); + const gluestick_folder = [node_modules, "gluestick", "src"].join("/"); - // Make sure babel does not ignore - if (filename.includes(gluestick_folder)) return false; + // Make sure babel does not ignore + if (filename.includes(gluestick_folder)) { return false; } return filename.includes(node_modules); } diff --git a/src/lib/server-side-rendering.js b/src/lib/server-side-rendering.js deleted file mode 100644 index ff625b5ca..000000000 --- a/src/lib/server-side-rendering.js +++ /dev/null @@ -1,39 +0,0 @@ -require("../run-through-babel"); - -var express = require("express"); -var logger = require("./logger"); -var logsColorScheme = require("./logsColorScheme"); -var filename = logsColorScheme.filename; - -var WebpackIsomorphicTools = require("webpack-isomorphic-tools"); -var projectBasePath = process.cwd(); - -var isProduction = process.env.NODE_ENV === "production"; - -var PORT = process.env.PORT || (isProduction ? 8888 : 8880); - -(function () { - global.webpackIsomorphicTools = new WebpackIsomorphicTools(require("./webpack-isomorphic-tools-configuration")) - .development(process.NODE_ENV !== "production") - .server(process.cwd(), function () { - var app = express(); - var serverRequestHandler = require("../lib/server-request-handler"); - - if (isProduction) { - app.use("/assets", express.static("build")); - logger.success(`Server side rendering server running at ${filename("http://localhost:" + PORT)}`); - } - else { - app.get("/gluestick-proxy-poll", function(req, res) { - // allow requests from our client side loading page - res.header("Access-Control-Allow-Origin", "*"); - res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); - res.status(200).json({up: true}); - }); - logger.success(`Server side rendering proxy running at ${filename("http://localhost:" + PORT)}`); - } - - app.use(serverRequestHandler); - app.listen(PORT); - }); -})(); diff --git a/src/shared/components/Body.js b/src/lib/server/Body.js similarity index 69% rename from src/shared/components/Body.js rename to src/lib/server/Body.js index 8972d9bf5..d24baa79e 100644 --- a/src/shared/components/Body.js +++ b/src/lib/server/Body.js @@ -1,7 +1,14 @@ -import React, { Component } from "react"; +/*eslint-disable react/no-danger*/ +import React, { Component, PropTypes} from "react"; import serialize from "serialize-javascript"; export default class Body extends Component { + static propTypes = { + config: PropTypes.object.isRequired, + html: PropTypes.string.isRequired, + initialState: PropTypes.any.isRequired + }; + render () { const { initialState, diff --git a/src/lib/server-error-handler.js b/src/lib/server/errorHandler.js similarity index 92% rename from src/lib/server-error-handler.js rename to src/lib/server/errorHandler.js index 41edd9d96..025d7ff04 100644 --- a/src/lib/server-error-handler.js +++ b/src/lib/server/errorHandler.js @@ -2,7 +2,7 @@ import fs from "fs"; import path from "path"; import PrettyError from "pretty-error"; import * as secureHandlebars from "secure-handlebars"; -import logger from "./logger"; +import logger from "../logger"; const pretty = new PrettyError(); @@ -30,7 +30,7 @@ secureHandlebars.registerHelper("notForProduction", function (options) { export default function serverErrorHandler(req, res, error) { res.status(500); const custom505FilePath = path.join(process.cwd(), "505.hbs"); - console.error('SERVER ERROR:', pretty.render(error)); + logger.error(pretty.render(error)); // Check if we have a custom 505 error page fs.stat(custom505FilePath, (statError, stats) => { @@ -43,7 +43,7 @@ export default function serverErrorHandler(req, res, error) { // If we do have a custom 505 page, render it fs.readFile(custom505FilePath, "utf8", (readFileError, data) => { - if (readFileError) return res.send({error: readFileError}); + if (readFileError) { return res.send({error: readFileError}); } // We use secure-handlebars to deliver the 505 page. This lets you // use a handlebars template so you can display the stack trace or diff --git a/src/shared/components/getHead.js b/src/lib/server/getHead.js similarity index 86% rename from src/shared/components/getHead.js rename to src/lib/server/getHead.js index fd74e691d..c46c6b54b 100644 --- a/src/shared/components/getHead.js +++ b/src/lib/server/getHead.js @@ -1,10 +1,11 @@ -import React, { Component } from "react"; +/*eslint-disable react/no-danger*/ +import React from "react"; import serialize from "serialize-javascript"; import path from "path"; import process from "process"; // Make sure path ends in forward slash -var assetPath = require(path.resolve(path.join(process.cwd(), "src", "config", "application"))).default.assetPath; +let assetPath = require(path.resolve(path.join(process.cwd(), "src", "config", "application"))).default.assetPath; if (assetPath.substr(-1) !== "/") { assetPath = assetPath + "/"; } @@ -12,7 +13,7 @@ if (assetPath.substr(-1) !== "/") { const isProduction = process.env.NODE_ENV === "production"; export default (config, assets) => { - let tags = []; + const tags = []; let key = 0; if (isProduction) { @@ -21,7 +22,7 @@ export default (config, assets) => { else { // Resolve style flicker on page load in dev mode Object.keys(assets.assets).forEach(assetPath => { - if (!/\.(css|scss|sass|less)$/.test(assetPath)) return; + if (!/\.(css|scss|sass|less)$/.test(assetPath)) { return; } // webpack isomorphic tools converts `node_modules` to `~` in these // paths. This means any css files imported directly out of a node_module diff --git a/src/lib/server/index.js b/src/lib/server/index.js new file mode 100644 index 000000000..a4e8f6db9 --- /dev/null +++ b/src/lib/server/index.js @@ -0,0 +1,29 @@ +import express from "express"; +import logger from "../logger"; +import { filename } from "../logsColorScheme"; +import requestHandler from "./requestHandler"; + +const isProduction = process.env.NODE_ENV === "production"; +// @TODO: allow host and port to be set elsewhere (https://github.com/TrueCar/gluestick/issues/129) +const host = "localhost"; +const port = process.env.PORT || (isProduction? 8888 : 8880); +const address = `http://${host}:${port}`; + +const app = express(); + +if (isProduction) { + app.use("/assets", express.static("build")); + logger.success(`Server side rendering server running at ${filename(address)}`); +} +else { + app.get("/gluestick-proxy-poll", function(req, res) { + // allow requests from our client side loading page + res.header("Access-Control-Allow-Origin", "*"); + res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + res.status(200).json({up: true}); + }); + logger.success(`Server side rendering proxy running at ${filename(address)}`); +} + +app.use(requestHandler); +app.listen(port); diff --git a/src/lib/server-request-handler.js b/src/lib/server/requestHandler.js similarity index 85% rename from src/lib/server-request-handler.js rename to src/lib/server/requestHandler.js index 9464def7e..b99382f9c 100644 --- a/src/lib/server-request-handler.js +++ b/src/lib/server/requestHandler.js @@ -1,18 +1,18 @@ -import chalk from "chalk"; +/*global webpackIsomorphicTools*/ import path from "path"; import { createElement } from "react"; import { renderToString } from "react-dom/server"; import { runBeforeRoutes, ROUTE_NAME_404_NOT_FOUND, prepareRoutesWithTransitionHooks } from "gluestick-shared"; -import { match, RouterContext, Route } from "react-router"; -import showHelpText, { MISSING_404_TEXT } from "../lib/help-text"; -import logger from "./logger"; +import { match, RouterContext } from "react-router"; +import errorHandler from "./errorHandler"; +import Body from "./Body"; +import getHead from "./getHead"; -import serverErrorHandler from "./server-error-handler"; -import Body from "../shared/components/Body"; -import getHead from "../shared/components/getHead"; +import logger from "../logger"; +import showHelpText, { MISSING_404_TEXT } from "../../lib/helpText"; process.on("unhandledRejection", (reason, promise) => { - console.log(chalk.red(reason), promise); + logger.error(reason, promise); }); module.exports = async function (req, res) { @@ -24,7 +24,7 @@ module.exports = async function (req, res) { // @TODO: Remove this in the future when people have had enough time to // upgrade. When this deprecation notice and backward compatibility check - // are removed, remove from new/src/config/.entry as well + // are removed, remove from templates/new/src/config/.entry as well if (typeof originalRoutes !== "function") { logger.warn(` ########################################################################## @@ -32,7 +32,7 @@ Deprecation Notice: src/config/routes.js is expected to export a function that returns the routes object, not the routes object itself. This gives you access to the redux store so you can use it in async react-router methods. For a simple example see: -https://github.com/TrueCar/gluestick/blob/develop/new/src/config/routes.js +https://github.com/TrueCar/gluestick/blob/develop/templates/new/src/config/routes.js ########################################################################## `); originalRoutes = () => originalRoutes; @@ -43,7 +43,7 @@ https://github.com/TrueCar/gluestick/blob/develop/new/src/config/routes.js match({routes: routes, location: req.path}, async (error, redirectLocation, renderProps) => { try { if (error) { - serverErrorHandler(req, res, error); + errorHandler(req, res, error); } else if (redirectLocation) { res.redirect(302, redirectLocation.pathname + redirectLocation.search); @@ -65,7 +65,7 @@ https://github.com/TrueCar/gluestick/blob/develop/new/src/config/routes.js // grab the react generated body stuff. This includes the // script tag that hooks up the client side react code. const body = createElement(Body, {html: renderToString(main), config: config, initialState: store.getState()}); - const head = getHead(config, webpackIsomorphicTools.assets()); + const head = getHead(config, webpackIsomorphicTools.assets()); // eslint-disable-line webpackIsomorphicTools if (renderProps.routes[renderProps.routes.length - 1].name === ROUTE_NAME_404_NOT_FOUND) { res.status(404); @@ -90,12 +90,12 @@ https://github.com/TrueCar/gluestick/blob/develop/new/src/config/routes.js } } catch (error) { - serverErrorHandler(req, res, error); + errorHandler(req, res, error); } }); } catch (error) { - serverErrorHandler(req, res, error); + errorHandler(req, res, error); } }; diff --git a/src/lib/test-helper.js b/src/lib/test-helper.js deleted file mode 100644 index d535c9d6c..000000000 --- a/src/lib/test-helper.js +++ /dev/null @@ -1,15 +0,0 @@ -/* global global */ -require("babel-polyfill"); -var path = require("path"); -var React = require("react"); -var ReactDOM = require("react-dom"); -var ReactTestUtils = require("react-addons-test-utils"); -global.React = React; -global.ReactDOM = ReactDOM; -global.TestUtils = ReactTestUtils; - -var context = require.context(TEST_PATH, true, /\.test\.js$/); -context.keys().forEach(context); - -var srcContext = require.context(SRC_PATH, true, /\.js$/); -srcContext.keys().forEach(srcContext); diff --git a/src/lib/testHelper.js b/src/lib/testHelper.js new file mode 100644 index 000000000..01999bf18 --- /dev/null +++ b/src/lib/testHelper.js @@ -0,0 +1,15 @@ +/*global TEST_PATH*/ +/*global SRC_PATH*/ +require("babel-polyfill"); +const React = require("react"); +const ReactDOM = require("react-dom"); +const ReactTestUtils = require("react-addons-test-utils"); +global.React = React; +global.ReactDOM = ReactDOM; +global.TestUtils = ReactTestUtils; + +const context = require.context(TEST_PATH, true, /\.test\.js$/); +context.keys().forEach(context); + +const srcContext = require.context(SRC_PATH, true, /\.js$/); +srcContext.keys().forEach(srcContext); diff --git a/generate/component.js b/templates/generate/component.js similarity index 100% rename from generate/component.js rename to templates/generate/component.js diff --git a/generate/component.test.js b/templates/generate/component.test.js similarity index 82% rename from generate/component.test.js rename to templates/generate/component.test.js index cc005ec39..3b8e3ec70 100644 --- a/generate/component.test.js +++ b/templates/generate/component.test.js @@ -1,3 +1,6 @@ +/*global React*/ +/*global describe it*/ +/*global expect*/ import __$NAME__ from "components/__$NAME__"; import { shallow } from "enzyme"; diff --git a/generate/container.js b/templates/generate/container.js similarity index 100% rename from generate/container.js rename to templates/generate/container.js diff --git a/generate/container.test.js b/templates/generate/container.test.js similarity index 82% rename from generate/container.test.js rename to templates/generate/container.test.js index 9d8c869a5..9e15d83c6 100644 --- a/generate/container.test.js +++ b/templates/generate/container.test.js @@ -1,3 +1,6 @@ +/*global React*/ +/*global describe it*/ +/*global expect*/ import { __$NAME__ } from "containers/__$NAME__"; import { shallow } from "enzyme"; diff --git a/generate/reducer.js b/templates/generate/reducer.js similarity index 100% rename from generate/reducer.js rename to templates/generate/reducer.js diff --git a/generate/reducer.test.js b/templates/generate/reducer.test.js similarity index 84% rename from generate/reducer.test.js rename to templates/generate/reducer.test.js index add6f24d9..3011e46c4 100644 --- a/generate/reducer.test.js +++ b/templates/generate/reducer.test.js @@ -1,3 +1,5 @@ +/*global describe it*/ +/*global expect*/ import reducer from "reducers/__$NAME__"; describe("reducers/__$NAME__", () => { diff --git a/new/.babelrc b/templates/new/.babelrc similarity index 100% rename from new/.babelrc rename to templates/new/.babelrc diff --git a/new/.dockerignore b/templates/new/.dockerignore similarity index 100% rename from new/.dockerignore rename to templates/new/.dockerignore diff --git a/new/.eslintrc b/templates/new/.eslintrc similarity index 100% rename from new/.eslintrc rename to templates/new/.eslintrc diff --git a/new/.gluestick b/templates/new/.gluestick similarity index 100% rename from new/.gluestick rename to templates/new/.gluestick diff --git a/new/.npmrc b/templates/new/.npmrc similarity index 100% rename from new/.npmrc rename to templates/new/.npmrc diff --git a/new/505.hbs b/templates/new/505.hbs similarity index 100% rename from new/505.hbs rename to templates/new/505.hbs diff --git a/new/Index.js b/templates/new/Index.js similarity index 100% rename from new/Index.js rename to templates/new/Index.js diff --git a/new/_gitignore b/templates/new/_gitignore similarity index 100% rename from new/_gitignore rename to templates/new/_gitignore diff --git a/new/assets/.empty b/templates/new/assets/.empty similarity index 100% rename from new/assets/.empty rename to templates/new/assets/.empty diff --git a/new/assets/css/normalize.css b/templates/new/assets/css/normalize.css similarity index 100% rename from new/assets/css/normalize.css rename to templates/new/assets/css/normalize.css diff --git a/new/package.json b/templates/new/package.json similarity index 100% rename from new/package.json rename to templates/new/package.json diff --git a/new/src/actions/.empty b/templates/new/src/actions/.empty similarity index 100% rename from new/src/actions/.empty rename to templates/new/src/actions/.empty diff --git a/new/src/components/Home.js b/templates/new/src/components/Home.js similarity index 100% rename from new/src/components/Home.js rename to templates/new/src/components/Home.js diff --git a/new/src/components/MasterLayout.js b/templates/new/src/components/MasterLayout.js similarity index 100% rename from new/src/components/MasterLayout.js rename to templates/new/src/components/MasterLayout.js diff --git a/templates/new/src/config/.Dockerfile b/templates/new/src/config/.Dockerfile new file mode 100644 index 000000000..0777d5998 --- /dev/null +++ b/templates/new/src/config/.Dockerfile @@ -0,0 +1,9 @@ +# DO NOT MODIFY +# This file is automatically generated. You can copy this file and add a +# Dockerfile to the root of the project if you would like to use a custom +# docker setup. +FROM truecar/gluestick:0.4.0 + +ADD . /app + +RUN npm install diff --git a/new/src/config/.entry.js b/templates/new/src/config/.entry.js similarity index 100% rename from new/src/config/.entry.js rename to templates/new/src/config/.entry.js diff --git a/new/src/config/.store.js b/templates/new/src/config/.store.js similarity index 100% rename from new/src/config/.store.js rename to templates/new/src/config/.store.js diff --git a/new/src/config/application.js b/templates/new/src/config/application.js similarity index 100% rename from new/src/config/application.js rename to templates/new/src/config/application.js diff --git a/new/src/config/redux-middleware.js b/templates/new/src/config/redux-middleware.js similarity index 100% rename from new/src/config/redux-middleware.js rename to templates/new/src/config/redux-middleware.js diff --git a/new/src/config/routes.js b/templates/new/src/config/routes.js similarity index 100% rename from new/src/config/routes.js rename to templates/new/src/config/routes.js diff --git a/new/src/config/webpack-additions.js b/templates/new/src/config/webpack-additions.js similarity index 100% rename from new/src/config/webpack-additions.js rename to templates/new/src/config/webpack-additions.js diff --git a/new/src/containers/HomeApp.js b/templates/new/src/containers/HomeApp.js similarity index 100% rename from new/src/containers/HomeApp.js rename to templates/new/src/containers/HomeApp.js diff --git a/new/src/containers/NoMatchApp.js b/templates/new/src/containers/NoMatchApp.js similarity index 100% rename from new/src/containers/NoMatchApp.js rename to templates/new/src/containers/NoMatchApp.js diff --git a/new/src/reducers/index.js b/templates/new/src/reducers/index.js similarity index 100% rename from new/src/reducers/index.js rename to templates/new/src/reducers/index.js diff --git a/new/test/components/Home.test.js b/templates/new/test/components/Home.test.js similarity index 100% rename from new/test/components/Home.test.js rename to templates/new/test/components/Home.test.js diff --git a/new/test/components/MasterLayout.test.js b/templates/new/test/components/MasterLayout.test.js similarity index 100% rename from new/test/components/MasterLayout.test.js rename to templates/new/test/components/MasterLayout.test.js diff --git a/new/test/containers/.empty b/templates/new/test/containers/.empty similarity index 100% rename from new/test/containers/.empty rename to templates/new/test/containers/.empty diff --git a/new/test/containers/HomeApp.test.js b/templates/new/test/containers/HomeApp.test.js similarity index 100% rename from new/test/containers/HomeApp.test.js rename to templates/new/test/containers/HomeApp.test.js diff --git a/new/test/containers/NoMatchApp.test.js b/templates/new/test/containers/NoMatchApp.test.js similarity index 100% rename from new/test/containers/NoMatchApp.test.js rename to templates/new/test/containers/NoMatchApp.test.js diff --git a/new/test/reducers/.empty b/templates/new/test/reducers/.empty similarity index 100% rename from new/test/reducers/.empty rename to templates/new/test/reducers/.empty diff --git a/test/commands/destroy.test.js b/test/commands/destroy.test.js index 85c7dbe7f..3b0470ac2 100644 --- a/test/commands/destroy.test.js +++ b/test/commands/destroy.test.js @@ -1,3 +1,4 @@ +/*global afterEach beforeEach describe it*/ import sinon from "sinon"; import { expect } from "chai"; import fs from "fs"; @@ -6,6 +7,7 @@ import path from "path"; import temp from "temp"; import rimraf from "rimraf"; import mkdirp from "mkdirp"; +import logger from "../../src/lib/logger"; import destroy from "../../src/commands/destroy"; @@ -50,8 +52,7 @@ describe("cli: gluestick destroy", function () { createFiles(componentPath, componentTestPath, containerPath, containerTestPath, reducerPath, reducerTestPath); sandbox = sinon.sandbox.create(); - sandbox.spy(console, "log"); - sandbox.spy(console, "error"); + sandbox.spy(logger, "error"); }); afterEach(done => { @@ -62,22 +63,22 @@ describe("cli: gluestick destroy", function () { it("should throw an error when (container|component|reducer) is not specified", () => { destroy("blah",""); - expect(console.log.calledWithMatch("is not a valid destroy command.")).to.be.true; + expect(logger.error.calledWithMatch("is not a valid destroy command.")).to.be.true; }); it("should throw an error when a name not specified", () => { destroy("component",""); - expect(console.log.calledWithMatch("ERROR: invalid arguments. You must specify a name.")).to.be.true; + expect(logger.error.calledWithMatch("invalid arguments. You must specify a name.")).to.be.true; }); it("should throw an error when a name only consists of whitespace", () => { destroy("component"," "); - expect(console.log.calledWithMatch("is not a valid name.")).to.be.true; + expect(logger.error.calledWithMatch("is not a valid name.")).to.be.true; }); it("should throw an error when the specified name does not exist", () => { destroy("component","somethingthatdoesnotexist"); - expect(console.log.calledWithMatch("does not exist")).to.be.true; + expect(logger.error.calledWithMatch("does not exist")).to.be.true; }); it("should ask whether to continue if the name does not match (case sensitive)", done => { diff --git a/test/commands/generate.test.js b/test/commands/generate.test.js index 118363564..55dfc4416 100644 --- a/test/commands/generate.test.js +++ b/test/commands/generate.test.js @@ -1,3 +1,4 @@ +/*global afterEach beforeEach describe it*/ import { expect } from "chai"; import fs from "fs"; import path from "path"; @@ -112,7 +113,7 @@ describe("cli: gluestick generate", function () { it("should generate a container that sets the document title", done => { const type = "container"; stubProject(type); - generate(type, "mycontainer", (err) => { + generate(type, "mycontainer", () => { const contents = fs.readFileSync(path.join(process.cwd(), `src/${type}s/Mycontainer.js`), "utf8"); expect(contents).to.contain(""); done(); diff --git a/test/commands/new.test.js b/test/commands/new.test.js index c270d4011..ad455771b 100644 --- a/test/commands/new.test.js +++ b/test/commands/new.test.js @@ -1,3 +1,4 @@ +/*global afterEach beforeEach describe it*/ import sinon from "sinon"; import { expect } from "chai"; import fs from "fs"; @@ -6,11 +7,11 @@ import rimraf from "rimraf"; import glob from "glob"; import path from "path"; import newApp from "../../src/commands/new"; -import npmDependencies from "../../src/lib/npm-dependencies"; +import npmDependencies from "../../src/lib/npmDependencies"; import logger from "../../src/lib/logger"; const newFilesTemplate = glob.sync("**", { - cwd: path.resolve("./new"), + cwd: path.resolve("./templates/new"), dot: true }); @@ -35,19 +36,19 @@ describe("cli: gluestick new", function () { fakeNpm.restore(); }); - it("should report an error if the project name has symbols", () => { - newApp("foo#$"); + it("should report an error if the project name has symbols", () => { + newApp("foo#$"); expect(logger.warn.calledWithMatch("Invalid name")).to.be.true; }); it("should prompt the user if a .gluestick file already exists", () => { fs.closeSync(fs.openSync(".gluestick", "w")); - newApp("gs-new-test"); + newApp("gs-new-test"); expect(logger.info.calledWithMatch("You are about to initialize a new gluestick project")).to.be.true; }); it("should copy the contents of `new` upon install", () => { - newApp("gs-new-test"); + newApp("gs-new-test"); // account for the fact that the gitignore file that gets renamed const generatedFiles = new Set(glob.sync("**", { dot: true })); @@ -59,10 +60,10 @@ describe("cli: gluestick new", function () { }); it("should generate a test for all of the initial components and containers", () => { - newApp("gs-new-test"); + newApp("gs-new-test"); const generatedFiles = new Set(glob.sync("**", { dot: true })); - + // create index from array so we can quickly lookup files by name const index = {}; generatedFiles.forEach((file) => { @@ -74,7 +75,7 @@ describe("cli: gluestick new", function () { // container but we do not add a test. generatedFiles.forEach((file) => { if (/^src\/(components|containers).*\.js$/.test(file)) { - let testFilename = file.replace(/^src\/(.*)\.js$/, "test/$1\.test\.js"); + const testFilename = file.replace(/^src\/(.*)\.js$/, "test/$1\.test\.js"); expect(index[testFilename]).to.be.true; } }); diff --git a/test/lib/updateVersion.test.js b/test/lib/updateVersion.test.js index 0cf44972c..025bde661 100644 --- a/test/lib/updateVersion.test.js +++ b/test/lib/updateVersion.test.js @@ -1,3 +1,4 @@ +/*global afterEach beforeEach describe it*/ import sinon from "sinon"; import { expect } from "chai"; import fs from "fs"; diff --git a/test/lib/utils.test.js b/test/lib/utils.test.js index 27788c0b1..85a644013 100644 --- a/test/lib/utils.test.js +++ b/test/lib/utils.test.js @@ -1,3 +1,4 @@ +/*global afterEach beforeEach describe it*/ import { expect } from "chai"; import sinon from "sinon"; import fs from "fs";