Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enhance createApplicationAccess to allow create applicationAccess by passing existing instance of 'project' to avoid getProject call #2905

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/rude-bananas-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-ux/project-access': patch
---

Enhance the `createApplicationAccess` function to allow creating `applicationAccess` by passing a optional `project` object through `ApplicationAccessOptions`, avoiding the need for a `getProject` call.
15 changes: 9 additions & 6 deletions packages/project-access/src/project/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,19 @@ export async function createApplicationAccess(
fs?: Editor | ApplicationAccessOptions
): Promise<ApplicationAccess> {
try {
const apps = await findAllApps([appRoot]);
const app = apps.find((app) => app.appRoot === appRoot);
if (!app) {
throw new Error(`Could not find app with root ${appRoot}`);
}
let options: ApplicationAccessOptions | undefined;
if (fs) {
options = isEditor(fs) ? { fs } : fs;
}
const project = await getProject(app.projectRoot, options?.fs);
let project = options?.project;
if (!project) {
const apps = await findAllApps([appRoot]);
const app = apps.find((app) => app.appRoot === appRoot);
if (!app) {
throw new Error(`Could not find app with root ${appRoot}`);
}
project = await getProject(app.projectRoot, options?.fs);
}
const appId = relative(project.root, appRoot);
return new ApplicationAccessImp(project, appId, options);
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions packages/project-access/src/types/access/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface BaseAccess {
}
export interface ApplicationAccessOptions extends ProjectAccessOptions {
fs?: Editor;
project?: Project;
}
export interface ApplicationAccess extends BaseAccess {
readonly app: ApplicationStructure;
Expand Down
15 changes: 14 additions & 1 deletion packages/project-access/test/project/access.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'path';
import type { Manifest, Package } from '../../src';
import { createApplicationAccess, createProjectAccess } from '../../src';
import { createApplicationAccess, createProjectAccess, getProject } from '../../src';
import * as i18nMock from '../../src/project/i18n/write';
import * as specMock from '../../src/project/specification';
import { create as createStorage } from 'mem-fs';
Expand Down Expand Up @@ -344,6 +344,19 @@ describe('Test function createApplicationAccess()', () => {
expect(error.message).toContain('non-existing-app');
}
});

test('Pass project through options', async () => {
const projectRoot = join(sampleRoot, 'cap-project');
const project = await getProject(projectRoot);
const appRoot = join(projectRoot, 'apps/two');
const appAccess = await createApplicationAccess(appRoot, {
project
});

expect(appAccess).toBeDefined();
expect(appAccess.root).toBe(projectRoot);
expect(appAccess.project === project).toBeTruthy();
});
});

describe('Test function createProjectAccess()', () => {
Expand Down
Loading