Skip to content

Commit

Permalink
Merge pull request #3 from TinkoffCreditSystems/add-import-to-module
Browse files Browse the repository at this point in the history
feat: add more fn
  • Loading branch information
IKatsuba authored Apr 28, 2021
2 parents 35518aa + 189e39d commit 7de80ef
Show file tree
Hide file tree
Showing 18 changed files with 846 additions and 36 deletions.
34 changes: 0 additions & 34 deletions libs/ng-morph/ng/helpers/add-import-to-module.ts

This file was deleted.

37 changes: 37 additions & 0 deletions libs/ng-morph/ng/helpers/push-to-array-property.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getDecorators } from 'ng-morph/decorators';
import { ClassDeclaration, Node } from 'ts-morph';

export function pushToArrayProperty(
classDeclaration: ClassDeclaration,
decoratorName: string,
propertyName: string,
initializer: string
) {
const [decorator] = getDecorators(classDeclaration, {
name: decoratorName,
});

const [
metadata = decorator.addArgument(`{${propertyName}: []}`),
] = decorator.getArguments();

if (!Node.isObjectLiteralExpression(metadata)) {
return;
}

const property =
metadata.getProperty(propertyName) ??
metadata.addProperty(`${propertyName}: []`);

if (!Node.isPropertyAssignment(property)) {
return;
}

const importsInitializer = property.getInitializer();

if (!Node.isArrayLiteralExpression(importsInitializer)) {
return;
}

importsInitializer.addElement(initializer);
}
3 changes: 2 additions & 1 deletion libs/ng-morph/ng/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './helpers/add-import-to-module';
export * from './helpers/get-bootstrap-fn';
export * from './helpers/get-main-mofule';

export * from './module';
121 changes: 121 additions & 0 deletions libs/ng-morph/ng/module/add-bootstrap-to-module.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { UnitTestTree } from '@angular-devkit/schematics/testing';
import { HostTree } from '@angular-devkit/schematics';
import {
createProject,
saveActiveProject,
setActiveProject,
} from 'ng-morph/project';
import { createSourceFile } from 'ng-morph/source-file';
import { getClasses } from 'ng-morph/classes';
import { addBootstrapToModule } from './add-bootstrap-to-module';

describe('addBootstrapToModule', () => {
let host: UnitTestTree;

beforeEach(() => {
host = new UnitTestTree(new HostTree());

setActiveProject(createProject(host));
});

describe('No bootstrap property', () => {
beforeEach(() => {
createSourceFile(
'src/main.ts',
`import { NgModule } from '@angular/core';
@NgModule({})
export class SomeModule {
}`
);
});

it('should create the declarations property', () => {
addBootstrapToModule(
getClasses('src/main.ts', { name: 'SomeModule' })[0],
'TestComponent'
);

saveActiveProject();

expect(host.readContent('src/main.ts'))
.toStrictEqual(`import { NgModule } from '@angular/core';
@NgModule({
bootstrap: [TestComponent]
})
export class SomeModule {
}`);
});
});

describe('No decorator arguments', () => {
beforeEach(() => {
createSourceFile(
'src/main.ts',
`import { NgModule } from '@angular/core';
@NgModule()
export class SomeModule {
}`
);
});

it('should create the bootstrap property', () => {
addBootstrapToModule(
getClasses('src/main.ts', { name: 'SomeModule' })[0],
'TestComponent'
);

saveActiveProject();

expect(host.readContent('src/main.ts'))
.toStrictEqual(`import { NgModule } from '@angular/core';
@NgModule({bootstrap: [TestComponent]})
export class SomeModule {
}`);
});
});

describe('The bootstrap property is exists', () => {
beforeEach(() => {
createSourceFile(
'src/main.ts',
`import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
bootstrap: [CommonComponent]
})
export class SomeModule {
}`
);
});

it('should add module to entryComponents', () => {
addBootstrapToModule(
getClasses('src/main.ts', { name: 'SomeModule' })[0],
'TestComponent'
);

saveActiveProject();

expect(host.readContent('src/main.ts'))
.toStrictEqual(`import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
bootstrap: [CommonComponent, TestComponent]
})
export class SomeModule {
}`);
});
});
});
9 changes: 9 additions & 0 deletions libs/ng-morph/ng/module/add-bootstrap-to-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ClassDeclaration } from 'ts-morph';
import { pushToArrayProperty } from '../helpers/push-to-array-property';

export function addBootstrapToModule(
classDeclaration: ClassDeclaration,
component: string
) {
pushToArrayProperty(classDeclaration, 'NgModule', 'bootstrap', component);
}
121 changes: 121 additions & 0 deletions libs/ng-morph/ng/module/add-declaration-to-module.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { UnitTestTree } from '@angular-devkit/schematics/testing';
import { HostTree } from '@angular-devkit/schematics';
import {
createProject,
saveActiveProject,
setActiveProject,
} from 'ng-morph/project';
import { createSourceFile } from 'ng-morph/source-file';
import { getClasses } from 'ng-morph/classes';
import { addDeclarationToModule } from './add-declaration-to-module';

describe('addDeclarationToModule', () => {
let host: UnitTestTree;

beforeEach(() => {
host = new UnitTestTree(new HostTree());

setActiveProject(createProject(host));
});

describe('No declaration property', () => {
beforeEach(() => {
createSourceFile(
'src/main.ts',
`import { NgModule } from '@angular/core';
@NgModule({})
export class SomeModule {
}`
);
});

it('should create the declarations property', () => {
addDeclarationToModule(
getClasses('src/main.ts', { name: 'SomeModule' })[0],
'TestComponent'
);

saveActiveProject();

expect(host.readContent('src/main.ts'))
.toStrictEqual(`import { NgModule } from '@angular/core';
@NgModule({
declarations: [TestComponent]
})
export class SomeModule {
}`);
});
});

describe('No decorator arguments', () => {
beforeEach(() => {
createSourceFile(
'src/main.ts',
`import { NgModule } from '@angular/core';
@NgModule()
export class SomeModule {
}`
);
});

it('should create the declarations property', () => {
addDeclarationToModule(
getClasses('src/main.ts', { name: 'SomeModule' })[0],
'TestComponent'
);

saveActiveProject();

expect(host.readContent('src/main.ts'))
.toStrictEqual(`import { NgModule } from '@angular/core';
@NgModule({declarations: [TestComponent]})
export class SomeModule {
}`);
});
});

describe('The declarations property is exists', () => {
beforeEach(() => {
createSourceFile(
'src/main.ts',
`import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
declarations: [CommonComponent]
})
export class SomeModule {
}`
);
});

it('should add module to entryComponents', () => {
addDeclarationToModule(
getClasses('src/main.ts', { name: 'SomeModule' })[0],
'TestComponent'
);

saveActiveProject();

expect(host.readContent('src/main.ts'))
.toStrictEqual(`import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
declarations: [CommonComponent, TestComponent]
})
export class SomeModule {
}`);
});
});
});
14 changes: 14 additions & 0 deletions libs/ng-morph/ng/module/add-declaration-to-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ClassDeclaration } from 'ts-morph';
import { pushToArrayProperty } from '../helpers/push-to-array-property';

export function addDeclarationToModule(
classDeclaration: ClassDeclaration,
declaration: string
) {
pushToArrayProperty(
classDeclaration,
'NgModule',
'declarations',
declaration
);
}
Loading

0 comments on commit 7de80ef

Please sign in to comment.