Skip to content

Commit af5c9c8

Browse files
jquenseKyleAMathews
authored andcommitted
CLI improvements ROUND 2 (gatsbyjs#1969)
* consolidate CLI logic and depend on it in gatsby * clean up dips * remove old built files * fix original issues * don't exit for indefinate commands * clean up some moar versions * temporarily fix tests * fix build script * clean up * add lifecycle hooks * prettier * rm old * dumb * rm generated files * Update .gitignore/yarn.lock * Update lerna/packages * Add missing dependency * Restore default port for 'serve'
1 parent 0de0ffe commit af5c9c8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+543
-909
lines changed

appveyor.yml

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ install:
5757
- choco install yarn --ignore-dependencies
5858
- refreshenv
5959
- echo we are running on %PLATFORM%
60+
- node --version
6061
- yarn global add gatsby-dev-cli@canary
6162
- yarn run bootstrap
6263
- node --version

lerna.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"lerna": "2.1.1",
2+
"lerna": "2.4.0",
33
"npmClient": "yarn",
44
"packages": [
55
"packages/*"

packages/gatsby-cli/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
/lib
12
/*.js
3+
/reporter
24
yarn.lock

packages/gatsby-cli/package.json

+14-6
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@
44
"version": "1.1.9",
55
"author": "Kyle Mathews <[email protected]>",
66
"bin": {
7-
"gatsby": "./index.js"
7+
"gatsby": "lib/index.js"
88
},
99
"dependencies": {
10+
"babel-code-frame": "^6.26.0",
1011
"babel-runtime": "^6.26.0",
11-
"commander": "^2.11.0",
12+
"bluebird": "^3.5.0",
13+
"common-tags": "^1.4.0",
14+
"convert-hrtime": "^2.0.0",
1215
"core-js": "^2.5.0",
16+
"execa": "^0.8.0",
1317
"fs-extra": "^4.0.1",
1418
"hosted-git-info": "^2.5.0",
1519
"lodash": "^4.17.4",
20+
"pretty-error": "^2.1.1",
1621
"resolve-cwd": "^2.0.0",
17-
"tracer": "^0.8.9"
22+
"source-map": "^0.5.7",
23+
"stack-trace": "^0.0.10",
24+
"yargs": "^8.0.2",
25+
"yurnalist": "^0.2.1"
1826
},
1927
"devDependencies": {
2028
"babel-cli": "^6.26.0",
@@ -24,10 +32,10 @@
2432
"gatsby"
2533
],
2634
"license": "MIT",
27-
"main": "index.js",
35+
"main": "lib/index.js",
2836
"scripts": {
29-
"build": "babel src --out-dir . --ignore __tests__",
30-
"watch": "babel -w src --out-dir . --ignore __tests__",
37+
"build": "babel src --out-dir lib --ignore __tests__",
38+
"watch": "babel -w src --out-dir lib --ignore __tests__",
3139
"prepublish": "cross-env NODE_ENV=production npm run build"
3240
}
3341
}

packages/gatsby-cli/src/.gitkeep

Whitespace-only changes.

packages/gatsby-cli/src/create-cli.js

+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
const path = require(`path`)
2+
const resolveCwd = require(`resolve-cwd`)
3+
const yargs = require(`yargs`)
4+
const report = require(`./reporter`)
5+
6+
const DEFAULT_BROWSERS = [`> 1%`, `last 2 versions`, `IE >= 9`]
7+
8+
const handlerP = fn => (...args) => {
9+
Promise.resolve(fn(...args)).then(
10+
() => process.exit(0),
11+
err => report.panic(err)
12+
)
13+
}
14+
15+
function buildLocalCommands(cli, isLocalSite) {
16+
const defaultHost = `localhost`
17+
const directory = path.resolve(`.`)
18+
19+
let siteInfo = { directory, browserslist: DEFAULT_BROWSERS }
20+
if (isLocalSite) {
21+
const json = require(path.join(directory, `package.json`))
22+
siteInfo.sitePackageJson = json
23+
siteInfo.browserslist = json.browserslist || siteInfo.browserslist
24+
}
25+
26+
function resolveLocalCommand(command) {
27+
if (!isLocalSite) {
28+
cli.showHelp()
29+
report.verbose(`current directory: ${directory}`)
30+
return report.panic(
31+
`gatsby <${command}> can only be run for a gatsby site. \n` +
32+
`Either the current working directory does not contain a package.json or ` +
33+
`'gatsby' is not specified as a dependency`
34+
)
35+
}
36+
37+
try {
38+
const cmdPath =
39+
resolveCwd.silent(`gatsby/dist/commands/${command}`) ||
40+
// Old location of commands
41+
resolveCwd.silent(`gatsby/dist/utils/${command}`)
42+
if (!cmdPath)
43+
return report.panic(
44+
`There was a problem loading the local ${command} command. Gatsby may not be installed.`
45+
)
46+
47+
report.verbose(`loading local command from: ${cmdPath}`)
48+
return require(cmdPath)
49+
} catch (err) {
50+
cli.showHelp()
51+
return report.panic(
52+
`There was a problem loading the local ${command} command. Gatsby may not be installed.`,
53+
err
54+
)
55+
}
56+
}
57+
58+
function getCommandHandler(command, handler) {
59+
return argv => {
60+
report.setVerbose(!!argv.verbose)
61+
62+
process.env.gatsby_log_level = argv.verbose ? `verbose` : `normal`
63+
report.verbose(`set gatsby_log_level: "${process.env.gatsby_log_level}"`)
64+
65+
process.env.gatsby_executing_command = command
66+
report.verbose(`set gatsby_executing_command: "${command}"`)
67+
68+
let localCmd = resolveLocalCommand(command)
69+
let args = { ...argv, ...siteInfo }
70+
71+
report.verbose(`running command: ${command}`)
72+
return handler ? handler(args, localCmd) : localCmd(args)
73+
}
74+
}
75+
76+
cli.command({
77+
command: `develop`,
78+
desc:
79+
`Start development server. Watches files, rebuilds, and hot reloads ` +
80+
`if something changes`,
81+
builder: _ =>
82+
_.option(`H`, {
83+
alias: `host`,
84+
type: `string`,
85+
default: defaultHost,
86+
describe: `Set host. Defaults to ${defaultHost}`,
87+
})
88+
.option(`p`, {
89+
alias: `port`,
90+
type: `string`,
91+
default: `8000`,
92+
describe: `Set port. Defaults to 8000`,
93+
})
94+
.option(`o`, {
95+
alias: `open`,
96+
type: `boolean`,
97+
describe: `Open the site in your browser for you.`,
98+
}),
99+
handler: getCommandHandler(`develop`),
100+
})
101+
102+
cli.command({
103+
command: `build`,
104+
desc: `Build a Gatsby project.`,
105+
builder: _ =>
106+
_.option(`prefix-paths`, {
107+
type: `boolean`,
108+
default: false,
109+
describe: `Build site with link paths prefixed (set prefix in your config).`,
110+
}),
111+
handler: handlerP(
112+
getCommandHandler(`build`, (args, cmd) => {
113+
process.env.NODE_ENV = `production`
114+
return cmd(args)
115+
})
116+
),
117+
})
118+
119+
cli.command({
120+
command: `serve`,
121+
desc: `Serve previously built Gatsby site.`,
122+
builder: _ =>
123+
_.option(`H`, {
124+
alias: `host`,
125+
type: `string`,
126+
default: defaultHost,
127+
describe: `Set host. Defaults to ${defaultHost}`,
128+
})
129+
.option(`p`, {
130+
alias: `port`,
131+
type: `string`,
132+
default: `9000`,
133+
describe: `Set port. Defaults to 9000`,
134+
})
135+
.option(`o`, {
136+
alias: `open`,
137+
type: `boolean`,
138+
describe: `Open the site in your browser for you.`,
139+
}),
140+
141+
handler: getCommandHandler(`serve`),
142+
})
143+
}
144+
145+
function isLocalGatsbySite() {
146+
let inGatsbySite = false
147+
try {
148+
let { dependencies, devDependencies } = require(path.resolve(
149+
`./package.json`
150+
))
151+
inGatsbySite =
152+
(dependencies && dependencies.gatsby) ||
153+
(devDependencies && devDependencies.gatsby)
154+
} catch (err) {
155+
/* ignore */
156+
}
157+
return inGatsbySite
158+
}
159+
160+
module.exports = (argv, handlers) => {
161+
let cli = yargs()
162+
let isLocalSite = isLocalGatsbySite()
163+
164+
cli
165+
.usage(`Usage: $0 <command> [options]`)
166+
.help(`h`)
167+
.alias(`h`, `help`)
168+
.version()
169+
.alias(`v`, `version`)
170+
.option(`verbose`, {
171+
default: false,
172+
type: `boolean`,
173+
describe: `Turn on verbose output`,
174+
global: true,
175+
})
176+
177+
buildLocalCommands(cli, isLocalSite)
178+
179+
return cli
180+
.command({
181+
command: `new [rootPath] [starter]`,
182+
desc: `Create new Gatsby project.`,
183+
handler: handlerP(
184+
({ rootPath, starter = `gatsbyjs/gatsby-starter-default` }) => {
185+
const initStarter = require(`./init-starter`)
186+
return initStarter(starter, { rootPath })
187+
}
188+
),
189+
})
190+
.wrap(cli.terminalWidth())
191+
.demandCommand(1, `Pass --help to see all available commands and options.`)
192+
.showHelpOnFail(true, `A command is required.`)
193+
.parse(argv.slice(2))
194+
}

0 commit comments

Comments
 (0)