Skip to content

Commit df2083f

Browse files
author
Peter Ullrich
committed
SharedOptions
1 parent 9d0d6c8 commit df2083f

File tree

6 files changed

+23
-17
lines changed

6 files changed

+23
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.1.18",
2+
"version": "0.2.1",
33
"name": "dotup-typescript-yeoman-generators",
44
"author": {
55
"name": "Peter Ullrich",

src/BaseGenerator.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ import { ProjectPathAnalyser } from './project/ProjectPathAnalyser';
1313
import { Question } from './questions/Question';
1414
import { GeneratorOptions, MethodsToRegister, IProperty, ITypedProperty } from './Types';
1515
import { SharedOptions } from './SharedOptions';
16+
import { ISharedOptionsSubscriber } from "./ISharedOptionsSubscriber";
1617

17-
export abstract class BaseGenerator<TStep extends string> extends generator {
18+
export abstract class BaseGenerator<TStep extends string> extends generator implements ISharedOptionsSubscriber {
19+
onValue(key: string, value: any): void {
20+
(<IProperty>this.options)[key] = value;
21+
};
1822

1923
static counter: number = 0;
2024
static sharedOptions: SharedOptions<string>;
@@ -39,13 +43,14 @@ export abstract class BaseGenerator<TStep extends string> extends generator {
3943

4044
currentStep: TStep;
4145

42-
constructor(args: string | string[], options: GeneratorOptions<TStep>) {
46+
constructor(args: string | string[], options: GeneratorOptions<TStep>, sharedOptions?: SharedOptions<TStep>) {
4347
super(args, options);
48+
4449
BaseGenerator.counter += 1;
45-
this.generatorName = this.constructor.name;
50+
BaseGenerator.sharedOptions = sharedOptions; // (<IProperty>options)['sharedOptions'];
4651

52+
this.generatorName = this.constructor.name;
4753
this.projectInfo = new ProjectInfo();
48-
BaseGenerator.sharedOptions = (<IProperty>options)['sharedOptions'];
4954

5055
this.setRootPath();
5156
}

src/ISharedOptionsSubscriber.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface ISharedOptionsSubscriber {
2+
onValue: (key: string, value: any) => void;
3+
}

src/SharedOptions.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import { IProperty, ITypedProperty } from './Types';
22
import { BaseGenerator } from './BaseGenerator';
33
import { SharedOptionsSubscription } from './SharedOptionsSubscription';
4-
5-
export interface ISubscriber {
6-
onValue: (value: string) => void;
7-
}
4+
import { ISharedOptionsSubscriber } from './ISharedOptionsSubscriber';
85

96
export class SharedOptions<TStep extends string> {
107

118
values: ITypedProperty<string> = {};
129

1310
subscriber: SharedOptionsSubscription<TStep>[] = [];
1411

15-
subscribe<K extends keyof TStep>(subscriber: ISubscriber, questionName: K | string): void {
12+
subscribe<K extends keyof TStep>(subscriber: ISharedOptionsSubscriber, questionName: K | string): void {
1613
const question = <string>questionName;
1714
const questionSubscriber = this.subscriber.find(s => s.isSubscriber(subscriber));
1815

@@ -37,7 +34,7 @@ export class SharedOptions<TStep extends string> {
3734
const subs = this.subscriber
3835
.filter(s => s.hasStepSubscription(<string>questionName))
3936
.map(x => x.getSubscriber());
40-
subs.forEach(x => x.onValue(value));
37+
subs.forEach(x => x.onValue(<string>questionName, value));
4138
}
4239

4340
}

src/SharedOptionsSubscription.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { ISubscriber } from './SharedOptions';
1+
import { ISharedOptionsSubscriber } from "./ISharedOptionsSubscriber";
22
export class SharedOptionsSubscription<TStep extends string> {
3-
private subscriber: ISubscriber;
3+
private subscriber: ISharedOptionsSubscriber;
44
stepNames: string[] = [];
5-
constructor(subscriber: ISubscriber) {
5+
constructor(subscriber: ISharedOptionsSubscriber) {
66
this.subscriber = subscriber;
77
}
8-
isSubscriber(subscriber: ISubscriber): boolean {
8+
isSubscriber(subscriber: ISharedOptionsSubscriber): boolean {
99
return subscriber === this.subscriber;
1010
}
11-
getSubscriber(): ISubscriber {
11+
getSubscriber(): ISharedOptionsSubscriber {
1212
return this.subscriber;
1313
}
14-
getStepSubscriber(stepname: string): ISubscriber {
14+
getStepSubscriber(stepname: string): ISharedOptionsSubscriber {
1515
if (this.hasStepSubscription(stepname)) {
1616
return undefined;
1717
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from './questions/index';
44
export * from './BaseGenerator';
55
export * from './questions/IStepQuestion';
66
export * from './questions/Question';
7+
export * from './ISharedOptionsSubscriber';
78
export * from './SharedOptions';
89
export * from './SharedOptionsSubscription';
910
export * from './Types';

0 commit comments

Comments
 (0)