-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular-devkit/core): add workspace reader/writer core API
NOTE: No changes have yet been made to the public API of the package. This introduces the core and eventual public API for the stable workspace API. This is not intended to be feature complete but rather represents the initial base infrastucture necessary for pending future feature additions.
- Loading branch information
Showing
7 changed files
with
1,195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* @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 { basename, getSystemPath, join, normalize } from '../virtual-fs'; | ||
import { ProjectDefinitionCollection, WorkspaceDefinition } from './definitions'; | ||
import { WorkspaceHost } from './host'; | ||
|
||
const formatLookup = new WeakMap<WorkspaceDefinition, WorkspaceFormat>(); | ||
|
||
export enum WorkspaceFormat { | ||
JSON, | ||
} | ||
|
||
export function _test_addWorkspaceFile(name: string, format: WorkspaceFormat): void { | ||
workspaceFiles[name] = format; | ||
} | ||
|
||
export function _test_removeWorkspaceFile(name: string): void { | ||
delete workspaceFiles[name]; | ||
} | ||
|
||
// NOTE: future additions could also perform content analysis to determine format/version | ||
const workspaceFiles: Record<string, WorkspaceFormat> = { | ||
'angular.json': WorkspaceFormat.JSON, | ||
'.angular.json': WorkspaceFormat.JSON, | ||
}; | ||
|
||
export async function readWorkspace( | ||
path: string, | ||
host: WorkspaceHost, | ||
format?: WorkspaceFormat, | ||
): Promise<WorkspaceDefinition> { | ||
if (await host.isDirectory(path)) { | ||
// TODO: Warn if multiple found (requires diagnostics support) | ||
const directory = normalize(path); | ||
let found = false; | ||
for (const [name, nameFormat] of Object.entries(workspaceFiles)) { | ||
if (format !== undefined && format !== nameFormat) { | ||
continue; | ||
} | ||
|
||
const potential = getSystemPath(join(directory, name)); | ||
if (await host.isFile(potential)) { | ||
// TEMP - remove disable when actual reader is used | ||
// tslint:disable-next-line:no-dead-store | ||
path = potential; | ||
format = nameFormat; | ||
found = true; | ||
break; | ||
} | ||
} | ||
if (!found) { | ||
throw new Error('Unable to locate a workspace file for workspace path.'); | ||
} | ||
} else if (format === undefined) { | ||
const filename = basename(normalize(path)); | ||
if (filename in workspaceFiles) { | ||
format = workspaceFiles[filename]; | ||
} | ||
} | ||
|
||
if (format === undefined) { | ||
throw new Error('Unable to determine format for workspace path.'); | ||
} | ||
|
||
let workspace; | ||
switch (format) { | ||
case WorkspaceFormat.JSON: | ||
// TEMP: remove the following two statements when JSON support is introduced | ||
await host.readFile(path); | ||
workspace = { | ||
extensions: {}, | ||
projects: new ProjectDefinitionCollection(), | ||
}; | ||
break; | ||
default: | ||
throw new Error('Unsupported workspace format.'); | ||
} | ||
|
||
formatLookup.set(workspace, WorkspaceFormat.JSON); | ||
|
||
return workspace; | ||
} | ||
|
||
export async function writeWorkspace( | ||
workspace: WorkspaceDefinition, | ||
_host: WorkspaceHost, | ||
_path?: string, | ||
format?: WorkspaceFormat, | ||
): Promise<void> { | ||
if (format === undefined) { | ||
format = formatLookup.get(workspace); | ||
if (format === undefined) { | ||
throw new Error('A format is required for custom workspace objects.'); | ||
} | ||
} | ||
|
||
switch (format) { | ||
case WorkspaceFormat.JSON: | ||
throw new Error('Not Implemented.'); | ||
default: | ||
throw new Error('Unsupported workspace format.'); | ||
} | ||
} |
Oops, something went wrong.