Skip to content

Commit

Permalink
feat(cli): add declaration-property-value-no-unknown via ng update (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk authored Jan 19, 2025
1 parent 7b3d56a commit 50dba0a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
- [ ] add `declaration-property-value-no-unknown` in `.stylelintrc.js`
-
34 changes: 34 additions & 0 deletions schematics/ng-update/upgrade-rules/V19/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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`);
});
});
25 changes: 24 additions & 1 deletion schematics/ng-update/upgrade-rules/V19/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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()]);
};
}
4 changes: 2 additions & 2 deletions schematics/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down

0 comments on commit 50dba0a

Please sign in to comment.