From becf28852b7f16e288cc5214ba54002186f87c12 Mon Sep 17 00:00:00 2001 From: David LJ Date: Thu, 24 Oct 2024 02:21:39 +0200 Subject: [PATCH] feat: improve `ng-add` logging (#988) --- .../make-add-root-provider-fn.ts | 15 +++------ .../ngx-meta/schematics/ng-add/index.spec.ts | 14 ++++---- projects/ngx-meta/schematics/ng-add/index.ts | 33 +++++++++++++++++-- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/projects/ngx-meta/schematics/ng-add/add-root-provider/make-add-root-provider-fn.ts b/projects/ngx-meta/schematics/ng-add/add-root-provider/make-add-root-provider-fn.ts index 854cbdf5..c97a29e5 100644 --- a/projects/ngx-meta/schematics/ng-add/add-root-provider/make-add-root-provider-fn.ts +++ b/projects/ngx-meta/schematics/ng-add/add-root-provider/make-add-root-provider-fn.ts @@ -1,21 +1,14 @@ import { makeV16AddRootProvider } from './make-v16-add-root-provider' import { AddRootProvider } from './index' import { type addRootProvider } from '@schematics/angular/utility' -import { noop } from '@angular-devkit/schematics' -export const makeAddRootProviderFn = async (): Promise => { +export const makeAddRootProviderFn = async (): Promise< + AddRootProvider | undefined +> => { const ngAddRootProvider = (await import('@schematics/angular/utility')) .addRootProvider as typeof addRootProvider | undefined - /* istanbul ignore next https://github.com/istanbuljs/istanbuljs/issues/719 */ if (!ngAddRootProvider) { - // Standalone schematic utils are only available for v16.1 and above. - // https://github.com/angular/angular-cli/commit/b14b959901d5a670da0df45e082b8fd4c3392d14 - const message = - 'ngx-meta: `ng add` schematics only work for Angular v16.1 and above, sorry :(\n' + - " Please, setup the library manually. Don't worry, it's just a few lines around :)\n" + - ' You can find a guide at: https://ngx-meta.dev/get-started/' - console.log(message) - return noop + return } return makeV16AddRootProvider(ngAddRootProvider) } diff --git a/projects/ngx-meta/schematics/ng-add/index.spec.ts b/projects/ngx-meta/schematics/ng-add/index.spec.ts index f22952ad..ade89597 100644 --- a/projects/ngx-meta/schematics/ng-add/index.spec.ts +++ b/projects/ngx-meta/schematics/ng-add/index.spec.ts @@ -15,6 +15,7 @@ import { createTestApp } from '../testing/create-test-app' import { shouldAddRootProvider } from './testing/should-add-root-provider' import { shouldNotAddRootProvider } from './testing/should-not-add-root-provider' import * as AngularSchematicsUtilities from '@schematics/angular/utility' +import { logging } from '@angular-devkit/core' // https://github.com/angular/components/blob/18.2.8/src/cdk/schematics/ng-add/index.spec.ts // https://github.com/angular/components/blob/18.2.8/src/material/schematics/ng-add/index.spec.ts @@ -29,11 +30,15 @@ describe('ng-add schematic', () => { } satisfies Partial beforeEach(async () => { + jest.spyOn(logging.Logger.prototype, 'info').mockReturnValue() runner = new SchematicTestRunner( 'schematics', join(__dirname, '..', 'collection.json'), ) }) + afterEach(() => { + jest.restoreAllMocks() + }) const CORE_PROVIDER = new ProviderTestCase({ name: 'core', @@ -144,10 +149,10 @@ describe('ng-add schematic', () => { // Below Angular v16.1 describe("when addRootProvider isn't available", () => { - let consoleLog: jest.Spied + let logSpy: jest.Spied<(typeof logging.Logger.prototype)['warn']> beforeEach(async () => { - consoleLog = jest.spyOn(console, 'log').mockReturnValue() + logSpy = jest.spyOn(logging.Logger.prototype, 'warn').mockReturnValue() jest.mock>( '@schematics/angular/utility', () => @@ -162,12 +167,9 @@ describe('ng-add schematic', () => { appTree, ) }) - afterEach(() => { - jest.restoreAllMocks() - }) it('should log a message', () => { - expect(consoleLog).toHaveBeenCalledWith( + expect(logSpy).toHaveBeenCalledWith( expect.stringContaining('Please, setup the library manually'), ) }) diff --git a/projects/ngx-meta/schematics/ng-add/index.ts b/projects/ngx-meta/schematics/ng-add/index.ts index 12acc9a5..f5effa39 100644 --- a/projects/ngx-meta/schematics/ng-add/index.ts +++ b/projects/ngx-meta/schematics/ng-add/index.ts @@ -1,11 +1,25 @@ -import { chain, noop, Rule } from '@angular-devkit/schematics' +import { chain, noop, Rule, SchematicContext } from '@angular-devkit/schematics' import { Schema } from './schema' import { makeAddRootProviderFn } from './add-root-provider' // noinspection JSUnusedGlobalSymbols (actually used in `collection.json`) export function ngAdd(options: Schema): Rule { - return async () => { + return async (_tree, context) => { const addRootProvider = await makeAddRootProviderFn() + if (!addRootProvider) { + // Standalone schematic utils are only available for v16.1 and above. + // https://github.com/angular/angular-cli/commit/b14b959901d5a670da0df45e082b8fd4c3392d14 + context.logger.warn( + [ + 'ngx-meta `ng add` schematics only work for Angular v16.1 and above, sorry :(', + "Please, setup the library manually. Don't worry, it's just a few lines around :)", + 'You can find a guide at: https://ngx-meta.dev/get-started/', + ].join('\n'), + ) + logLibraryInfo(context.logger) + return + } + const addRootProviderToProject = (name: string) => addRootProvider({ name, project: options.project }) @@ -15,6 +29,21 @@ export function ngAdd(options: Schema): Rule { ...options.metadataModules.map((metadataModule) => addRootProviderToProject(metadataModule), ), + () => { + context.logger.info('ngx-meta library was successfully set up 🚀') + logLibraryInfo(context.logger) + }, ]) } } + +const logLibraryInfo = (logger: SchematicContext['logger']) => + logger.info( + [ + 'For more information, you can check out:', + ' - Documentation: https://ngx-meta.dev', + ' - Repository: https://github.com/davidlj95/ngx', + ' - NPM: https://www.npmjs.com/package/@davidlj95/ngx-meta', + 'If you enjoyed using the library, please star its GitHub repository! ⭐️❤️', + ].join('\n'), + )