Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/api/src/codegen/languages/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ function handleExecFailure(err: Error, opts: InstallerOptions = {}) {
throw err;
}

async function detectPackageManager(installDir: string) {
const pm = await preferredPM(installDir);
async function detectPackageManager() {
const projectDir = Storage.getProjectDir();

const pm = await preferredPM(projectDir);
if (pm) {
return pm.name;
}
Expand Down Expand Up @@ -162,7 +164,7 @@ 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 packageManager = await detectPackageManager();

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

Expand All @@ -189,8 +191,7 @@ 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 packageManager = await detectPackageManager();

const args = ['uninstall', pkgName, opts.dryRun ? '--dry-run' : ''].filter(Boolean);
return execa(packageManager, args)
Expand Down
15 changes: 15 additions & 0 deletions packages/api/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ export default class Storage {
fs.mkdirSync(Storage.getAPIsDir(), { recursive: true });
}

/**
* Retrieves the project's root directory path.
*
* If a storage directory has not been explicitly set, this method will default it to
* the current working directory. It then returns the directory name of the storage path.
*
*/
static getProjectDir() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind adding a small test for this?

if (!Storage.dir) {
Storage.setStorageDir();
}

return path.dirname(Storage.dir);
}

/**
* Reset the state of the entire storage system.
*
Expand Down
16 changes: 16 additions & 0 deletions packages/api/test/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ describe('storage', () => {
});
});

describe('#getProjectDir', () => {
it('should return the parent directory of the storage directory', () => {
Storage.setStorageDir(uniqueTempDir());
const projectDir = Storage.getProjectDir();
expect(projectDir).toBe(path.dirname(Storage.dir));
});

it('should set and return default storage directory if none is set', () => {
Storage.dir = '';
const projectDir = Storage.getProjectDir();
expect(Storage.dir).toBe(path.join(process.cwd(), '.api'));
expect(projectDir).toBe(path.dirname(Storage.dir));
expect(projectDir).toBe(process.cwd());
});
});

describe('#generateIntegrityHash', () => {
it('should generate an integrity hash for an API definition', () => {
expect(Storage.generateIntegrityHash(petstoreSimple as OASDocument)).toBe(
Expand Down