fix(cli): reset command options (#2645)

This commit is contained in:
chenos 2023-09-14 06:24:30 +08:00 committed by GitHub
parent 282645ed8b
commit 0c132d7940
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,70 @@
import { mockDatabase } from '@nocobase/database';
import Application, { ApplicationOptions } from '../application';
const mockServer = (options?: ApplicationOptions) => {
return new Application({
database: mockDatabase(),
acl: false,
...options,
});
};
describe('command', () => {
let app: Application;
afterEach(async () => {
if (app) {
await app.destroy();
}
});
test('case1', async () => {
app = mockServer({
plugins: [],
});
let val;
app
.command('c1')
// .option('-p, --pepper')
.option('-r1, --retry1 [retry1]')
.option('-r2, --retry2 [retry2]')
.option('-r3, --retry3 [retry3]')
.action((opts, cli) => {
val = opts;
});
await app.runCommand('c1', '-r1', '2');
expect(val).toEqual({ retry1: '2' });
await app.runCommand('c1', '-r2', '3');
expect(val).toEqual({ retry2: '3' });
await app.runCommand('c1', '-r3', '4');
expect(val).toEqual({ retry3: '4' });
});
test('case2', async () => {
app = mockServer({
plugins: [],
});
let val;
app
.command('c1')
// .option('-p, --pepper')
.option('-r1, --retry1 [retry1]', '', '1')
.option('-r2, --retry2 [retry2]', '', '2')
.option('-r3, --retry3 [retry3]', '', '3')
.action((opts, cli) => {
val = opts;
});
await app.runCommand('c1', '-r1', '2');
expect(val).toEqual({ retry1: '2', retry2: '2', retry3: '3' });
await app.runCommand('c1', '-r2', '3');
expect(val).toEqual({ retry1: '1', retry2: '3', retry3: '3' });
await app.runCommand('c1', '-r3', '4');
expect(val).toEqual({ retry1: '1', retry2: '2', retry3: '4' });
});
});

View File

@ -433,8 +433,13 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
} finally {
const _actionCommand = this._actionCommand;
if (_actionCommand) {
const options = _actionCommand['options'];
_actionCommand['_optionValues'] = {};
_actionCommand['_optionValueSources'] = {};
_actionCommand['options'] = [];
for (const option of options) {
_actionCommand.addOption(option);
}
}
this.activatedCommand = null;
this._actionCommand = null;