mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-30 02:37:59 +02:00
fix(core): handle case where package.json is not available at CWD (#7187)
This commit is contained in:
parent
e12947e818
commit
3b32a41f22
6 changed files with 22 additions and 16 deletions
|
@ -16,6 +16,10 @@ export const NODE_MINOR_VERSION = parseInt(
|
||||||
10,
|
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
|
* Can be overridden with cli option `--out-dir`. Code should generally use
|
||||||
* `context.outDir` instead (which is always absolute and localized).
|
* `context.outDir` instead (which is always absolute and localized).
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
export {
|
export {
|
||||||
NODE_MAJOR_VERSION,
|
NODE_MAJOR_VERSION,
|
||||||
NODE_MINOR_VERSION,
|
NODE_MINOR_VERSION,
|
||||||
|
DOCUSAURUS_VERSION,
|
||||||
DEFAULT_BUILD_DIR_NAME,
|
DEFAULT_BUILD_DIR_NAME,
|
||||||
DEFAULT_CONFIG_FILE_NAME,
|
DEFAULT_CONFIG_FILE_NAME,
|
||||||
BABEL_CONFIG_FILE_NAME,
|
BABEL_CONFIG_FILE_NAME,
|
||||||
|
|
|
@ -14,15 +14,21 @@ import path from 'path';
|
||||||
import updateNotifier from 'update-notifier';
|
import updateNotifier from 'update-notifier';
|
||||||
import boxen from 'boxen';
|
import boxen from 'boxen';
|
||||||
import {createRequire} from 'module';
|
import {createRequire} from 'module';
|
||||||
|
import {DOCUSAURUS_VERSION} from '@docusaurus/utils';
|
||||||
|
|
||||||
const packageJson = createRequire(import.meta.url)('../package.json');
|
const packageJson = createRequire(import.meta.url)('../package.json');
|
||||||
const sitePkg = createRequire(path.join(process.cwd(), 'package.json'))(
|
/** @type {Record<string, any>} */
|
||||||
'./package.json',
|
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 {
|
const {
|
||||||
name,
|
name,
|
||||||
version,
|
|
||||||
engines: {node: requiredVersion},
|
engines: {node: requiredVersion},
|
||||||
} = packageJson;
|
} = packageJson;
|
||||||
|
|
||||||
|
@ -40,7 +46,7 @@ export default async function beforeCli() {
|
||||||
const notifier = updateNotifier({
|
const notifier = updateNotifier({
|
||||||
pkg: {
|
pkg: {
|
||||||
name,
|
name,
|
||||||
version,
|
version: DOCUSAURUS_VERSION,
|
||||||
},
|
},
|
||||||
// Check is in background so it's fine to use a small value like 1h
|
// Check is in background so it's fine to use a small value like 1h
|
||||||
// Use 0 for debugging
|
// Use 0 for debugging
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
import logger from '@docusaurus/logger';
|
import logger from '@docusaurus/logger';
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import cli from 'commander';
|
import cli from 'commander';
|
||||||
import {createRequire} from 'module';
|
import {DOCUSAURUS_VERSION} from '@docusaurus/utils';
|
||||||
import {
|
import {
|
||||||
build,
|
build,
|
||||||
swizzle,
|
swizzle,
|
||||||
|
@ -29,9 +29,7 @@ await beforeCli();
|
||||||
|
|
||||||
const resolveDir = (dir = '.') => fs.realpath(dir);
|
const resolveDir = (dir = '.') => fs.realpath(dir);
|
||||||
|
|
||||||
cli
|
cli.version(DOCUSAURUS_VERSION).usage('<command> [options]');
|
||||||
.version(createRequire(import.meta.url)('../package.json').version)
|
|
||||||
.usage('<command> [options]');
|
|
||||||
|
|
||||||
cli
|
cli
|
||||||
.command('build [siteDir]')
|
.command('build [siteDir]')
|
||||||
|
|
|
@ -26,9 +26,7 @@ import logger from '@docusaurus/logger';
|
||||||
// eslint-disable-next-line no-restricted-imports
|
// eslint-disable-next-line no-restricted-imports
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import type {Locals} from '@slorber/static-site-generator-webpack-plugin';
|
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) =>
|
const getCompiledSSRTemplate = _.memoize((template: string) =>
|
||||||
eta.compile(template.trim(), {
|
eta.compile(template.trim(), {
|
||||||
|
@ -131,7 +129,7 @@ async function doRender(locals: Locals & {path: string}) {
|
||||||
scripts,
|
scripts,
|
||||||
stylesheets,
|
stylesheets,
|
||||||
noIndex,
|
noIndex,
|
||||||
version: packageJson.version,
|
version: DOCUSAURUS_VERSION,
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -10,6 +10,7 @@ import type {
|
||||||
PluginVersionInformation,
|
PluginVersionInformation,
|
||||||
SiteMetadata,
|
SiteMetadata,
|
||||||
} from '@docusaurus/types';
|
} from '@docusaurus/types';
|
||||||
|
import {DOCUSAURUS_VERSION} from '@docusaurus/utils';
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import logger from '@docusaurus/logger';
|
import logger from '@docusaurus/logger';
|
||||||
|
@ -98,9 +99,7 @@ export async function loadSiteMetadata({
|
||||||
siteDir: string;
|
siteDir: string;
|
||||||
}): Promise<SiteMetadata> {
|
}): Promise<SiteMetadata> {
|
||||||
const siteMetadata: SiteMetadata = {
|
const siteMetadata: SiteMetadata = {
|
||||||
docusaurusVersion: (await getPackageJsonVersion(
|
docusaurusVersion: DOCUSAURUS_VERSION,
|
||||||
path.join(__dirname, '../../package.json'),
|
|
||||||
))!,
|
|
||||||
siteVersion: await getPackageJsonVersion(
|
siteVersion: await getPackageJsonVersion(
|
||||||
path.join(siteDir, 'package.json'),
|
path.join(siteDir, 'package.json'),
|
||||||
),
|
),
|
||||||
|
|
Loading…
Add table
Reference in a new issue