Skip to content

Commit 900f7ba

Browse files
clydinalan-agius4
authored andcommitted
refactor(@angular-devkit/core): add allowed extensions options to JSON workspace reader
Adjust the internal `readJsonWorkspace` to allow for future generalization of the allowed unprefixed extension fields for the workspace and project objects. Custom fields that start with a one to three lowercase letter prefix are still allowed in both locations. (cherry picked from commit fa0404d)
1 parent 0089ec1 commit 900f7ba

File tree

1 file changed

+26
-6
lines changed
  • packages/angular_devkit/core/src/workspace/json

1 file changed

+26
-6
lines changed

packages/angular_devkit/core/src/workspace/json/reader.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,33 @@ import { WorkspaceHost } from '../host';
2020
import { JsonWorkspaceMetadata, JsonWorkspaceSymbol } from './metadata';
2121
import { createVirtualAstObject } from './utilities';
2222

23+
const ANGULAR_WORKSPACE_EXTENSIONS = Object.freeze([
24+
'cli',
25+
'defaultProject',
26+
'newProjectRoot',
27+
'schematics',
28+
]);
29+
const ANGULAR_PROJECT_EXTENSIONS = Object.freeze(['cli', 'schematics', 'projectType', 'i18n']);
30+
2331
interface ParserContext {
2432
readonly host: WorkspaceHost;
2533
readonly metadata: JsonWorkspaceMetadata;
2634
readonly trackChanges: boolean;
35+
readonly unprefixedWorkspaceExtensions: ReadonlySet<string>;
36+
readonly unprefixedProjectExtensions: ReadonlySet<string>;
2737
error(message: string, node: JsonValue): void;
2838
warn(message: string, node: JsonValue): void;
2939
}
3040

41+
export interface JsonWorkspaceOptions {
42+
allowedProjectExtensions?: string[];
43+
allowedWorkspaceExtensions?: string[];
44+
}
45+
3146
export async function readJsonWorkspace(
3247
path: string,
3348
host: WorkspaceHost,
49+
options: JsonWorkspaceOptions = {},
3450
): Promise<WorkspaceDefinition> {
3551
const raw = await host.readFile(path);
3652
if (raw === undefined) {
@@ -56,6 +72,14 @@ export async function readJsonWorkspace(
5672
host,
5773
metadata: new JsonWorkspaceMetadata(path, ast, raw),
5874
trackChanges: true,
75+
unprefixedWorkspaceExtensions: new Set([
76+
...ANGULAR_WORKSPACE_EXTENSIONS,
77+
...(options.allowedWorkspaceExtensions ?? []),
78+
]),
79+
unprefixedProjectExtensions: new Set([
80+
...ANGULAR_PROJECT_EXTENSIONS,
81+
...(options.allowedProjectExtensions ?? []),
82+
]),
5983
error(message, _node) {
6084
// TODO: Diagnostic reporting support
6185
throw new Error(message);
@@ -72,10 +96,6 @@ export async function readJsonWorkspace(
7296
return workspace;
7397
}
7498

75-
const specialWorkspaceExtensions = ['cli', 'defaultProject', 'newProjectRoot', 'schematics'];
76-
77-
const specialProjectExtensions = ['cli', 'schematics', 'projectType', 'i18n'];
78-
7999
function parseWorkspace(workspaceNode: Node, context: ParserContext): WorkspaceDefinition {
80100
const jsonMetadata = context.metadata;
81101
let projects;
@@ -99,7 +119,7 @@ function parseWorkspace(workspaceNode: Node, context: ParserContext): WorkspaceD
99119

100120
projects = parseProjectsObject(nodes, context);
101121
} else {
102-
if (!specialWorkspaceExtensions.includes(name) && !/^[a-z]{1,3}-.*/.test(name)) {
122+
if (!context.unprefixedWorkspaceExtensions.has(name) && !/^[a-z]{1,3}-.*/.test(name)) {
103123
context.warn(`Project extension with invalid name (${name}) found.`, name);
104124
}
105125
if (extensions) {
@@ -201,7 +221,7 @@ function parseProject(
201221
}
202222
break;
203223
default:
204-
if (!specialProjectExtensions.includes(name) && !/^[a-z]{1,3}-.*/.test(name)) {
224+
if (!context.unprefixedProjectExtensions.has(name) && !/^[a-z]{1,3}-.*/.test(name)) {
205225
context.warn(`Project extension with invalid name (${name}) found.`, name);
206226
}
207227
if (extensions) {

0 commit comments

Comments
 (0)