From 11c70fc94d16d359d0585188c854b98d6566b78b Mon Sep 17 00:00:00 2001 From: Waldek Mastykarz Date: Fri, 10 Nov 2023 10:35:29 +0100 Subject: [PATCH] Fixes generation completion. Closes #5655 --- .gitignore | 2 +- scripts/write-all-commands.js | 7 ++++++- src/cli/Cli.spec.ts | 11 ++++++++++- src/cli/Cli.ts | 9 ++++++--- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 0fc58659637..c32e2d001f3 100644 --- a/.gitignore +++ b/.gitignore @@ -61,7 +61,7 @@ coverage docs/site # generated commands info commands.json -allCommands.json +allCommands*.json # VS env folder .vs/ diff --git a/scripts/write-all-commands.js b/scripts/write-all-commands.js index ffca4acf596..afa0a676c2d 100644 --- a/scripts/write-all-commands.js +++ b/scripts/write-all-commands.js @@ -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)); } diff --git a/src/cli/Cli.spec.ts b/src/cli/Cli.spec.ts index 040a802bacb..a4444d02aab 100644 --- a/src/cli/Cli.spec.ts +++ b/src/cli/Cli.spec.ts @@ -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'); @@ -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)); + }); }); \ No newline at end of file diff --git a/src/cli/Cli.ts b/src/cli/Cli.ts index 0d5cd956630..43f8c939259 100644 --- a/src/cli/Cli.ts +++ b/src/cli/Cli.ts @@ -86,7 +86,9 @@ export class Cli { public async execute(rawArgs: string[]): Promise { 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 @@ -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)); } /**