From 896f76c0b2c676a7dd863b7403d530aa01a42ce8 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sun, 17 Apr 2022 13:14:04 +0800 Subject: [PATCH 1/4] fix(core): handle case where package.json is not available at CWD --- packages/docusaurus/bin/beforeCli.mjs | 14 +++++++++++--- packages/docusaurus/bin/docusaurus.mjs | 5 +---- packages/docusaurus/src/client/serverEntry.tsx | 5 +---- packages/docusaurus/src/server/siteMetadata.ts | 4 +--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/docusaurus/bin/beforeCli.mjs b/packages/docusaurus/bin/beforeCli.mjs index 53c5ee9e84d3..16fcfb783b3d 100644 --- a/packages/docusaurus/bin/beforeCli.mjs +++ b/packages/docusaurus/bin/beforeCli.mjs @@ -16,9 +16,15 @@ import boxen from 'boxen'; import {createRequire} from 'module'; const packageJson = createRequire(import.meta.url)('../package.json'); -const sitePkg = createRequire(path.join(process.cwd(), 'package.json'))( - './package.json', -); +/** @type {Record} */ +let sitePkg; +try { + sitePkg = createRequire(path.resolve('package.json'))('./package.json'); +} catch { + logger.warn`path=${'package.json'} file not found at CWD: path=${process.cwd()}.`; + logger.info`This is non-critical, but could lead to non-deterministic behavior downstream. Docusaurus assumes that path=${'package.json'} exists at CWD, because it's where the package manager looks up the script at. A common reason is because you have changed directory in the script. Instead of writing code=${'"start": "cd website && docusaurus start"'}, consider using the code=${'[siteDir]'} argument: code=${'"start": "docusaurus start website"'}.`; + sitePkg = {}; +} const { name, @@ -26,6 +32,8 @@ const { engines: {node: requiredVersion}, } = packageJson; +process.env.DOCUSAURUS_VERSION = version; + /** * Notify user if `@docusaurus` packages are outdated * diff --git a/packages/docusaurus/bin/docusaurus.mjs b/packages/docusaurus/bin/docusaurus.mjs index eee4fc9919e9..cf174771c24d 100755 --- a/packages/docusaurus/bin/docusaurus.mjs +++ b/packages/docusaurus/bin/docusaurus.mjs @@ -11,7 +11,6 @@ import logger from '@docusaurus/logger'; import fs from 'fs-extra'; import cli from 'commander'; -import {createRequire} from 'module'; import { build, swizzle, @@ -29,9 +28,7 @@ await beforeCli(); const resolveDir = (dir = '.') => fs.realpath(dir); -cli - .version(createRequire(import.meta.url)('../package.json').version) - .usage(' [options]'); +cli.version(process.env.DOCUSAURUS_VERSION).usage(' [options]'); cli .command('build [siteDir]') diff --git a/packages/docusaurus/src/client/serverEntry.tsx b/packages/docusaurus/src/client/serverEntry.tsx index 4ff192881b5f..95e7a47b5073 100644 --- a/packages/docusaurus/src/client/serverEntry.tsx +++ b/packages/docusaurus/src/client/serverEntry.tsx @@ -27,9 +27,6 @@ import logger from '@docusaurus/logger'; import _ from 'lodash'; import type {Locals} from '@slorber/static-site-generator-webpack-plugin'; -// eslint-disable-next-line @typescript-eslint/no-var-requires -const packageJson = require('../../package.json'); - const getCompiledSSRTemplate = _.memoize((template: string) => eta.compile(template.trim(), { rmWhitespace: true, @@ -132,7 +129,7 @@ async function doRender(locals: Locals & {path: string}) { scripts, stylesheets, noIndex, - version: packageJson.version, + version: process.env.DOCUSAURUS_VERSION, }); try { diff --git a/packages/docusaurus/src/server/siteMetadata.ts b/packages/docusaurus/src/server/siteMetadata.ts index ec855f94d57b..dba0e0037c9c 100644 --- a/packages/docusaurus/src/server/siteMetadata.ts +++ b/packages/docusaurus/src/server/siteMetadata.ts @@ -98,9 +98,7 @@ export async function loadSiteMetadata({ siteDir: string; }): Promise { const siteMetadata: SiteMetadata = { - docusaurusVersion: (await getPackageJsonVersion( - path.join(__dirname, '../../package.json'), - ))!, + docusaurusVersion: process.env.DOCUSAURUS_VERSION!, siteVersion: await getPackageJsonVersion( path.join(siteDir, 'package.json'), ), From 8b92b981f1e3653d3efb4d91d55e202ef1d359d2 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sun, 17 Apr 2022 13:27:50 +0800 Subject: [PATCH 2/4] fix --- jest.config.mjs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/jest.config.mjs b/jest.config.mjs index 5c241b301ed8..b7017ccd9183 100644 --- a/jest.config.mjs +++ b/jest.config.mjs @@ -6,8 +6,12 @@ */ import {fileURLToPath} from 'url'; +import {createRequire} from 'module'; process.env.TZ = 'UTC'; +process.env.DOCUSAURUS_VERSION = createRequire(import.meta.url)( + '@docusaurus/core/package.json', +).version; const ignorePatterns = [ '/node_modules/', From 7cc5aa4a3f7d8ffae06c0468233cfd8de5b9b26c Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sun, 17 Apr 2022 13:47:39 +0800 Subject: [PATCH 3/4] better wording --- packages/docusaurus/bin/beforeCli.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus/bin/beforeCli.mjs b/packages/docusaurus/bin/beforeCli.mjs index 16fcfb783b3d..cca422a1f1a4 100644 --- a/packages/docusaurus/bin/beforeCli.mjs +++ b/packages/docusaurus/bin/beforeCli.mjs @@ -22,7 +22,7 @@ try { sitePkg = createRequire(path.resolve('package.json'))('./package.json'); } catch { logger.warn`path=${'package.json'} file not found at CWD: path=${process.cwd()}.`; - logger.info`This is non-critical, but could lead to non-deterministic behavior downstream. Docusaurus assumes that path=${'package.json'} exists at CWD, because it's where the package manager looks up the script at. A common reason is because you have changed directory in the script. Instead of writing code=${'"start": "cd website && docusaurus start"'}, consider using the code=${'[siteDir]'} argument: code=${'"start": "docusaurus start website"'}.`; + logger.info`This is non-critical, but could lead to undesired behavior downstream. Docusaurus assumes that path=${'package.json'} exists at CWD, because it's where the package manager looks up the script at. A common reason is because you have changed directory in the script. Instead of writing code=${'"start": "cd website && docusaurus start"'}, consider using the code=${'[siteDir]'} argument: code=${'"start": "docusaurus start website"'}.`; sitePkg = {}; } From ec720ab499708db5c00e585c99fb6c2e0e7d9cc7 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Thu, 21 Apr 2022 18:13:34 +0800 Subject: [PATCH 4/4] move docusaurus version to utils --- jest.config.mjs | 4 ---- packages/docusaurus-utils/src/constants.ts | 4 ++++ packages/docusaurus-utils/src/index.ts | 1 + packages/docusaurus/bin/beforeCli.mjs | 6 ++---- packages/docusaurus/bin/docusaurus.mjs | 3 ++- packages/docusaurus/src/client/serverEntry.tsx | 3 ++- packages/docusaurus/src/server/siteMetadata.ts | 3 ++- 7 files changed, 13 insertions(+), 11 deletions(-) diff --git a/jest.config.mjs b/jest.config.mjs index b7017ccd9183..5c241b301ed8 100644 --- a/jest.config.mjs +++ b/jest.config.mjs @@ -6,12 +6,8 @@ */ import {fileURLToPath} from 'url'; -import {createRequire} from 'module'; process.env.TZ = 'UTC'; -process.env.DOCUSAURUS_VERSION = createRequire(import.meta.url)( - '@docusaurus/core/package.json', -).version; const ignorePatterns = [ '/node_modules/', diff --git a/packages/docusaurus-utils/src/constants.ts b/packages/docusaurus-utils/src/constants.ts index 0b413e6f4182..a9a91394f48c 100644 --- a/packages/docusaurus-utils/src/constants.ts +++ b/packages/docusaurus-utils/src/constants.ts @@ -16,6 +16,10 @@ export const NODE_MINOR_VERSION = parseInt( 10, ); +/** Docusaurus core version. */ +// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires +export const DOCUSAURUS_VERSION = require('../package.json').version; + /** * Can be overridden with cli option `--out-dir`. Code should generally use * `context.outDir` instead (which is always absolute and localized). diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index 575f67e62920..07ff78dc98c4 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -8,6 +8,7 @@ export { NODE_MAJOR_VERSION, NODE_MINOR_VERSION, + DOCUSAURUS_VERSION, DEFAULT_BUILD_DIR_NAME, DEFAULT_CONFIG_FILE_NAME, BABEL_CONFIG_FILE_NAME, diff --git a/packages/docusaurus/bin/beforeCli.mjs b/packages/docusaurus/bin/beforeCli.mjs index cca422a1f1a4..5df4d965ee79 100644 --- a/packages/docusaurus/bin/beforeCli.mjs +++ b/packages/docusaurus/bin/beforeCli.mjs @@ -14,6 +14,7 @@ import path from 'path'; import updateNotifier from 'update-notifier'; import boxen from 'boxen'; import {createRequire} from 'module'; +import {DOCUSAURUS_VERSION} from '@docusaurus/utils'; const packageJson = createRequire(import.meta.url)('../package.json'); /** @type {Record} */ @@ -28,12 +29,9 @@ try { const { name, - version, engines: {node: requiredVersion}, } = packageJson; -process.env.DOCUSAURUS_VERSION = version; - /** * Notify user if `@docusaurus` packages are outdated * @@ -48,7 +46,7 @@ export default async function beforeCli() { const notifier = updateNotifier({ pkg: { name, - version, + version: DOCUSAURUS_VERSION, }, // Check is in background so it's fine to use a small value like 1h // Use 0 for debugging diff --git a/packages/docusaurus/bin/docusaurus.mjs b/packages/docusaurus/bin/docusaurus.mjs index cf174771c24d..d8b6a418c80b 100755 --- a/packages/docusaurus/bin/docusaurus.mjs +++ b/packages/docusaurus/bin/docusaurus.mjs @@ -11,6 +11,7 @@ import logger from '@docusaurus/logger'; import fs from 'fs-extra'; import cli from 'commander'; +import {DOCUSAURUS_VERSION} from '@docusaurus/utils'; import { build, swizzle, @@ -28,7 +29,7 @@ await beforeCli(); const resolveDir = (dir = '.') => fs.realpath(dir); -cli.version(process.env.DOCUSAURUS_VERSION).usage(' [options]'); +cli.version(DOCUSAURUS_VERSION).usage(' [options]'); cli .command('build [siteDir]') diff --git a/packages/docusaurus/src/client/serverEntry.tsx b/packages/docusaurus/src/client/serverEntry.tsx index 95e7a47b5073..4c9bf0241cd1 100644 --- a/packages/docusaurus/src/client/serverEntry.tsx +++ b/packages/docusaurus/src/client/serverEntry.tsx @@ -26,6 +26,7 @@ import logger from '@docusaurus/logger'; // eslint-disable-next-line no-restricted-imports import _ from 'lodash'; import type {Locals} from '@slorber/static-site-generator-webpack-plugin'; +import {DOCUSAURUS_VERSION} from '@docusaurus/utils'; const getCompiledSSRTemplate = _.memoize((template: string) => eta.compile(template.trim(), { @@ -129,7 +130,7 @@ async function doRender(locals: Locals & {path: string}) { scripts, stylesheets, noIndex, - version: process.env.DOCUSAURUS_VERSION, + version: DOCUSAURUS_VERSION, }); try { diff --git a/packages/docusaurus/src/server/siteMetadata.ts b/packages/docusaurus/src/server/siteMetadata.ts index dba0e0037c9c..bde32ad967c3 100644 --- a/packages/docusaurus/src/server/siteMetadata.ts +++ b/packages/docusaurus/src/server/siteMetadata.ts @@ -10,6 +10,7 @@ import type { PluginVersionInformation, SiteMetadata, } from '@docusaurus/types'; +import {DOCUSAURUS_VERSION} from '@docusaurus/utils'; import fs from 'fs-extra'; import path from 'path'; import logger from '@docusaurus/logger'; @@ -98,7 +99,7 @@ export async function loadSiteMetadata({ siteDir: string; }): Promise { const siteMetadata: SiteMetadata = { - docusaurusVersion: process.env.DOCUSAURUS_VERSION!, + docusaurusVersion: DOCUSAURUS_VERSION, siteVersion: await getPackageJsonVersion( path.join(siteDir, 'package.json'), ),