mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-19 04:26:01 +08:00
refactor(@angular/cli): use jsonc-parser for JSON configuration parsing
This commit is contained in:
parent
0a02ea55bb
commit
7cbc72e16f
@ -78,6 +78,7 @@ ts_library(
|
||||
"@npm//@types/universal-analytics",
|
||||
"@npm//@types/uuid",
|
||||
"@npm//ansi-colors",
|
||||
"@npm//jsonc-parser",
|
||||
],
|
||||
)
|
||||
|
||||
|
@ -35,6 +35,7 @@
|
||||
"debug": "4.2.0",
|
||||
"ini": "1.3.5",
|
||||
"inquirer": "7.3.3",
|
||||
"jsonc-parser": "2.3.1",
|
||||
"npm-package-arg": "8.1.0",
|
||||
"npm-pick-manifest": "6.1.0",
|
||||
"open": "7.3.0",
|
||||
|
@ -5,17 +5,9 @@
|
||||
* 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 {
|
||||
JsonAstObject,
|
||||
JsonObject,
|
||||
JsonParseMode,
|
||||
json,
|
||||
parseJson,
|
||||
parseJsonAst,
|
||||
workspaces,
|
||||
} from '@angular-devkit/core';
|
||||
import { json, parseJsonAst, workspaces } from '@angular-devkit/core';
|
||||
import { existsSync, readFileSync, statSync, writeFileSync } from 'fs';
|
||||
import { parse as parseJson } from 'jsonc-parser';
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
import { findUp } from './find-up';
|
||||
@ -193,7 +185,7 @@ export function createGlobalSettings(): string {
|
||||
|
||||
export function getWorkspaceRaw(
|
||||
level: 'local' | 'global' = 'local',
|
||||
): [JsonAstObject | null, string | null] {
|
||||
): [json.JsonAstObject | null, string | null] {
|
||||
let configPath = level === 'local' ? projectFilePath() : globalFilePath();
|
||||
|
||||
if (!configPath) {
|
||||
@ -211,7 +203,7 @@ export function getWorkspaceRaw(
|
||||
start = 3;
|
||||
}
|
||||
const content = data.toString('utf-8', start);
|
||||
const ast = parseJsonAst(content, JsonParseMode.Loose);
|
||||
const ast = parseJsonAst(content, json.JsonParseMode.Loose);
|
||||
|
||||
if (ast.kind != 'object') {
|
||||
throw new Error(`Invalid JSON file: ${configPath}`);
|
||||
@ -220,12 +212,12 @@ export function getWorkspaceRaw(
|
||||
return [ast, configPath];
|
||||
}
|
||||
|
||||
export async function validateWorkspace(data: JsonObject): Promise<void> {
|
||||
export async function validateWorkspace(data: json.JsonObject): Promise<void> {
|
||||
const schemaContent = readFileSync(
|
||||
path.join(__dirname, '..', 'lib', 'config', 'schema.json'),
|
||||
'utf-8',
|
||||
);
|
||||
const schema = parseJson(schemaContent, JsonParseMode.Loose) as json.schema.JsonSchema;
|
||||
const schema = parseJson(schemaContent) as json.schema.JsonSchema;
|
||||
const { formats } = await import('@angular-devkit/schematics');
|
||||
const registry = new json.schema.CoreSchemaRegistry(formats.standardFormats);
|
||||
const validator = await registry.compile(schema).toPromise();
|
||||
@ -340,12 +332,12 @@ export function migrateLegacyGlobalConfig(): boolean {
|
||||
const legacyGlobalConfigPath = path.join(homeDir, '.angular-cli.json');
|
||||
if (existsSync(legacyGlobalConfigPath)) {
|
||||
const content = readFileSync(legacyGlobalConfigPath, 'utf-8');
|
||||
const legacy = parseJson(content, JsonParseMode.Loose);
|
||||
const legacy = parseJson(content);
|
||||
if (!isJsonObject(legacy)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const cli: JsonObject = {};
|
||||
const cli: json.JsonObject = {};
|
||||
|
||||
if (
|
||||
legacy.packageManager &&
|
||||
@ -364,7 +356,7 @@ export function migrateLegacyGlobalConfig(): boolean {
|
||||
}
|
||||
|
||||
if (isJsonObject(legacy.warnings)) {
|
||||
const warnings: JsonObject = {};
|
||||
const warnings: json.JsonObject = {};
|
||||
if (typeof legacy.warnings.versionMismatch == 'boolean') {
|
||||
warnings['versionMismatch'] = legacy.warnings.versionMismatch;
|
||||
}
|
||||
@ -394,7 +386,7 @@ function getLegacyPackageManager(): string | null {
|
||||
if (existsSync(legacyGlobalConfigPath)) {
|
||||
const content = readFileSync(legacyGlobalConfigPath, 'utf-8');
|
||||
|
||||
const legacy = parseJson(content, JsonParseMode.Loose);
|
||||
const legacy = parseJson(content);
|
||||
if (!isJsonObject(legacy)) {
|
||||
return null;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user