Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): handle case where package.json is not available at CWD #7187

Merged
merged 4 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
Expand Down
14 changes: 11 additions & 3 deletions packages/docusaurus/bin/beforeCli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ 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<string, any>} */
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 = {};
slorber marked this conversation as resolved.
Show resolved Hide resolved
}

const {
name,
version,
engines: {node: requiredVersion},
} = packageJson;

process.env.DOCUSAURUS_VERSION = version;
Josh-Cena marked this conversation as resolved.
Show resolved Hide resolved

/**
* Notify user if `@docusaurus` packages are outdated
*
Expand Down
5 changes: 1 addition & 4 deletions packages/docusaurus/bin/docusaurus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,9 +28,7 @@ await beforeCli();

const resolveDir = (dir = '.') => fs.realpath(dir);

cli
.version(createRequire(import.meta.url)('../package.json').version)
.usage('<command> [options]');
cli.version(process.env.DOCUSAURUS_VERSION).usage('<command> [options]');

cli
.command('build [siteDir]')
Expand Down
5 changes: 1 addition & 4 deletions packages/docusaurus/src/client/serverEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -132,7 +129,7 @@ async function doRender(locals: Locals & {path: string}) {
scripts,
stylesheets,
noIndex,
version: packageJson.version,
version: process.env.DOCUSAURUS_VERSION,
});

try {
Expand Down
4 changes: 1 addition & 3 deletions packages/docusaurus/src/server/siteMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ export async function loadSiteMetadata({
siteDir: string;
}): Promise<SiteMetadata> {
const siteMetadata: SiteMetadata = {
docusaurusVersion: (await getPackageJsonVersion(
path.join(__dirname, '../../package.json'),
))!,
docusaurusVersion: process.env.DOCUSAURUS_VERSION!,
siteVersion: await getPackageJsonVersion(
path.join(siteDir, 'package.json'),
),
Expand Down