-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathrecipes.ts
59 lines (51 loc) · 1.79 KB
/
recipes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { trackCli } from "gatsby-telemetry"
import execa from "execa"
import * as path from "path"
import * as fs from "fs"
import detectPort from "detect-port"
export async function recipesHandler(
recipe: string | undefined
): 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)
trackCli(`RECIPE_RUN`, { name: recipe })
// Start GraphQL serve
const scriptPath = require.resolve(`gatsby-recipes/dist/graphql.js`)
const subprocess = execa(`node`, [scriptPath, graphqlPort], {
all: true,
env: {
// Chalk doesn't want to output color in a child process
// as it (correctly) thinks it's not in a normal terminal environemnt.
// Since we're just returning data, we'll override that.
FORCE_COLOR: `true`,
},
})
// eslint-disable-next-line no-unused-expressions
subprocess.stderr?.on(`data`, data => {
console.log(data.toString())
})
process.on(`exit`, () => {
subprocess.kill(`SIGTERM`, {
forceKillAfterTimeout: 2000,
})
})
// Log server output to a file.
if (process.env.DEBUG) {
const logFile = path.resolve(`./recipe-server.log`)
fs.writeFileSync(logFile, `\n-----\n${new Date().toJSON()}\n`)
const writeStream = fs.createWriteStream(logFile, { flags: `a` })
// eslint-disable-next-line no-unused-expressions
subprocess.stdout?.pipe(writeStream)
}
let started = false
// 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: process.cwd() })
started = true
}
})
return subprocess.then(() => {})
}