microsoft / microsoft/kiota

npx @microsoft/kiota should run kiota as with typical NPM packages

Open
#6,442 1 comment 0 reactions 1 assignee Claimed by @gareth-a-jones View on GitHub
enhancement help wanted type:feature
Dominant language
C#
Stars
3.8k
Forks
333
Avg merge
16h 29m
Merged PRs (30d)
116

Description

### Is your feature request related to a problem? Please describe the problem.

I am trying to use kiota in a standard TypeSpec npm-based project at the command line. I'm not using TTK. This is just an example of usage - any number of use cases exist for typical NPM package behavior.

I expect to be able to do the following as a canonical example:

`npx @microsoft/kiota --version`

### Client library/SDK language

None

### Describe the solution you'd like

To do this effectively with minimal code we need to use standard NPM techniques.

1) Remove the platform-specific subfolder from the download. E.g. switch from
`@microsoft\kiota\dist\cjs\.kiotabin\1.25.1\win-x64` to
`@microsoft\kiota\dist\cjs\.kiotabin\1.25.1`

As we will only have one architecture on a given machine this is fine.
It provides a stable cross-platform path that can be referenced from the `bin` node in package.json
By using the `bin` node like so
``` typescript
"bin": {
"kiota": "./dist\cjs\.kiotabin\1.25.1\kiota"
}
```

NPM will then automatically create links to the named bin in the node_modules/.bin folder.

in order to trigger all of this, an install script is needed running from the postinstall trigger:

Trigger:
``` json
"scripts": {
"postinstall": "node scripts/install.js"
```

Script: `scripts\install.js'

``` typescript
// Import the ensureKiotaIsPresent function
const { ensureKiotaIsPresent } = require('./dist/cjs/install.js');

// Call the function and handle the promise
(async () => {
try {
const result = await ensureKiotaIsPresent();
if (result) {
console.log(`Kiota successfully ensured: ${result}`);
} else {
console.log("Kiota is already present.");
}
} catch (error) {
console.error("Error during Kiota installation:", error);
process.exit(1); // Exit with an error code if something goes wrong
}
})();
```

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.