question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose CLI API to execute commands

See original GitHub issue

To 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:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
waldekmastykarzcommented, Feb 10, 2022

After looking at different options together with @manekinekko, we came to the following implementation:

const { executeCommand } = require('@pnp/cli-microsoft365');

executeCommand('status', { output: 'text' })
  .then(res => {
    if (res.stdout === 'Logged out') {
      return executeCommand('login', { output: 'text' }, {
        stdout: message => console.log(message)
      });
    }

    return Promise.resolve();
  })
  .then(_ => executeCommand('spo site list', { output: 'json' }))
  .then(res => {
    const sites = JSON.parse(res.stdout);

    if (sites.length === 0) {
      return Promise.reject('No sites found');
    }

    const siteUrl = sites[0].Url;
    return executeCommand('spo web get', { webUrl: siteUrl, output: 'json' });
  })
  .then(res => console.log(res.stdout))
  .catch(err => console.error(err));

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.

1reaction
zhenyasavcommented, Feb 9, 2022

yeah zod is probably going to help delete many lines and offer free types. “parse, don’t validate!” 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found