chore(core): nx reset command, updates to cli help output and docs

This commit is contained in:
James Henry 2021-10-13 16:51:28 +04:00 committed by Victor Savkin
parent 28f28af487
commit e7d49aa55d
16 changed files with 80 additions and 63 deletions

View File

@ -2,9 +2,7 @@
EXPERIMENTAL: Nx Daemon EXPERIMENTAL: Nx Daemon
The Daemon is not currently running you can start it manually by running the following command: The Nx Daemon is a local server which runs in the background in order to intelligently cache information about the workspace's project graph.
npx nx daemon:start
## Usage ## Usage

View File

@ -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 ## Usage
```bash ```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`. [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`.

View File

@ -292,9 +292,9 @@
"file": "angular/cli/connect-to-nx-cloud" "file": "angular/cli/connect-to-nx-cloud"
}, },
{ {
"name": "clear-cache", "name": "reset",
"id": "clear-cache", "id": "reset",
"file": "angular/cli/clear-cache" "file": "angular/cli/reset"
} }
] ]
}, },
@ -1575,9 +1575,9 @@
"file": "react/cli/connect-to-nx-cloud" "file": "react/cli/connect-to-nx-cloud"
}, },
{ {
"name": "clear-cache", "name": "reset",
"id": "clear-cache", "id": "reset",
"file": "react/cli/clear-cache" "file": "react/cli/reset"
} }
] ]
}, },
@ -2822,9 +2822,9 @@
"file": "node/cli/connect-to-nx-cloud" "file": "node/cli/connect-to-nx-cloud"
}, },
{ {
"name": "clear-cache", "name": "reset",
"id": "clear-cache", "id": "reset",
"file": "node/cli/clear-cache" "file": "node/cli/reset"
} }
] ]
}, },

View File

@ -2,9 +2,7 @@
EXPERIMENTAL: Nx Daemon EXPERIMENTAL: Nx Daemon
The Daemon is not currently running you can start it manually by running the following command: The Nx Daemon is a local server which runs in the background in order to intelligently cache information about the workspace's project graph.
npx nx daemon:start
## Usage ## Usage

View File

@ -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 ## Usage
```bash ```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`. [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`.

View File

@ -2,9 +2,7 @@
EXPERIMENTAL: Nx Daemon EXPERIMENTAL: Nx Daemon
The Daemon is not currently running you can start it manually by running the following command: The Nx Daemon is a local server which runs in the background in order to intelligently cache information about the workspace's project graph.
npx nx daemon:start
## Usage ## Usage

View File

@ -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 ## Usage
```bash ```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`. [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`.

View File

@ -57,6 +57,7 @@ const invalidTargetNames = [
'workspace-schematic', 'workspace-schematic',
'connect-to-nx-cloud', 'connect-to-nx-cloud',
'clear-cache', 'clear-cache',
'reset',
'report', 'report',
'list', 'list',
]; ];

View File

@ -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.',
});
}

View File

@ -11,10 +11,12 @@ import {
import { generateDaemonHelpOutput } from '../core/project-graph/daemon/client/generate-help-output'; import { generateDaemonHelpOutput } from '../core/project-graph/daemon/client/generate-help-output';
import { nxVersion } from '../utils/versions'; import { nxVersion } from '../utils/versions';
import { examples } from './examples'; import { examples } from './examples';
import { reset } from './reset';
const noop = (yargs: yargs.Argv): yargs.Argv => yargs; 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 // Ensure that the output takes up the available width of the terminal
yargs.wrap(yargs.terminalWidth()); yargs.wrap(yargs.terminalWidth());
@ -306,7 +308,7 @@ npx nx daemon:start
) )
.command(require('./report').report) .command(require('./report').report)
.command(require('./list').list) .command(require('./list').list)
.command(require('./clear-cache').clearCache) .command(reset)
.command( .command(
'connect-to-nx-cloud', 'connect-to-nx-cloud',
chalk.bold(`Makes sure the workspace is connected to Nx Cloud`), chalk.bold(`Makes sure the workspace is connected to Nx Cloud`),

View File

@ -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.',
});
}

View File

@ -23,6 +23,7 @@ export const supportedNxCommands: string[] = [
'run-many', 'run-many',
'connect-to-nx-cloud', 'connect-to-nx-cloud',
'clear-cache', 'clear-cache',
'reset',
'list', 'list',
'help', 'help',
'--help', '--help',

View File

@ -59,12 +59,12 @@ export function startInCurrentProcess(): void {
} }
export function stop(): void { export function stop(): void {
logger.info(`NX Daemon Server - Stopping...`);
spawnSync(process.execPath, ['../server/stop.js'], { spawnSync(process.execPath, ['../server/stop.js'], {
cwd: __dirname, cwd: __dirname,
stdio: 'inherit', stdio: 'inherit',
}); });
logger.info('NX Daemon Server - Stopped');
} }
/** /**

View File

@ -2,7 +2,13 @@ import { spawnSync } from 'child_process';
import { getDaemonProcessId } from '../cache'; import { getDaemonProcessId } from '../cache';
import { DAEMON_OUTPUT_LOG_FILE } from '../tmp-dir'; 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 * A workaround for cases such as yargs output where we need to synchronously
* get the value of this async operation. * get the value of this async operation.

View File

@ -274,7 +274,6 @@ export async function stopServer(): Promise<void> {
} }
killSocketOrPath(); killSocketOrPath();
logger.info('NX Daemon Server - Stopped');
return resolve(); return resolve();
}); });
}); });

View File

@ -3,7 +3,6 @@ import { readFileSync } from 'fs';
import { removeSync } from 'fs-extra'; import { removeSync } from 'fs-extra';
import { join } from 'path'; import { join } from 'path';
import { dedent } from 'tslint/lib/utils'; import { dedent } from 'tslint/lib/utils';
import { commandsObject } from '../../packages/workspace';
import { Framework, Frameworks } from './frameworks'; import { Framework, Frameworks } from './frameworks';
import { import {
formatDeprecated, formatDeprecated,
@ -37,8 +36,17 @@ interface ParsedCommand {
} }
export async function generateCLIDocumentation() { 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`); console.log(`\n${chalk.blue('i')} Generating Documentation for Nx Commands`);
const { commandsObject } = importFresh('../../packages/workspace');
await Promise.all( await Promise.all(
Frameworks.map(async (framework: Framework) => { Frameworks.map(async (framework: Framework) => {
const commandsOutputDirectory = join( 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`); console.log(`${chalk.green('✓')} Generated Documentation for Nx Commands`);
} }