forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace-loader.ts
More file actions
54 lines (46 loc) · 1.69 KB
/
workspace-loader.ts
File metadata and controls
54 lines (46 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
Path,
basename,
dirname,
experimental,
normalize,
virtualFs,
} from '@angular-devkit/core';
import { findUp } from '../utilities/find-up';
export class WorkspaceLoader {
// TODO: add remaining fallbacks.
private _configFileNames = [
normalize('.angular.json'),
normalize('angular.json'),
];
constructor(private _host: virtualFs.Host) { }
loadWorkspace(projectPath?: string): Promise<experimental.workspace.Workspace> {
return this._loadWorkspaceFromPath(this._getProjectWorkspaceFilePath(projectPath));
}
// TODO: do this with the host instead of fs.
private _getProjectWorkspaceFilePath(projectPath?: string): Path {
// Find the workspace file, either where specified, in the Angular CLI project
// (if it's in node_modules) or from the current process.
const workspaceFilePath = (projectPath && findUp(this._configFileNames, projectPath))
|| findUp(this._configFileNames, process.cwd())
|| findUp(this._configFileNames, __dirname);
if (workspaceFilePath) {
return normalize(workspaceFilePath);
} else {
throw new Error(`Local workspace file ('angular.json') could not be found.`);
}
}
private _loadWorkspaceFromPath(workspacePath: Path) {
const workspaceRoot = dirname(workspacePath);
const workspaceFileName = basename(workspacePath);
const workspace = new experimental.workspace.Workspace(workspaceRoot, this._host);
return workspace.loadWorkspaceFromHost(workspaceFileName).toPromise();
}
}