From 896f76c0b2c676a7dd863b7403d530aa01a42ce8 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sun, 17 Apr 2022 13:14:04 +0800 Subject: [PATCH] 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'), ),