Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new gatsby-cli package #1696

Merged
merged 7 commits into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/tutorial/part-one/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*.js
yarn.lock
34 changes: 34 additions & 0 deletions packages/gatsby-cli/.npmignore
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions packages/gatsby-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# gatsby-cli

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.
25 changes: 25 additions & 0 deletions packages/gatsby-cli/package.json
Original file line number Diff line number Diff line change
@@ -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 <mathews.kyle@gmail.com>",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.24.1"
},
"dependencies": {
"commander": "^2.11.0",
"resolve-cwd": "^2.0.0"
}
}
Empty file.
140 changes: 140 additions & 0 deletions packages/gatsby-cli/src/index.js
Original file line number Diff line number Diff line change
@@ -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 <url>`,
`Set host. Defaults to ${defaultHost}`,
defaultHost
)
.option(`-p, --port <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 <url>`,
`Set host. Defaults to ${defaultHost}`,
defaultHost
)
.option(`-p, --port <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)
}
1 change: 1 addition & 0 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading