Skip to content

Commit

Permalink
develop controller copy asset generator
Browse files Browse the repository at this point in the history
  • Loading branch information
thomrick committed May 30, 2017
1 parent 580bd80 commit 70f01fb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
24 changes: 23 additions & 1 deletion src/core/generators/controller.generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
import {Generator} from '../../common/interfaces/generator.interface';
import {Readable, Writable} from 'stream';
import * as fs from 'fs';
import * as path from 'path';

export class ControllerGenerator implements Generator {
private templatePath: string = '../../assets/ts/controller/controller.ts.template';
private testTemplatePath: string = '../../assets/ts/controller/controller.spec.ts.template';
private filename: string = '[name].controller.ts';
private testFilename: string = '[name].controller.spec.ts';

public generate(name: string): Promise<void> {
throw new Error("Method not implemented.");
this.generateAsset(name);
this.generateTestAsset(name);
return Promise.resolve();
}

private generateAsset(name: string): void {
const reader: Readable = fs.createReadStream(path.resolve(__dirname, this.templatePath));
const writer: Writable = fs.createWriteStream(path.resolve(process.cwd(), name, this.filename));
reader.pipe(writer);
}

private generateTestAsset(name: string): void {
const reader: Readable = fs.createReadStream(path.resolve(__dirname, this.testTemplatePath));
const writer: Writable = fs.createWriteStream(path.resolve(process.cwd(), name, this.testFilename));
reader.pipe(writer);
}
}
22 changes: 8 additions & 14 deletions src/core/tests/generators/controller.generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,8 @@ describe('ControllerGenerator', () => {
pipeStub = sandbox.stub(PassThrough.prototype, 'pipe');
});

it('should throw an error', () => {
expect(() => {
generator.generate('name');
}).to.throw;
});

context('mange the controller asset generation', () => {
it.skip('should create a read stream from the controller asset file', done => {
it('should create a read stream from the controller asset file', done => {
generator.generate('path/to/asset')
.then(() => {
expect(createReadStreamStub.calledWith(
Expand All @@ -42,7 +36,7 @@ describe('ControllerGenerator', () => {
.catch(done);
});

it.skip('should create a write stream to the generated asset', done => {
it('should create a write stream to the generated asset', done => {
const name: string = 'path/to/asset';
generator.generate(name)
.then(() => {
Expand All @@ -54,18 +48,18 @@ describe('ControllerGenerator', () => {
.catch(done);
});

it.skip('should pipe read stream to the write stream', done => {
it('should pipe read stream to the write stream', done => {
generator.generate('path/to/asset')
.then(() => {
expect(pipeStub.calledOnce).to.be.true;
expect(pipeStub.calledTwice).to.be.true;
done();
})
.catch(done);
});
});

context('managing the test controller asset generation', () => {
it.skip('should create a read stream from the test controller asset file', done => {
it('should create a read stream from the test controller asset file', done => {
generator.generate('path/to/asset')
.then(() => {
expect(createReadStreamStub.calledWith(
Expand All @@ -76,7 +70,7 @@ describe('ControllerGenerator', () => {
.catch(done);
});

it.skip('should create a write stream to the generated asset', done => {
it('should create a write stream to the generated asset', done => {
const name: string = 'path/to/asset';
generator.generate(name)
.then(() => {
Expand All @@ -88,10 +82,10 @@ describe('ControllerGenerator', () => {
.catch(done);
});

it.skip('should pipe read stream to the write stream', done => {
it('should pipe read stream to the write stream', done => {
generator.generate('path/to/asset')
.then(() => {
expect(pipeStub.calledOnce).to.be.true;
expect(pipeStub.calledTwice).to.be.true;
done();
})
.catch(done);
Expand Down

0 comments on commit 70f01fb

Please sign in to comment.