Skip to content

Commit

Permalink
feat: use execa for cross platform search path support (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Apr 7, 2023
1 parent cabbe7b commit 313bc19
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 42 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

**Auto-installs and use exactly expected version** of package manager using [nodejs/corepack](https://github.com/nodejs/corepack)

**Zero dependency** and low overhead implementation
**Minimal** implementation

nypm, detects package manager type and version and converts command into package manager CLI arguments. It then uses corepack to execute package manager's command (and download it if necessary).

Expand Down Expand Up @@ -61,10 +61,10 @@ Import:

```js
// ESM
import { detectPackageManager, addDependency } from 'nypm'
import { detectPackageManager, addDependency } from "nypm";

// CommonJS
const { detectPackageManager, addDependency } = require('nypm')
const { detectPackageManager, addDependency } = require("nypm");
```

## 💻 Development
Expand Down Expand Up @@ -93,14 +93,12 @@ Made with 💛
Published under [MIT License](./LICENSE).

<!-- Badges -->

[npm-version-src]: https://img.shields.io/npm/v/nypm?style=flat-square
[npm-version-href]: https://npmjs.com/package/nypm

[npm-downloads-src]: https://img.shields.io/npm/dm/nypm?style=flat-square
[npm-downloads-href]: https://npmjs.com/package/nypm

[github-actions-src]: https://img.shields.io/github/workflow/status/unjs/nypm/ci/main?style=flat-square
[github-actions-href]: https://github.com/unjs/nypm/actions?query=workflow%3Aci

[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/nypm/main?style=flat-square
[codecov-href]: https://codecov.io/gh/unjs/nypm
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"release": "pnpm test && standard-version && git push --follow-tags && pnpm publish",
"test": "pnpm lint && vitest run --coverage"
},
"dependencies": {
"execa": "^7.1.1"
},
"devDependencies": {
"@types/node": "^18.15.11",
"@vitest/coverage-c8": "^0.29.8",
Expand Down
22 changes: 5 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 22 additions & 13 deletions src/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@ export interface RunCommandOptions {
silent?: boolean;
}

export function runCorepack (pm: string, argv: string[], options: RunCommandOptions = {}): Promise<true> {
export function runCorepack(
pm: string,
argv: string[],
options: RunCommandOptions = {}
): Promise<boolean> {
if (pm === "npm") {
return runCommand("npm", argv, options);
}
return runCommand("corepack", [pm, ...argv], options);
}

function runCommand (command: string, argv: string[], options: RunCommandOptions = {}): Promise<true> {
const child = spawn(command, argv, {
cwd: resolve(options.cwd || process.cwd()),
stdio: options.silent ? "ignore" : "inherit"
});
return new Promise((resolve, reject) => {
child.on("exit", (code) => {
if (code !== 0) {
return reject(new Error(`${command} ${argv.join(" ")} failed (exit code: ${code})`));
}
return resolve(true);
async function runCommand(
command: string,
argv: string[],
options: RunCommandOptions = {}
): Promise<boolean> {
const { execa } = await import("execa");
try {
await execa(command, argv, {
cwd: resolve(options.cwd || process.cwd()),
stdio: options.silent ? "ignore" : "inherit",
});
});
} catch {
return false;
}
return true;
}
8 changes: 2 additions & 6 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,12 @@ describe("api", () => {
const fixtureDirectory = resolveFixtureDirectory(fixture.name);
it("addDependency", async () => {
expect(
await addDependency("pathe", { cwd: fixtureDirectory, silent: false })
).toBeTruthy();
expect(
await addDependency("ufo", {
await addDependency("pathe", {
cwd: fixtureDirectory,
dev: true,
silent: false,
})
).toBeTruthy();
});
}, 30_000);
});
}
});

0 comments on commit 313bc19

Please sign in to comment.