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
85 lines (72 loc) · 2.81 KB
/
workspace-loader.ts
File metadata and controls
85 lines (72 loc) · 2.81 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// tslint:disable:no-global-tslint-disable file-header
import {
Path,
basename,
dirname,
experimental,
join,
normalize,
virtualFs,
} from '@angular-devkit/core';
import * as fs from 'fs';
import { homedir } from 'os';
import { Observable, of } from 'rxjs';
import { concatMap, tap } from 'rxjs/operators';
import { findUp } from '../utilities/find-up';
// TODO: error out instead of returning null when workspace cannot be found.
export class WorkspaceLoader {
private _workspaceCacheMap = new Map<string, experimental.workspace.Workspace>();
// TODO: add remaining fallbacks.
private _configFileNames = [
normalize('.angular.json'),
normalize('angular.json'),
];
constructor(private _host: virtualFs.Host) { }
loadGlobalWorkspace(): Observable<experimental.workspace.Workspace | null> {
return this._getGlobalWorkspaceFilePath().pipe(
concatMap(globalWorkspacePath => this._loadWorkspaceFromPath(globalWorkspacePath)),
);
}
loadWorkspace(projectPath?: string): Observable<experimental.workspace.Workspace | null> {
return this._getProjectWorkspaceFilePath(projectPath).pipe(
concatMap(globalWorkspacePath => this._loadWorkspaceFromPath(globalWorkspacePath)),
);
}
// TODO: do this with the host instead of fs.
private _getProjectWorkspaceFilePath(projectPath?: string): Observable<Path | null> {
// 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 of(normalize(workspaceFilePath));
} else {
throw new Error(`Local workspace file ('angular.json') could not be found.`);
}
}
// TODO: do this with the host instead of fs.
private _getGlobalWorkspaceFilePath(): Observable<Path | null> {
for (const fileName of this._configFileNames) {
const workspaceFilePath = join(normalize(homedir()), fileName);
if (fs.existsSync(workspaceFilePath)) {
return of(normalize(workspaceFilePath));
}
}
return of(null);
}
private _loadWorkspaceFromPath(workspacePath: Path | null) {
if (!workspacePath) {
return of(null);
}
if (this._workspaceCacheMap.has(workspacePath)) {
return of(this._workspaceCacheMap.get(workspacePath) || null);
}
const workspaceRoot = dirname(workspacePath);
const workspaceFileName = basename(workspacePath);
const workspace = new experimental.workspace.Workspace(workspaceRoot, this._host);
return workspace.loadWorkspaceFromHost(workspaceFileName).pipe(
tap(workspace => this._workspaceCacheMap.set(workspacePath, workspace)),
);
}
}