diff --git a/docs/angular/cli/daemon.md b/docs/angular/cli/daemon.md index b9644a0f37..2577feda51 100644 --- a/docs/angular/cli/daemon.md +++ b/docs/angular/cli/daemon.md @@ -2,9 +2,7 @@ EXPERIMENTAL: Nx Daemon -The Daemon is not currently running you can start it manually by running the following command: - -npx nx daemon:start +The Nx Daemon is a local server which runs in the background in order to intelligently cache information about the workspace's project graph. ## Usage diff --git a/docs/angular/cli/clear-cache.md b/docs/angular/cli/reset.md similarity index 83% rename from docs/angular/cli/clear-cache.md rename to docs/angular/cli/reset.md index 3f3a5b3c47..39dc0a2e8f 100644 --- a/docs/angular/cli/clear-cache.md +++ b/docs/angular/cli/reset.md @@ -1,11 +1,11 @@ -# clear-cache +# reset -Clears all the cached Nx artifacts and metadata about the workspace. +Clears all the cached Nx artifacts and metadata about the workspace and shuts down the Nx Daemon. ## Usage ```bash -nx clear-cache +nx reset ``` [Install `nx` globally]({{framework}}/getting-started/nx-setup#install-nx) to invoke the command directly using `nx`, or use `npx nx`, `yarn nx`, or `pnpx nx`. diff --git a/docs/map.json b/docs/map.json index b9f4bf727f..881737301c 100644 --- a/docs/map.json +++ b/docs/map.json @@ -292,9 +292,9 @@ "file": "angular/cli/connect-to-nx-cloud" }, { - "name": "clear-cache", - "id": "clear-cache", - "file": "angular/cli/clear-cache" + "name": "reset", + "id": "reset", + "file": "angular/cli/reset" } ] }, @@ -1575,9 +1575,9 @@ "file": "react/cli/connect-to-nx-cloud" }, { - "name": "clear-cache", - "id": "clear-cache", - "file": "react/cli/clear-cache" + "name": "reset", + "id": "reset", + "file": "react/cli/reset" } ] }, @@ -2822,9 +2822,9 @@ "file": "node/cli/connect-to-nx-cloud" }, { - "name": "clear-cache", - "id": "clear-cache", - "file": "node/cli/clear-cache" + "name": "reset", + "id": "reset", + "file": "node/cli/reset" } ] }, diff --git a/docs/node/cli/daemon.md b/docs/node/cli/daemon.md index b9644a0f37..2577feda51 100644 --- a/docs/node/cli/daemon.md +++ b/docs/node/cli/daemon.md @@ -2,9 +2,7 @@ EXPERIMENTAL: Nx Daemon -The Daemon is not currently running you can start it manually by running the following command: - -npx nx daemon:start +The Nx Daemon is a local server which runs in the background in order to intelligently cache information about the workspace's project graph. ## Usage diff --git a/docs/node/cli/clear-cache.md b/docs/node/cli/reset.md similarity index 83% rename from docs/node/cli/clear-cache.md rename to docs/node/cli/reset.md index 3f3a5b3c47..39dc0a2e8f 100644 --- a/docs/node/cli/clear-cache.md +++ b/docs/node/cli/reset.md @@ -1,11 +1,11 @@ -# clear-cache +# reset -Clears all the cached Nx artifacts and metadata about the workspace. +Clears all the cached Nx artifacts and metadata about the workspace and shuts down the Nx Daemon. ## Usage ```bash -nx clear-cache +nx reset ``` [Install `nx` globally]({{framework}}/getting-started/nx-setup#install-nx) to invoke the command directly using `nx`, or use `npx nx`, `yarn nx`, or `pnpx nx`. diff --git a/docs/react/cli/daemon.md b/docs/react/cli/daemon.md index b9644a0f37..2577feda51 100644 --- a/docs/react/cli/daemon.md +++ b/docs/react/cli/daemon.md @@ -2,9 +2,7 @@ EXPERIMENTAL: Nx Daemon -The Daemon is not currently running you can start it manually by running the following command: - -npx nx daemon:start +The Nx Daemon is a local server which runs in the background in order to intelligently cache information about the workspace's project graph. ## Usage diff --git a/docs/react/cli/clear-cache.md b/docs/react/cli/reset.md similarity index 83% rename from docs/react/cli/clear-cache.md rename to docs/react/cli/reset.md index 3f3a5b3c47..39dc0a2e8f 100644 --- a/docs/react/cli/clear-cache.md +++ b/docs/react/cli/reset.md @@ -1,11 +1,11 @@ -# clear-cache +# reset -Clears all the cached Nx artifacts and metadata about the workspace. +Clears all the cached Nx artifacts and metadata about the workspace and shuts down the Nx Daemon. ## Usage ```bash -nx clear-cache +nx reset ``` [Install `nx` globally]({{framework}}/getting-started/nx-setup#install-nx) to invoke the command directly using `nx`, or use `npx nx`, `yarn nx`, or `pnpx nx`. diff --git a/packages/cli/lib/parse-run-one-options.ts b/packages/cli/lib/parse-run-one-options.ts index 623b5e7233..e74c7bf09b 100644 --- a/packages/cli/lib/parse-run-one-options.ts +++ b/packages/cli/lib/parse-run-one-options.ts @@ -57,6 +57,7 @@ const invalidTargetNames = [ 'workspace-schematic', 'connect-to-nx-cloud', 'clear-cache', + 'reset', 'report', 'list', ]; diff --git a/packages/workspace/src/command-line/clear-cache.ts b/packages/workspace/src/command-line/clear-cache.ts deleted file mode 100644 index 9338543fbc..0000000000 --- a/packages/workspace/src/command-line/clear-cache.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { appRootPath } from '@nrwl/tao/src/utils/app-root'; -import { - cacheDirectory, - readCacheDirectoryProperty, -} from '../utilities/cache-directory'; -import { removeSync } from 'fs-extra'; -import { output } from '../utilities/output'; - -export const clearCache = { - command: 'clear-cache', - describe: - 'Clears all the cached Nx artifacts and metadata about the workspace.', - handler: clearCacheHandler, -}; - -async function clearCacheHandler() { - output.note({ - title: 'Deleting the cache directory.', - bodyLines: [`This might take a few minutes.`], - }); - const dir = cacheDirectory( - appRootPath, - readCacheDirectoryProperty(appRootPath) - ); - removeSync(dir); - output.success({ - title: 'Deleted the cache directory.', - }); -} diff --git a/packages/workspace/src/command-line/nx-commands.ts b/packages/workspace/src/command-line/nx-commands.ts index f806cd3b36..bd2f69a1d8 100644 --- a/packages/workspace/src/command-line/nx-commands.ts +++ b/packages/workspace/src/command-line/nx-commands.ts @@ -11,10 +11,12 @@ import { import { generateDaemonHelpOutput } from '../core/project-graph/daemon/client/generate-help-output'; import { nxVersion } from '../utils/versions'; import { examples } from './examples'; +import { reset } from './reset'; const noop = (yargs: yargs.Argv): yargs.Argv => yargs; -const daemonHelpOutput = generateDaemonHelpOutput(); +const isGenerateDocsProcess = process.env.NX_GENERATE_DOCS_PROCESS === 'true'; +const daemonHelpOutput = generateDaemonHelpOutput(isGenerateDocsProcess); // Ensure that the output takes up the available width of the terminal yargs.wrap(yargs.terminalWidth()); @@ -306,7 +308,7 @@ npx nx daemon:start ) .command(require('./report').report) .command(require('./list').list) - .command(require('./clear-cache').clearCache) + .command(reset) .command( 'connect-to-nx-cloud', chalk.bold(`Makes sure the workspace is connected to Nx Cloud`), diff --git a/packages/workspace/src/command-line/reset.ts b/packages/workspace/src/command-line/reset.ts new file mode 100644 index 0000000000..ce7f8cd6b7 --- /dev/null +++ b/packages/workspace/src/command-line/reset.ts @@ -0,0 +1,33 @@ +import { appRootPath } from '@nrwl/tao/src/utils/app-root'; +import { remove } from 'fs-extra'; +import type { CommandModule } from 'yargs'; +import { stop as stopDaemon } from '../core/project-graph/daemon/client/client'; +import { + cacheDirectory, + readCacheDirectoryProperty, +} from '../utilities/cache-directory'; +import { output } from '../utilities/output'; + +export const reset: CommandModule = { + command: 'reset', + describe: + 'Clears all the cached Nx artifacts and metadata about the workspace and shuts down the Nx Daemon.', + handler: resetHandler, + // Prior to v13 clear-cache was a top level nx command, so preserving as an alias + aliases: ['clear-cache'], +}; + +async function resetHandler() { + output.note({ + title: 'Resetting the Nx workspace cache and stopping the Nx Daemon.', + bodyLines: [`This might take a few minutes.`], + }); + const dir = cacheDirectory( + appRootPath, + readCacheDirectoryProperty(appRootPath) + ); + await Promise.all([stopDaemon(), remove(dir)]); + output.success({ + title: 'Successful reset the Nx workspace.', + }); +} diff --git a/packages/workspace/src/command-line/supported-nx-commands.ts b/packages/workspace/src/command-line/supported-nx-commands.ts index 83f45c947f..77b2908177 100644 --- a/packages/workspace/src/command-line/supported-nx-commands.ts +++ b/packages/workspace/src/command-line/supported-nx-commands.ts @@ -23,6 +23,7 @@ export const supportedNxCommands: string[] = [ 'run-many', 'connect-to-nx-cloud', 'clear-cache', + 'reset', 'list', 'help', '--help', diff --git a/packages/workspace/src/core/project-graph/daemon/client/client.ts b/packages/workspace/src/core/project-graph/daemon/client/client.ts index 8921d60800..832ce670f9 100644 --- a/packages/workspace/src/core/project-graph/daemon/client/client.ts +++ b/packages/workspace/src/core/project-graph/daemon/client/client.ts @@ -59,12 +59,12 @@ export function startInCurrentProcess(): void { } export function stop(): void { - logger.info(`NX Daemon Server - Stopping...`); - spawnSync(process.execPath, ['../server/stop.js'], { cwd: __dirname, stdio: 'inherit', }); + + logger.info('NX Daemon Server - Stopped'); } /** diff --git a/packages/workspace/src/core/project-graph/daemon/client/generate-help-output.ts b/packages/workspace/src/core/project-graph/daemon/client/generate-help-output.ts index f8decf824f..de31949dea 100644 --- a/packages/workspace/src/core/project-graph/daemon/client/generate-help-output.ts +++ b/packages/workspace/src/core/project-graph/daemon/client/generate-help-output.ts @@ -2,7 +2,13 @@ import { spawnSync } from 'child_process'; import { getDaemonProcessId } from '../cache'; import { DAEMON_OUTPUT_LOG_FILE } from '../tmp-dir'; -export function generateDaemonHelpOutput(): string { +export function generateDaemonHelpOutput( + isGenerateDocsProcess = false +): string { + if (isGenerateDocsProcess) { + return `The Nx Daemon is a local server which runs in the background in order to intelligently cache information about the workspace's project graph.`; + } + /** * A workaround for cases such as yargs output where we need to synchronously * get the value of this async operation. diff --git a/packages/workspace/src/core/project-graph/daemon/server/server.ts b/packages/workspace/src/core/project-graph/daemon/server/server.ts index 6fd471ddfc..05c864f17c 100644 --- a/packages/workspace/src/core/project-graph/daemon/server/server.ts +++ b/packages/workspace/src/core/project-graph/daemon/server/server.ts @@ -274,7 +274,6 @@ export async function stopServer(): Promise { } killSocketOrPath(); - logger.info('NX Daemon Server - Stopped'); return resolve(); }); }); diff --git a/scripts/documentation/generate-cli-data.ts b/scripts/documentation/generate-cli-data.ts index 382f66e301..da23dd7e2a 100644 --- a/scripts/documentation/generate-cli-data.ts +++ b/scripts/documentation/generate-cli-data.ts @@ -3,7 +3,6 @@ import { readFileSync } from 'fs'; import { removeSync } from 'fs-extra'; import { join } from 'path'; import { dedent } from 'tslint/lib/utils'; -import { commandsObject } from '../../packages/workspace'; import { Framework, Frameworks } from './frameworks'; import { formatDeprecated, @@ -37,8 +36,17 @@ interface ParsedCommand { } export async function generateCLIDocumentation() { + /** + * For certain commands, they will output dynamic data at runtime in a real workspace, + * so we leverage an envrionment variable to inform the logic of the context that we + * are just statically generating documentation for the current execution. + */ + process.env.NX_GENERATE_DOCS_PROCESS = 'true'; + console.log(`\n${chalk.blue('i')} Generating Documentation for Nx Commands`); + const { commandsObject } = importFresh('../../packages/workspace'); + await Promise.all( Frameworks.map(async (framework: Framework) => { const commandsOutputDirectory = join( @@ -187,5 +195,7 @@ nx ${command.name} }) ); + delete process.env.NX_GENERATE_DOCS_PROCESS; + console.log(`${chalk.green('✓')} Generated Documentation for Nx Commands`); }