Skip to content

Commit

Permalink
feat: improve ng-add logging (#988)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlj95 authored Oct 24, 2024
1 parent d478b34 commit becf288
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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<AddRootProvider> => {
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)
}
14 changes: 8 additions & 6 deletions projects/ngx-meta/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,11 +30,15 @@ describe('ng-add schematic', () => {
} satisfies Partial<NgAddSchema>

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',
Expand Down Expand Up @@ -144,10 +149,10 @@ describe('ng-add schematic', () => {

// Below Angular v16.1
describe("when addRootProvider isn't available", () => {
let consoleLog: jest.Spied<Console['log']>
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<Partial<typeof AngularSchematicsUtilities>>(
'@schematics/angular/utility',
() =>
Expand All @@ -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'),
)
})
Expand Down
33 changes: 31 additions & 2 deletions projects/ngx-meta/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -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 })

Expand All @@ -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'),
)

0 comments on commit becf288

Please sign in to comment.