-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new gatsby-cli package (#1696)
* Remove new command from gatsby package * Add gatsby-cli package which should now be installed globally * Change docs to instruct users to install the gatsby-cli * Revert "Remove new command from gatsby package" This reverts commit cf58bb7. * Fix gatsby cli script requiring wrong modules * Add README for gatsby-cli * Only show develop/build/serve commands if in a site
- Loading branch information
1 parent
927815c
commit 3634022
Showing
11 changed files
with
327 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/*.js | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.