|
| 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