Skip to content

Commit

Permalink
feat(@angular-devkit/core): add workspace getProjectByPath
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva committed Apr 24, 2018
1 parent 49e9285 commit 54d5d56
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
35 changes: 35 additions & 0 deletions packages/angular_devkit/core/src/workspace/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import {
JsonObject,
JsonParseMode,
Path,
isAbsolute,
join,
normalize,
parseJson,
relative,
resolve,
schema,
virtualFs,
} from '..';
Expand Down Expand Up @@ -143,6 +146,38 @@ export class Workspace {
return null;
}

getProjectByPath(path: Path): string | null {
this._assertLoaded();

const projectNames = this.listProjectNames();
if (projectNames.length === 1) {
return projectNames[0];
}

const isInside = (base: Path, potential: Path): boolean => {
const absoluteBase = resolve(this.root, base);
const absolutePotential = resolve(this.root, potential);
const relativePotential = relative(absoluteBase, absolutePotential);
if (!relativePotential.startsWith('..') && !isAbsolute(relativePotential)) {
return true;
}

return false;
};

const projects = this.listProjectNames()
.map(name => [this.getProject(name).root, name] as [Path, string])
.filter(tuple => isInside(tuple[0], path))
// Sort tuples by depth, with the deeper ones first.
.sort((a, b) => isInside(a[0], b[0]) ? 1 : 0);

if (projects[0]) {
return projects[0][1];
}

return null;
}

getCli() {
return this._getTool('cli');
}
Expand Down
22 changes: 21 additions & 1 deletion packages/angular_devkit/core/src/workspace/workspace_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,34 @@ describe('Workspace', () => {
).subscribe(undefined, done.fail, done);
});

it('gets default project when there is a single one', (done) => {
it('gets default project returns null when there is none', (done) => {
const customWorkspaceJson = { ...workspaceJson, defaultProject: undefined, projects: {} };
const workspace = new Workspace(root, host);
workspace.loadWorkspaceFromJson(customWorkspaceJson).pipe(
tap((ws) => expect(ws.getDefaultProject()).toEqual(null)),
).subscribe(undefined, done.fail, done);
});

it('gets project by path', (done) => {
const workspace = new Workspace(root, host);
workspace.loadWorkspaceFromJson(workspaceJson).pipe(
tap((ws) => expect(ws.getProjectByPath(ws.root)).toEqual('app')),
).subscribe(undefined, done.fail, done);
});

it('gets closest project by path', (done) => {
const app = workspaceJson.projects['app'];
const anotherAppRoot = join(normalize(app.root), 'folder');
const customWorkspaceJson = { ...workspaceJson, projects: {
'app': app,
'another-app': { ...app, root: anotherAppRoot},
} };
const workspace = new Workspace(root, host);
workspace.loadWorkspaceFromJson(customWorkspaceJson).pipe(
tap((ws) => expect(ws.getProjectByPath(anotherAppRoot)).toEqual('another-app')),
).subscribe(undefined, done.fail, done);
});

it('gets workspace cli', (done) => {
const workspace = new Workspace(root, host);
workspace.loadWorkspaceFromJson(workspaceJson).pipe(
Expand Down

0 comments on commit 54d5d56

Please sign in to comment.