From 3682e2d3de5267b26389dfc6d8dd80fa76c8c954 Mon Sep 17 00:00:00 2001 From: cipchk Date: Sun, 30 Jun 2024 15:01:30 +0800 Subject: [PATCH] chore: fix cli test --- schematics/collection.json | 2 +- schematics/migration.json | 4 +- schematics/ng-add/index.ts | 2 +- .../ng-update/data/attribute-selectors.ts | 2 +- schematics/ng-update/data/class-names.ts | 2 +- .../ng-update/data/constructor-checks.ts | 2 +- schematics/ng-update/data/css-selectors.ts | 2 +- schematics/ng-update/data/css-tokens.ts | 5 + .../ng-update/data/element-selectors.ts | 2 +- schematics/ng-update/data/index.ts | 1 + schematics/ng-update/data/input-names.ts | 2 +- .../ng-update/data/method-call-checks.ts | 2 +- schematics/ng-update/data/output-names.ts | 2 +- schematics/ng-update/data/property-names.ts | 2 +- schematics/ng-update/index.ts | 5 +- schematics/ng-update/upgrade-data.ts | 4 +- .../upgrade-rules/v17/angularJson.ts | 23 --- .../v17/autoRegisterFormWidgets.ts | 52 ------- .../v17/files/jsonSchemaModule.ts | 20 --- .../ng-update/upgrade-rules/v17/index.spec.ts | 145 ------------------ .../ng-update/upgrade-rules/v17/index.ts | 42 ----- .../ng-update/upgrade-rules/v17/nljep.ts | 21 --- .../ng-update/upgrade-rules/v17/preloader.ts | 73 --------- .../upgrade-rules/v17/removeForRoot.ts | 52 ------- .../upgrade-rules/v17/replaceProvideConfig.ts | 113 -------------- 25 files changed, 24 insertions(+), 558 deletions(-) create mode 100644 schematics/ng-update/data/css-tokens.ts delete mode 100644 schematics/ng-update/upgrade-rules/v17/angularJson.ts delete mode 100644 schematics/ng-update/upgrade-rules/v17/autoRegisterFormWidgets.ts delete mode 100644 schematics/ng-update/upgrade-rules/v17/files/jsonSchemaModule.ts delete mode 100644 schematics/ng-update/upgrade-rules/v17/index.spec.ts delete mode 100644 schematics/ng-update/upgrade-rules/v17/index.ts delete mode 100644 schematics/ng-update/upgrade-rules/v17/nljep.ts delete mode 100644 schematics/ng-update/upgrade-rules/v17/preloader.ts delete mode 100644 schematics/ng-update/upgrade-rules/v17/removeForRoot.ts delete mode 100644 schematics/ng-update/upgrade-rules/v17/replaceProvideConfig.ts diff --git a/schematics/collection.json b/schematics/collection.json index ba305955c..58bf5bbf4 100644 --- a/schematics/collection.json +++ b/schematics/collection.json @@ -7,7 +7,7 @@ "aliases": ["alain-shell"] }, "ng-update": { - "factory": "./ng-update/index#updateToV17", + "factory": "./ng-update/index#updateToV18", "description": "Just TEST" }, "application": { diff --git a/schematics/migration.json b/schematics/migration.json index b8aecd331..bd9f10a0d 100644 --- a/schematics/migration.json +++ b/schematics/migration.json @@ -3,8 +3,8 @@ "schematics": { "migration-v17": { "version": "17", - "description": "Updates NG-ALAIN to v17 [https://github.com/ng-alain/ng-alain/issues/2440]", - "factory": "./ng-update/index#updateToV17" + "description": "Updates NG-ALAIN to v18 [https://github.com/ng-alain/ng-alain/issues/2502]", + "factory": "./ng-update/index#updateToV18" }, "ng-post-update": { "description": "Performs cleanup after ng-update.", diff --git a/schematics/ng-add/index.ts b/schematics/ng-add/index.ts index c0a1dd22b..cbf5aeccd 100644 --- a/schematics/ng-add/index.ts +++ b/schematics/ng-add/index.ts @@ -7,7 +7,7 @@ import { DEFAULT_WORKSPACE_PATH, readJSON, readPackage } from '../utils'; import { getNodeMajorVersion } from '../utils/node'; import { Schema as NgAddOptions } from './schema'; -const V = 17; +const V = 18; function genRules(options: NgAddOptions): Rule { return () => { diff --git a/schematics/ng-update/data/attribute-selectors.ts b/schematics/ng-update/data/attribute-selectors.ts index 99fb8069c..c63d804a5 100644 --- a/schematics/ng-update/data/attribute-selectors.ts +++ b/schematics/ng-update/data/attribute-selectors.ts @@ -1,5 +1,5 @@ import { AttributeSelectorUpgradeData, TargetVersion, VersionChanges } from '@angular/cdk/schematics'; export const attributeSelectors: VersionChanges = { - [TargetVersion.V17]: [] + [TargetVersion.V18]: [] }; diff --git a/schematics/ng-update/data/class-names.ts b/schematics/ng-update/data/class-names.ts index 15f218f96..216fc2538 100644 --- a/schematics/ng-update/data/class-names.ts +++ b/schematics/ng-update/data/class-names.ts @@ -1,5 +1,5 @@ import { ClassNameUpgradeData, TargetVersion, VersionChanges } from '@angular/cdk/schematics'; export const classNames: VersionChanges = { - [TargetVersion.V17]: [] + [TargetVersion.V18]: [] }; diff --git a/schematics/ng-update/data/constructor-checks.ts b/schematics/ng-update/data/constructor-checks.ts index fa4b4fc40..68b794f32 100644 --- a/schematics/ng-update/data/constructor-checks.ts +++ b/schematics/ng-update/data/constructor-checks.ts @@ -6,5 +6,5 @@ import { ConstructorChecksUpgradeData, TargetVersion, VersionChanges } from '@an * automatically through type checking. */ export const constructorChecks: VersionChanges = { - [TargetVersion.V17]: [] + [TargetVersion.V18]: [] }; diff --git a/schematics/ng-update/data/css-selectors.ts b/schematics/ng-update/data/css-selectors.ts index b5af06ba5..f36a6fca4 100644 --- a/schematics/ng-update/data/css-selectors.ts +++ b/schematics/ng-update/data/css-selectors.ts @@ -1,5 +1,5 @@ import { CssSelectorUpgradeData, TargetVersion, VersionChanges } from '@angular/cdk/schematics'; export const cssSelectors: VersionChanges = { - [TargetVersion.V17]: [] + [TargetVersion.V18]: [] }; diff --git a/schematics/ng-update/data/css-tokens.ts b/schematics/ng-update/data/css-tokens.ts new file mode 100644 index 000000000..01ab94127 --- /dev/null +++ b/schematics/ng-update/data/css-tokens.ts @@ -0,0 +1,5 @@ +import { CssTokenUpgradeData, TargetVersion, VersionChanges } from '@angular/cdk/schematics'; + +export const cssTokens: VersionChanges = { + [TargetVersion.V18]: [] +}; diff --git a/schematics/ng-update/data/element-selectors.ts b/schematics/ng-update/data/element-selectors.ts index cc1bf62bc..3a2502c9d 100644 --- a/schematics/ng-update/data/element-selectors.ts +++ b/schematics/ng-update/data/element-selectors.ts @@ -1,5 +1,5 @@ import { ElementSelectorUpgradeData, TargetVersion, VersionChanges } from '@angular/cdk/schematics'; export const elementSelectors: VersionChanges = { - [TargetVersion.V17]: [] + [TargetVersion.V18]: [] }; diff --git a/schematics/ng-update/data/index.ts b/schematics/ng-update/data/index.ts index e3d8903d7..c6f4f2d06 100644 --- a/schematics/ng-update/data/index.ts +++ b/schematics/ng-update/data/index.ts @@ -7,3 +7,4 @@ export * from './input-names'; export * from './method-call-checks'; export * from './output-names'; export * from './property-names'; +export * from './css-tokens'; diff --git a/schematics/ng-update/data/input-names.ts b/schematics/ng-update/data/input-names.ts index 9eb63b727..e165b33aa 100644 --- a/schematics/ng-update/data/input-names.ts +++ b/schematics/ng-update/data/input-names.ts @@ -1,5 +1,5 @@ import { InputNameUpgradeData, TargetVersion, VersionChanges } from '@angular/cdk/schematics'; export const inputNames: VersionChanges = { - [TargetVersion.V17]: [] + [TargetVersion.V18]: [] }; diff --git a/schematics/ng-update/data/method-call-checks.ts b/schematics/ng-update/data/method-call-checks.ts index ad4716d88..a028dcf8c 100644 --- a/schematics/ng-update/data/method-call-checks.ts +++ b/schematics/ng-update/data/method-call-checks.ts @@ -1,5 +1,5 @@ import { MethodCallUpgradeData, TargetVersion, VersionChanges } from '@angular/cdk/schematics'; export const methodCallChecks: VersionChanges = { - [TargetVersion.V17]: [] + [TargetVersion.V18]: [] }; diff --git a/schematics/ng-update/data/output-names.ts b/schematics/ng-update/data/output-names.ts index 0743366b2..67a3fbc7c 100644 --- a/schematics/ng-update/data/output-names.ts +++ b/schematics/ng-update/data/output-names.ts @@ -1,5 +1,5 @@ import { OutputNameUpgradeData, TargetVersion, VersionChanges } from '@angular/cdk/schematics'; export const outputNames: VersionChanges = { - [TargetVersion.V17]: [] + [TargetVersion.V18]: [] }; diff --git a/schematics/ng-update/data/property-names.ts b/schematics/ng-update/data/property-names.ts index b51d3cd45..dee969b34 100644 --- a/schematics/ng-update/data/property-names.ts +++ b/schematics/ng-update/data/property-names.ts @@ -1,5 +1,5 @@ import { PropertyNameUpgradeData, TargetVersion, VersionChanges } from '@angular/cdk/schematics'; export const propertyNames: VersionChanges = { - [TargetVersion.V17]: [] + [TargetVersion.V18]: [] }; diff --git a/schematics/ng-update/index.ts b/schematics/ng-update/index.ts index f7030e1df..85c0fa07b 100644 --- a/schematics/ng-update/index.ts +++ b/schematics/ng-update/index.ts @@ -3,12 +3,11 @@ import { createMigrationSchematicRule, NullableDevkitMigration, TargetVersion } import { chain, Rule, SchematicContext } from '@angular-devkit/schematics'; import { ruleUpgradeData } from './upgrade-data'; -import { v17Rule } from './upgrade-rules/v17'; const migrations: NullableDevkitMigration[] = []; -export function updateToV17(): Rule { - return chain([v17Rule(), createMigrationSchematicRule(TargetVersion.V17, migrations, ruleUpgradeData, postUpdate)]); +export function updateToV18(): Rule { + return chain([createMigrationSchematicRule(TargetVersion.V18, migrations, ruleUpgradeData, postUpdate)]); } /** Post-update schematic to be called when update is finished. */ diff --git a/schematics/ng-update/upgrade-data.ts b/schematics/ng-update/upgrade-data.ts index 073355d88..2f9524020 100644 --- a/schematics/ng-update/upgrade-data.ts +++ b/schematics/ng-update/upgrade-data.ts @@ -5,6 +5,7 @@ import { classNames, constructorChecks, cssSelectors, + cssTokens, elementSelectors, inputNames, methodCallChecks, @@ -22,5 +23,6 @@ export const ruleUpgradeData: UpgradeData = { methodCallChecks, outputNames, propertyNames, - symbolRemoval + symbolRemoval, + cssTokens }; diff --git a/schematics/ng-update/upgrade-rules/v17/angularJson.ts b/schematics/ng-update/upgrade-rules/v17/angularJson.ts deleted file mode 100644 index 055deeb6a..000000000 --- a/schematics/ng-update/upgrade-rules/v17/angularJson.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { Rule } from '@angular-devkit/schematics'; -import { updateWorkspace } from '@schematics/angular/utility/workspace'; - -import { BUILD_TARGET_BUILD } from '../../../utils'; - -export function fixAngularJson(): Rule { - return updateWorkspace(async workspace => { - workspace.projects.forEach(project => { - const build = project.targets?.get(BUILD_TARGET_BUILD); - if (build == null) return; - if (build.builder !== '@angular-devkit/build-angular:browser') return; - - build.builder = '@angular-devkit/build-angular:application'; - const options = build.options; - options['browser'] = options.main; - delete options.main; - const dev = build.configurations.development; - delete dev.buildOptimizer; - delete dev.vendorChunk; - delete dev.namedChunks; - }); - }); -} diff --git a/schematics/ng-update/upgrade-rules/v17/autoRegisterFormWidgets.ts b/schematics/ng-update/upgrade-rules/v17/autoRegisterFormWidgets.ts deleted file mode 100644 index cdd5b6bdf..000000000 --- a/schematics/ng-update/upgrade-rules/v17/autoRegisterFormWidgets.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; -import { insertImport, addSymbolToNgModuleMetadata } from '@schematics/angular/utility/ast-utils'; -import { Change, InsertChange } from '@schematics/angular/utility/change'; - -import { DEFAULT_WORKSPACE_PATH, getSourceFile, readJSON, applyChanges, logWarn, logEx } from '../../../utils'; - -export function autoRegisterFormWidgets(): Rule { - return (tree: Tree, context: SchematicContext) => { - const angularJson = readJSON(tree, DEFAULT_WORKSPACE_PATH); - const projectNames = Object.keys(angularJson.projects); - for (const name of projectNames) { - autoRegisterFormWidgetsRun(tree, name, angularJson.projects[name].sourceRoot, context); - } - }; -} - -function autoRegisterFormWidgetsRun(tree: Tree, name: string, sourceRoot: string, context: SchematicContext): void { - const modulePath = `${sourceRoot}/app/shared/json-schema/json-schema.module.ts`; - if (!tree.exists(modulePath)) return; - - const list = [ - { symbolName: 'AutoCompleteWidgetModule', fileName: '@delon/form/widgets/autocomplete' }, - { symbolName: 'CascaderWidgetModule', fileName: '@delon/form/widgets/cascader' }, - { symbolName: 'MentionWidgetModule', fileName: '@delon/form/widgets/mention' }, - { symbolName: 'RateWidgetModule', fileName: '@delon/form/widgets/rate' }, - { symbolName: 'SliderWidgetModule', fileName: '@delon/form/widgets/slider' }, - { symbolName: 'TagWidgetModule', fileName: '@delon/form/widgets/tag' }, - { symbolName: 'TimeWidgetModule', fileName: '@delon/form/widgets/time' }, - { symbolName: 'TransferWidgetModule', fileName: '@delon/form/widgets/transfer' }, - { symbolName: 'TreeSelectWidgetModule', fileName: '@delon/form/widgets/tree-select' }, - { symbolName: 'UploadWidgetModule', fileName: '@delon/form/widgets/upload' } - ]; - try { - const source = getSourceFile(tree, modulePath); - const changes: Change[] = []; - for (const item of list) { - changes.push(insertImport(source, modulePath, item.symbolName, item.fileName) as InsertChange); - changes.push(...addSymbolToNgModuleMetadata(source, modulePath, 'imports', item.symbolName)); - } - applyChanges(tree, modulePath, changes); - - logWarn( - context, - `[@delon/form] Register all widgets in ${name} project, you can reduce package size by removing unnecessary parts in ${modulePath}` - ); - } catch (ex) { - logEx( - context, - `Import all @delon/form/widgets/* errors, need to manually import the required modules. ERROR: ${ex.message}` - ); - } -} diff --git a/schematics/ng-update/upgrade-rules/v17/files/jsonSchemaModule.ts b/schematics/ng-update/upgrade-rules/v17/files/jsonSchemaModule.ts deleted file mode 100644 index c192e4f4b..000000000 --- a/schematics/ng-update/upgrade-rules/v17/files/jsonSchemaModule.ts +++ /dev/null @@ -1,20 +0,0 @@ -export default ` -import { NgModule } from '@angular/core'; - import { DelonFormModule, WidgetRegistry } from '@delon/form'; - - import { TestWidget } from './test/test.widget'; - import { SharedModule } from '../shared.module'; - - export const SCHEMA_THIRDS_COMPONENTS = [TestWidget]; - - @NgModule({ - declarations: SCHEMA_THIRDS_COMPONENTS, - imports: [SharedModule, DelonFormModule.forRoot()], - exports: SCHEMA_THIRDS_COMPONENTS - }) - export class JsonSchemaModule { - constructor(widgetRegistry: WidgetRegistry) { - widgetRegistry.register(TestWidget.KEY, TestWidget); - } - } -` diff --git a/schematics/ng-update/upgrade-rules/v17/index.spec.ts b/schematics/ng-update/upgrade-rules/v17/index.spec.ts deleted file mode 100644 index 179571ded..000000000 --- a/schematics/ng-update/upgrade-rules/v17/index.spec.ts +++ /dev/null @@ -1,145 +0,0 @@ -import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; - -import { tryAddFile } from '../../../utils'; -import { createAlainApp, migrationCollection } from '../../../utils/testing'; -import jsonSchema from './files/jsonSchemaModule'; - -describe('Schematic: ng-update: v17Rule', () => { - let runner: SchematicTestRunner; - let tree: UnitTestTree; - const logs: string[] = []; - - beforeEach(async () => { - ({ runner, tree } = await createAlainApp()); - }); - - async function runMigration(): Promise { - logs.length = 0; - runner = new SchematicTestRunner('schematics', migrationCollection); - runner.logger.subscribe(e => logs.push(e.message)); - await runner.runSchematic('migration-v17', {}, tree); - } - - it('should be working', async () => { - const jsonSchemaModulePath = '/projects/foo/src/app/shared/json-schema/json-schema.module.ts'; - tryAddFile(tree, jsonSchemaModulePath, jsonSchema); - await runMigration(); - const content = tree.readContent(jsonSchemaModulePath); - expect(content).toContain(`import { UploadWidgetModule } from '@delon/form/widgets/upload';`); - expect(content).toContain(`, UploadWidgetModule`); - }); - - it('#removeForRoot', async () => { - const globalConfigPath = '/projects/foo/src/app/global-config.module.ts'; - tryAddFile( - tree, - globalConfigPath, - ` - import { AlainThemeModule } from '@delon/theme'; - const alainModules: any[] = [AlainThemeModule.forRoot(), DelonACLModule.forRoot()]; - const alainProvides = [{ provide: ALAIN_CONFIG, useValue: alainConfig }]; - ` - ); - await runMigration(); - const content = tree.readContent(globalConfigPath); - expect(content).not.toContain(`AlainThemeModule.forRoot()`); - expect(content).toContain(`provideAlain(`); - expect(content).not.toContain(`DelonACLModule.forRoot()`); - }); - - it('#replaceProvideAlainConfig', async () => { - const globalConfigPath = '/projects/foo/src/app/global-config.module.ts'; - tryAddFile(tree, globalConfigPath, `const alainProvides = [{ provide: ALAIN_CONFIG, useValue: alainConfig }];`); - await runMigration(); - const content = tree.readContent(globalConfigPath); - expect(content).toContain(`provideAlain({ config: alainConfig })`); - }); - - it('should be use provideMockConfig instead of DelonMockModule', async () => { - const globalConfigPath = '/projects/foo/src/environments/environment.ts'; - tryAddFile( - tree, - globalConfigPath, - `import * as MOCKDATA from '@_mock'; - import { DelonMockModule } from '@delon/mock'; - import { Environment } from '@delon/theme'; - - export const environment = { - production: false, - useHash: true, - api: { - baseUrl: './', - refreshTokenEnabled: true, - refreshTokenType: 'auth-refresh' - }, - modules: [DelonMockModule.forRoot({ data: MOCKDATA })] - } as Environment;` - ); - await runMigration(); - const content = tree.readContent(globalConfigPath); - expect(content).toContain(`provideMockConfig`); - }); - - it('#preloader', async () => { - const appCompPath = '/projects/foo/src/app/app.component.ts'; - tryAddFile( - tree, - appCompPath, - `import { Component, ElementRef, OnInit, Renderer2 } from '@angular/core'; -import { NavigationEnd, NavigationError, RouteConfigLoadStart, Router } from '@angular/router'; -import { TitleService, VERSION as VERSION_ALAIN } from '@delon/theme'; -import { environment } from '@env/environment'; -import { NzModalService } from 'ng-zorro-antd/modal'; -import { VERSION as VERSION_ZORRO } from 'ng-zorro-antd/version'; - -@Component({ - selector: 'app-root', -}) -export class AppComponent implements OnInit { - constructor( - el: ElementRef, - renderer: Renderer2, - private router: Router, - private titleSrv: TitleService, - private modalSrv: NzModalService - ) { - renderer.setAttribute(el.nativeElement, 'ng-alain-version', VERSION_ALAIN.full); - renderer.setAttribute(el.nativeElement, 'ng-zorro-version', VERSION_ZORRO.full); - } - - ngOnInit(): void { - let configLoad = false; - this.router.events.subscribe(ev => { - if (ev instanceof RouteConfigLoadStart) { - configLoad = true; - } - if (ev instanceof NavigationEnd) { - this.titleSrv.setTitle(); - this.modalSrv.closeAll(); - } - }); - } -} -` - ); - await runMigration(); - const content = tree.readContent(appCompPath); - expect(content).toContain(`private donePreloader = stepPreloader();`); - expect(content).toContain(`this.donePreloader();`); - }); - - it('add ng-less-javascript-enabled-patch', async () => { - const packageJsonPath = '/package.json'; - tryAddFile( - tree, - packageJsonPath, - `{ - "scripts": {}, - "devDependencies": {} - }` - ); - await runMigration(); - const content = tree.readContent(packageJsonPath); - expect(content).toContain(`"ng-less-javascript-enabled-patch": "^17.0.0"`); - }); -}); diff --git a/schematics/ng-update/upgrade-rules/v17/index.ts b/schematics/ng-update/upgrade-rules/v17/index.ts deleted file mode 100644 index 349486e95..000000000 --- a/schematics/ng-update/upgrade-rules/v17/index.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { chain, Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; -import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks'; - -import { logFinished, logInfo, logWarn } from '../../../utils'; -import { UpgradeMainVersions } from '../../../utils/versions'; -import { fixAngularJson } from './angularJson'; -import { autoRegisterFormWidgets } from './autoRegisterFormWidgets'; -import { addNljep } from './nljep'; -import { updatePreloader } from './preloader'; -import { removeForRoot } from './removeForRoot'; -import { replaceProvideConfig } from './replaceProvideConfig'; - -function finished(): Rule { - return (_tree: Tree, context: SchematicContext) => { - context.addTask(new NodePackageInstallTask()); - - logWarn(context, `Please check the modified files. Some upgrades may have incorrect formats or missing imports.`); - logWarn(context, `Please check the modified files. Some upgrades may have incorrect formats or missing imports.`); - logWarn(context, `Please check the modified files. Some upgrades may have incorrect formats or missing imports.`); - - logFinished( - context, - `Congratulations, Abort more detail please refer to upgrade guide https://github.com/ng-alain/ng-alain/issues/2390` - ); - }; -} - -export function v17Rule(): Rule { - return async (tree: Tree, context: SchematicContext) => { - UpgradeMainVersions(tree); - logInfo(context, `Upgrade dependency version number`); - return chain([ - fixAngularJson(), - removeForRoot(), - autoRegisterFormWidgets(), - replaceProvideConfig(), - updatePreloader(), - addNljep(), - finished() - ]); - }; -} diff --git a/schematics/ng-update/upgrade-rules/v17/nljep.ts b/schematics/ng-update/upgrade-rules/v17/nljep.ts deleted file mode 100644 index f808c81f4..000000000 --- a/schematics/ng-update/upgrade-rules/v17/nljep.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; - -import { logInfo, readPackage, writePackage } from '../../../utils'; - -const PACKAGE_NAME = 'ng-less-javascript-enabled-patch'; -export function addNljep(): Rule { - return (tree: Tree, context: SchematicContext) => { - const json = readPackage(tree); - if (json == null) return; - if (json.devDependencies[PACKAGE_NAME]) return; - - if (typeof json.scripts.postinstall !== 'string') json.scripts.postinstall = ''; - if (json.scripts.postinstall.length > 0) json.scripts.postinstall += ' && '; - json.scripts.postinstall += PACKAGE_NAME; - - json.devDependencies[PACKAGE_NAME] = '^17.0.0'; - writePackage(tree, json); - - logInfo(context, `Add [https://github.com/cipchk/${PACKAGE_NAME}] for devDependencies`); - }; -} diff --git a/schematics/ng-update/upgrade-rules/v17/preloader.ts b/schematics/ng-update/upgrade-rules/v17/preloader.ts deleted file mode 100644 index ac7c164b6..000000000 --- a/schematics/ng-update/upgrade-rules/v17/preloader.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; - -import { DEFAULT_WORKSPACE_PATH, logInfo, readJSON } from '../../../utils'; - -export function updatePreloader(): Rule { - return (tree: Tree, context: SchematicContext) => { - addESLintIgnore(tree); - - const angularJson = readJSON(tree, DEFAULT_WORKSPACE_PATH); - const projectNames = Object.keys(angularJson.projects); - for (const name of projectNames) { - const sourceRoot = angularJson.projects[name].sourceRoot; - fixIndexHtml(tree, name, sourceRoot, context); - run(tree, name, sourceRoot, context); - } - }; -} - -function addESLintIgnore(tree: Tree): void { - const filePath = '/.eslintignore'; - if (!tree.exists(filePath)) return; - const content = tree.readText(filePath); - if (!content.includes('**/src/index.html')) { - tree.overwrite(filePath, `${content}\n**/src/index.html`); - } -} - -function fixIndexHtml(tree: Tree, _: string, sourceRoot: string, __: SchematicContext): void { - const indexPath = `${sourceRoot}/index.html`; - if (!tree.exists(indexPath)) return; - - let indexContent = tree.readText(indexPath); - - const selfClose = ''; - if (!indexContent.includes(selfClose)) return; - - tree.overwrite(indexPath, indexContent.replace(selfClose, '')); -} - -function run(tree: Tree, name: string, sourceRoot: string, context: SchematicContext): void { - // main.ts - const mainPath = `${sourceRoot}/main.ts`; - if (!tree.exists(mainPath)) return; - - let mainContent = tree.readText(mainPath); - [`import { preloaderFinished } from '@delon/theme';`, 'preloaderFinished();'].forEach(item => { - if (mainContent.includes(item)) mainContent = mainContent.replace(item, ''); - }); - tree.overwrite(mainPath, mainContent); - - // app - const appPath = `${sourceRoot}/app/app.component.ts`; - if (!tree.exists(appPath)) return; - const appContent = tree.readText(appPath); - if (appContent.includes(', stepPreloader')) return; - - const appContentLines = appContent.split('\n'); - const importIndex = appContentLines.findIndex(line => line.includes(', VERSION as VERSION_ALAIN')); - const addIndex = appContentLines.findIndex(line => line.includes('export class AppComponent')); - const callDoneIndex = appContentLines.findIndex(line => line.includes('if (ev instanceof NavigationEnd) {')); - if (importIndex === -1 || addIndex === -1 || callDoneIndex === -1) return; - - appContentLines[importIndex] = appContentLines[importIndex].replace( - ', VERSION as VERSION_ALAIN', - ', VERSION as VERSION_ALAIN, stepPreloader' - ); - appContentLines.splice(addIndex + 1, 0, 'private donePreloader = stepPreloader();'); - appContentLines.splice(callDoneIndex + 2, 0, 'this.donePreloader();'); - - tree.overwrite(appPath, appContentLines.join('\n')); - - logInfo(context, `Upgrade preloader in ${name} project`); -} diff --git a/schematics/ng-update/upgrade-rules/v17/removeForRoot.ts b/schematics/ng-update/upgrade-rules/v17/removeForRoot.ts deleted file mode 100644 index 0565479b1..000000000 --- a/schematics/ng-update/upgrade-rules/v17/removeForRoot.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; - -import { DEFAULT_WORKSPACE_PATH, logInfo, readJSON } from '../../../utils'; - -export function removeForRoot(): Rule { - return (tree: Tree, context: SchematicContext) => { - const angularJson = readJSON(tree, DEFAULT_WORKSPACE_PATH); - const projectNames = Object.keys(angularJson.projects); - for (const name of projectNames) { - const sourceRoot = angularJson.projects[name].sourceRoot; - removeAlainThemeForRoot(tree, name, sourceRoot, context); - removeAlainThemeForChild(tree, name, sourceRoot, context); - removeDelonACLModuleForRoot(tree, name, sourceRoot, context); - } - }; -} - -function removeAlainThemeForRoot(tree: Tree, name: string, sourceRoot: string, context: SchematicContext): void { - const modulePath = `${sourceRoot}/app/global-config.module.ts`; - if (!tree.exists(modulePath)) return; - - const content = tree.readText(modulePath).replace(/AlainThemeModule\.forRoot\(\),?/g, ''); - tree.overwrite(modulePath, content); - - logInfo(context, `Remove AlainThemeModule.forRoot in ${name} project`); -} - -function removeAlainThemeForChild(tree: Tree, name: string, sourceRoot: string, context: SchematicContext): void { - const forChild = 'AlainThemeModule.forChild()'; - - tree.visit((path, entry) => { - if (!entry || !path.endsWith('.ts') || !path.startsWith(sourceRoot)) return; - - const content = tree.readText(path); - if (!content.includes(forChild)) return; - - tree.overwrite(path, content.replace(forChild, 'AlainThemeModule')); - }); - - logInfo(context, `Remove ${forChild} in ${name} project`); -} - -function removeDelonACLModuleForRoot(tree: Tree, name: string, sourceRoot: string, context: SchematicContext): void { - const modulePath = `${sourceRoot}/app/global-config.module.ts`; - if (!tree.exists(modulePath)) return; - - const forRoot = 'DelonACLModule.forRoot()'; - const content = tree.readText(modulePath); - tree.overwrite(modulePath, content.replace(/DelonACLModule\.forRoot\(\),?/g, '')); - - logInfo(context, `Remove ${forRoot} in ${name} project`); -} diff --git a/schematics/ng-update/upgrade-rules/v17/replaceProvideConfig.ts b/schematics/ng-update/upgrade-rules/v17/replaceProvideConfig.ts deleted file mode 100644 index 94eb46205..000000000 --- a/schematics/ng-update/upgrade-rules/v17/replaceProvideConfig.ts +++ /dev/null @@ -1,113 +0,0 @@ -import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; - -import { DEFAULT_WORKSPACE_PATH, logInfo, readJSON } from '../../../utils'; - -export function replaceProvideConfig(): Rule { - return (tree: Tree, context: SchematicContext) => { - const angularJson = readJSON(tree, DEFAULT_WORKSPACE_PATH); - const projectNames = Object.keys(angularJson.projects); - logInfo(context, `Use provider style (TIPS: References need to be fixed manually)`); - for (const name of projectNames) { - runAlain(tree, name, angularJson.projects[name].sourceRoot, context); - runZorro(tree, name, angularJson.projects[name].sourceRoot, context); - delonMock(tree, name, angularJson.projects[name].sourceRoot, context); - } - }; -} - -function runAlain(tree: Tree, name: string, sourceRoot: string, context: SchematicContext): void { - const filePath = `${sourceRoot}/app/global-config.module.ts`; - if (!tree.exists(filePath)) return; - - const text = '{ provide: ALAIN_CONFIG, useValue: alainConfig }'; - const content = tree - .readText(filePath) - .replace(text, 'provideAlain({ config: alainConfig })') - .replace('AlainThemeModule', 'provideAlain'); - tree.overwrite(filePath, content); - - logInfo(context, ` Use provideAlain instead of ALAIN_CONFIG in ${name} project`); -} - -function runZorro(tree: Tree, name: string, sourceRoot: string, context: SchematicContext): void { - const filePath = `${sourceRoot}/app/global-config.module.ts`; - if (!tree.exists(filePath)) return; - - const text = '{ provide: provideNzConfig, useValue: ngZorroConfig }'; - const content = tree.readText(filePath).replace(text, 'provideNzConfig(ngZorroConfig)'); - tree.overwrite(filePath, content); - - logInfo(context, ` Use provideNzConfig instead of NzConfig in ${name} project`); -} - -function delonMock(tree: Tree, name: string, sourceRoot: string, context: SchematicContext): void { - const filePath = `${sourceRoot}/environments/environment.ts`; - if (!tree.exists(filePath)) return; - - const text = 'DelonMockModule.forRoot({ data: MOCKDATA })'; - let content = tree.readText(filePath); - if (content.includes(text)) content = content.replace(text, ''); - - content = content - .replace( - 'modules: [', - 'providers: [provideMockConfig({ data: MOCKDATA })], \ninterceptorFns: [mockInterceptor],\nmodules: [' - ) - .replace('DelonMockModule', 'provideMockConfig, mockInterceptor'); - - tree.overwrite(filePath, content); - - // remove HttpClientModule - let useAuthType = ''; - const appModuleFile = `${sourceRoot}/app/app.module.ts`; - if (tree.exists(appModuleFile)) { - let appModuleContent = tree.readText(appModuleFile); - if (appModuleContent.includes('JWTInterceptor')) { - useAuthType = 'jwt'; - appModuleContent = appModuleContent - .replace( - `{ provide: HTTP_INTERCEPTORS, useClass: JWTInterceptor, multi: true }`, - `// { provide: HTTP_INTERCEPTORS, useClass: JWTInterceptor, multi: true }` - ) - .replace(`import { JWTInterceptor } from '@delon/auth';`, `// import { JWTInterceptor } from '@delon/auth';`); - } else if (appModuleContent.includes('SimpleInterceptor')) { - useAuthType = 'simple'; - appModuleContent = appModuleContent - .replace( - `{ provide: HTTP_INTERCEPTORS, useClass: SimpleInterceptor, multi: true }`, - `// { provide: HTTP_INTERCEPTORS, useClass: SimpleInterceptor, multi: true }` - ) - .replace( - `import { SimpleInterceptor } from '@delon/auth';`, - `// import { SimpleInterceptor } from '@delon/auth';` - ); - } - appModuleContent = appModuleContent - .replace(`import { HttpClientModule } from '@angular/common/http';`, '') - .replace(/HttpClientModule,?/g, ''); - - tree.overwrite(appModuleFile, appModuleContent); - } - - const globalFile = `${sourceRoot}/app/global-config.module.ts`; - if (tree.exists(globalFile)) { - // SimpleInterceptor -> authSimpleInterceptor - // JWTInterceptor -> authJWTInterceptor - let globalContent = tree.readText(globalFile); - let authInterceptor = ''; - if (useAuthType === 'jwt') { - authInterceptor = ', authJWTInterceptor'; - } else if (useAuthType === 'simple') { - authInterceptor = ', authSimpleInterceptor'; - } - tree.overwrite( - globalFile, - globalContent.replace( - ', ...zorroProvides', - `, ...zorroProvides, ...(environment.providers || []), \nprovideHttpClient(withInterceptors([...(environment.interceptorFns || [])${authInterceptor}]), withInterceptorsFromDi())` - ) - ); - } - - logInfo(context, ` Use provideMockConfig instead of DelonMockModule in ${name} project`); -}