Skip to content

Commit

Permalink
fix(cli): fix can not add index to parent of type array (#1577)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk authored Jan 29, 2023
1 parent 89c7b67 commit 3ac463f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 53 deletions.
76 changes: 38 additions & 38 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,31 @@
"ssr:prerender": "ng run site:prerender"
},
"dependencies": {
"@angular/animations": "^15.0.0",
"@angular/common": "^15.0.0",
"@angular/compiler": "^15.0.0",
"@angular/core": "^15.0.0",
"@angular/forms": "^15.0.0",
"@angular/platform-browser": "^15.0.0",
"@angular/platform-browser-dynamic": "^15.0.0",
"@angular/router": "^15.0.0",
"rxjs": "~7.5.0",
"@angular/animations": "^15.1.0",
"@angular/common": "^15.1.0",
"@angular/compiler": "^15.1.0",
"@angular/core": "^15.1.0",
"@angular/forms": "^15.1.0",
"@angular/platform-browser": "^15.1.0",
"@angular/platform-browser-dynamic": "^15.1.0",
"@angular/router": "^15.1.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.12.0",
"@angular/service-worker": "^15.0.4",
"@angular/platform-server": "^15.0.4",
"@angular/elements": "^15.0.4",
"@angular/service-worker": "^15.1.0",
"@angular/platform-server": "^15.1.0",
"@angular/elements": "^15.1.0",
"@antv/data-set": "^0.11.8",
"@antv/g2": "^4.2.8",
"echarts": "^5.4.1",
"@stackblitz/sdk": "^1.8.1",
"@stackblitz/sdk": "^1.8.2",
"codesandbox": "^2.2.3",
"ajv": "^8.11.2",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
"extend": "^3.0.2",
"file-saver": "^2.0.5",
"ng-github-button": "^15.0.0",
"ng-zorro-antd": "^15.0.1",
"ng-zorro-antd": "^15.0.3",
"ngx-color": "~8.0.3",
"ngx-countdown": "^15.0.0",
"ngx-highlight-js": "^15.0.0",
Expand All @@ -90,28 +90,28 @@
"@webcomponents/custom-elements": "^1.5.1",
"aos": "^3.0.0-beta.6",
"@ng-util/monaco-editor": "^15.0.0",
"@nguniversal/express-engine": "~15.0.0",
"@nguniversal/express-engine": "~15.1.0",
"express": "^4.18.2",
"isutf8": "^4.0.0",
"@github/hotkey": "^2.0.1"
},
"devDependencies": {
"@angular-devkit/build-angular": "^15.0.4",
"@angular/cli": "~15.0.4",
"@angular/compiler-cli": "^15.0.0",
"@angular-devkit/build-angular": "^15.1.3",
"@angular/cli": "~15.1.3",
"@angular/compiler-cli": "^15.1.0",
"@types/jasmine": "~4.3.0",
"jasmine-core": "~4.5.0",
"karma": "~6.4.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.0.0",
"typescript": "~4.8.2",
"typescript": "~4.9.4",
"codelyzer": "^6.0.2",
"jasmine-spec-reporter": "^7.0.0",
"protractor": "~7.0.0",
"ts-node": "~10.9.1",
"@angular/language-service": "^15.0.4",
"@angular/language-service": "^15.1.0",
"@types/jasminewd2": "~2.0.10",
"karma-junit-reporter": "^2.0.1",
"karma-spec-reporter": "0.0.36",
Expand All @@ -138,17 +138,17 @@
"mark-twain": "^2.0.3",
"mockjs": "^1.1.0",
"mustache": "^4.2.0",
"ng-packagr": "^15.0.3",
"ng-packagr": "^15.1.0",
"parse5": "^7.1.2",
"prettier": "^2.8.1",
"prettier": "^2.8.3",
"readline-sync": "^1.4.10",
"stream": "0.0.2",
"stylelint": "^14.16.1",
"stylelint-config-prettier": "^9.0.4",
"stylelint-config-rational-order": "^0.1.2",
"stylelint-config-standard": "^29.0.0",
"stylelint-declaration-block-no-ignored-properties": "^2.6.0",
"stylelint-order": "^5.0.0",
"stylelint-order": "^6.0.1",
"source-map-explorer": "^2.5.3",
"xlsx": "^0.18.5",
"jszip": "^3.10.1",
Expand All @@ -160,23 +160,23 @@
"ng-alain-sts": "^0.0.2",
"ng-alain-plugin-theme": "^15.0.1",
"tsconfig-paths": "^4.1.2",
"@nguniversal/builders": "^15.0.0",
"@types/express": "^4.17.15",
"@nguniversal/builders": "^15.1.0",
"@types/express": "^4.17.16",
"html-minifier-terser": "^7.1.0",
"terser": "^5.16.1",
"@commitlint/cli": "^17.3.0",
"@commitlint/config-angular": "^17.3.0",
"@angular-eslint/builder": "~15.1.0",
"@angular-eslint/eslint-plugin": "~15.1.0",
"@angular-eslint/eslint-plugin-template": "~15.1.0",
"@angular-eslint/schematics": "~15.1.0",
"@angular-eslint/template-parser": "~15.1.0",
"@typescript-eslint/eslint-plugin": "~5.47.1",
"@typescript-eslint/parser": "~5.47.1",
"eslint": "^8.31.0",
"eslint-config-prettier": "~8.5.0",
"@commitlint/cli": "^17.4.2",
"@commitlint/config-angular": "^17.4.2",
"@angular-eslint/builder": "~15.2.0",
"@angular-eslint/eslint-plugin": "~15.2.0",
"@angular-eslint/eslint-plugin-template": "~15.2.0",
"@angular-eslint/schematics": "~15.2.0",
"@angular-eslint/template-parser": "~15.2.0",
"@typescript-eslint/eslint-plugin": "~5.49.0",
"@typescript-eslint/parser": "~5.49.0",
"eslint": "^8.33.0",
"eslint-config-prettier": "~8.6.0",
"eslint-plugin-import": "~2.26.0",
"eslint-plugin-jsdoc": "~39.6.4",
"eslint-plugin-jsdoc": "~39.7.4",
"eslint-plugin-prefer-arrow": "~1.2.3",
"eslint-plugin-prettier": "~4.2.1",
"eslint-plugin-deprecation": "~1.3.3",
Expand Down
48 changes: 37 additions & 11 deletions schematics/application/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JsonObject, strings } from '@angular-devkit/core';
import { strings } from '@angular-devkit/core';
import { ProjectDefinition } from '@angular-devkit/core/src/workspace';
import {
apply,
Expand All @@ -14,7 +14,7 @@ import {
Tree,
url
} from '@angular-devkit/schematics';
import { updateWorkspace } from '@schematics/angular/utility/workspace';
import { getWorkspace, updateWorkspace } from '@schematics/angular/utility/workspace';

import { getLangData } from '../core/lang.config';
import {
Expand All @@ -28,9 +28,10 @@ import {
addStylePreprocessorOptionsToAllProject,
BUILD_TARGET_BUILD,
BUILD_TARGET_SERVE,
DEFAULT_WORKSPACE_PATH,
getProject,
getProjectFromWorkspace,
getProjectTarget,
getProjectName,
readContent,
readJSON,
readPackage,
Expand Down Expand Up @@ -75,9 +76,35 @@ function fixAngularJson(options: ApplicationOptions): Rule {
const serveTarget = p.targets?.get(BUILD_TARGET_SERVE);
if (serveTarget.options == null) serveTarget.options = {};
serveTarget.options.proxyConfig = 'proxy.conf.js';
// 调整budgets
const budgets = (getProjectTarget(p, BUILD_TARGET_BUILD, 'configurations').production as JsonObject)
.budgets as Array<{

// // 调整budgets, error in angular 15.1
// const budgets = (getProjectTarget(p, BUILD_TARGET_BUILD, 'configurations').production as JsonObject)
// .budgets as Array<{
// type: string;
// maximumWarning: string;
// maximumError: string;
// }>;
// if (budgets && budgets.length > 0) {
// const initial = budgets.find(w => w.type === 'initial');
// if (initial) {
// initial.maximumWarning = '2mb';
// initial.maximumError = '3mb';
// }
// }

addStylePreprocessorOptionsToAllProject(workspace);
addSchematicCollections(workspace);
});
}

/**
* Fix https://github.com/ng-alain/ng-alain/issues/2359
*/
function fixBrowserBuilderBudgets(options: ApplicationOptions): Rule {
return async (tree: Tree) => {
const projectName = getProjectName(await getWorkspace(tree), options.project);
const json = readJSON(tree, DEFAULT_WORKSPACE_PATH);
const budgets = json.projects[projectName].architect.build.configurations.production.budgets as Array<{
type: string;
maximumWarning: string;
maximumError: string;
Expand All @@ -87,12 +114,10 @@ function fixAngularJson(options: ApplicationOptions): Rule {
if (initial) {
initial.maximumWarning = '2mb';
initial.maximumError = '3mb';
writeJSON(tree, DEFAULT_WORKSPACE_PATH, json);
}
}

addStylePreprocessorOptionsToAllProject(workspace);
addSchematicCollections(workspace);
});
};
}

function addDependenciesToPackageJson(options: ApplicationOptions): Rule {
Expand Down Expand Up @@ -372,7 +397,8 @@ export default function (options: ApplicationOptions): Rule {
addStyle(),
fixLang(options),
fixVsCode(),
fixAngularJson(options)
fixAngularJson(options),
fixBrowserBuilderBudgets(options)
]);
};
}
9 changes: 9 additions & 0 deletions schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/te

