From d5a079deff4220d8bf6ca8dd74c6c34fad831bab Mon Sep 17 00:00:00 2001 From: Reece Dunham Date: Wed, 4 May 2022 15:28:44 -0400 Subject: [PATCH] Revert "fix(core): handle case where package.json is not available at CWD (#7187)" This reverts commit 3b32a41f229a1cbeb869ba4232035af3ad6b8a37. --- packages/docusaurus-utils/src/constants.ts | 4 ---- packages/docusaurus-utils/src/index.ts | 1 - packages/docusaurus/bin/beforeCli.mjs | 16 +++++----------- packages/docusaurus/bin/docusaurus.mjs | 6 ++++-- packages/docusaurus/src/client/serverEntry.tsx | 6 ++++-- packages/docusaurus/src/server/siteMetadata.ts | 5 +++-- 6 files changed, 16 insertions(+), 22 deletions(-) diff --git a/packages/docusaurus-utils/src/constants.ts b/packages/docusaurus-utils/src/constants.ts index a9a91394f48c..0b413e6f4182 100644 --- a/packages/docusaurus-utils/src/constants.ts +++ b/packages/docusaurus-utils/src/constants.ts @@ -16,10 +16,6 @@ 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 fb2c66435ea2..6431c9192658 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -8,7 +8,6 @@ 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 0c374dba1722..522867ec1b55 100644 --- a/packages/docusaurus/bin/beforeCli.mjs +++ b/packages/docusaurus/bin/beforeCli.mjs @@ -14,21 +14,15 @@ 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} */ -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 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 = {}; -} +const sitePkg = createRequire(path.join(process.cwd(), 'package.json'))( + './package.json', +); const { name, + version, engines: {node: requiredVersion}, } = packageJson; @@ -46,7 +40,7 @@ export default async function beforeCli() { const notifier = updateNotifier({ pkg: { name, - version: DOCUSAURUS_VERSION, + 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 759aad41cc98..8d156d790185 100755 --- a/packages/docusaurus/bin/docusaurus.mjs +++ b/packages/docusaurus/bin/docusaurus.mjs @@ -11,7 +11,7 @@ import logger from '@docusaurus/logger'; import fs from 'fs-extra'; import cli from 'commander'; -import {DOCUSAURUS_VERSION} from '@docusaurus/utils'; +import {createRequire} from 'module'; import { build, swizzle, @@ -29,7 +29,9 @@ await beforeCli(); const resolveDir = (dir = '.') => fs.realpath(dir); -cli.version(DOCUSAURUS_VERSION).usage(' [options]'); +cli + .version(createRequire(import.meta.url)('../package.json').version) + .usage(' [options]'); cli .command('build [siteDir]') diff --git a/packages/docusaurus/src/client/serverEntry.tsx b/packages/docusaurus/src/client/serverEntry.tsx index 2f14ecabd238..ec01d1b9b441 100644 --- a/packages/docusaurus/src/client/serverEntry.tsx +++ b/packages/docusaurus/src/client/serverEntry.tsx @@ -26,7 +26,9 @@ 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'; + +// eslint-disable-next-line @typescript-eslint/no-var-requires +const packageJson = require('../../package.json'); const getCompiledSSRTemplate = _.memoize((template: string) => eta.compile(template.trim(), { @@ -129,7 +131,7 @@ async function doRender(locals: Locals & {path: string}) { scripts, stylesheets, noIndex, - version: DOCUSAURUS_VERSION, + version: packageJson.version, }); try { diff --git a/packages/docusaurus/src/server/siteMetadata.ts b/packages/docusaurus/src/server/siteMetadata.ts index bde32ad967c3..ec855f94d57b 100644 --- a/packages/docusaurus/src/server/siteMetadata.ts +++ b/packages/docusaurus/src/server/siteMetadata.ts @@ -10,7 +10,6 @@ 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'; @@ -99,7 +98,9 @@ export async function loadSiteMetadata({ siteDir: string; }): Promise { const siteMetadata: SiteMetadata = { - docusaurusVersion: DOCUSAURUS_VERSION, + docusaurusVersion: (await getPackageJsonVersion( + path.join(__dirname, '../../package.json'), + ))!, siteVersion: await getPackageJsonVersion( path.join(siteDir, 'package.json'), ),