Skip to content

Commit fefef02

Browse files
hanslalexeagle
authored andcommitted
refactor(@angular/cli): remove parseJsonFile and add options to parseJson instead
1 parent c72c45f commit fefef02

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

‎packages/angular/cli/models/command-runner.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,21 @@ export async function runCommand(
5252
}
5353
const cliDir = dirname(commandMapPath);
5454
const commandsText = readFileSync(commandMapPath).toString('utf-8');
55-
const commandJson = json.parseJsonFile(
55+
const commandJson = json.parseJson(
5656
commandsText,
5757
JsonParseMode.Loose,
58-
commandMapPath,
59-
) as { [name: string]: string };
58+
{ path: commandMapPath },
59+
);
60+
if (!isJsonObject(commandJson)) {
61+
throw Error('Invalid command.json');
62+
}
6063

6164
commands = {};
6265
for (const commandName of Object.keys(commandJson)) {
63-
commands[commandName] = resolve(cliDir, commandJson[commandName]);
66+
const commandValue = commandJson[commandName];
67+
if (typeof commandValue == 'string') {
68+
commands[commandName] = resolve(cliDir, commandValue);
69+
}
6470
}
6571
}
6672

@@ -79,7 +85,7 @@ export async function runCommand(
7985
for (const name of Object.keys(commands)) {
8086
const schemaPath = commands[name];
8187
const schemaContent = readFileSync(schemaPath, 'utf-8');
82-
const schema = json.parseJsonFile(schemaContent, JsonParseMode.Loose, schemaPath);
88+
const schema = json.parseJson(schemaContent, JsonParseMode.Loose, { path: schemaPath });
8389
if (!isJsonObject(schema)) {
8490
throw new Error('Invalid command JSON loaded from ' + JSON.stringify(schemaPath));
8591
}

‎packages/angular_devkit/core/src/json/parser.ts

+31-23
Original file line numberDiff line numberDiff line change
@@ -862,40 +862,48 @@ export function parseJsonAst(input: string, mode = JsonParseMode.Default): JsonA
862862

863863

864864
/**
865-
* Parse a JSON string into its value. This discards the AST and only returns the value itself.
866-
* @param input The string to parse.
867-
* @param mode The mode to parse the input with. {@see JsonParseMode}.
868-
* @returns {JsonValue} The value represented by the JSON string.
865+
* Options for the parseJson() function.
869866
*/
870-
export function parseJson(input: string, mode = JsonParseMode.Default): JsonValue {
871-
// Try parsing for the fastest path available, if error, uses our own parser for better errors.
872-
if (mode == JsonParseMode.Strict) {
873-
try {
874-
return JSON.parse(input);
875-
} catch (err) {
876-
return parseJsonAst(input, mode).value;
877-
}
878-
}
879-
880-
return parseJsonAst(input, mode).value;
867+
interface ParseJsonOptions {
868+
/**
869+
* If omitted, will only emit errors related to the content of the JSON. If specified, any
870+
* JSON errors will also include the path of the file that caused the error.
871+
*/
872+
path?: string;
881873
}
882874

875+
883876
/**
884877
* Parse a JSON string into its value. This discards the AST and only returns the value itself.
885-
* It also absorbs JSON parsing errors and return a new error with the path in it. Useful for
886-
* showing errors when parsing from a file.
878+
*
879+
* If a path option is pass, it also absorbs JSON parsing errors and return a new error with the
880+
* path in it. Useful for showing errors when parsing from a file.
881+
*
887882
* @param input The string to parse.
888883
* @param mode The mode to parse the input with. {@see JsonParseMode}.
884+
* @param options Additional optinos for parsing.
889885
* @returns {JsonValue} The value represented by the JSON string.
890886
*/
891-
export function parseJsonFile(input: string, mode = JsonParseMode.Default, path: string) {
887+
export function parseJson(
888+
input: string,
889+
mode = JsonParseMode.Default,
890+
options?: ParseJsonOptions,
891+
): JsonValue {
892892
try {
893-
return parseJson(input, mode);
893+
// Try parsing for the fastest path available, if error, uses our own parser for better errors.
894+
if (mode == JsonParseMode.Strict) {
895+
try {
896+
return JSON.parse(input);
897+
} catch (err) {
898+
return parseJsonAst(input, mode).value;
899+
}
900+
}
901+
902+
return parseJsonAst(input, mode).value;
894903
} catch (e) {
895-
if (e instanceof JsonException) {
896-
throw new PathSpecificJsonException(path, e);
897-
} else {
898-
throw e;
904+
if (options && options.path && e instanceof JsonException) {
905+
throw new PathSpecificJsonException(options.path, e);
899906
}
907+
throw e;
900908
}
901909
}

0 commit comments

Comments
��(0)