From 50dba0a4b7e2a4d927a211695c279ce7d1517cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A1=E8=89=B2?= Date: Sun, 19 Jan 2025 19:02:27 +0800 Subject: [PATCH] feat(cli): add declaration-property-value-no-unknown via ng update (#1879) --- TODO.md | 1 - .../ng-update/upgrade-rules/V19/index.spec.ts | 34 +++++++++++++++++++ .../ng-update/upgrade-rules/V19/index.ts | 25 +++++++++++++- schematics/test.ts | 4 +-- 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 schematics/ng-update/upgrade-rules/V19/index.spec.ts diff --git a/TODO.md b/TODO.md index ccef7c16d..277cc02ff 100644 --- a/TODO.md +++ b/TODO.md @@ -1,2 +1 @@ -- [ ] add `declaration-property-value-no-unknown` in `.stylelintrc.js` - diff --git a/schematics/ng-update/upgrade-rules/V19/index.spec.ts b/schematics/ng-update/upgrade-rules/V19/index.spec.ts new file mode 100644 index 000000000..353abab06 --- /dev/null +++ b/schematics/ng-update/upgrade-rules/V19/index.spec.ts @@ -0,0 +1,34 @@ +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; + +import { tryAddFile } from '../../../utils'; +import { createAlainApp, migrationCollection } from '../../../utils/testing'; + +describe('Schematic: ng-update: v19Rule', () => { + 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-v19', {}, tree); + } + + it('add declaration-property-value-no-unknown', async () => { + const filePath = '/.stylelintrc.js'; + tryAddFile( + tree, + filePath, + ` 'media-query-no-invalid': null, + 'order/order': [` + ); + await runMigration(); + const content = tree.readContent(filePath); + expect(content).toContain(`declaration-property-value-no-unknown`); + }); +}); diff --git a/schematics/ng-update/upgrade-rules/V19/index.ts b/schematics/ng-update/upgrade-rules/V19/index.ts index 7f19e4bdf..27ece3478 100644 --- a/schematics/ng-update/upgrade-rules/V19/index.ts +++ b/schematics/ng-update/upgrade-rules/V19/index.ts @@ -4,6 +4,29 @@ import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks'; import { logFinished, logInfo } from '../../../utils'; import { UpgradeMainVersions } from '../../../utils/versions'; +function addDPVNU() { + return (tree: Tree) => { + const filePath = '.stylelintrc.js'; + const content = tree.read(filePath); + if (!content) { + return; + } + let contentStr = content.toString('utf-8'); + if (contentStr.includes('declaration-property-value-no-unknown')) { + return; + } + // 在 'order/order' 之前插入 'declaration-property-value-no-unknown': null, + const findContent = ` 'order/order'`; + const idx = contentStr.indexOf(findContent.trim()); + if (idx === -1) { + return; + } + const insertContent = ` 'declaration-property-value-no-unknown': null,\n`; + contentStr = contentStr.replace(findContent, `${insertContent}${findContent}`); + tree.overwrite(filePath, contentStr); + }; +} + function finished(): Rule { return (_tree: Tree, context: SchematicContext) => { context.addTask(new NodePackageInstallTask()); @@ -19,6 +42,6 @@ export function v19Rule(): Rule { return async (tree: Tree, context: SchematicContext) => { UpgradeMainVersions(tree); logInfo(context, `Upgrade dependency version number`); - return chain([finished()]); + return chain([addDPVNU(), finished()]); }; } diff --git a/schematics/test.ts b/schematics/test.ts index c28599971..d4d49998d 100644 --- a/schematics/test.ts +++ b/schematics/test.ts @@ -12,8 +12,8 @@ require('source-map-support').install({ const Jasmine = require('jasmine'); const runner = new Jasmine({ projectBaseDir }); -// const files = `schematics/**/*.spec.ts`; -const files = `schematics/plugin/plugin.icon.spec.ts`; +const files = `schematics/**/*.spec.ts`; +// const files = `schematics/ng-update/upgrade-rules/V19/index.spec.ts`; const tests = glob.sync(files).map(p => relative(projectBaseDir, p));