Expose CLI API to execute commands
See original GitHub issueTo allow integrating CLI for Microsoft 365 in other tools we should export CLI’s API for executing commands in the CLI’s npm package. This will allow other tools that want to use CLI get a direct access to the API and not need to rely on executing CLI as a child process and interact with it through stdout, stderr and exit codes. When you install CLI for M365 as a dependency in your Node.js project, you should be able to do import { executeCommandWithOutput } from '@pnp/cli-microsoft365
and use the executeCommandWithOutput
function to execute the specified command with its arguments and capture its output.
After we add this, we should also add a new page about integrating CLI in other tools in our documentation.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:13 (11 by maintainers)
Top Results From Across the Web
create-api — AWS CLI 1.27.37 Command Reference
Specifies whether clients can invoke your API by using the default execute-api endpoint. By default, clients can invoke your API with the default...
Read more >Building a Python Agent with CLI and Web API - CodeRed
In this article I will show you how to create a simple python agent that runs as a system daemon and can be...
Read more >Use CLI programmatically - CLI for Microsoft 365
This API lets you call any of CLI's commands. The following example shows how you could call several CLI for Microsoft 365 commands...
Read more >Use curl to interact with an API | Enable Sysadmin - Red Hat
Use curl to interact with an API. Learn how to use command line gem curl to do more than simple file transfer.
Read more >Command-line API | Node.js v19.3.0 Documentation
Node.js comes with a variety of CLI options. These options expose built-in debugging, multiple ways to execute scripts, and other helpful runtime options....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
After looking at different options together with @manekinekko, we came to the following implementation:
We use current CLI’s implementation with Promises to return command output after the command finished its execution. If you need to get command output while the command is still running (such as
login
to get the device code), you can pass an optional listener attached to stdout (main output) or stderr (verbose, debug, and error output).In an ideal world, we’d align more closely with Node’s design of non-blocking IO and use streams eg.
executeCommand().on('stdout', ...).on('stderr', ...).on('end', ...)
but right now this would mean a significant rewrite of CLI’s internals. We might consider this approach in the future. For now, we’ll stick with promises.You can find the latest sample and instructions to test in the repo I shared earlier in this issue.
yeah zod is probably going to help delete many lines and offer free types. “parse, don’t validate!” 😃