Skip to content

Commit

Permalink
feat: support other package managers (#895)
Browse files Browse the repository at this point in the history
* feat: support other package managers

* feat: use preferred pm for detecting package manager

* fix: lint errors
  • Loading branch information
DaniAkash authored Jun 26, 2024
1 parent 5e89873 commit 21230f8
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 17 deletions.
101 changes: 87 additions & 14 deletions package-lock.json

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

1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"lodash-es": "^4.17.21",
"oas": "^24.0.0",
"ora": "^8.0.1",
"preferred-pm": "^3.1.3",
"prompts": "^2.4.2",
"semver": "^7.3.8",
"ssri": "^10.0.1",
Expand Down
19 changes: 16 additions & 3 deletions packages/api/src/codegen/languages/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import corePkg from '@readme/api-core/package.json' assert { type: 'json' };
import { execa } from 'execa';
import { getLicense } from 'license';
import { setWith } from 'lodash-es';
import preferredPM from 'preferred-pm';
import semver from 'semver';
import { IndentationText, Project, QuoteKind, ScriptTarget, VariableDeclarationKind } from 'ts-morph';

Expand Down Expand Up @@ -79,6 +80,15 @@ function handleExecFailure(err: Error, opts: InstallerOptions = {}) {
throw err;
}

async function detectPackageManager(installDir: string) {
const pm = await preferredPM(installDir);
if (pm) {
return pm.name;
}
// Default to npm if no preferred package manager is detected
return 'npm';
}

export default class TSGenerator extends CodeGenerator {
project: Project;

Expand Down Expand Up @@ -152,13 +162,14 @@ export default class TSGenerator extends CodeGenerator {
// eslint-disable-next-line class-methods-use-this
async install(storage: Storage, opts: InstallerOptions = {}): Promise<void> {
const installDir = storage.getIdentifierStorageDir();
const packageManager = await detectPackageManager(installDir);

const npmInstall = ['install', '--save', opts.dryRun ? '--dry-run' : ''].filter(Boolean);
const installCommand = ['install', '--save', opts.dryRun ? '--dry-run' : ''].filter(Boolean);

// This will install the installed SDK as a dependency within the current working directory,
// adding `@api/<sdk identifier>` as a dependency there so you can load it with
// `require('@api/<sdk identifier>)`.
return execa('npm', [...npmInstall, installDir].filter(Boolean))
return execa(packageManager, [...installCommand, installDir].filter(Boolean))
.then(res => handleExecSuccess(res, opts))
.catch(err => {
// If `npm install` throws this error it always happens **after** our dependencies have been
Expand All @@ -178,9 +189,11 @@ export default class TSGenerator extends CodeGenerator {

static async uninstall(storage: Storage, opts: InstallerOptions = {}): Promise<void> {
const pkgName = storage.getPackageName() as string;
const installDir = storage.getIdentifierStorageDir();
const packageManager = await detectPackageManager(installDir);

const args = ['uninstall', pkgName, opts.dryRun ? '--dry-run' : ''].filter(Boolean);
return execa('npm', args)
return execa(packageManager, args)
.then(res => handleExecSuccess(res, opts))
.catch(err => handleExecFailure(err, opts));
}
Expand Down

0 comments on commit 21230f8

Please sign in to comment.