-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2970c86
commit c24fcb5
Showing
12 changed files
with
350 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,7 @@ coverage | |
docs/site | ||
# generated commands info | ||
commands.json | ||
allCommands.json | ||
|
||
# VS env folder | ||
.vs/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import url, { pathToFileURL } from 'url'; | ||
import Command from '../dist/Command.js'; | ||
import { Cli } from '../dist/cli/Cli.js'; | ||
import { fsUtil } from '../dist/utils/fsUtil.js'; | ||
|
||
const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); | ||
const commandsFolder = path.join(__dirname, '..', 'dist', 'm365'); | ||
const commandHelpFolder = path.join(commandsFolder, '..', '..', 'docs', 'docs', 'cmd'); | ||
|
||
async function loadAllCommands() { | ||
const files = fsUtil.readdirR(commandsFolder); | ||
const cli = Cli.getInstance(); | ||
|
||
await Promise.all(files.map(async (filePath) => { | ||
const file = pathToFileURL(filePath).toString(); | ||
if (file.indexOf(`/commands/`) > -1 && | ||
file.indexOf(`/assets/`) < 0 && | ||
file.endsWith('.js') && | ||
!file.endsWith('.spec.js')) { | ||
|
||
const command = await import(file); | ||
if (command.default instanceof Command) { | ||
const helpFilePath = path.relative(commandHelpFolder, getCommandHelpFilePath(command.default.name)); | ||
cli.commands.push(Cli.getCommandInfo(command.default, path.relative(commandsFolder, filePath), helpFilePath)); | ||
} | ||
} | ||
})); | ||
|
||
cli.commands.forEach(c => { | ||
delete c.command; | ||
delete c.defaultProperties; | ||
delete c.options; | ||
}); | ||
|
||
fs.writeFileSync('allCommands.json', JSON.stringify(cli.commands)); | ||
} | ||
|
||
function getCommandHelpFilePath(commandName) { | ||
const commandNameWords = commandName.split(' '); | ||
const pathChunks = []; | ||
|
||
if (commandNameWords.length === 1) { | ||
pathChunks.push(`${commandNameWords[0]}.mdx`); | ||
} | ||
else { | ||
if (commandNameWords.length === 2) { | ||
pathChunks.push(commandNameWords[0], `${commandNameWords.join('-')}.mdx`); | ||
} | ||
else { | ||
pathChunks.push(commandNameWords[0], commandNameWords[1], commandNameWords.slice(1).join('-') + '.mdx'); | ||
} | ||
} | ||
|
||
const helpFilePath = path.join(commandHelpFolder, ...pathChunks); | ||
return helpFilePath; | ||
} | ||
|
||
loadAllCommands(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
import { Cli, CommandOutput } from "./cli/Cli.js"; | ||
import path from 'path'; | ||
|
||
export function executeCommand(commandName: string, options: any, listener?: { | ||
export async function executeCommand(commandName: string, options: any, listener?: { | ||
stdout: (message: any) => void, | ||
stderr: (message: any) => void, | ||
}): Promise<CommandOutput> { | ||
const cli = Cli.getInstance(); | ||
cli.commandsFolder = path.join(__dirname, 'm365'); | ||
cli.commands = []; | ||
cli.loadCommandFromArgs(commandName.split(' ')); | ||
if (cli.commands.length !== 1) { | ||
cli.loadAllCommandsInfo(); | ||
await cli.loadCommandFromArgs(commandName.split(' ')); | ||
if (!cli.commandToExecute) { | ||
return Promise.reject(`Command not found: ${commandName}`); | ||
} | ||
|
||
return Cli.executeCommandWithOutput(cli.commands[0].command, { options: options ?? {} }, listener); | ||
return Cli.executeCommandWithOutput(cli.commandToExecute.command, { options: options ?? {} }, listener); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.