Skip to content

Commit

Permalink
feat(@angular-devkit/core): add workspace reader/writer core API
Browse files Browse the repository at this point in the history
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
clydin authored and hansl committed Mar 6, 2019
1 parent 263231e commit a1aa8d5
Show file tree
Hide file tree
Showing 7 changed files with 1,195 additions and 0 deletions.
108 changes: 108 additions & 0 deletions packages/angular_devkit/core/src/workspace/core.ts
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.');
}
}
Loading

0 comments on commit a1aa8d5

Please sign in to comment.