Skip to content

Commit

Permalink
fix(core): handle case where package.json is not available at CWD
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Apr 17, 2022
1 parent 2000090 commit 896f76c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
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 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,
version,
engines: {node: requiredVersion},
} = packageJson;

process.env.DOCUSAURUS_VERSION = version;

/**
* 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

0 comments on commit 896f76c

Please sign in to comment.