UNPKG

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