fix(core): print actual config name when using scope host

This commit is contained in:
Victor Savkin 2021-01-05 10:35:54 -05:00
parent f4098354b6
commit f907e83dd1

View File

@ -31,6 +31,7 @@ import { readFileSync } from 'fs';
import { detectPackageManager } from '@nrwl/tao/src/shared/package-manager'; import { detectPackageManager } from '@nrwl/tao/src/shared/package-manager';
import { GenerateOptions } from './generate'; import { GenerateOptions } from './generate';
import * as taoTree from '../shared/tree'; import * as taoTree from '../shared/tree';
import { Tree } from '../shared/tree';
import { import {
toNewFormatOrNull, toNewFormatOrNull,
toOldFormatOrNull, toOldFormatOrNull,
@ -43,7 +44,7 @@ import { dirname, extname, join, resolve } from 'path';
import * as stripJsonComments from 'strip-json-comments'; import * as stripJsonComments from 'strip-json-comments';
import { FileBuffer } from '@angular-devkit/core/src/virtual-fs/host/interface'; import { FileBuffer } from '@angular-devkit/core/src/virtual-fs/host/interface';
import { Observable, of } from 'rxjs'; import { Observable, of } from 'rxjs';
import { concatMap, map, switchMap } from 'rxjs/operators'; import { map, switchMap } from 'rxjs/operators';
import { NX_ERROR, NX_PREFIX } from '../shared/logger'; import { NX_ERROR, NX_PREFIX } from '../shared/logger';
export async function run(root: string, opts: RunOptions, verbose: boolean) { export async function run(root: string, opts: RunOptions, verbose: boolean) {
@ -98,64 +99,28 @@ function getCollection(workflow: any, name: string) {
return collection; return collection;
} }
function convertEventTypeToHandleMultipleConfigNames( async function createRecorder(
host: any, host: NxScopedHost,
eventPath: string,
content: Buffer | never
) {
const actualConfigName = host.exists('/workspace.json')
? 'workspace.json'
: 'angular.json';
const isWorkspaceConfig =
eventPath === 'angular.json' || eventPath === 'workspace.json';
if (isWorkspaceConfig) {
let isNewFormat = true;
try {
isNewFormat =
JSON.parse(host.read(actualConfigName).toString()).version === 2;
} catch (e) {}
if (content && isNewFormat) {
const formatted = toNewFormatOrNull(JSON.parse(content.toString()));
if (formatted) {
return {
eventPath: actualConfigName,
content: Buffer.from(JSON.stringify(formatted, null, 2)),
};
} else {
return { eventPath: actualConfigName, content: content };
}
} else {
return { eventPath: actualConfigName, content: content };
}
} else {
return { eventPath, content };
}
}
function createRecorder(
host: any,
record: { record: {
loggingQueue: string[]; loggingQueue: string[];
error: boolean; error: boolean;
}, },
logger: logging.Logger logger: logging.Logger
) { ) {
const actualConfigName = await host.workspaceConfigName();
return (event: DryRunEvent) => { return (event: DryRunEvent) => {
const eventPath = event.path.startsWith('/') let eventPath = event.path.startsWith('/')
? event.path.substr(1) ? event.path.substr(1)
: event.path; : event.path;
const r = convertEventTypeToHandleMultipleConfigNames( if (eventPath === 'workspace.json' || eventPath === 'angular.json') {
host, eventPath = actualConfigName;
eventPath, }
(event as any).content
);
if (event.kind === 'error') { if (event.kind === 'error') {
record.error = true; record.error = true;
logger.warn( logger.warn(
`ERROR! ${r.eventPath} ${ `ERROR! ${eventPath} ${
event.description == 'alreadyExist' event.description == 'alreadyExist'
? 'already exists' ? 'already exists'
: 'does not exist.' : 'does not exist.'
@ -163,24 +128,24 @@ function createRecorder(
); );
} else if (event.kind === 'update') { } else if (event.kind === 'update') {
record.loggingQueue.push( record.loggingQueue.push(
tags.oneLine`${chalk.white('UPDATE')} ${r.eventPath}` tags.oneLine`${chalk.white('UPDATE')} ${eventPath}`
); );
} else if (event.kind === 'create') { } else if (event.kind === 'create') {
record.loggingQueue.push( record.loggingQueue.push(
tags.oneLine`${chalk.green('CREATE')} ${r.eventPath}` tags.oneLine`${chalk.green('CREATE')} ${eventPath}`
); );
} else if (event.kind === 'delete') { } else if (event.kind === 'delete') {
record.loggingQueue.push(`${chalk.yellow('DELETE')} ${r.eventPath}`); record.loggingQueue.push(`${chalk.yellow('DELETE')} ${eventPath}`);
} else if (event.kind === 'rename') { } else if (event.kind === 'rename') {
record.loggingQueue.push( record.loggingQueue.push(
`${chalk.blue('RENAME')} ${r.eventPath} => ${event.to}` `${chalk.blue('RENAME')} ${eventPath} => ${event.to}`
); );
} }
}; };
} }
async function runSchematic( async function runSchematic(
host: any, host: NxScopedHost,
root: string, root: string,
workflow: NodeWorkflow, workflow: NodeWorkflow,
logger: logging.Logger, logger: logging.Logger,
@ -193,7 +158,9 @@ async function runSchematic(
recorder: any = null recorder: any = null
): Promise<{ status: number; loggingQueue: string[] }> { ): Promise<{ status: number; loggingQueue: string[] }> {
const record = { loggingQueue: [] as string[], error: false }; const record = { loggingQueue: [] as string[], error: false };
workflow.reporter.subscribe(recorder || createRecorder(host, record, logger)); workflow.reporter.subscribe(
recorder || (await createRecorder(host, record, logger))
);
try { try {
await workflow await workflow
@ -382,6 +349,17 @@ export class NxScopedHost extends virtualFs.ScopedHost<any> {
); );
} }
workspaceConfigName(): Promise<string> {
return super
.exists('/angular.json' as any)
.pipe(
map((hasAngularJson) =>
hasAngularJson ? 'angular.json' : 'workspace.json'
)
)
.toPromise();
}
private context( private context(
path: string path: string
): Observable<{ ): Observable<{
@ -552,11 +530,46 @@ export async function runMigration(
.toPromise(); .toPromise();
} }
function convertEventTypeToHandleMultipleConfigNames(
host: Tree,
eventPath: string,
content: Buffer | never
) {
const actualConfigName = host.exists('/workspace.json')
? 'workspace.json'
: 'angular.json';
const isWorkspaceConfig =
eventPath === 'angular.json' || eventPath === 'workspace.json';
if (isWorkspaceConfig) {
let isNewFormat = true;
try {
isNewFormat =
JSON.parse(host.read(actualConfigName).toString()).version === 2;
} catch (e) {}
if (content && isNewFormat) {
const formatted = toNewFormatOrNull(JSON.parse(content.toString()));
if (formatted) {
return {
eventPath: actualConfigName,
content: Buffer.from(JSON.stringify(formatted, null, 2)),
};
} else {
return { eventPath: actualConfigName, content: content };
}
} else {
return { eventPath: actualConfigName, content: content };
}
} else {
return { eventPath, content };
}
}
export function wrapAngularDevkitSchematic( export function wrapAngularDevkitSchematic(
collectionName: string, collectionName: string,
generatorName: string generatorName: string
): any { ): any {
return async (host: any, generatorOptions: { [k: string]: any }) => { return async (host: Tree, generatorOptions: { [k: string]: any }) => {
const emptyLogger = { const emptyLogger = {
log: (e) => {}, log: (e) => {},
info: (e) => {}, info: (e) => {},