Skip to content

Commit becf288

Browse files
authored
feat: improve ng-add logging (#988)
1 parent d478b34 commit becf288

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
import { makeV16AddRootProvider } from './make-v16-add-root-provider'
22
import { AddRootProvider } from './index'
33
import { type addRootProvider } from '@schematics/angular/utility'
4-
import { noop } from '@angular-devkit/schematics'
54

6-
export const makeAddRootProviderFn = async (): Promise<AddRootProvider> => {
5+
export const makeAddRootProviderFn = async (): Promise<
6+
AddRootProvider | undefined
7+
> => {
78
const ngAddRootProvider = (await import('@schematics/angular/utility'))
89
.addRootProvider as typeof addRootProvider | undefined
9-
/* istanbul ignore next https://github.com/istanbuljs/istanbuljs/issues/719 */
1010
if (!ngAddRootProvider) {
11-
// Standalone schematic utils are only available for v16.1 and above.
12-
// https://github.com/angular/angular-cli/commit/b14b959901d5a670da0df45e082b8fd4c3392d14
13-
const message =
14-
'ngx-meta: `ng add` schematics only work for Angular v16.1 and above, sorry :(\n' +
15-
" Please, setup the library manually. Don't worry, it's just a few lines around :)\n" +
16-
' You can find a guide at: https://ngx-meta.dev/get-started/'
17-
console.log(message)
18-
return noop
11+
return
1912
}
2013
return makeV16AddRootProvider(ngAddRootProvider)
2114
}

projects/ngx-meta/schematics/ng-add/index.spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { createTestApp } from '../testing/create-test-app'
1515
import { shouldAddRootProvider } from './testing/should-add-root-provider'
1616
import { shouldNotAddRootProvider } from './testing/should-not-add-root-provider'
1717
import * as AngularSchematicsUtilities from '@schematics/angular/utility'
18+
import { logging } from '@angular-devkit/core'
1819

1920
// https://github.com/angular/components/blob/18.2.8/src/cdk/schematics/ng-add/index.spec.ts
2021
// 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', () => {
2930
} satisfies Partial<NgAddSchema>
3031

3132
beforeEach(async () => {
33+
jest.spyOn(logging.Logger.prototype, 'info').mockReturnValue()
3234
runner = new SchematicTestRunner(
3335
'schematics',
3436
join(__dirname, '..', 'collection.json'),
3537
)
3638
})
39+
afterEach(() => {
40+
jest.restoreAllMocks()
41+
})
3742

3843
const CORE_PROVIDER = new ProviderTestCase({
3944
name: 'core',
@@ -144,10 +149,10 @@ describe('ng-add schematic', () => {
144149

145150
// Below Angular v16.1
146151
describe("when addRootProvider isn't available", () => {
147-
let consoleLog: jest.Spied<Console['log']>
152+
let logSpy: jest.Spied<(typeof logging.Logger.prototype)['warn']>
148153

149154
beforeEach(async () => {
150-
consoleLog = jest.spyOn(console, 'log').mockReturnValue()
155+
logSpy = jest.spyOn(logging.Logger.prototype, 'warn').mockReturnValue()
151156
jest.mock<Partial<typeof AngularSchematicsUtilities>>(
152157
'@schematics/angular/utility',
153158
() =>
@@ -162,12 +167,9 @@ describe('ng-add schematic', () => {
162167
appTree,
163168
)
164169
})
165-
afterEach(() => {
166-
jest.restoreAllMocks()
167-
})
168170

169171
it('should log a message', () => {
170-
expect(consoleLog).toHaveBeenCalledWith(
172+
expect(logSpy).toHaveBeenCalledWith(
171173
expect.stringContaining('Please, setup the library manually'),
172174
)
173175
})
Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
import { chain, noop, Rule } from '@angular-devkit/schematics'
1+
import { chain, noop, Rule, SchematicContext } from '@angular-devkit/schematics'
22
import { Schema } from './schema'
33
import { makeAddRootProviderFn } from './add-root-provider'
44

55
// noinspection JSUnusedGlobalSymbols (actually used in `collection.json`)
66
export function ngAdd(options: Schema): Rule {
7-
return async () => {
7+
return async (_tree, context) => {
88
const addRootProvider = await makeAddRootProviderFn()
9+
if (!addRootProvider) {
10+
// Standalone schematic utils are only available for v16.1 and above.
11+
// https://github.com/angular/angular-cli/commit/b14b959901d5a670da0df45e082b8fd4c3392d14
12+
context.logger.warn(
13+
[
14+
'ngx-meta `ng add` schematics only work for Angular v16.1 and above, sorry :(',
15+
"Please, setup the library manually. Don't worry, it's just a few lines around :)",
16+
'You can find a guide at: https://ngx-meta.dev/get-started/',
17+
].join('\n'),
18+
)
19+
logLibraryInfo(context.logger)
20+
return
21+
}
22+
923
const addRootProviderToProject = (name: string) =>
1024
addRootProvider({ name, project: options.project })
1125

@@ -15,6 +29,21 @@ export function ngAdd(options: Schema): Rule {
1529
...options.metadataModules.map((metadataModule) =>
1630
addRootProviderToProject(metadataModule),
1731
),
32+
() => {
33+
context.logger.info('ngx-meta library was successfully set up 🚀')
34+
logLibraryInfo(context.logger)
35+
},
1836
])
1937
}
2038
}
39+
40+
const logLibraryInfo = (logger: SchematicContext['logger']) =>
41+
logger.info(
42+
[
43+
'For more information, you can check out:',
44+
' - Documentation: https://ngx-meta.dev',
45+
' - Repository: https://github.com/davidlj95/ngx',
46+
' - NPM: https://www.npmjs.com/package/@davidlj95/ngx-meta',
47+
'If you enjoyed using the library, please star its GitHub repository! ⭐️❤️',
48+
].join('\n'),
49+
)

0 commit comments

Comments
 (0)