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

feat: docusaurus upgrade cli command #4082

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions packages/docusaurus/bin/docusaurus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
externalCommand,
serve,
clear,
upgrade,
writeTranslations,
writeHeadingIds,
} from '../lib/index.js';
Expand Down Expand Up @@ -189,6 +190,17 @@ cli
.description('Remove build artifacts.')
.action(clear);

cli
.command('upgrade [siteDir]')
.description('Upgrade @docusaurus packages.')
.option(
'-t, --tag <tag>',
'Tag of npm to look for upgrading. This option accepts any of: <alpha, beta, next, latest>',
)
.action(async (siteDir, {tag = undefined}) => {
upgrade(await resolveDir(siteDir), {tag});
});

cli
.command('write-translations [siteDir]')
.description('Extract required translations of your site.')
Expand Down Expand Up @@ -239,6 +251,7 @@ function isInternalCommand(command) {
'start',
'build',
'swizzle',
'upgrade',
'deploy',
'serve',
'clear',
Expand Down
59 changes: 59 additions & 0 deletions packages/docusaurus/src/commands/upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import logger from '@docusaurus/logger';
import fs from 'fs-extra';
import path from 'path';
import os from 'os';
import childProcess from 'child_process';
import {clear} from './clear';

async function hasYarn() {
return fs.pathExists(path.resolve(path.dirname(process.cwd()), 'yarn.lock'));
}

type CommonNpmTags = 'alpha' | 'beta' | 'latest' | 'next';

export async function upgrade(
siteDir: string,
{tag}: {tag: CommonNpmTags},
): Promise<void> {
const [npmClient, npmCommand] = (await hasYarn())
? ['yarn', 'upgrade']
: ['npm', 'install'];

const {dependencies = {}, devDependencies = {}} = await fs.readJSON(
path.join(siteDir, 'package.json'),
);

const packageNames = Array.from(
new Set(
[...Object.keys(dependencies), ...Object.keys(devDependencies)].filter(
(pkg) => pkg.startsWith('@docusaurus'),
),
),
)
.sort()
.map((name) => (tag ? `${name}@${tag}` : name));

if (!packageNames.length) {
logger.error(`Found 0 packages with scope @docusaurus`);
return;
}

logger.info`Found number=${packageNames.length} to update: name=${packageNames}`;
logger.info`Executing code=${`${npmClient} ${npmCommand} ${packageNames.join(
' ',
)}`}`;

childProcess.spawnSync(npmClient, [npmCommand, ...packageNames], {
stdio: 'inherit',
shell: os.platform() === 'win32',
});

await clear(siteDir);
}
1 change: 1 addition & 0 deletions packages/docusaurus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ export {externalCommand} from './commands/external';
export {serve} from './commands/serve';
export {start} from './commands/start/start';
export {swizzle} from './commands/swizzle';
export {upgrade} from './commands/upgrade';
export {writeHeadingIds} from './commands/writeHeadingIds';
export {writeTranslations} from './commands/writeTranslations';
1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"deploy": "docusaurus deploy",
"clear": "docusaurus clear && rimraf changelog && rimraf _dogfooding/_swizzle_theme_tests",
"serve": "docusaurus serve",
"upgrade": "docusaurus upgrade",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to add this to init templates as well

"test:css-order": "node testCSSOrder.mjs",
"test:swizzle:eject:js": "cross-env SWIZZLE_ACTION='eject' SWIZZLE_TYPESCRIPT='false' node _dogfooding/testSwizzleThemeClassic.mjs",
"test:swizzle:eject:ts": "cross-env SWIZZLE_ACTION='eject' SWIZZLE_TYPESCRIPT='true' node _dogfooding/testSwizzleThemeClassic.mjs",
Expand Down
Loading