Skip to content

Commit

Permalink
feat(gatsby-core-utils): Add node.js export, and move site-metadata i…
Browse files Browse the repository at this point in the history
…nto its own function (#26237)

* Move site-metadata into its own helper, and export node stuff

* Moving everthing to index

* fix tests

* fix packages for real

* add Peter's nit and trigger re-run

Co-authored-by: Ward Peeters <ward@coding-tech.com>
Co-authored-by: Laurie Barth <laurie@LauriesrkLaptop.fios-router.home>
  • Loading branch information
3 people committed Aug 20, 2020
1 parent 90e66c7 commit b164147
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 35 deletions.
15 changes: 9 additions & 6 deletions packages/gatsby-cli/src/init-starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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`)
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-core-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-core-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
32 changes: 32 additions & 0 deletions packages/gatsby-core-utils/src/site-metadata.ts
Original file line number Diff line number Diff line change
@@ -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<ISiteMetadata | null> {
return getService(sitePath, `metadata`, true)
}

export async function updateSiteMetadata(
metadata: ISiteMetadata,
merge = true
): Promise<void> {
if (merge) {
const oldMetadata = await getSiteMetadata(metadata.sitePath)
if (oldMetadata) {
metadata = { ...oldMetadata, ...metadata }
}
}

return createServiceLock(
metadata.sitePath,
`metadata`,
metadata
).then(unlock => unlock?.())
}
8 changes: 7 additions & 1 deletion packages/gatsby-core-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
jest.mock(`fs`, () => {
return {
...jest.requireActual(`fs`),
existsSync: jest.fn().mockImplementation(() => true),
writeFileSync: jest.fn(),
mkdirSync: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
jest.mock(`fs`, () => {
return {
...jest.requireActual(`fs`),
readFileSync: jest.fn().mockImplementation(() => `someIconImage`),
}
})
Expand Down
5 changes: 1 addition & 4 deletions packages/gatsby-recipes/src/graphql-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-recipes/src/providers/gatsby/page.js
Original file line number Diff line number Diff line change
@@ -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`)
Expand Down
7 changes: 4 additions & 3 deletions packages/gatsby/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -64,11 +64,12 @@ module.exports = async function build(program: IBuildArgs): Promise<void> {
)
}

await createServiceLock(program.directory, `metadata`, {
await updateSiteMetadata({
name: program.sitePackageJson.name,
sitePath: program.directory,
lastRun: Date.now(),
}).then(unlock => unlock?.())
pid: process.pid,
})

markWebpackStatusAsPending()

Expand Down
10 changes: 5 additions & 5 deletions packages/gatsby/src/commands/develop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -283,13 +284,12 @@ module.exports = async (program: IProgram): Promise<void> => {
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`)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/develop-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 0 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit b164147

Please sign in to comment.