forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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'
- Loading branch information
1 parent
0de0ffe
commit af5c9c8
Showing
43 changed files
with
543 additions
and
909 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"lerna": "2.1.1", | ||
"lerna": "2.4.0", | ||
"npmClient": "yarn", | ||
"packages": [ | ||
"packages/*" | ||
|
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
/lib | ||
/*.js | ||
/reporter | ||
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 |
---|---|---|
|
@@ -4,17 +4,25 @@ | |
"version": "1.1.9", | ||
"author": "Kyle Mathews <[email protected]>", | ||
"bin": { | ||
"gatsby": "./index.js" | ||
"gatsby": "lib/index.js" | ||
}, | ||
"dependencies": { | ||
"babel-code-frame": "^6.26.0", | ||
"babel-runtime": "^6.26.0", | ||
"commander": "^2.11.0", | ||
"bluebird": "^3.5.0", | ||
"common-tags": "^1.4.0", | ||
"convert-hrtime": "^2.0.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", | ||
"tracer": "^0.8.9" | ||
"source-map": "^0.5.7", | ||
"stack-trace": "^0.0.10", | ||
"yargs": "^8.0.2", | ||
"yurnalist": "^0.2.1" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
|
@@ -24,10 +32,10 @@ | |
"gatsby" | ||
], | ||
"license": "MIT", | ||
"main": "index.js", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"build": "babel src --out-dir . --ignore __tests__", | ||
"watch": "babel -w src --out-dir . --ignore __tests__", | ||
"build": "babel src --out-dir lib --ignore __tests__", | ||
"watch": "babel -w src --out-dir lib --ignore __tests__", | ||
"prepublish": "cross-env NODE_ENV=production npm run build" | ||
} | ||
} |
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,194 @@ | ||
const path = require(`path`) | ||
const resolveCwd = require(`resolve-cwd`) | ||
const yargs = require(`yargs`) | ||
const report = require(`./reporter`) | ||
|
||
const DEFAULT_BROWSERS = [`> 1%`, `last 2 versions`, `IE >= 9`] | ||
|
||
const handlerP = fn => (...args) => { | ||
Promise.resolve(fn(...args)).then( | ||
() => process.exit(0), | ||
err => report.panic(err) | ||
) | ||
} | ||
|
||
function buildLocalCommands(cli, isLocalSite) { | ||
const defaultHost = `localhost` | ||
const directory = path.resolve(`.`) | ||
|
||
let siteInfo = { directory, browserslist: DEFAULT_BROWSERS } | ||
if (isLocalSite) { | ||
const json = require(path.join(directory, `package.json`)) | ||
siteInfo.sitePackageJson = json | ||
siteInfo.browserslist = json.browserslist || siteInfo.browserslist | ||
} | ||
|
||
function resolveLocalCommand(command) { | ||
if (!isLocalSite) { | ||
cli.showHelp() | ||
report.verbose(`current directory: ${directory}`) | ||
return report.panic( | ||
`gatsby <${command}> can only be run for a gatsby site. \n` + | ||
`Either the current working directory does not contain a package.json or ` + | ||
`'gatsby' is not specified as a dependency` | ||
) | ||
} | ||
|
||
try { | ||
const cmdPath = | ||
resolveCwd.silent(`gatsby/dist/commands/${command}`) || | ||
// Old location of commands | ||
resolveCwd.silent(`gatsby/dist/utils/${command}`) | ||
if (!cmdPath) | ||
return report.panic( | ||
`There was a problem loading the local ${command} command. Gatsby may not be installed.` | ||
) | ||
|
||
report.verbose(`loading local command from: ${cmdPath}`) | ||
return require(cmdPath) | ||
} catch (err) { | ||
cli.showHelp() | ||
return report.panic( | ||
`There was a problem loading the local ${command} command. Gatsby may not be installed.`, | ||
err | ||
) | ||
} | ||
} | ||
|
||
function getCommandHandler(command, handler) { | ||
return argv => { | ||
report.setVerbose(!!argv.verbose) | ||
|
||
process.env.gatsby_log_level = argv.verbose ? `verbose` : `normal` | ||
report.verbose(`set gatsby_log_level: "${process.env.gatsby_log_level}"`) | ||
|
||
process.env.gatsby_executing_command = command | ||
report.verbose(`set gatsby_executing_command: "${command}"`) | ||
|
||
let localCmd = resolveLocalCommand(command) | ||
let args = { ...argv, ...siteInfo } | ||
|
||
report.verbose(`running command: ${command}`) | ||
return handler ? handler(args, localCmd) : localCmd(args) | ||
} | ||
} | ||
|
||
cli.command({ | ||
command: `develop`, | ||
desc: | ||
`Start development server. Watches files, rebuilds, and hot reloads ` + | ||
`if something changes`, | ||
builder: _ => | ||
_.option(`H`, { | ||
alias: `host`, | ||
type: `string`, | ||
default: defaultHost, | ||
describe: `Set host. Defaults to ${defaultHost}`, | ||
}) | ||
.option(`p`, { | ||
alias: `port`, | ||
type: `string`, | ||
default: `8000`, | ||
describe: `Set port. Defaults to 8000`, | ||
}) | ||
.option(`o`, { | ||
alias: `open`, | ||
type: `boolean`, | ||
describe: `Open the site in your browser for you.`, | ||
}), | ||
handler: getCommandHandler(`develop`), | ||
}) | ||
|
||
cli.command({ | ||
command: `build`, | ||
desc: `Build a Gatsby project.`, | ||
builder: _ => | ||
_.option(`prefix-paths`, { | ||
type: `boolean`, | ||
default: false, | ||
describe: `Build site with link paths prefixed (set prefix in your config).`, | ||
}), | ||
handler: handlerP( | ||
getCommandHandler(`build`, (args, cmd) => { | ||
process.env.NODE_ENV = `production` | ||
return cmd(args) | ||
}) | ||
), | ||
}) | ||
|
||
cli.command({ | ||
command: `serve`, | ||
desc: `Serve previously built Gatsby site.`, | ||
builder: _ => | ||
_.option(`H`, { | ||
alias: `host`, | ||
type: `string`, | ||
default: defaultHost, | ||
describe: `Set host. Defaults to ${defaultHost}`, | ||
}) | ||
.option(`p`, { | ||
alias: `port`, | ||
type: `string`, | ||
default: `9000`, | ||
describe: `Set port. Defaults to 9000`, | ||
}) | ||
.option(`o`, { | ||
alias: `open`, | ||
type: `boolean`, | ||
describe: `Open the site in your browser for you.`, | ||
}), | ||
|
||
handler: getCommandHandler(`serve`), | ||
}) | ||
} | ||
|
||
function isLocalGatsbySite() { | ||
let inGatsbySite = false | ||
try { | ||
let { dependencies, devDependencies } = require(path.resolve( | ||
`./package.json` | ||
)) | ||
inGatsbySite = | ||
(dependencies && dependencies.gatsby) || | ||
(devDependencies && devDependencies.gatsby) | ||
} catch (err) { | ||
/* ignore */ | ||
} | ||
return inGatsbySite | ||
} | ||
|
||
module.exports = (argv, handlers) => { | ||
let cli = yargs() | ||
let isLocalSite = isLocalGatsbySite() | ||
|
||
cli | ||
.usage(`Usage: $0 <command> [options]`) | ||
.help(`h`) | ||
.alias(`h`, `help`) | ||
.version() | ||
.alias(`v`, `version`) | ||
.option(`verbose`, { | ||
default: false, | ||
type: `boolean`, | ||
describe: `Turn on verbose output`, | ||
global: true, | ||
}) | ||
|
||
buildLocalCommands(cli, isLocalSite) | ||
|
||
return cli | ||
.command({ | ||
command: `new [rootPath] [starter]`, | ||
desc: `Create new Gatsby project.`, | ||
handler: handlerP( | ||
({ rootPath, starter = `gatsbyjs/gatsby-starter-default` }) => { | ||
const initStarter = require(`./init-starter`) | ||
return initStarter(starter, { rootPath }) | ||
} | ||
), | ||
}) | ||
.wrap(cli.terminalWidth()) | ||
.demandCommand(1, `Pass --help to see all available commands and options.`) | ||
.showHelpOnFail(true, `A command is required.`) | ||
.parse(argv.slice(2)) | ||
} |
Oops, something went wrong.