Skip to content

Commit

Permalink
Move site-metadata into its own helper, and export node stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Aug 5, 2020
1 parent 36367c4 commit 29cdd16
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 22 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/node"
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
1 change: 1 addition & 0 deletions packages/gatsby-core-utils/node.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dist/node"
1 change: 1 addition & 0 deletions packages/gatsby-core-utils/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./dist/node")
5 changes: 4 additions & 1 deletion 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 @@ -26,7 +27,9 @@
},
"files": [
"dist/",
"dist/index.d.ts"
"dist/index.d.ts",
"node.js",
"node.d.ts"
],
"dependencies": {
"ci-info": "2.0.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-core-utils/src/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./service-lock"
export * from "./site-metadata"
30 changes: 30 additions & 0 deletions packages/gatsby-core-utils/src/site-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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)) || {}
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"
]
}
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/node`)

// 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createServiceLock } = require(`gatsby-core-utils/dist/service-lock`)
const { createServiceLock } = require(`gatsby-core-utils/node`)
const execa = require(`execa`)

const startServer = async () => {
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/node`)
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/node"

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
8 changes: 4 additions & 4 deletions packages/gatsby/src/commands/develop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { isCI, slash } from "gatsby-core-utils"
import {
createServiceLock,
getService,
} from "gatsby-core-utils/dist/service-lock"
updateSiteMetadata,
} from "gatsby-core-utils/node"
import { UnlockFn } from "gatsby-core-utils/src/service-lock"
import reporter from "gatsby-cli/lib/reporter"
import { getSslCert } from "../utils/get-ssl-cert"
Expand Down Expand Up @@ -268,13 +269,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/node"
import st from "st"
import restartingScreen from "./restarting-screen"
import { IProgram } from "../commands/types"
Expand Down

0 comments on commit 29cdd16

Please sign in to comment.