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

Revert "consolidate CLI logic and depend on it in gatsby (#1880)" #1903

Merged
merged 1 commit into from
Aug 24, 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 packages/gatsby-cli/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/lib
/*.js
yarn.lock
17 changes: 6 additions & 11 deletions packages/gatsby-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@
"version": "1.1.2",
"author": "Kyle Mathews <mathews.kyle@gmail.com>",
"bin": {
"gatsby": "lib/index.js"
"gatsby": "./index.js"
},
"dependencies": {
"babel-runtime": "^6.25.0",
"bluebird": "^3.5.0",
"common-tags": "^1.4.0",
"convert-hrtime": "^2.0.0",
"commander": "^2.11.0",
"core-js": "^2.5.0",
"execa": "^0.8.0",
"fs-extra": "^4.0.1",
"hosted-git-info": "^2.5.0",
"lodash": "^4.17.4",
"pretty-error": "^2.1.1",
"resolve-cwd": "^2.0.0",
"yargs": "^8.0.2",
"yurnalist": "^0.2.1"
"tracer": "^0.8.9"
},
"devDependencies": {
"babel-cli": "^6.24.1"
Expand All @@ -28,9 +23,9 @@
"gatsby"
],
"license": "MIT",
"main": "lib/index.js",
"main": "index.js",
"scripts": {
"build": "babel src --out-dir lib --ignore __tests__",
"watch": "babel -w src --out-dir lib --ignore __tests__"
"build": "babel src --out-dir . --ignore __tests__",
"watch": "babel -w src --out-dir . --ignore __tests__"
}
}
Empty file.
175 changes: 0 additions & 175 deletions packages/gatsby-cli/src/create-cli.js

This file was deleted.

163 changes: 137 additions & 26 deletions packages/gatsby-cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,150 @@
// use require() with backtick strings so use the es6 syntax
import "babel-polyfill"

const program = require(`commander`)
const packageJson = require(`./package.json`)
const path = require(`path`)
const _ = require(`lodash`)
const resolveCwd = require(`resolve-cwd`)
const createCli = require(`./create-cli`)
const report = require(`./reporter`)

global.Promise = require(`bluebird`)
program.version(packageJson.version).usage(`[command] [options]`)

const version = process.version
const verDigit = Number(version.match(/\d+/)[0])
let inGatsbySite = false
let localPackageJSON
try {
localPackageJSON = require(path.resolve(`./package.json`))
if (
(localPackageJSON.dependencies && localPackageJSON.dependencies.gatsby) ||
(localPackageJSON.devDependencies &&
localPackageJSON.devDependencies.gatsby)
) {
inGatsbySite = true
} else if (
localPackageJSON.devDependencies &&
localPackageJSON.devDependencies.gatsby
) {
inGatsbySite = true
}
} catch (err) {
// ignore
}

if (verDigit < 4) {
report.panic(
`Gatsby 1.0+ requires node.js v4 or higher (you have ${version}). \n` +
`Upgrade node to the latest stable release.`
)
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 }
}

Promise.onPossiblyUnhandledRejection(error => {
report.error(error)
throw error
})
// 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)
})

process.on(`unhandledRejection`, error => {
// This will exit the process in newer Node anyway so lets be consistent
// across versions and crash
report.panic(`UNHANDLED REJECTION`, error)
})
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`

process.on(`uncaughtException`, error => {
report.panic(`UNHANDLED EXCEPTION`, error)
})
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(`./new`)
newCommand(rootPath, starter)
})

createCli(process.argv, {
develop: () => require(resolveCwd(`gatsby/dist/commands/develop`)),
build: () => require(resolveCwd(`gatsby/dist/commands/build`)),
serve: () => require(resolveCwd(`gatsby/dist/commands/serve`)),
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)
}
Loading