diff --git a/packages/gatsby-cli/src/init-starter.ts b/packages/gatsby-cli/src/init-starter.ts index 48548e749cf03..d1c3de9683b26 100644 --- a/packages/gatsby-cli/src/init-starter.ts +++ b/packages/gatsby-cli/src/init-starter.ts @@ -9,7 +9,7 @@ import isValid from "is-valid-path" import sysPath from "path" import prompts from "prompts" import url from "url" -import { createServiceLock } from "gatsby-core-utils/dist/service-lock" +import { updateSiteMetadata } from "gatsby-core-utils" import report from "./reporter" import { getPackageManager, promptPackageManager } from "./util/package-manager" import { isTTY } from "./util/is-tty" @@ -358,11 +358,14 @@ export async function initStarter( ) }) - await createServiceLock(sitePath, `metadata`, { - name: sitePackageJson?.name || rootPath, - sitePath, - lastRun: Date.now(), - }).then(unlock => unlock?.()) + await updateSiteMetadata( + { + name: sitePackageJson?.name || rootPath, + sitePath, + lastRun: Date.now(), + }, + false + ) successMessage(rootPath) trackCli(`NEW_PROJECT_END`) diff --git a/packages/gatsby-core-utils/package.json b/packages/gatsby-core-utils/package.json index 4fbeacf761254..afa9e9cc079e2 100644 --- a/packages/gatsby-core-utils/package.json +++ b/packages/gatsby-core-utils/package.json @@ -10,6 +10,7 @@ "homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-core-utils#readme", "license": "MIT", "main": "dist/index.js", + "types": "dist/index.d.ts", "repository": { "type": "git", "url": "https://github.com/gatsbyjs/gatsby.git", @@ -34,6 +35,7 @@ "fs-extra": "^8.1.0", "node-object-hash": "^2.0.0", "proper-lockfile": "^4.1.1", + "tmp": "^0.2.1", "xdg-basedir": "^4.0.0" }, "devDependencies": { diff --git a/packages/gatsby-core-utils/src/index.ts b/packages/gatsby-core-utils/src/index.ts index e7ee972f9057f..b60b5ff681e8b 100644 --- a/packages/gatsby-core-utils/src/index.ts +++ b/packages/gatsby-core-utils/src/index.ts @@ -6,3 +6,5 @@ export { getCIName, isCI } from "./ci" export { createRequireFromPath } from "./create-require-from-path" export { getConfigStore } from "./get-config-store" export { getGatsbyVersion } from "./get-gatsby-version" +export * from "./service-lock" +export * from "./site-metadata" diff --git a/packages/gatsby-core-utils/src/site-metadata.ts b/packages/gatsby-core-utils/src/site-metadata.ts new file mode 100644 index 0000000000000..e42e6896591a2 --- /dev/null +++ b/packages/gatsby-core-utils/src/site-metadata.ts @@ -0,0 +1,32 @@ +import { createServiceLock, getService } from "./service-lock" + +export interface ISiteMetadata { + sitePath: string + name?: string + pid?: number + lastRun?: number +} + +export async function getSiteMetadata( + sitePath: string +): Promise { + return getService(sitePath, `metadata`, true) +} + +export async function updateSiteMetadata( + metadata: ISiteMetadata, + merge = true +): Promise { + if (merge) { + const oldMetadata = await getSiteMetadata(metadata.sitePath) + if (oldMetadata) { + metadata = { ...oldMetadata, ...metadata } + } + } + + return createServiceLock( + metadata.sitePath, + `metadata`, + metadata + ).then(unlock => unlock?.()) +} diff --git a/packages/gatsby-core-utils/tsconfig.json b/packages/gatsby-core-utils/tsconfig.json index dd788c21d6cab..214c464b5e456 100644 --- a/packages/gatsby-core-utils/tsconfig.json +++ b/packages/gatsby-core-utils/tsconfig.json @@ -1,4 +1,10 @@ { "extends": "../../tsconfig.json", - "exclude": ["node_modules", "src/__tests__", "src/__mocks__", "dist"] + "exclude": [ + "node_modules", + "src/__tests__", + "src/__mocks__", + "dist", + "./node.d.ts" + ] } diff --git a/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js index 84cae490d7d7e..16cfe17183cb5 100644 --- a/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js @@ -1,5 +1,6 @@ jest.mock(`fs`, () => { return { + ...jest.requireActual(`fs`), existsSync: jest.fn().mockImplementation(() => true), writeFileSync: jest.fn(), mkdirSync: jest.fn(), diff --git a/packages/gatsby-plugin-manifest/src/__tests__/gatsby-ssr.js b/packages/gatsby-plugin-manifest/src/__tests__/gatsby-ssr.js index 61bbabf6bd763..c7b73a203e153 100644 --- a/packages/gatsby-plugin-manifest/src/__tests__/gatsby-ssr.js +++ b/packages/gatsby-plugin-manifest/src/__tests__/gatsby-ssr.js @@ -1,5 +1,6 @@ jest.mock(`fs`, () => { return { + ...jest.requireActual(`fs`), readFileSync: jest.fn().mockImplementation(() => `someIconImage`), } }) diff --git a/packages/gatsby-recipes/src/graphql-server/index.js b/packages/gatsby-recipes/src/graphql-server/index.js index e5b1c631e939d..e3ca634190d14 100644 --- a/packages/gatsby-recipes/src/graphql-server/index.js +++ b/packages/gatsby-recipes/src/graphql-server/index.js @@ -2,10 +2,7 @@ const execa = require(`execa`) const path = require(`path`) const fs = require(`fs`) const detectPort = require(`detect-port`) -const { - getService, - createServiceLock, -} = require(`gatsby-core-utils/dist/service-lock`) +const { getService, createServiceLock } = require(`gatsby-core-utils`) // NOTE(@mxstbr): The forceStart boolean enforces us to start the recipes graphql server // even if another instance might already be running. This is necessary to ensure the gatsby diff --git a/packages/gatsby-recipes/src/providers/gatsby/page.js b/packages/gatsby-recipes/src/providers/gatsby/page.js index 63bf49229ec34..6aedb0f8f8279 100644 --- a/packages/gatsby-recipes/src/providers/gatsby/page.js +++ b/packages/gatsby-recipes/src/providers/gatsby/page.js @@ -1,5 +1,5 @@ const Joi = require(`@hapi/joi`) -const { getService } = require(`gatsby-core-utils/dist/service-lock`) +const { getService } = require(`gatsby-core-utils`) const fetch = require(`node-fetch`) const { REQUIRES_KEYS } = require(`./utils/constants`) diff --git a/packages/gatsby/src/commands/build.ts b/packages/gatsby/src/commands/build.ts index aad0076f589a8..06a5f065d51f3 100644 --- a/packages/gatsby/src/commands/build.ts +++ b/packages/gatsby/src/commands/build.ts @@ -36,7 +36,7 @@ import { markWebpackStatusAsPending, markWebpackStatusAsDone, } from "../utils/webpack-status" -import { createServiceLock } from "gatsby-core-utils/dist/service-lock" +import { updateSiteMetadata } from "gatsby-core-utils" let cachedPageData let cachedWebpackCompilationHash @@ -64,11 +64,12 @@ module.exports = async function build(program: IBuildArgs): Promise { ) } - await createServiceLock(program.directory, `metadata`, { + await updateSiteMetadata({ name: program.sitePackageJson.name, sitePath: program.directory, lastRun: Date.now(), - }).then(unlock => unlock?.()) + pid: process.pid, + }) markWebpackStatusAsPending() diff --git a/packages/gatsby/src/commands/develop.ts b/packages/gatsby/src/commands/develop.ts index 9d47b4f13c3cc..d9e99d8dce1e8 100644 --- a/packages/gatsby/src/commands/develop.ts +++ b/packages/gatsby/src/commands/develop.ts @@ -12,8 +12,9 @@ import { isCI, slash } from "gatsby-core-utils" import { createServiceLock, getService, -} from "gatsby-core-utils/dist/service-lock" -import { UnlockFn } from "gatsby-core-utils/src/service-lock" + updateSiteMetadata, + UnlockFn, +} from "gatsby-core-utils" import reporter from "gatsby-cli/lib/reporter" import { getSslCert } from "../utils/get-ssl-cert" import { startDevelopProxy } from "../utils/develop-proxy" @@ -283,13 +284,12 @@ module.exports = async (program: IProgram): Promise => { port: proxyPort, } ) - // We don't need to keep a lock on this, as it's just site metadata - await createServiceLock(program.directory, `metadata`, { + await updateSiteMetadata({ name: program.sitePackageJson.name, sitePath: program.directory, pid: process.pid, lastRun: Date.now(), - }).then(unlock => unlock?.()) + }) if (!statusUnlock || !developUnlock) { const data = await getService(program.directory, `developproxy`) diff --git a/packages/gatsby/src/utils/develop-proxy.ts b/packages/gatsby/src/utils/develop-proxy.ts index e0f44f780b895..9f46bf8fe78c4 100644 --- a/packages/gatsby/src/utils/develop-proxy.ts +++ b/packages/gatsby/src/utils/develop-proxy.ts @@ -3,7 +3,7 @@ import https from "https" import httpProxy from "http-proxy" import path from "path" import fs from "fs-extra" -import { getServices } from "gatsby-core-utils/dist/service-lock" +import { getServices } from "gatsby-core-utils" import st from "st" import restartingScreen from "./restarting-screen" import { IProgram } from "../commands/types" diff --git a/yarn.lock b/yarn.lock index 239e7d906b345..d487b0e506e11 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6139,20 +6139,6 @@ boxen@^1.2.1: term-size "^1.2.0" widest-line "^2.0.0" -boxen@^3.0.0: - version "3.2.0" - resolved "https://registry.yarnpkg.com/boxen/-/boxen-3.2.0.tgz#fbdff0de93636ab4450886b6ff45b92d098f45eb" - integrity sha512-cU4J/+NodM3IHdSL2yN8bqYqnmlBTidDR4RC7nJs61ZmtGz8VZzM3HLQX0zY5mrSmPtR3xWwsq2jOUQqFZN8+A== - dependencies: - ansi-align "^3.0.0" - camelcase "^5.3.1" - chalk "^2.4.2" - cli-boxes "^2.2.0" - string-width "^3.0.0" - term-size "^1.2.0" - type-fest "^0.3.0" - widest-line "^2.0.0" - boxen@^4.2.0: version "4.2.0" resolved "https://registry.yarnpkg.com/boxen/-/boxen-4.2.0.tgz#e411b62357d6d6d36587c8ac3d5d974daa070e64"