import * as nodeUtils from '../utils/node';
import { createAlainApp } from '../utils/testing';
import { DEFAULT_WORKSPACE_PATH } from '../utils/workspace';

describe('Schematic: ng-add', () => {
let runner: SchematicTestRunner;
Expand All @@ -13,6 +14,14 @@ describe('Schematic: ng-add', () => {
expect(packageJson.dependencies['@delon/theme']).toBeDefined();
});

it('#issues-https://github.com/ng-alain/ng-alain/issues/2359', async () => {
({ runner, tree } = await createAlainApp());
const json = JSON.parse(tree.readContent(DEFAULT_WORKSPACE_PATH));
const budgets = json.projects['foo'].architect.build.configurations.production.budgets;
expect(budgets[0].maximumWarning).toBe('2mb');
expect(budgets[0].maximumError).toBe('3mb');
});

it('should throw errr when node version is not valid range', async () => {
spyOn(nodeUtils, 'getNodeMajorVersion').and.returnValue(10);
try {
Expand Down
4 changes: 2 additions & 2 deletions schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import * as colors from 'ansi-colors';

import { Schema as ApplicationOptions } from '../application/schema';
import { readJSON, readPackage } from '../utils';
import { DEFAULT_WORKSPACE_PATH, readJSON, readPackage } from '../utils';
import { getNodeMajorVersion } from '../utils/node';
import { Schema as NgAddOptions } from './schema';

Expand Down Expand Up @@ -55,7 +55,7 @@ function genRules(options: NgAddOptions): Rule {
}

function isYarn(tree: Tree): boolean {
return readJSON(tree, '/angular.json')?.cli?.packageManager === 'yarn';
return readJSON(tree, DEFAULT_WORKSPACE_PATH)?.cli?.packageManager === 'yarn';
}

function finished(): Rule {
Expand Down
2 changes: 1 addition & 1 deletion schematics/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Jasmine = require('jasmine');
const runner = new Jasmine({ projectBaseDir });

// const files = `schematics/**/*.spec.ts`;
const files = `schematics/sta/index.spec.ts`;
const files = `schematics/ng-add/index.spec.ts`;

const tests = glob.sync(files).map(p => relative(projectBaseDir, p));

Expand Down
3 changes: 2 additions & 1 deletion schematics/utils/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const BUILD_TARGET_TEST = 'test';
export const BUILD_TARGET_SERVE = 'serve';
export const BUILD_TARGET_LINT = 'lint';
export const NG_ALAIN_JSON = `ng-alain.json`;
export const DEFAULT_WORKSPACE_PATH = `/angular.json`;

export interface NgAlainDefinition {
projects?: { [key: string]: NgAlainProjectDefinition };
Expand All @@ -19,7 +20,7 @@ export interface NgAlainProjectDefinition {
routesRoot?: string;
}

function getProjectName(workspace: WorkspaceDefinition, name?: string): string | null {
export function getProjectName(workspace: WorkspaceDefinition, name?: string): string | null {
if (name && workspace.projects.has(name)) {
return name;
}
Expand Down

0 comments on commit 3ac463f

Please sign in to comment.