Skip to content

Commit

Permalink
fix(schematics): update to new CLI api (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil147 authored and GitHub Enterprise committed Jan 19, 2021
1 parent 690aa98 commit d5286de
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 107 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"typescript.tsdk": "node_modules/typescript/lib"
}
75 changes: 10 additions & 65 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@commitlint/cli": "^11.0.0",
"@commitlint/config-angular": "^11.0.0",
"@fortawesome/fontawesome-free": "^5.15.1",
"@schematics/angular": "^10.2.1",
"@schematics/angular": "^11.0.6",
"@types/fs-extra": "^9.0.6",
"chalk": "^4.1.0",
"classlist.js": "^1.1.20150312",
Expand Down
10 changes: 5 additions & 5 deletions projects/ng-aquila/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
},
"license": "MIT",
"peerDependencies": {
"@angular/core": "^10.0.0",
"@angular/common": "^10.0.0",
"@angular/cdk": "^10.0.0",
"@angular/animations": "^10.0.0",
"@angular/router": "^10.0.0",
"@angular/core": "^11.0.0",
"@angular/common": "^11.0.0",
"@angular/cdk": "^11.0.0",
"@angular/animations": "^11.0.0",
"@angular/router": "^11.0.0",
"moment": "^2.22.2",
"object-fit-images": "^3.2.3",
"iban": "^0.0.12",
Expand Down
28 changes: 15 additions & 13 deletions projects/ng-aquila/src/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import { SchematicTestSetup, Collection } from '../utils/testing/test-setup';
import { getWorkspace } from '@schematics/angular/utility/config';
import { getWorkspace } from '@schematics/angular/utility/workspace';
import { getProjectFromWorkspace } from '@angular/cdk/schematics';
import { ProjectDefinition } from '@angular-devkit/core/src/workspace';
import { isJsonArray } from '@angular-devkit/core';

describe('ng-aquila ng add', () => {
const testSetup = new SchematicTestSetup('ng-add-setup-project', Collection.SCHEMATICS);
let testProjectConfig: any;
let testProjectConfig: ProjectDefinition;

function getTestProjectConfig() {
const workspace = getWorkspace(testSetup.appTree);
async function getTestProjectConfig() {
const workspace = await getWorkspace(testSetup.appTree);
return getProjectFromWorkspace(workspace, 'aquila-testing');
}

describe('general and b2c', () => {
beforeEach(async () => {
await testSetup.runMigration();
testProjectConfig = getTestProjectConfig();
testProjectConfig = await getTestProjectConfig();
});

it('should add normalize.css', async () => {
expect(testProjectConfig.architect!.build.options.styles).toContain('node_modules/@aposin/ng-aquila/css/normalize.css');
expect(testProjectConfig.targets?.get('build')?.options?.styles).toContain('node_modules/@aposin/ng-aquila/css/normalize.css');
});

it('should add aposin theme', async () => {
expect(testProjectConfig.architect!.build.options.styles).toContain('node_modules/@aposin/ng-aquila/themes/aposin.css');
expect(testProjectConfig.architect!.build.options.styles).not.toContain('node_modules/@aposin/ng-aquila/themes/expert.css');
expect(testProjectConfig.targets?.get('build')?.options?.styles).toContain('node_modules/@aposin/ng-aquila/themes/aposin.css');
expect(testProjectConfig.targets?.get('build')?.options?.styles).not.toContain('node_modules/@aposin/ng-aquila/themes/expert.css');
});

it('should add CDK styles', async () => {
Expand All @@ -41,12 +43,12 @@ describe('ng-aquila ng add', () => {
describe('expert', () => {
beforeEach(async () => {
await testSetup.runMigration({ type: 'b2b' });
testProjectConfig = getTestProjectConfig();
testProjectConfig = await getTestProjectConfig();
});

it('should add expert theme', async () => {
expect(testProjectConfig.architect!.build.options.styles).not.toContain('node_modules/@aposin/ng-aquila/themes/aposin.css');
expect(testProjectConfig.architect!.build.options.styles).toContain('node_modules/@aposin/ng-aquila/themes/expert.css');
expect(testProjectConfig.targets.get('build')?.options?.styles).not.toContain('node_modules/@aposin/ng-aquila/themes/aposin.css');
expect(testProjectConfig.targets.get('build')?.options?.styles).toContain('node_modules/@aposin/ng-aquila/themes/expert.css');
});

it('should add Expert Module', async () => {
Expand All @@ -57,11 +59,11 @@ describe('ng-aquila ng add', () => {
describe('no theme', () => {
beforeEach(async () => {
await testSetup.runMigration({ type: 'b2c', noTheme: true });
testProjectConfig = getTestProjectConfig();
testProjectConfig = await getTestProjectConfig();
});

it('should not add a theme file if no-theme is set to true', () => {
expect(testProjectConfig.architect!.build.options.styles).not.toContain('node_modules/@aposin/ng-aquila/themes/aposin.css');
expect(testProjectConfig.targets.get('build')?.options?.styles).not.toContain('node_modules/@aposin/ng-aquila/themes/aposin.css');
});
});
});
2 changes: 1 addition & 1 deletion projects/ng-aquila/src/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { aquilaVersion } from './version-names';

export default function (options: Schema): Rule {
return (host: Tree, context: SchematicContext) => {
addPackageToPackageJson(host, '@aposin/ng-aquila', `${aquilaVersion}`);
addPackageToPackageJson(host, '@aposin/ng-aquila', `^${aquilaVersion}`);
// the angular cli just adds `@aposin/ng-aquila` to the package.json but it is not installed
// yet so we run the install first before we install the peer dependencies
const installTaskId = context.addTask(new NodePackageInstallTask());
Expand Down
41 changes: 20 additions & 21 deletions projects/ng-aquila/src/schematics/ng-add/setup-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
} from '@angular/cdk/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import * as chalk from 'chalk';
import { getWorkspace } from '@schematics/angular/utility/config';
import { getWorkspace, updateWorkspace } from '@schematics/angular/utility/workspace';
import { Schema } from './schema';
import { JsonArray } from '@angular-devkit/core';

export default function (options: Schema): Rule {
return (host: Tree, context: SchematicContext) => {
return async (host: Tree, context: SchematicContext) => {
const installTaskId = context.addTask(new NodePackageInstallTask());
return chain([
options && options.type && options.type === 'b2b' ? addExpertModule(options) : noop(),
Expand All @@ -24,38 +25,36 @@ export default function (options: Schema): Rule {
}

function addExpertModule(options: Schema) {
return (host: Tree) => {
const workspace = getWorkspace(host);
return async (host: Tree) => {
const workspace = await getWorkspace(host);
const project = getProjectFromWorkspace(workspace, options.project);
addModuleImportToRootModule(host, 'NxExpertModule',
'@aposin/ng-aquila/config', project);
return host;
};
}

function addAposinTheme(options: Schema) {
return (host: Tree) => {
return async (host: Tree) => {
if (options.noTheme) {
return host;
return;
}

const workspace = getWorkspace(host);
const workspace = await getWorkspace(host);
const project = getProjectFromWorkspace(workspace, options.project);
const newFilePath = 'node_modules/@aposin/ng-aquila/css/normalize.css';

const buildOptions = getProjectTargetOptions(project, 'build');
if (!buildOptions.styles) {
buildOptions.styles = [newFilePath];
} else if (!buildOptions.styles.includes(newFilePath)) {
buildOptions.styles.push(newFilePath);
let styles = buildOptions.styles as JsonArray;
if (!styles) {
styles = [newFilePath];
} else if (!styles.includes(newFilePath)) {
styles.push(newFilePath);
}

const themeToAdd = options.type === 'b2b' ? 'expert.css' : 'aposin.css';
buildOptions.styles.push(`node_modules/@aposin/ng-aquila/themes/${themeToAdd}`);

host.overwrite('angular.json', JSON.stringify(workspace, null, 2));
styles.push(`node_modules/@aposin/ng-aquila/themes/${themeToAdd}`);

return host;
return updateWorkspace(workspace);
};
}

Expand All @@ -68,8 +67,8 @@ function addCdkA11yStyles(options: Schema) {
}

function addStyles(options: Schema, path: string, importString: string) {
return (host: Tree) => {
const workspace = getWorkspace(host);
return async (host: Tree) => {
const workspace = await getWorkspace(host);
const project = getProjectFromWorkspace(workspace, options.project);
const styleFilePath = getProjectStyleFile(project);

Expand Down Expand Up @@ -104,16 +103,16 @@ function addStyles(options: Schema, path: string, importString: string) {
}

export function addPonyfillToPolyfills(options: Schema) {
return (host: Tree) => {
const workspace = getWorkspace(host);
return async (host: Tree) => {
const workspace = await getWorkspace(host);
const project = getProjectFromWorkspace(workspace, options.project);
const buildOptions = getProjectTargetOptions(project, 'build');

if (!buildOptions.polyfills) {
throw new Error(`Could not find polyfills.ts in ${project.sourceRoot}`);
}

const polyfillsTs = buildOptions.polyfills;
const polyfillsTs = buildOptions.polyfills as string;
const polyfillsFile = host.read(polyfillsTs);

if (!polyfillsFile) {
Expand Down

0 comments on commit d5286de

Please sign in to comment.