From cc3156745907bd321886bd513ea7c2170e3ad1c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henry=20V=C3=A9lez?= Date: Mon, 28 Sep 2020 09:33:35 -0500 Subject: [PATCH] feat(v2): new docusaurus clear command (#3446) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: new docusaurus clear command * Update packages/docusaurus/bin/docusaurus.js Co-authored-by: Alexey Pyltsyn * docs: new docusaurus clear command * fix doc * update monorepo clear cmd * make clear async function wait for deletes to complete + factorize a bit * prettier fix * better clear docs Co-authored-by: Alexey Pyltsyn Co-authored-by: slorber Co-authored-by: Sébastien Lorber --- package.json | 2 +- packages/docusaurus/bin/docusaurus.js | 12 ++++++++- packages/docusaurus/src/commands/clear.ts | 30 +++++++++++++++++++++++ packages/docusaurus/src/index.ts | 1 + website/docs/cli.md | 9 ++++++- website/package.json | 1 + 6 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 packages/docusaurus/src/commands/clear.ts diff --git a/package.json b/package.json index c526e81043..f0e8442c12 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "test": "jest", "test:build:v2": "./admin/scripts/test-release.sh", "watch": "yarn lerna run --parallel --no-private watch", - "clear": "yarn rimraf website/.docusaurus && rimraf -rf website/node_modules/.cache && yarn lerna exec 'yarn rimraf lib' --ignore docusaurus", + "clear": "yarn workspace docusaurus-2-website clear && yarn lerna exec 'yarn rimraf lib' --ignore docusaurus", "test:v1Migration:migrate": "rimraf website-1.x-migrated && docusaurus-migrate migrate ./website-1.x ./website-1.x-migrated && sed -i -- 's/docusaurus-1-website/docusaurus-1-website-migrated/g;' website-1.x-migrated/package.json", "test:v1Migration:start": "yarn workspace docusaurus-1-website-migrated start", "test:v1Migration:build": "yarn workspace docusaurus-1-website-migrated build", diff --git a/packages/docusaurus/bin/docusaurus.js b/packages/docusaurus/bin/docusaurus.js index e5b4d9faa4..ec3f39594a 100755 --- a/packages/docusaurus/bin/docusaurus.js +++ b/packages/docusaurus/bin/docusaurus.js @@ -18,6 +18,7 @@ const { start, externalCommand, serve, + clear, } = require('../lib'); const requiredVersion = require('../package.json').engines.node; const pkg = require('../package.json'); @@ -187,6 +188,13 @@ cli }, ); +cli + .command('clear') + .description('Remove build artifacts') + .action(() => { + wrapCommand(clear)(path.resolve('.')); + }); + cli.arguments('').action((cmd) => { cli.outputHelp(); console.log(` ${chalk.red(`\n Unknown command ${chalk.yellow(cmd)}.`)}`); @@ -194,7 +202,9 @@ cli.arguments('').action((cmd) => { }); function isInternalCommand(command) { - return ['start', 'build', 'swizzle', 'deploy', 'serve'].includes(command); + return ['start', 'build', 'swizzle', 'deploy', 'serve', 'clear'].includes( + command, + ); } if (!isInternalCommand(process.argv.slice(2)[0])) { diff --git a/packages/docusaurus/src/commands/clear.ts b/packages/docusaurus/src/commands/clear.ts new file mode 100644 index 0000000000..c221e86f9e --- /dev/null +++ b/packages/docusaurus/src/commands/clear.ts @@ -0,0 +1,30 @@ +/** + * 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 fs from 'fs-extra'; +import path from 'path'; +import chalk = require('chalk'); +import {BUILD_DIR_NAME, GENERATED_FILES_DIR_NAME} from '../constants'; + +function removePath(fsPath: string) { + return fs + .remove(path.join(fsPath)) + .then(() => { + console.log(`${chalk.green(`Removing ${fsPath}`)}`); + }) + .catch((err) => { + console.error(`Could not remove ${fsPath}`); + console.error(err); + }); +} + +export default async function clear(siteDir: string): Promise { + return Promise.all([ + removePath(path.join(siteDir, GENERATED_FILES_DIR_NAME)), + removePath(path.join(siteDir, BUILD_DIR_NAME)), + removePath(path.join(siteDir, 'node_modules/.cache/cache-loader')), + ]); +} diff --git a/packages/docusaurus/src/index.ts b/packages/docusaurus/src/index.ts index 455d5a42bd..2c9ca7bd03 100644 --- a/packages/docusaurus/src/index.ts +++ b/packages/docusaurus/src/index.ts @@ -11,3 +11,4 @@ export {default as swizzle} from './commands/swizzle'; export {default as deploy} from './commands/deploy'; export {default as externalCommand} from './commands/external'; export {default as serve} from './commands/serve'; +export {default as clear} from './commands/clear'; diff --git a/website/docs/cli.md b/website/docs/cli.md index 48ebbf3c42..8577221461 100644 --- a/website/docs/cli.md +++ b/website/docs/cli.md @@ -14,7 +14,8 @@ Once your website is bootstrapped, the website source will contain the Docusauru "start": "docusaurus start", "build": "docusaurus build", "swizzle": "docusaurus swizzle", - "deploy": "docusaurus deploy" + "deploy": "docusaurus deploy", + "clear": "docusaurus clear" } } ``` @@ -126,3 +127,9 @@ Serve your built website locally. | `--dir` | `build` | The full path for the output directory, relative to the current workspace | | `--build` | `false` | Build website before serving | | `--host` | `localhost` | Specify a host to use. For example, if you want your server to be accessible externally, you can use `--host 0.0.0.0`. | + +### `docusaurus clear` + +Clear a Docusaurus site's generated assets, caches, build artifacts... + +We recommend running this command before reporting bugs, after upgrading versions, or anytime you have issues with your Docusaurus site. diff --git a/website/package.json b/website/package.json index cb4061df37..dc654f40be 100644 --- a/website/package.json +++ b/website/package.json @@ -8,6 +8,7 @@ "build": "docusaurus build", "swizzle": "docusaurus swizzle", "deploy": "docusaurus deploy", + "clear": "docusaurus clear", "start:baseUrl": "BASE_URL='/build/' yarn start", "build:baseUrl": "BASE_URL='/build/' yarn build", "start:bootstrap": "DOCUSAURUS_PRESET=bootstrap yarn start",