Skip to content

Commit

Permalink
feat(core): add --quiet option to suppress generate output (#15802)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Mar 21, 2023
1 parent 6834b65 commit 0370794
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/shared/reference/environment-variables.md
Expand Up @@ -20,6 +20,7 @@ The following environment variables are ones that you can set to change the beha
| NX_VERBOSE_LOGGING | boolean | If set to `true`, will print debug information useful for troubleshooting |
| NX_DRY_RUN | boolean | If set to `true`, will perform a dry run of the generator. No files will be created and no packages will be installed. |
| NX_INTERACTIVE | boolean | If set to `true`, will allow Nx to prompt you in the terminal to answer some further questions when running generators. |
| NX_GENERATE_QUIET | boolean | If set to `true`, will prevent Nx logging file operations during generate |

Nx will set the following environment variables so they can be accessible within the process even outside of executors and generators

Expand Down
8 changes: 8 additions & 0 deletions e2e/nx-misc/src/extras.test.ts
Expand Up @@ -260,4 +260,12 @@ describe('Extra Nx Misc Tests', () => {
checkFilesExist(`${folder}/dummy.txt`);
}, 120000);
});

describe('generate --quiet', () => {
it('should not log tree operations or install tasks', () => {
const output = runCLI('generate @nrwl/react:app --quiet test-project');
expect(output).not.toContain('CREATE');
expect(output).not.toContain('Installed');
});
});
});
2 changes: 1 addition & 1 deletion packages/create-nx-workspace/src/create-preset.ts
Expand Up @@ -38,7 +38,7 @@ export async function createPreset<T extends CreateWorkspaceOptions>(
}
}

const command = `g ${preset}:preset ${args}`;
const command = `g ${preset}:preset --quiet ${args}`;

try {
const [exec, ...args] = pmc.exec.split(' ');
Expand Down
2 changes: 1 addition & 1 deletion packages/devkit/src/tasks/install-packages-task.ts
Expand Up @@ -40,7 +40,7 @@ export function installPackagesTask(
const pmc = getPackageManagerCommand(packageManager);
execSync(pmc.install, {
cwd: join(tree.root, cwd),
stdio: [0, 1, 2],
stdio: process.env.NX_GENERATE_QUIET === 'true' ? 'ignore' : 'inherit',
});
}
}
4 changes: 3 additions & 1 deletion packages/devkit/src/utils/package-json.ts
Expand Up @@ -446,9 +446,11 @@ export function ensurePackage<T extends any = any>(
}

const tempDir = dirSync().name;

console.log(`Fetching ${pkg}...`);
execSync(`${getPackageManagerCommand().addDev} ${pkg}@${requiredVersion}`, {
cwd: tempDir,
stdio: [0, 1, 2],
stdio: 'ignore',
});

addToNodePath(join(workspaceRoot, 'node_modules'));
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/adapter/ngcli-adapter.ts
Expand Up @@ -792,6 +792,7 @@ export function wrapAngularDevkitSchematic(
generatorName,
force: false,
defaults: false,
quiet: false,
};
const workflow = createWorkflow(fsHost, host.root, options);

Expand Down
7 changes: 6 additions & 1 deletion packages/nx/src/command-line/generate.ts
Expand Up @@ -31,6 +31,7 @@ export interface GenerateOptions {
dryRun: boolean;
interactive: boolean;
defaults: boolean;
quiet: boolean;
}

export function printChanges(fileChanges: FileChange[]) {
Expand Down Expand Up @@ -245,6 +246,7 @@ async function convertToGenerateOptions(
dryRun: generatorOptions.dryRun as boolean,
interactive,
defaults: generatorOptions.defaults as boolean,
quiet: generatorOptions.quiet,
};

delete generatorOptions.d;
Expand All @@ -257,6 +259,7 @@ async function convertToGenerateOptions(
delete generatorOptions.generator;
delete generatorOptions['--'];
delete generatorOptions['$0'];
delete generatorOptions.quiet;

return res;
}
Expand Down Expand Up @@ -376,7 +379,9 @@ export async function generate(cwd: string, args: { [k: string]: any }) {

const changes = host.listChanges();

printChanges(changes);
if (!opts.quiet) {
printChanges(changes);
}
if (!opts.dryRun) {
flushChanges(workspaceRoot, changes);
if (task) {
Expand Down
10 changes: 10 additions & 0 deletions packages/nx/src/command-line/nx-commands.ts
Expand Up @@ -754,6 +754,11 @@ function withGenerateOptions(yargs: yargs.Argv) {
type: 'boolean',
default: false,
})
.option('quiet', {
describe: 'Hides logs from tree operations (e.g. `CREATE package.json`)',
type: 'boolean',
default: false,
})
.middleware((args) => {
if (process.env.NX_INTERACTIVE === 'false') {
args.interactive = false;
Expand All @@ -765,6 +770,11 @@ function withGenerateOptions(yargs: yargs.Argv) {
} else {
process.env.NX_DRY_RUN = `${args.dryRun}`;
}
if (process.env.NX_GENERATE_QUIET === 'true') {
args.quiet = true;
} else {
process.env.NX_GENERATE_QUIET = `${args.quiet}`;
}
});

if (generatorWillShowHelp) {
Expand Down

1 comment on commit 0370794

@vercel
Copy link

@vercel vercel bot commented on 0370794 Mar 21, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-dev-nrwl.vercel.app
nx-five.vercel.app

Please sign in to comment.