From cf58bb76b4fc258c367f802464c3eb7bd77fd91c Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 2 Aug 2017 13:01:14 -0700 Subject: [PATCH 1/7] Remove new command from gatsby package --- packages/gatsby/src/bin/cli.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/gatsby/src/bin/cli.js b/packages/gatsby/src/bin/cli.js index 955809bca9bf6..4998b416e9413 100644 --- a/packages/gatsby/src/bin/cli.js +++ b/packages/gatsby/src/bin/cli.js @@ -103,14 +103,6 @@ program serve(p) }) -program - .command(`new [rootPath] [starter]`) - .description(`Create new Gatsby project.`) - .action((rootPath, starter) => { - const newCommand = require(`../utils/new`) - newCommand(rootPath, starter) - }) - program.on(`--help`, () => { console.log( `To show subcommand help: From da604840fbc123c98a383f58e913c0e48d5ba8ca Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 2 Aug 2017 13:02:39 -0700 Subject: [PATCH 2/7] Add gatsby-cli package which should now be installed globally --- packages/gatsby-cli/.gitignore | 2 + packages/gatsby-cli/.npmignore | 34 ++++++++ packages/gatsby-cli/README.md | 3 + packages/gatsby-cli/package.json | 25 ++++++ packages/gatsby-cli/src/.gitkeep | 0 packages/gatsby-cli/src/index.js | 140 +++++++++++++++++++++++++++++++ 6 files changed, 204 insertions(+) create mode 100644 packages/gatsby-cli/.gitignore create mode 100644 packages/gatsby-cli/.npmignore create mode 100644 packages/gatsby-cli/README.md create mode 100644 packages/gatsby-cli/package.json create mode 100644 packages/gatsby-cli/src/.gitkeep create mode 100755 packages/gatsby-cli/src/index.js diff --git a/packages/gatsby-cli/.gitignore b/packages/gatsby-cli/.gitignore new file mode 100644 index 0000000000000..2132b55e6405e --- /dev/null +++ b/packages/gatsby-cli/.gitignore @@ -0,0 +1,2 @@ +/*.js +yarn.lock diff --git a/packages/gatsby-cli/.npmignore b/packages/gatsby-cli/.npmignore new file mode 100644 index 0000000000000..e771d2c9fa299 --- /dev/null +++ b/packages/gatsby-cli/.npmignore @@ -0,0 +1,34 @@ +# Logs +logs +*.log + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git +node_modules +*.un~ +yarn.lock +src +flow-typed +coverage +decls +examples diff --git a/packages/gatsby-cli/README.md b/packages/gatsby-cli/README.md new file mode 100644 index 0000000000000..64d6093a00cda --- /dev/null +++ b/packages/gatsby-cli/README.md @@ -0,0 +1,3 @@ +# gatsby-cli + +Stub README diff --git a/packages/gatsby-cli/package.json b/packages/gatsby-cli/package.json new file mode 100644 index 0000000000000..e6c371a005ee1 --- /dev/null +++ b/packages/gatsby-cli/package.json @@ -0,0 +1,25 @@ +{ + "name": "gatsby-cli", + "version": "", + "description": "Gatsby command-line interface for creating new sites and running Gatsby commands", + "main": "index.js", + "bin": { + "gatsby": "./index.js" + }, + "scripts": { + "build": "babel src --out-dir . --ignore __tests__", + "watch": "babel -w src --out-dir . --ignore __tests__" + }, + "keywords": [ + "gatsby" + ], + "author": "Kyle Mathews ", + "license": "MIT", + "devDependencies": { + "babel-cli": "^6.24.1" + }, + "dependencies": { + "commander": "^2.11.0", + "resolve-cwd": "^2.0.0" + } +} diff --git a/packages/gatsby-cli/src/.gitkeep b/packages/gatsby-cli/src/.gitkeep new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/packages/gatsby-cli/src/index.js b/packages/gatsby-cli/src/index.js new file mode 100755 index 0000000000000..1ecd3a603653a --- /dev/null +++ b/packages/gatsby-cli/src/index.js @@ -0,0 +1,140 @@ +#!/usr/bin/env node +const program = require(`commander`) +const packageJson = require(`./package.json`) +const path = require(`path`) +const _ = require(`lodash`) +const resolveCwd = require("resolve-cwd") + +program.version(packageJson.version).usage(`[command] [options]`) + +let inGatsbySite = false +let localPackageJSON +try { + localPackageJSON = require(path.resolve(`./package.json`)) + if (localPackageJSON.dependencies && localPackageJSON.dependencies.gatsby) { + inGatsbySite = true + } +} catch (e) { + console.log(e) + // ignore +} + +const defaultHost = `localhost` + +const directory = path.resolve(`.`) +const getSiteInfo = () => { + const sitePackageJson = require(path.join(directory, `package.json`)) + const browserslist = sitePackageJson.browserslist || [ + `> 1%`, + `last 2 versions`, + `IE >= 9`, + ] + return { sitePackageJson, browserslist } +} + +// If there's a package.json in the current directory w/ a gatsby dependency +// include the develop/build/serve commands. Otherwise, just the new. +if (inGatsbySite) { + program + .command(`develop`) + .description( + `Start development server. Watches files and rebuilds and hot reloads ` + + `if something changes` + ) // eslint-disable-line max-len + .option( + `-H, --host `, + `Set host. Defaults to ${defaultHost}`, + defaultHost + ) + .option(`-p, --port `, `Set port. Defaults to 8000`, `8000`) + .option(`-o, --open`, `Open the site in your browser for you.`) + .action(command => { + const developPath = resolveCwd(`gatsby/dist/utils/develop`) + const develop = require(developPath) + const { sitePackageJson, browserslist } = getSiteInfo() + const p = { + ...command, + directory, + sitePackageJson, + browserslist, + } + develop(p) + }) + + program + .command(`build`) + .description(`Build a Gatsby project.`) + .option( + `--prefix-paths`, + `Build site with link paths prefixed (set prefix in your config).` + ) + .action(command => { + // Set NODE_ENV to 'production' + process.env.NODE_ENV = `production` + + const buildPath = resolveCwd(`gatsby/dist/utils/build`) + const build = require(buildPath) + const { sitePackageJson, browserslist } = getSiteInfo() + const p = { + ...command, + directory, + sitePackageJson, + browserslist, + } + build(p).then(() => { + console.log(`Done building in`, process.uptime(), `seconds`) + process.exit() + }) + }) + + program + .command(`serve`) + .description(`Serve built site.`) + .option( + `-H, --host `, + `Set host. Defaults to ${defaultHost}`, + defaultHost + ) + .option(`-p, --port `, `Set port. Defaults to 9000`, `9000`) + .option(`-o, --open`, `Open the site in your browser for you.`) + .action(command => { + const servePath = resolveCwd(`gatsby/dist/utils/serve`) + const serve = require(servePath) + const { sitePackageJson, browserslist } = getSiteInfo() + const p = { + ...command, + directory, + sitePackageJson, + browserslist, + } + serve(p) + }) +} + +program + .command(`new [rootPath] [starter]`) + .description(`Create new Gatsby project.`) + .action((rootPath, starter) => { + const newCommand = require(`../utils/new`) + newCommand(rootPath, starter) + }) + +program.on(`--help`, () => { + console.log( + `To show subcommand help: + + gatsby [command] -h +` + ) +}) + +// If the user types an unknown sub-command, just display the help. +const subCmd = process.argv.slice(2, 3)[0] +let cmds = _.map(program.commands, `_name`) +cmds = cmds.concat([`--version`, `-V`]) + +if (!_.includes(cmds, subCmd)) { + program.help() +} else { + program.parse(process.argv) +} From d8e6b6c3dc2e6e04d46a7b7939a24acad3828948 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 2 Aug 2017 13:04:58 -0700 Subject: [PATCH 3/7] Change docs to instruct users to install the gatsby-cli --- docs/tutorial/part-one/index.md | 2 +- scripts/publish-site.sh | 2 +- www/src/pages/docs/index.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/tutorial/part-one/index.md b/docs/tutorial/part-one/index.md index f29020219bb2d..9bf4f1adc001b 100644 --- a/docs/tutorial/part-one/index.md +++ b/docs/tutorial/part-one/index.md @@ -32,7 +32,7 @@ Gatsby uses "starters" for starting new projects. As the name suggests, starters To install a starter, first install Gatsby's terminal program. ```sh -npm install --global gatsby +npm install --global gatsby-cli ``` diff --git a/scripts/publish-site.sh b/scripts/publish-site.sh index 4831d914226e3..3f5c628b258cb 100644 --- a/scripts/publish-site.sh +++ b/scripts/publish-site.sh @@ -1,7 +1,7 @@ echo "=== Building ES5 version of Gatsby" ./node_modules/.bin/lerna run build -yarn global add gatsby-dev-cli@canary +yarn global add gatsby-dev-cli gatsby-dev --set-path-to-repo . echo "=== Installing the website dependencies" diff --git a/www/src/pages/docs/index.js b/www/src/pages/docs/index.js index 892f764df9b6a..fe842a855328b 100644 --- a/www/src/pages/docs/index.js +++ b/www/src/pages/docs/index.js @@ -14,7 +14,7 @@ class IndexRoute extends React.Component {

Gatsby is a blazing-fast static site generator for React.

Install Gatsby's command line tool

- npm install -g gatsby + npm install --global gatsby-cli

Using the Gatsby CLI

    From 57c321d065d21aef1b77f0422d4ccbd50c6700b2 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 2 Aug 2017 14:37:07 -0700 Subject: [PATCH 4/7] Revert "Remove new command from gatsby package" This reverts commit cf58bb76b4fc258c367f802464c3eb7bd77fd91c. --- packages/gatsby/src/bin/cli.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/gatsby/src/bin/cli.js b/packages/gatsby/src/bin/cli.js index 4998b416e9413..955809bca9bf6 100644 --- a/packages/gatsby/src/bin/cli.js +++ b/packages/gatsby/src/bin/cli.js @@ -103,6 +103,14 @@ program serve(p) }) +program + .command(`new [rootPath] [starter]`) + .description(`Create new Gatsby project.`) + .action((rootPath, starter) => { + const newCommand = require(`../utils/new`) + newCommand(rootPath, starter) + }) + program.on(`--help`, () => { console.log( `To show subcommand help: From d4428618f73c559fe073cdf43bdc231d2e5e7e2c Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 2 Aug 2017 14:41:38 -0700 Subject: [PATCH 5/7] Fix gatsby cli script requiring wrong modules --- packages/gatsby/package.json | 1 + packages/gatsby/src/bin/cli.js | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/gatsby/package.json b/packages/gatsby/package.json index e5216e0469bcb..1056667bd80aa 100644 --- a/packages/gatsby/package.json +++ b/packages/gatsby/package.json @@ -95,6 +95,7 @@ "redux": "^3.6.0", "relay-compiler": "^1.1.0", "remote-redux-devtools": "^0.5.7", + "resolve-cwd": "^2.0.0", "sift": "^3.2.6", "slash": "^1.0.0", "socket.io": "^2.0.3", diff --git a/packages/gatsby/src/bin/cli.js b/packages/gatsby/src/bin/cli.js index 955809bca9bf6..cfc02b12e7393 100644 --- a/packages/gatsby/src/bin/cli.js +++ b/packages/gatsby/src/bin/cli.js @@ -3,6 +3,7 @@ const packageJson = require(`../../package.json`) const path = require(`path`) const _ = require(`lodash`) const Promise = require(`bluebird`) +const resolveCwd = require("resolve-cwd") // Improve Promise error handling. Maybe... what's the best // practice for this these days? @@ -44,7 +45,8 @@ program .option(`-p, --port `, `Set port. Defaults to 8000`, `8000`) .option(`-o, --open`, `Open the site in your browser for you.`) .action(command => { - const develop = require(`../utils/develop`) + const developPath = resolveCwd(`gatsby/dist/utils/develop`) + const develop = require(developPath) // console.timeEnd(`time to load develop`) const { sitePackageJson, browserslist } = getSiteInfo() const p = { @@ -67,7 +69,8 @@ program // Set NODE_ENV to 'production' process.env.NODE_ENV = `production` - const build = require(`../utils/build`) + const buildPath = resolveCwd(`gatsby/dist/utils/build`) + const build = require(buildPath) const { sitePackageJson, browserslist } = getSiteInfo() const p = { ...command, @@ -92,7 +95,8 @@ program .option(`-p, --port `, `Set port. Defaults to 9000`, `9000`) .option(`-o, --open`, `Open the site in your browser for you.`) .action(command => { - const serve = require(`../utils/serve`) + const servePath = resolveCwd(`gatsby/dist/utils/serve`) + const serve = require(servePath) const { sitePackageJson, browserslist } = getSiteInfo() const p = { ...command, From a832f5c9b096fd49c44c503ae3fa591b2507777e Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 2 Aug 2017 14:50:59 -0700 Subject: [PATCH 6/7] Add README for gatsby-cli --- packages/gatsby-cli/README.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-cli/README.md b/packages/gatsby-cli/README.md index 64d6093a00cda..56180815bbc2a 100644 --- a/packages/gatsby-cli/README.md +++ b/packages/gatsby-cli/README.md @@ -1,3 +1,34 @@ # gatsby-cli -Stub README +Gatsby command line tool. + +Let's you create new Gatsby sites using [Gatsby starters](https://www.gatsbyjs.org/docs/gatsby-starters/) + +Also let's you run commands on sites. The tool runs code from the `gatsby` package +installed locally. + +## Install + +`npm install --global gatsby-cli` + +## How to use + +Run `gatsby --help` for full help. + +### New + +`gatsby new gatsby-site` + +See the [Gatsby starters docs](https://www.gatsbyjs.org/docs/gatsby-starters/) for more + +### Develop + +At the root of a Gatsby site run `gatsby develop` to start the Gatsby development server. + +### Build + +At the root of a Gatsby site run `gatsby build` to do a production build of a site. + +### Serve + +At the root of a Gatsby site run `gatsby serve` to serve the production build of the site for testing. From c7e1e63689a851170470908eed3c10d60bec0806 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 2 Aug 2017 14:53:07 -0700 Subject: [PATCH 7/7] Only show develop/build/serve commands if in a site --- packages/gatsby/src/bin/cli.js | 159 ++++++++++++++++++--------------- 1 file changed, 87 insertions(+), 72 deletions(-) diff --git a/packages/gatsby/src/bin/cli.js b/packages/gatsby/src/bin/cli.js index cfc02b12e7393..37cdb23faa906 100644 --- a/packages/gatsby/src/bin/cli.js +++ b/packages/gatsby/src/bin/cli.js @@ -17,6 +17,18 @@ process.on(`unhandledRejection`, error => { const defaultHost = `localhost` +let inGatsbySite = false +let localPackageJSON +try { + localPackageJSON = require(path.resolve(`./package.json`)) + if (localPackageJSON.dependencies && localPackageJSON.dependencies.gatsby) { + inGatsbySite = true + } +} catch (e) { + console.log(e) + // ignore +} + const directory = path.resolve(`.`) const getSiteInfo = () => { const sitePackageJson = require(path.join(directory, `package.json`)) @@ -30,82 +42,85 @@ const getSiteInfo = () => { program.version(packageJson.version).usage(`[command] [options]`) -// console.time(`time to load develop`) -program - .command(`develop`) - .description( - `Start development server. Watches files and rebuilds and hot reloads ` + - `if something changes` - ) // eslint-disable-line max-len - .option( - `-H, --host `, - `Set host. Defaults to ${defaultHost}`, - defaultHost - ) - .option(`-p, --port `, `Set port. Defaults to 8000`, `8000`) - .option(`-o, --open`, `Open the site in your browser for you.`) - .action(command => { - const developPath = resolveCwd(`gatsby/dist/utils/develop`) - const develop = require(developPath) - // console.timeEnd(`time to load develop`) - const { sitePackageJson, browserslist } = getSiteInfo() - const p = { - ...command, - directory, - sitePackageJson, - browserslist, - } - develop(p) - }) +// If there's a package.json in the current directory w/ a gatsby dependency +// include the develop/build/serve commands. Otherwise, just the new. +if (inGatsbySite) { + program + .command(`develop`) + .description( + `Start development server. Watches files and rebuilds and hot reloads ` + + `if something changes` + ) // eslint-disable-line max-len + .option( + `-H, --host `, + `Set host. Defaults to ${defaultHost}`, + defaultHost + ) + .option(`-p, --port `, `Set port. Defaults to 8000`, `8000`) + .option(`-o, --open`, `Open the site in your browser for you.`) + .action(command => { + const developPath = resolveCwd(`gatsby/dist/utils/develop`) + const develop = require(developPath) + // console.timeEnd(`time to load develop`) + const { sitePackageJson, browserslist } = getSiteInfo() + const p = { + ...command, + directory, + sitePackageJson, + browserslist, + } + develop(p) + }) -program - .command(`build`) - .description(`Build a Gatsby project.`) - .option( - `--prefix-paths`, - `Build site with link paths prefixed (set prefix in your config).` - ) - .action(command => { - // Set NODE_ENV to 'production' - process.env.NODE_ENV = `production` + program + .command(`build`) + .description(`Build a Gatsby project.`) + .option( + `--prefix-paths`, + `Build site with link paths prefixed (set prefix in your config).` + ) + .action(command => { + // Set NODE_ENV to 'production' + process.env.NODE_ENV = `production` - const buildPath = resolveCwd(`gatsby/dist/utils/build`) - const build = require(buildPath) - const { sitePackageJson, browserslist } = getSiteInfo() - const p = { - ...command, - directory, - sitePackageJson, - browserslist, - } - build(p).then(() => { - console.log(`Done building in`, process.uptime(), `seconds`) - process.exit() + const buildPath = resolveCwd(`gatsby/dist/utils/build`) + const build = require(buildPath) + const { sitePackageJson, browserslist } = getSiteInfo() + const p = { + ...command, + directory, + sitePackageJson, + browserslist, + } + build(p).then(() => { + console.log(`Done building in`, process.uptime(), `seconds`) + process.exit() + }) }) - }) -program - .command(`serve`) - .description(`Serve built site.`) - .option( - `-H, --host `, - `Set host. Defaults to ${defaultHost}`, - defaultHost - ) - .option(`-p, --port `, `Set port. Defaults to 9000`, `9000`) - .option(`-o, --open`, `Open the site in your browser for you.`) - .action(command => { - const servePath = resolveCwd(`gatsby/dist/utils/serve`) - const serve = require(servePath) - const { sitePackageJson, browserslist } = getSiteInfo() - const p = { - ...command, - directory, - sitePackageJson, - browserslist, - } - serve(p) - }) + program + .command(`serve`) + .description(`Serve built site.`) + .option( + `-H, --host `, + `Set host. Defaults to ${defaultHost}`, + defaultHost + ) + .option(`-p, --port `, `Set port. Defaults to 9000`, `9000`) + .option(`-o, --open`, `Open the site in your browser for you.`) + .action(command => { + const servePath = resolveCwd(`gatsby/dist/utils/serve`) + const serve = require(servePath) + const { sitePackageJson, browserslist } = getSiteInfo() + const p = { + ...command, + directory, + sitePackageJson, + browserslist, + } + serve(p) + }) +} program .command(`new [rootPath] [starter]`)