Skip to content

Commit

Permalink
fix(schematics): fix eva-icons schematic (#1380)
Browse files Browse the repository at this point in the history
  • Loading branch information
nnixaa authored and yggg committed May 27, 2019
1 parent bc2ee49 commit 0c809da
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 26 deletions.
7 changes: 5 additions & 2 deletions src/framework/theme/schematics/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
"factory": "./ng-add/index",
"schema": "./ng-add/schema.json"
},
"post-install": {
"description": "Adds Nebular Theme to the application.",
"factory": "./ng-add/post-install"
},
"setup": {
"description": "Sets up the specified project after the ng-add dependencies have been installed.",
"factory": "./ng-add/setup",
"schema": "./ng-add/schema.json"
"factory": "./ng-add/setup"
}
}
}
21 changes: 16 additions & 5 deletions src/framework/theme/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ describe('ng-add', () => {
return runner.runSchematic('setup', options, appTree);
}

function runPostInstallSchematic(options: Partial<NgAddOptions> = {}) {
return runner.runSchematic('post-install', options, appTree);
}

beforeEach(() => {
const collectionPath = require.resolve('../collection.json');
runner = new SchematicTestRunner('schematics', collectionPath);
Expand All @@ -63,10 +67,10 @@ describe('ng-add', () => {
it('should add @angular/cdk in package.json', () => {
const tree = runNgAddSchematic();
const dependencies = getPackageDependencies(tree);
const angularCoreVersion = dependencies['@angular/core'];
const angularCdkVersion = require('../../package.json').peerDependencies['@angular/cdk'];

expect(dependencies['@angular/cdk']).toBeDefined();
expect(dependencies['@angular/cdk']).toBe(angularCoreVersion);
expect(dependencies['@angular/cdk']).toBe(angularCdkVersion);
});

it('should add @angular/animations in package.json', function () {
Expand All @@ -88,12 +92,19 @@ describe('ng-add', () => {
});

it('should add @nebular/eva-icons in package.json', function () {
const tree = runNgAddSchematic();
const dependencies = getPackageDependencies(tree);
let tree = runNgAddSchematic();
let dependencies = getPackageDependencies(tree);
const nebularEvaIconsVersion = require('../../package.json').version;

expect(dependencies['@nebular/eva-icons']).toBeDefined();
expect(dependencies['@nebular/eva-icons']).toBe(nebularEvaIconsVersion);

tree = runPostInstallSchematic();
dependencies = getPackageDependencies(tree);

const evaIconsVersion = require('../../../eva-icons/package.json').peerDependencies['eva-icons'];
expect(dependencies['eva-icons']).toBeDefined();
expect(dependencies['eva-icons']).toBe(evaIconsVersion);
});

it('should register NbThemeModule.forRoot()', () => {
Expand Down Expand Up @@ -167,7 +178,7 @@ $nb-themes: nb-register-theme((
});

it('should add the BrowserAnimationsModule to the project module', () => {
const tree = runSetupSchematic();
const tree = runSetupSchematic({ animations: true });
const fileContent = getFileContent(tree, '/projects/nebular/src/app/app.module.ts');

expect(fileContent).toContain('BrowserAnimationsModule',
Expand Down
37 changes: 20 additions & 17 deletions src/framework/theme/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,48 @@ import {
addDependencyToPackageJson,
addDevDependencyToPackageJson,
getDependencyVersionFromPackageJson,
getNebularPeerDependencyVersionFromPackageJson,
getNebularVersion,
} from '../util/package';


/**
* ng-add schematics, installs peer dependencies and runs project setup schematics.
* */
export default function (options: Schema): Rule {
return (host: Tree, context: SchematicContext) => {
registerPeerDependencies(host);
runSetupSchematics(context, options);
}
return runSetupSchematics(options);
}

/**
* Add required peer dependencies in package.json if needed.
* Add required peer dependencies in package.json
* */
function registerPeerDependencies(host: Tree) {
const angularCoreVersion = getDependencyVersionFromPackageJson(host, '@angular/core');
function installMainPeerDependencies(tree) {
const angularCoreVersion = getDependencyVersionFromPackageJson(tree, '@angular/core');
const nebularThemeVersion = getNebularVersion();
const angularCdkVersion = getNebularPeerDependencyVersionFromPackageJson('@angular/cdk');

addDependencyToPackageJson(host, '@angular/cdk', angularCoreVersion);
addDependencyToPackageJson(host, '@angular/animations', angularCoreVersion);
addDependencyToPackageJson(host, '@nebular/theme', nebularThemeVersion);
addDependencyToPackageJson(host, '@nebular/eva-icons', nebularThemeVersion);
addDependencyToPackageJson(tree, '@angular/animations', angularCoreVersion);
addDependencyToPackageJson(tree, '@angular/cdk', angularCdkVersion, true);
addDependencyToPackageJson(tree, '@nebular/theme', nebularThemeVersion);
addDependencyToPackageJson(tree, '@nebular/eva-icons', nebularThemeVersion);

addDevDependencyToPackageJson(host, '@schematics/angular', angularCoreVersion);
addDevDependencyToPackageJson(tree, '@schematics/angular', angularCoreVersion);
}

/**
* Runs `npm install` and after complete runs `setup` schematics.
* Runs `npm install`, then `post-install` schematic and after complete runs `setup` schematics.
* The rest part of the ng-add schematics uses `@angular/cdk/schematics` and `@schematics/angular`
* utilities. That's why we have to install `@angular/cdk` and `@schematics/angular` package
* before running Nebular setup in the project.
*
* The only possibility to run `setup` schematics after required packages installed
* is to use context tasks and add `npm install` task as the dependency to `setup` schematics task.
* */
function runSetupSchematics(context: SchematicContext, options: Schema) {
const installTaskId = context.addTask(new NodePackageInstallTask());
context.addTask(new RunSchematicTask('setup', options), [installTaskId]);
function runSetupSchematics(options: Schema) {
return (tree: Tree, context: SchematicContext) => {
installMainPeerDependencies(tree);

const installTaskId = context.addTask(new NodePackageInstallTask());
const postInstallTaskId = context.addTask(new RunSchematicTask('post-install', options), [installTaskId]);
context.addTask(new RunSchematicTask('setup', options), [postInstallTaskId]);
};
}
45 changes: 45 additions & 0 deletions src/framework/theme/schematics/ng-add/post-install.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* @license
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';

/**
* This utils has to imported directly from the `/util/package`, not from the `/util/`.
* Other utilities use `@angular/sdk/schematics` and `@schematics/angular` packages.
* But these packages are not installed in this step.
* */
import {
addDependencyToPackageJson,
getEvaIconsVersion,
} from '../util/package';

/**
* post-install schematics, install dependant packages
* */
export default function (): Rule {
return runPostInstallSchematics();
}

/**
* Add icons peer dependencies in package.json
* */
function installDependantPeerDependencies(tree: Tree) {
const evaIconsVersion = getEvaIconsVersion();

addDependencyToPackageJson(tree, 'eva-icons', evaIconsVersion);
}

/**
* Runs `npm install`
* */
function runPostInstallSchematics() {
return (tree: Tree, context: SchematicContext) => {
installDependantPeerDependencies(tree);

context.addTask(new NodePackageInstallTask());
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import { appRoutingModuleContent } from './app-routing-module-content';


export function registerModules(options: Schema): Rule {
if (!options.theme) {
options.theme = 'default';
}

return chain([
registerAnimationsModule(options),
registerNebularModules(options),
Expand Down
24 changes: 22 additions & 2 deletions src/framework/theme/schematics/util/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ export function getNebularPeerDependencyVersionFromPackageJson(packageName: stri
return packageJson.peerDependencies[packageName];
}

/**
* Eva Icons version
* */
export function getEvaIconsVersion(): string {
const packageJson: PackageJson = getNebularEvaIconsPackageJson();
const packageName = 'eva-icons';

if (noInfoAboutPeerDependency(packageJson, packageName)) {
throwNoPackageInfoInPackageJson(packageName);
}

return packageJson.peerDependencies[packageName];
}

/**
* Gets the version of the specified dependency by looking at the package.json in the specified tree
* */
Expand All @@ -50,7 +64,7 @@ export function getDependencyVersionFromPackageJson(tree: Tree, packageName: str
return packageJson.dependencies[packageName];
}

export function addDependencyToPackageJson(tree: Tree, packageName: string, packageVersion: string) {
export function addDependencyToPackageJson(tree: Tree, packageName: string, packageVersion: string, force = false) {
if (!tree.exists(packageJsonName)) {
throwNoPackageJsonError();
}
Expand All @@ -61,7 +75,7 @@ export function addDependencyToPackageJson(tree: Tree, packageName: string, pack
packageJson.dependencies = {};
}

if (!packageJson.dependencies[packageName]) {
if (!packageJson.dependencies[packageName] || force) {
packageJson.dependencies[packageName] = packageVersion;
packageJson.dependencies = sortObjectByKeys(packageJson.dependencies);
}
Expand Down Expand Up @@ -135,3 +149,9 @@ function sortObjectByKeys(obj: object) {
function getNebularPackageJson(): PackageJson {
return require('../../package.json');
}

function getNebularEvaIconsPackageJson(): PackageJson {
return require('../../../eva-icons/package.json');
}


0 comments on commit 0c809da

Please sign in to comment.