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.
consolidate CLI logic and depend on it in gatsby (gatsbyjs#1880)
* consolidate CLI logic and depend on it in gatsby * clean up dips * Add gatsby-cli as a dependency
- Loading branch information
1 parent
60d25bc
commit 2a6d803
Showing
30 changed files
with
441 additions
and
644 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
/*.js | ||
/lib | ||
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,21 @@ | |
"version": "1.0.12", | ||
"author": "Kyle Mathews <[email protected]>", | ||
"bin": { | ||
"gatsby": "./index.js" | ||
"gatsby": "lib/index.js" | ||
}, | ||
"dependencies": { | ||
"babel-runtime": "^6.25.0", | ||
"commander": "^2.11.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" | ||
"yargs": "^8.0.2", | ||
"yurnalist": "^0.2.1" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1" | ||
|
@@ -23,9 +27,9 @@ | |
"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__" | ||
} | ||
} |
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,172 @@ | ||
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`, | ||
] | ||
|
||
function buildLocalCommands(cli, isLocalSite) { | ||
const defaultHost = `localhost` | ||
const directory = path.resolve(`.`) | ||
|
||
function getSiteInfo() { | ||
if (!isLocalSite) return {} | ||
|
||
const sitePackageJson = require(path.join(directory, `package.json`)) | ||
const browserslist = sitePackageJson.browserslist || DEFAULT_BROWSERS | ||
return { sitePackageJson, 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 { | ||
return require(resolveCwd(`gatsby/dist/commands/${command}`)) | ||
} catch (err) { | ||
cli.showHelp() | ||
return report.panic( | ||
`There was a problem loading the local ${command} command. Gatsby may not be installed.`, | ||
err | ||
) | ||
} | ||
} | ||
|
||
cli.command({ | ||
command: `develop`, | ||
desc: | ||
`Start development server. Watches files, rebuilds, and hot reloads ` + | ||
`if something changes`, | ||
builder: _ => _ | ||
.option(`H`, { | ||
alias: `host`, | ||
type: `string`, | ||
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: argv => { | ||
const { sitePackageJson, browserslist } = getSiteInfo() | ||
|
||
resolveLocalCommand(`develop`)({ | ||
...argv, | ||
directory, | ||
sitePackageJson, | ||
browserslist, | ||
}) | ||
}, | ||
}) | ||
|
||
cli | ||
.command({ | ||
command: `build`, | ||
desc: `Build a Gatsby project.`, | ||
builder: _ => _ | ||
.option(`prefix-paths`, { | ||
type: `string`, | ||
describe: `Build site with link paths prefixed (set prefix in your config).`, | ||
}), | ||
handler: argv => { | ||
process.env.NODE_ENV = `production` | ||
const { sitePackageJson, browserslist } = getSiteInfo() | ||
|
||
resolveLocalCommand(`build`)({ | ||
...argv, | ||
directory, | ||
sitePackageJson, | ||
browserslist, | ||
}) | ||
}, | ||
}) | ||
|
||
cli | ||
.command({ | ||
command: `serve`, | ||
desc: `Serve previously built Gatsby site.`, | ||
builder: _ => _ | ||
.option(`H`, { | ||
alias: `host`, | ||
type: `string`, | ||
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`, | ||
default: `8000`, | ||
describe: `Open the site in your browser for you.`, | ||
}), | ||
|
||
handler: argv => { | ||
const { sitePackageJson, browserslist } = getSiteInfo() | ||
|
||
resolveLocalCommand(`serve`)({ | ||
...argv, | ||
directory, | ||
sitePackageJson, | ||
browserslist, | ||
}) | ||
}, | ||
}) | ||
} | ||
|
||
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`) | ||
|
||
buildLocalCommands(cli, isLocalSite) | ||
|
||
return cli | ||
.command({ | ||
command: `new [rootPath] [starter]`, | ||
desc: `Create new Gatsby project.`, | ||
handler: ({ rootPath, starter = `gatsbyjs/gatsby-starter-default` }) => { | ||
require(`./init-starter`)(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)) | ||
} |
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.