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