Skip to content

Commit d98f00a

Browse files
author
Sergio
committed
feat: docusaurus upgrade cli command
1 parent a2efe9f commit d98f00a

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

packages/docusaurus/bin/docusaurus.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
externalCommand,
2020
serve,
2121
clear,
22+
upgrade,
2223
writeTranslations,
2324
} = require('../lib');
2425
const requiredVersion = require('../package.json').engines.node;
@@ -206,6 +207,17 @@ cli
206207
wrapCommand(clear)(path.resolve(siteDir));
207208
});
208209

210+
cli
211+
.command('upgrade')
212+
.description('Upgrades @docusaurus packages')
213+
.option(
214+
'-t, --tag <tag>',
215+
'Tag of npm to look for upgrading. This option accepts any of: <alpha, beta, next, latest>',
216+
)
217+
.action(({tag = undefined}) => {
218+
wrapCommand(upgrade)({tag});
219+
});
220+
209221
cli
210222
.command('write-translations [siteDir]')
211223
.description('Extract required translations of your site')
@@ -245,6 +257,7 @@ function isInternalCommand(command) {
245257
'start',
246258
'build',
247259
'swizzle',
260+
'upgrade',
248261
'deploy',
249262
'serve',
250263
'clear',
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
import chalk = require('chalk');
8+
import fs from 'fs-extra';
9+
import path from 'path';
10+
import os from 'os';
11+
import childProcess from 'child_process';
12+
import clear from './clear';
13+
14+
function hasYarn() {
15+
return fs.existsSync(path.resolve(path.dirname(process.cwd()), 'yarn.lock'));
16+
}
17+
18+
type CommonNpmTags = 'alpha' | 'beta' | 'latest' | 'next';
19+
export default function upgrade(opts: {tag: CommonNpmTags}): void {
20+
const [npmClient, npmCommand] = hasYarn()
21+
? ['yarn', 'upgrade']
22+
: ['npm', 'install'];
23+
24+
const {dependencies = {}, devDependencies = {}} = JSON.parse(
25+
fs.readFileSync(path.join(process.cwd(), './package.json'), 'utf8'),
26+
);
27+
28+
let packageNames = Array.from(
29+
new Set(
30+
[
31+
...Object.keys(dependencies),
32+
...Object.keys(devDependencies),
33+
].filter((pkg) => pkg.startsWith('@docusaurus')),
34+
),
35+
).sort();
36+
37+
if (!packageNames.length) {
38+
console.log(chalk.red(`Found 0 packages with scope @docusaurus`));
39+
return;
40+
}
41+
42+
if (opts.tag) {
43+
packageNames = packageNames.map((_) => `${_}@${opts.tag}`);
44+
}
45+
46+
console.log(chalk.green(`Found ${packageNames.length} to update:`));
47+
console.log(packageNames);
48+
console.log(
49+
chalk.cyan(
50+
`Executing "${npmClient} ${npmCommand} ${packageNames.join(' ')}"`,
51+
),
52+
);
53+
54+
childProcess.spawnSync(npmClient, [npmCommand, ...packageNames], {
55+
stdio: 'inherit',
56+
shell: os.platform() === 'win32',
57+
});
58+
59+
clear(path.resolve('.'));
60+
}

packages/docusaurus/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export {default as externalCommand} from './commands/external';
1313
export {default as serve} from './commands/serve';
1414
export {default as clear} from './commands/clear';
1515
export {default as writeTranslations} from './commands/writeTranslations';
16+
export {default as upgrade} from './commands/upgrade';

website/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"deploy": "docusaurus deploy",
1111
"clear": "docusaurus clear",
1212
"serve": "docusaurus serve",
13+
"upgrade": "docusaurus upgrade",
1314
"start:baseUrl": "cross-env BASE_URL='/build/' yarn start",
1415
"build:baseUrl": "cross-env BASE_URL='/build/' yarn build",
1516
"start:bootstrap": "cross-env DOCUSAURUS_PRESET=bootstrap yarn start",
@@ -60,4 +61,4 @@
6061
"cross-env": "^7.0.2",
6162
"raw-loader": "^4.0.1"
6263
}
63-
}
64+
}

0 commit comments

Comments
 (0)