Skip to content

Commit

Permalink
chore(gatsby-cli): move recipes out of gatsby (#23190)
Browse files Browse the repository at this point in the history
* chore(gatsby-cli): move recipes out of gatsby

* make typescript happy

* typescript-eslint & optional chaining do not work
  • Loading branch information
wardpeet authored Apr 17, 2020
1 parent 104f2cc commit 67b7dff
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 28 deletions.
1 change: 1 addition & 0 deletions packages/gatsby-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"fs-exists-cached": "^1.0.0",
"fs-extra": "^8.1.0",
"gatsby-core-utils": "^1.1.2",
"gatsby-recipes": "^0.0.6",
"gatsby-telemetry": "^1.2.4",
"hosted-git-info": "^3.0.4",
"is-valid-path": "^0.1.1",
Expand Down
14 changes: 4 additions & 10 deletions packages/gatsby-cli/src/create-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
setDefaultTags,
setTelemetryEnabled,
} = require(`gatsby-telemetry`)
const { recipesHandler } = require(`./recipes`)

const handlerP = fn => (...args) => {
Promise.resolve(fn(...args)).then(
Expand Down Expand Up @@ -293,18 +294,11 @@ function buildLocalCommands(cli, isLocalSite) {
return cmd(args)
}),
})

cli.command({
command: `recipes`,
command: `recipes [recipe]`,
desc: `[EXPERIMENTAL] Run a recipe`,
handler: handlerP(
getCommandHandler(`recipes`, (args, cmd) => {
cmd(args)
// Return an empty promise to prevent handlerP from exiting early.
// The recipe command shouldn't ever exit until the user directly
// kills it so this is fine.
return new Promise(resolve => {})
})
),
handler: handlerP(({ recipe }) => recipesHandler(recipe)),
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import telemetry from "gatsby-telemetry"
import { trackCli } from "gatsby-telemetry"
import execa from "execa"
import path from "path"
import fs from "fs"
import * as path from "path"
import * as fs from "fs"
import detectPort from "detect-port"

import { IProgram } from "./types"

module.exports = async (program: IProgram): Promise<void> => {
const recipe = program._[1]
export async function recipesHandler(recipe: string): Promise<void> {
// We don't really care what port is used for GraphQL as it's
// generally only for code to code communication or debugging.
const graphqlPort = await detectPort(4000)
telemetry.trackCli(`RECIPE_RUN`, { name: recipe })
trackCli(`RECIPE_RUN`, { name: recipe })

// Start GraphQL serve
const scriptPath = require.resolve(`gatsby-recipes/dist/graphql.js`)

const subprocess = execa(`node`, [scriptPath, graphqlPort], {
cwd: program.directory,
all: true,
env: {
// Chalk doesn't want to output color in a child process
Expand All @@ -26,28 +22,36 @@ module.exports = async (program: IProgram): Promise<void> => {
FORCE_COLOR: `true`,
},
})
subprocess.stderr.on(`data`, data => {

// eslint-disable-next-line no-unused-expressions
subprocess.stderr?.on(`data`, data => {
console.log(data.toString())
})
process.on(`exit`, () =>

process.on(`exit`, () => {
subprocess.kill(`SIGTERM`, {
forceKillAfterTimeout: 2000,
})
)
})

// Log server output to a file.
if (process.env.DEBUG) {
const logFile = path.join(program.directory, `./recipe-server.log`)
const logFile = path.resolve(`./recipe-server.log`)
fs.writeFileSync(logFile, `\n-----\n${new Date().toJSON()}\n`)
const writeStream = fs.createWriteStream(logFile, { flags: `a` })
subprocess.stdout.pipe(writeStream)
// eslint-disable-next-line no-unused-expressions
subprocess.stdout?.pipe(writeStream)
}

let started = false
subprocess.stdout.on(`data`, () => {
// eslint-disable-next-line no-unused-expressions
subprocess.stdout?.on(`data`, () => {
if (!started) {
const runRecipe = require(`gatsby-recipes/dist/index.js`)
runRecipe({ recipe, graphqlPort, projectRoot: program.directory })
runRecipe({ recipe, graphqlPort, projectRoot: process.cwd() })
started = true
}
})

return subprocess.then(() => {})
}
1 change: 0 additions & 1 deletion packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
"gatsby-link": "^2.3.3",
"gatsby-plugin-page-creator": "^2.2.2",
"gatsby-react-router-scroll": "^2.2.1",
"gatsby-recipes": "^0.0.6",
"gatsby-telemetry": "^1.2.4",
"glob": "^7.1.6",
"got": "8.3.2",
Expand Down
1 change: 0 additions & 1 deletion packages/gatsby/src/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export interface IProgram {
https?: boolean
sitePackageJson: PackageJson
ssl?: ICert
_?: any
}

// @deprecated
Expand Down

0 comments on commit 67b7dff

Please sign in to comment.