Skip to content

Commit

Permalink
Fixes generation completion. Closes pnp#5655
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz authored and martinlingstuyl committed Nov 10, 2023
1 parent d785eb6 commit 11c70fc
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ coverage
docs/site
# generated commands info
commands.json
allCommands.json
allCommands*.json

# VS env folder
.vs/
Expand Down
7 changes: 6 additions & 1 deletion scripts/write-all-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ async function loadAllCommands() {
cli.commands.forEach(c => {
delete c.command;
delete c.defaultProperties;
delete c.options;
});
// this file is used by command completion
fs.writeFileSync('allCommandsFull.json', JSON.stringify(cli.commands));

cli.commands.forEach(c => {
delete c.options;
});
// this file is use for regular command execution
fs.writeFileSync('allCommands.json', JSON.stringify(cli.commands));
}

Expand Down
11 changes: 10 additions & 1 deletion src/cli/Cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import { settingsNames } from '../settingsNames.js';
import { telemetry } from '../telemetry.js';
import { md } from '../utils/md.js';
import { pid } from '../utils/pid.js';
import { Choice, SelectionConfig, prompt } from '../utils/prompt.js';
import { session } from '../utils/session.js';
import { sinonUtil } from '../utils/sinonUtil.js';
import { Cli, CommandOutput } from './Cli.js';
import { Logger } from './Logger.js';
import { Choice, SelectionConfig, prompt } from '../utils/prompt.js';

const require = createRequire(import.meta.url);
const packageJSON = require('../../package.json');
Expand Down Expand Up @@ -2092,4 +2092,13 @@ describe('Cli', () => {
done();
}, _ => done('Promise fulfilled with error, no error expected'));
});

it('for completion commands loads full command info', async () => {
sinonUtil.restore(cli.loadAllCommandsInfo);
const loadAllCommandsInfoStub = sinon.spy(cli, 'loadAllCommandsInfo');
sinon.stub(Cli, 'executeCommand').callsFake(() => Promise.resolve());

await cli.execute(['cli', 'completion', 'sh', 'update']);
assert(loadAllCommandsInfoStub.calledWith(true));
});
});
9 changes: 6 additions & 3 deletions src/cli/Cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ export class Cli {
public async execute(rawArgs: string[]): Promise<void> {
const start = process.hrtime.bigint();

this.loadAllCommandsInfo();
// for completion commands we also need information about commands' options
const loadAllCommandInfo: boolean = rawArgs.indexOf('completion') > -1;
this.loadAllCommandsInfo(loadAllCommandInfo);

// check if help for a specific command has been requested using the
// 'm365 help xyz' format. If so, remove 'help' from the array of words
Expand Down Expand Up @@ -343,8 +345,9 @@ export class Cli {
}
}

public loadAllCommandsInfo(): void {
this.commands = require(path.join(__dirname, '../../allCommands.json'));
public loadAllCommandsInfo(loadFull: boolean = false): void {
const commandsInfoFileName = loadFull ? 'allCommandsFull.json' : 'allCommands.json';
this.commands = require(path.join(__dirname, '..', '..', commandsInfoFileName));
}

/**
Expand Down

0 comments on commit 11c70fc

Please sign in to comment.