Skip to content

Commit cdab9fa

Browse files
committed
fix(@angular/cli): provide an actionable error when using --configuration with ng run
With this commit we issue a more actionable error message when using the unsupported `--configuration` option with the`ng run` command. Closes #23385 (cherry picked from commit fd92eaa)
1 parent 3aac08e commit cdab9fa

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

packages/angular/cli/src/commands/run/cli.ts

+15
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ export class RunCommandModule
4545
// Also, hide choices from JSON help so that we don't display them in AIO.
4646
choices: (getYargsCompletions || help) && !jsonHelp ? this.getTargetChoices() : undefined,
4747
})
48+
.middleware((args) => {
49+
// TODO: remove in version 15.
50+
const { configuration, target } = args;
51+
if (typeof configuration === 'string' && target) {
52+
const targetWithConfig = target.split(':', 2);
53+
targetWithConfig.push(configuration);
54+
55+
throw new CommandModuleError(
56+
'Unknown argument: configuration.\n' +
57+
`Provide the configuration as part of the target 'ng run ${targetWithConfig.join(
58+
':',
59+
)}'.`,
60+
);
61+
}
62+
}, true)
4863
.strict();
4964

5065
const target = this.makeTargetSpecifier();
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { getGlobalVariable } from '../../utils/env';
2+
import { expectFileToMatch } from '../../utils/fs';
3+
import { silentNg } from '../../utils/process';
4+
5+
export default async function () {
6+
// Development build
7+
await silentNg('run', 'test-project:build:development');
8+
await expectFileToMatch('dist/test-project/index.html', 'main.js');
9+
10+
// Production build
11+
await silentNg('run', 'test-project:build');
12+
if (getGlobalVariable('argv')['esbuild']) {
13+
// esbuild uses an 8 character hash
14+
await expectFileToMatch('dist/test-project/index.html', /main\.[a-zA-Z0-9]{8}\.js/);
15+
} else {
16+
await expectFileToMatch('dist/test-project/index.html', /main\.[a-zA-Z0-9]{16}\.js/);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { silentNg } from '../../utils/process';
2+
import { expectToFail } from '../../utils/utils';
3+
4+
export default async function () {
5+
const errorMatch = `Provide the configuration as part of the target 'ng run test-project:build:production`;
6+
7+
{
8+
const { message } = await expectToFail(() =>
9+
silentNg('run', 'test-project:build:development', '--configuration=production'),
10+
);
11+
12+
if (!message.includes(errorMatch)) {
13+
throw new Error(`Expected error to include '${errorMatch}' but didn't.\n\n${message}`);
14+
}
15+
}
16+
17+
{
18+
const { message } = await expectToFail(() =>
19+
silentNg('run', 'test-project:build', '--configuration=production'),
20+
);
21+
22+
if (!message.includes(errorMatch)) {
23+
throw new Error(`Expected error to include '${errorMatch}' but didn't.\n\n${message}`);
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)