Skip to content

Commit 36367c4

Browse files
ascorbicgatsbybot
and
gatsbybot
authored
feat(gatsby): Store site metadata (#26162)
Store pid Allow lockfile to be ignored for services that don't need to be running Get correct port for running service Add last run time init site info in gatsby new Store metadata on build Make getService generic so we can avoid casting Log exception Co-authored-by: gatsbybot <[email protected]>
1 parent edefce7 commit 36367c4

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

packages/gatsby-cli/src/init-starter.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import isValid from "is-valid-path"
99
import sysPath from "path"
1010
import prompts from "prompts"
1111
import url from "url"
12-
12+
import { createServiceLock } from "gatsby-core-utils/dist/service-lock"
1313
import report from "./reporter"
1414
import { getPackageManager, promptPackageManager } from "./util/package-manager"
1515
import { isTTY } from "./util/is-tty"
16+
import reporter from "../lib/reporter"
1617

1718
const spawnWithArgs = (
1819
file: string,
@@ -341,8 +342,28 @@ export async function initStarter(
341342
trackCli(`NEW_PROJECT`, {
342343
starterName: hostedInfo ? hostedInfo.shortcut() : `local:starter`,
343344
})
344-
if (hostedInfo) await clone(hostedInfo, rootPath)
345-
else await copy(starterPath, rootPath)
345+
if (hostedInfo) {
346+
await clone(hostedInfo, rootPath)
347+
} else {
348+
await copy(starterPath, rootPath)
349+
}
350+
351+
const sitePath = sysPath.resolve(rootPath)
352+
353+
const sitePackageJson = await fs
354+
.readJSON(sysPath.join(sitePath, `package.json`))
355+
.catch(() => {
356+
reporter.verbose(
357+
`Could not read "${sysPath.join(sitePath, `package.json`)}"`
358+
)
359+
})
360+
361+
await createServiceLock(sitePath, `metadata`, {
362+
name: sitePackageJson?.name || rootPath,
363+
sitePath,
364+
lastRun: Date.now(),
365+
}).then(unlock => unlock?.())
366+
346367
successMessage(rootPath)
347368
trackCli(`NEW_PROJECT_END`)
348369
}

packages/gatsby-core-utils/src/service-lock.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,21 @@ export const createServiceLock = async (
6666
}
6767
}
6868

69-
export const getService = async (
69+
export const getService = async <T = Record<string, unknown>>(
7070
programPath: string,
71-
serviceName: string
72-
): Promise<string | null> => {
71+
serviceName: string,
72+
ignoreLockfile: boolean = false
73+
): Promise<T | null> => {
7374
if (isCI()) return memoryServices[serviceName] || null
7475

7576
const siteDir = getSiteDir(programPath)
7677
const serviceDataFile = getDataFilePath(siteDir, serviceName)
7778

7879
try {
79-
if (await lockfile.check(serviceDataFile, lockfileOptions)) {
80+
if (
81+
ignoreLockfile ||
82+
(await lockfile.check(serviceDataFile, lockfileOptions))
83+
) {
8084
return JSON.parse(
8185
await fs.readFile(serviceDataFile, `utf8`).catch(() => `null`)
8286
)

packages/gatsby/src/commands/build.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
markWebpackStatusAsPending,
3737
markWebpackStatusAsDone,
3838
} from "../utils/webpack-status"
39+
import { createServiceLock } from "gatsby-core-utils/dist/service-lock"
3940

4041
let cachedPageData
4142
let cachedWebpackCompilationHash
@@ -63,6 +64,12 @@ module.exports = async function build(program: IBuildArgs): Promise<void> {
6364
)
6465
}
6566

67+
await createServiceLock(program.directory, `metadata`, {
68+
name: program.sitePackageJson.name,
69+
sitePath: program.directory,
70+
lastRun: Date.now(),
71+
}).then(unlock => unlock?.())
72+
6673
markWebpackStatusAsPending()
6774

6875
const publicDir = path.join(program.directory, `public`)

packages/gatsby/src/commands/develop.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import { detectPortInUseAndPrompt } from "../utils/detect-port-in-use-and-prompt
99
import socket from "socket.io"
1010
import fs from "fs-extra"
1111
import { isCI, slash } from "gatsby-core-utils"
12-
import { createServiceLock } from "gatsby-core-utils/dist/service-lock"
12+
import {
13+
createServiceLock,
14+
getService,
15+
} from "gatsby-core-utils/dist/service-lock"
1316
import { UnlockFn } from "gatsby-core-utils/src/service-lock"
1417
import reporter from "gatsby-cli/lib/reporter"
1518
import { getSslCert } from "../utils/get-ssl-cert"
@@ -265,10 +268,19 @@ module.exports = async (program: IProgram): Promise<void> => {
265268
port: proxyPort,
266269
}
267270
)
271+
// We don't need to keep a lock on this, as it's just site metadata
272+
await createServiceLock(program.directory, `metadata`, {
273+
name: program.sitePackageJson.name,
274+
sitePath: program.directory,
275+
pid: process.pid,
276+
lastRun: Date.now(),
277+
}).then(unlock => unlock?.())
268278

269279
if (!statusUnlock || !developUnlock) {
280+
const data = await getService(program.directory, `developproxy`)
281+
const port = data?.port || 8000
270282
console.error(
271-
`Looks like develop for this site is already running. Try visiting http://localhost:8000/ maybe?`
283+
`Looks like develop for this site is already running. Try visiting http://localhost:${port}/ maybe?`
272284
)
273285
process.exit(1)
274286
}

0 commit comments

Comments
 (0)