Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions projects/igniteui-angular/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ describe('ng-add schematics', () => {
const collectionPath = path.join(__dirname, '../collection.json');
const runner: SchematicTestRunner = new SchematicTestRunner('cli-schematics', collectionPath);
let tree: UnitTestTree;
const sourceRoot = 'testSrc';
const ngJsonConfig = {
defaultProject: 'testProj',
projects: {
testProj: {
sourceRoot: 'src',
sourceRoot: sourceRoot,
projectType: ProjectType.Application,
architect: {
serve: {},
build: {
options: {
main: 'src/main.ts',
main: `${sourceRoot}/main.ts`,
polyfills: `${sourceRoot}/polyfills.ts`,
scripts: []
}
}
Expand All @@ -42,7 +44,7 @@ describe('ng-add schematics', () => {
tree = new UnitTestTree(new EmptyTree());
tree.create('/angular.json', JSON.stringify(ngJsonConfig));
tree.create('/package.json', JSON.stringify(pkgJsonConfig));
tree.create('src/main.ts', '// test comment');
tree.create(`${sourceRoot}/main.ts`, '// test comment');
});

it('should create the needed files correctly', () => {
Expand Down Expand Up @@ -71,7 +73,7 @@ describe('ng-add schematics', () => {

it('should add hammer.js to the main.ts file', () => {
runner.runSchematic('ng-add', { normalizeCss: false }, tree);
const mainTs = tree.read('src/main.ts').toString();
const mainTs = tree.read(`${sourceRoot}/main.ts`).toString();
expect(mainTs).toContain('import \'hammerjs\';');
});

Expand All @@ -82,12 +84,12 @@ describe('ng-add schematics', () => {
tree.overwrite('angular.json', JSON.stringify(workspace));
runner.runSchematic('ng-add', { normalizeCss: false }, tree);

const newContent = tree.read('src/main.ts').toString();
const newContent = tree.read(`${sourceRoot}/main.ts`).toString();
expect(newContent.split('import \'hammerjs\';\n// test comment').length).toEqual(1);
});

it('should not add hammer.js if it exists in main.ts', () => {
const mainTsPath = 'src/main.ts';
const mainTsPath = `${sourceRoot}/main.ts`;
const content = tree.read(mainTsPath).toString();
tree.overwrite(mainTsPath, 'import \'hammerjs\';\n' + content);
runner.runSchematic('ng-add', { normalizeCss: false }, tree);
Expand Down Expand Up @@ -138,54 +140,54 @@ import 'core-js/es6/set';
import 'web-animations-js'; // Run \`npm install --save web-animations-js\`.
`;

tree.create('src/polyfills.ts', polyfills);
tree.create(`${sourceRoot}/polyfills.ts`, polyfills);
runner.runSchematic('ng-add', { polyfills: true, normalizeCss: false }, tree);
expect(tree.readContent('src/polyfills.ts').replace(/\r\n/g, '\n')).toEqual(result.replace(/\r\n/g, '\n'));
expect(tree.readContent(`${sourceRoot}/polyfills.ts`).replace(/\r\n/g, '\n')).toEqual(result.replace(/\r\n/g, '\n'));
});

it('should properly add css reset', () => {
tree.create('src/styles.scss', '');
tree.create(`${sourceRoot}/styles.scss`, '');
runner.runSchematic('ng-add', { normalizeCss: true }, tree);
let pkgJsonData = JSON.parse(tree.readContent('/package.json'));
expect(tree.readContent('src/styles.scss')).toEqual(scssImport);
expect(tree.readContent(`${sourceRoot}/styles.scss`)).toEqual(scssImport);
expect(pkgJsonData.dependencies['minireset.css']).toBeTruthy();
resetJsonConfigs(tree);
tree.delete('src/styles.scss');
tree.delete(`${sourceRoot}/styles.scss`);

tree.create('src/styles.sass', '');
tree.create(`${sourceRoot}/styles.sass`, '');
runner.runSchematic('ng-add', { normalizeCss: true }, tree);
pkgJsonData = JSON.parse(tree.readContent('/package.json'));
expect(tree.readContent('src/styles.sass')).toEqual(scssImport);
expect(tree.readContent(`${sourceRoot}/styles.sass`)).toEqual(scssImport);
expect(pkgJsonData.dependencies['minireset.css']).toBeTruthy();
resetJsonConfigs(tree);
tree.delete('src/styles.sass');
tree.delete(`${sourceRoot}/styles.sass`);

tree.create('src/styles.css', '');
tree.create(`${sourceRoot}/styles.css`, '');
runner.runSchematic('ng-add', { normalizeCss: true }, tree);
pkgJsonData = JSON.parse(tree.readContent('/package.json'));
expect(tree.readContent('src/styles.css')).toBe('');
expect(tree.readContent(`${sourceRoot}/styles.css`)).toBe('');
expect(pkgJsonData.dependencies['minireset.css']).toBeTruthy();
expect(JSON.parse(tree.readContent('/angular.json')).projects['testProj'].architect.build.options.styles).toContain(cssImport);
resetJsonConfigs(tree);
tree.delete('src/styles.css');
tree.delete(`${sourceRoot}/styles.css`);

tree.create('src/styles.less', '');
tree.create(`${sourceRoot}/styles.less`, '');
runner.runSchematic('ng-add', { normalizeCss: true }, tree);
pkgJsonData = JSON.parse(tree.readContent('/package.json'));
expect(tree.readContent('src/styles.less')).toBe('');
expect(tree.readContent(`${sourceRoot}/styles.less`)).toBe('');
expect(pkgJsonData.dependencies['minireset.css']).toBeTruthy();
expect(JSON.parse(tree.readContent('/angular.json')).projects['testProj'].architect.build.options.styles).toContain(cssImport);
resetJsonConfigs(tree);
tree.delete('src/styles.less');
tree.delete(`${sourceRoot}/styles.less`);

tree.create('src/styles.styl', '');
tree.create(`${sourceRoot}/styles.styl`, '');
runner.runSchematic('ng-add', { normalizeCss: true }, tree);
pkgJsonData = JSON.parse(tree.readContent('/package.json'));
expect(tree.readContent('src/styles.styl')).toBe('');
expect(tree.readContent(`${sourceRoot}/styles.styl`)).toBe('');
expect(pkgJsonData.dependencies['minireset.css']).toBeTruthy();
expect(JSON.parse(tree.readContent('/angular.json')).projects['testProj'].architect.build.options.styles).toContain(cssImport);
resetJsonConfigs(tree);
tree.delete('src/styles.styl');
tree.delete(`${sourceRoot}/styles.styl`);
});

it('should properly add web animations', () => {
Expand All @@ -202,7 +204,7 @@ import 'web-animations-js'; // Run \`npm install --save web-animations-js\`.
* that is built with AngularCLI v7.3 or above. All else are considered below v7.3.
*/
it('should enable es5BrowserSupport on projects with ng cli version >= 7.3', () => {
tree.create('src/polyfills.ts', '');
tree.create(`${sourceRoot}/polyfills.ts`, '');
const newJson: any = JSON.parse(tree.read('/angular.json').toString());
newJson.projects['testProj'].architect.build.options['es5BrowserSupport'] = false;
tree.overwrite('/angular.json', JSON.stringify(newJson));
Expand All @@ -226,11 +228,11 @@ import 'web-animations-js'; // Run \`npm install --save web-animations-js\`.
import 'web-animations-js'; // Run \`npm install --save web-animations-js\`.
`;

tree.create('src/polyfills.ts', polyfills);
tree.create(`${sourceRoot}/polyfills.ts`, polyfills);
const newJson: any = JSON.parse(tree.read('/angular.json').toString());
newJson.projects['testProj'].architect.build.options['es5BrowserSupport'] = false;
tree.overwrite('/angular.json', JSON.stringify(newJson));
runner.runSchematic('ng-add', { polyfills: true }, tree);
expect(tree.readContent('src/polyfills.ts').replace(/\r\n/g, '\n')).toEqual(result.replace(/\r\n/g, '\n'));
expect(tree.readContent(`${sourceRoot}/polyfills.ts`).replace(/\r\n/g, '\n')).toEqual(result.replace(/\r\n/g, '\n'));
});
});
12 changes: 8 additions & 4 deletions projects/igniteui-angular/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { chain, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { Options } from '../interfaces/options';
import { installPackageJsonDependencies } from '../utils/package-handler';
import { logSuccess, addDependencies, overwriteJsonFile, getPropertyFromWorkspace } from '../utils/dependency-handler';
import { logSuccess, addDependencies, overwriteJsonFile,
getPropertyFromWorkspace, getConfigFile } from '../utils/dependency-handler';

import { addResetCss } from './add-normalize';
import { getWorkspace } from '@schematics/angular/utility/config';
import { getWorkspace, getConfig } from '@schematics/angular/utility/config';
import { WorkspaceSchema } from '@schematics/angular/utility/workspace-models';


Expand All @@ -17,7 +18,9 @@ function propertyExistsInWorkspace(targetProp: string, workspace: WorkspaceSchem
}

function enablePolyfills(tree: Tree, context: SchematicContext): string {
const targetFile = 'src/polyfills.ts';
const workspace = getWorkspace(tree);
const project = workspace.projects[workspace.defaultProject];
const targetFile = getConfigFile(project, 'polyfills');
if (!tree.exists(targetFile)) {
context.logger.warn(`${targetFile} not found. You may need to update polyfills.ts manually.`);
return;
Expand Down Expand Up @@ -50,7 +53,8 @@ function readInput(options: Options): Rule {
if (options.polyfills) {
const workspace = getWorkspace(tree);
const targetProperty = 'es5BrowserSupport';
const polyfillsFile = 'src/polyfills.ts';
const project = workspace.projects[workspace.defaultProject];
const polyfillsFile = getConfigFile(project, 'polyfills');
const propertyExists = propertyExistsInWorkspace(targetProperty, workspace);
let polyfillsData = tree.read(polyfillsFile).toString();
if (propertyExists) {
Expand Down
10 changes: 5 additions & 5 deletions projects/igniteui-angular/schematics/utils/dependency-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ function getTargetedProjectOptions(project: WorkspaceProject<ProjectType>, targe
throw new SchematicsException(`Cannot determine the project's configuration for: ${target}`);
}

function getMainFile(project: WorkspaceProject<ProjectType>): string {
export function getConfigFile(project: WorkspaceProject<ProjectType>, option: string): string {
const buildOptions = getTargetedProjectOptions(project, 'build');
if (!buildOptions.main) {
throw new SchematicsException(`Could not find the project main file inside of the ` +
if (!buildOptions[option]) {
throw new SchematicsException(`Could not find the project ${option} file inside of the ` +
`workspace config (${project.sourceRoot})`);
}

return buildOptions.main;
return buildOptions[option];
}

export function overwriteJsonFile(tree: Tree, targetFile: string, data: any) {
Expand Down Expand Up @@ -111,7 +111,7 @@ function includeDependencies(pkgJson: any, context: SchematicContext, tree: Tree
const workspace = getWorkspace(tree);
const project = workspace.projects[workspace.defaultProject];
const projectOptions = getTargetedProjectOptions(project, 'build');
const mainTsPath = getMainFile(project);
const mainTsPath = getConfigFile(project, 'main');
const hammerImport = 'import \'hammerjs\';\n';
const mainTsContent = tree.read(mainTsPath).toString();
// if there are no elements in the architect.build.options.scripts array that contain hammerjs
Expand Down